Skip to content
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

bpo-39336: Allow packages to not let their child modules be set on them #18006

Merged
merged 8 commits into from Jan 23, 2020

Conversation

@DinoV
Copy link
Contributor

DinoV commented Jan 14, 2020

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

@DinoV DinoV force-pushed the DinoV:bpo_39336_error branch 3 times, most recently from feb58e2 to 6e45545 Jan 15, 2020
DinoV and others added 2 commits Jan 14, 2020
Fix whitespace

Summary:

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:
@DinoV DinoV force-pushed the DinoV:bpo_39336_error branch 2 times, most recently from 6e45545 to 862b536 Jan 15, 2020
@brettcannon brettcannon self-requested a review Jan 15, 2020
try:
setattr(parent_module, child, module)
except AttributeError:
msg = (f"Can't set child package '{child}' on "

This comment has been minimized.

Copy link
@brettcannon

brettcannon Jan 15, 2020

Member
Suggested change
msg = (f"Can't set child package '{child}' on "
msg = (f"Cannot set an attribute on {parent!r} for child module {child!r}"
@@ -1339,6 +1342,39 @@ def test_circular_from_import(self):
str(cm.exception),
)

def test_unwritable_module(self):
package = inspect.cleandoc('''

This comment has been minimized.

Copy link
@brettcannon

brettcannon Jan 15, 2020

Member

Any reason to not put this as a module in test_import/data/?

This comment has been minimized.

Copy link
@DinoV

DinoV Jan 16, 2020

Author Contributor

Nope, I just didn't realize that was a thing :)

@bedevere-bot

This comment has been minimized.

Copy link

bedevere-bot commented Jan 15, 2020

When you're done making the requested changes, leave the comment: I have made the requested changes; please review again.

@DinoV

This comment has been minimized.

Copy link
Contributor Author

DinoV commented Jan 16, 2020

I have made the requested changes; please review again.

@bedevere-bot

This comment has been minimized.

Copy link

bedevere-bot commented Jan 16, 2020

Thanks for making the requested changes!

@brettcannon: please review the changes made to this pull request.

@bedevere-bot bedevere-bot requested a review from brettcannon Jan 16, 2020
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'))

This comment has been minimized.

Copy link
@brettcannon

brettcannon Jan 17, 2020

Member

Why the path insertion instead of importing test.test_import.data.unwritable?

This comment has been minimized.

Copy link
@DinoV

DinoV Jan 22, 2020

Author Contributor

I was actually just following the pattern I saw in test_concurrency but this is certainly much cleaner!

finally:
del sys.modules['spam.x']

sys.modules['unwritable.x']

This comment has been minimized.

Copy link
@brettcannon

brettcannon Jan 17, 2020

Member

If this is delete the module then you can use a helper in test.support and register it with addCleanup().

This comment has been minimized.

Copy link
@DinoV

DinoV Jan 22, 2020

Author Contributor

Thanks, will do!

@bedevere-bot

This comment has been minimized.

Copy link

bedevere-bot commented Jan 17, 2020

When you're done making the requested changes, leave the comment: I have made the requested changes; please review again.

Copy link
Contributor Author

DinoV left a comment

I have made the requested changes; please review again.

@brettcannon brettcannon self-requested a review Jan 22, 2020
@brettcannon

This comment has been minimized.

Copy link
Member

brettcannon commented Jan 22, 2020

Looks like Bedevere/GitHub hiccuped, so I manually updated the status for another review.

Copy link
Member

brettcannon left a comment

Two minor things to clean up, but I trust you to do that before you hit the green button. 😄

DinoV and others added 4 commits Jan 23, 2020
Co-Authored-By: Brett Cannon <54418+brettcannon@users.noreply.github.com>
….nJ7W9I.rst

Co-Authored-By: Brett Cannon <54418+brettcannon@users.noreply.github.com>
Remove unnecessary change in _ready_to_import
Remove white space change
@DinoV

This comment has been minimized.

Copy link
Contributor Author

DinoV commented Jan 23, 2020

Made one other small tweak to remove the change to _ready_to_import which was no longer necessary

@DinoV DinoV merged commit 9b6fec4 into python:master Jan 23, 2020
9 checks passed
9 checks passed
Docs
Details
Windows (x86)
Details
Windows (x64)
Details
macOS
Details
Ubuntu
Details
Azure Pipelines PR #20200123.4 succeeded
Details
bedevere/issue-number Issue number 39336 found
Details
bedevere/news News entry found in Misc/NEWS.d
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@DinoV DinoV deleted the DinoV:bpo_39336_error branch Jan 23, 2020
@bedevere-bot

This comment has been minimized.

Copy link

bedevere-bot commented Jan 23, 2020

@DinoV: Please replace # with GH- in the commit message next time. Thanks!

@bedevere-bot

This comment has been minimized.

Copy link

bedevere-bot commented Jan 23, 2020

⚠️⚠️⚠️ Buildbot failure ⚠️⚠️⚠️

Hi! The buildbot x86 Gentoo Installed with X 3.x has failed when building commit 9b6fec4.

What do you need to do:

  1. Don't panic.
  2. Check the buildbot page in the devguide if you don't know what the buildbots are or how they work.
  3. Go to the page of the buildbot that failed (https://buildbot.python.org/all/#builders/128/builds/181) and take a look at the build logs.
  4. Check if the failure is related to this commit (9b6fec4) or if it is a false positive.
  5. If the failure is related to this commit, please, reflect that on the issue and make a new Pull Request with a fix.

You can take a look at the buildbot page here:

https://buildbot.python.org/all/#builders/128/builds/181

Failed tests:

  • test_import

Failed subtests:

  • test_unwritable_module - test.test_import.CircularImportTests

Summary of the results of the build (if available):

== Tests result: FAILURE then FAILURE ==

406 tests OK.

1 test failed:
test_import

13 tests skipped:
test_asdl_parser test_check_c_globals test_clinic test_devpoll
test_gdb test_ioctl test_kqueue test_msilib test_startfile
test_winconsoleio test_winreg test_winsound test_zipfile64

1 re-run test:
test_import

Total duration: 24 min 4 sec

Click to see traceback logs
TracebackTests) ... ok


Traceback (most recent call last):
  File "/buildbot/buildarea/cpython/3.x.ware-gentoo-x86.installed/build/target/lib/python3.9/test/test_import/__init__.py", line 1347, in test_unwritable_module
    import test.test_import.data.unwritable as unwritable
ModuleNotFoundError: No module named 'test.test_import.data.unwritable'
@pablogsal

This comment has been minimized.

Copy link
Member

pablogsal commented Jan 23, 2020

Hummmm, seems that that buildbot failure is legitimate as this is the new test:

Traceback (most recent call last):
  File "/buildbot/buildarea/cpython/3.x.ware-gentoo-x86.installed/build/target/lib/python3.9/test/test_import/__init__.py", line 1347, in test_unwritable_module
    import test.test_import.data.unwritable as unwritable
ModuleNotFoundError: No module named 'test.test_import.data.unwritable'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants
You can’t perform that action at this time.