-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
bpo-39336: Allow packages to not let their child modules be set on them #18006
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
Conversation
feb58e2
to
6e45545
Compare
Fix whitespace Summary: Test Plan: Reviewers: Subscribers: Tasks: Tags:
6e45545
to
862b536
Compare
Lib/importlib/_bootstrap.py
Outdated
try: | ||
setattr(parent_module, child, module) | ||
except AttributeError: | ||
msg = (f"Can't set child package '{child}' on " |
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.
msg = (f"Can't set child package '{child}' on " | |
msg = (f"Cannot set an attribute on {parent!r} for child module {child!r}" |
Lib/test/test_import/__init__.py
Outdated
@@ -1339,6 +1342,39 @@ def test_circular_from_import(self): | |||
str(cm.exception), | |||
) | |||
|
|||
def test_unwritable_module(self): | |||
package = inspect.cleandoc(''' |
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.
Any reason to not put this as a module in test_import/data/
?
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.
Nope, I just didn't realize that was a thing :)
When you're done making the requested changes, leave the comment: |
I have made the requested changes; please review again. |
Thanks for making the requested changes! @brettcannon: please review the changes made to this pull request. |
Lib/test/test_import/__init__.py
Outdated
with open(os.path.join(os.path.dirname(path), 'x.py'), 'w+'): | ||
pass | ||
|
||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'data')) |
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.
Why the path insertion instead of importing test.test_import.data.unwritable
?
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 was actually just following the pattern I saw in test_concurrency but this is certainly much cleaner!
Lib/test/test_import/__init__.py
Outdated
finally: | ||
del sys.modules['spam.x'] | ||
|
||
sys.modules['unwritable.x'] |
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.
If this is delete the module then you can use a helper in test.support
and register it with addCleanup()
.
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.
Thanks, will do!
When you're done making the requested changes, leave the comment: |
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 have made the requested changes; please review again.
Looks like Bedevere/GitHub hiccuped, so I manually updated the status for another review. |
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.
Two minor things to clean up, but I trust you to do that before you hit the green button. 😄
Misc/NEWS.d/next/Core and Builtins/2020-01-15-01-39-29.bpo-39336.nJ7W9I.rst
Outdated
Show resolved
Hide resolved
Co-Authored-By: Brett Cannon <[email protected]>
…36.nJ7W9I.rst Co-Authored-By: Brett Cannon <[email protected]>
Remove unnecessary change in _ready_to_import
Remove white space change
Made one other small tweak to remove the change to _ready_to_import which was no longer necessary |
@DinoV: Please replace |
|
Hummmm, seems that that buildbot failure is legitimate as this is the new test:
|
@DinoV I think you forgot to update the Makefile and Windows-related build files to add |
…em (python#18006) * bpo-39336: Allow setattr to fail on modules which aren't assignable When attaching a child module to a package if the object in sys.modules raises an AttributeError (e.g. because it is immutable) it causes the whole import to fail. This now allows immutable packages to exist and an ImportWarning is reported and the AttributeError exception is ignored.
This allows an import to proceed if a module raises AttributeError when attempting to set a child module on the parent package. This can occur if a loader wants to make its modules immutable, in which case the module cannot be set. In this case an ImportWarning is still logged in case this behavior is unexpected.
https://bugs.python.org/issue39336