-
Notifications
You must be signed in to change notification settings - Fork 199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle module.__file__ == None #1042
base: master
Are you sure you want to change the base?
Conversation
Looking at the errors at one of the failing workflows (https://dev.azure.com/mitogen-hq/mitogen/_build/results?buildId=671&view=logs&j=38fb768a-91bc-52fb-9300-923df8625209&t=0744b325-ce4a-569b-7cdc-1da0e44989ad&l=25)
Not sure it's related to the change.
|
By the way, the base branch for it is |
It should be master. I've updated the PR, please rebase. |
It's been discovered that some modules have the `__file__` property being set as `None`. An example of a such module is `backports`. Providing `None` to `os.path.abspath()` causes an exception: `TypeError: expected str, bytes or os.PathLike object, not NoneType` Related to mitogen-hq#946
20fe0fb
to
591e542
Compare
@moreati rebased onto master. Some CI jobs are still failing, but I don't have enough knowledge to say if it's expected or not. |
I've just asked Azure Pipelines to rerun the failed jobs. It's probably a case of flaky tests, they're taking 1 or 2 reruns to get 100% green. Thought I had a ticket for it ... In general one can trigger rerun of all Azure Pipelines jobs in a github project by adding a comment with "/AzurePipelines r-u-n", without the dashes. https://learn.microsoft.com/en-us/azure/devops/pipelines/repos/github?view=azure-devops&tabs=yaml#comment-triggers |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you sure this code change is still doing anything? The third argument to getattr(..., ..., '')
already provides ''
as a default. E.g.
>>> getattr(None, 'does_not_exist', 'default value')
'default value'
The Similar to something like class X:
def __init__(self):
self.attr = None
x = X()
a = getattr(x, 'attr', 'default value')
print(a)
# prints None Then, if not handled, the following happens modpath = os.path.abspath(None) and causes the error >>> import os
>>> os.path.abspath(None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<frozen posixpath>", line 399, in abspath
TypeError: expected str, bytes or os.PathLike object, not NoneType |
Added https://github.com/mitogen-hq/mitogen/wiki/import-and-importlib-notes and mentioned this. |
Hunting for other cases that might need fixing
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Finally this needs
- An entry in
docs/changelog.rst
- Rebasing against current master (I'll hold off merging other PRs for a day or two)
Optionally
- Add yourself to
docs/contributers.rst
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is more misleading than helpful. Better to remove it.
@@ -148,7 +148,11 @@ def is_stdlib_name(modname): | |||
return False | |||
|
|||
# six installs crap with no __file__ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is now more mislaeading than helpful, please remove it.
@@ -148,7 +148,11 @@ def is_stdlib_name(modname): | |||
return False | |||
|
|||
# six installs crap with no __file__ | |||
modpath = os.path.abspath(getattr(module, '__file__', '')) | |||
path = getattr(module, '__file__', '') | |||
if path is None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would like to have an accompanying unit test for this case. If you'd like to add one I recommend doing so in tests.module_finder_test.IsStdlibNameTest
, and adding the package that caused this in the wild as a test dependency in tests/requirements.txt
.
Otherwise I'm happy to add one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding the package might be tricky, since it seems to be a part of Python distribution, though I'm not 100% sure.
The package was backports
.
Will try to reproduce on different environments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
backports.*
(e.g. https://pypi.org/project/backports.textwrap/) are all thrid party distributions on PyPI. They aren't part of the stdlib.
backports
is a namespace package they all declare. The project on PyPI called backports (https://pypi.org/project/backports/) is a placeholder only. I don't think it is meant to be installed, and it doesn't contain an Python code.
So I recommend you use a small backports.<your choice>
, or if you don't have a preference use backports.textwrap
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I see, thanks for clarification 👍
It's been discovered that some modules have the
__file__
property being set asNone
. An example of a such module isbackports
.Providing
None
toos.path.abspath()
causes an exception:TypeError: expected str, bytes or os.PathLike object, not NoneType
Related to
Has been used in the Nix environment: