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-30436: Fix exception raised for invalid parent. #1899

Merged
merged 12 commits into from Jun 14, 2017
Merged

bpo-30436: Fix exception raised for invalid parent. #1899

merged 12 commits into from Jun 14, 2017

Conversation

zvyn
Copy link
Contributor

@zvyn zvyn commented Jun 1, 2017

Changes find_spec() to raise ModuleNotFoundError instead of
AttributeError when parent does not exist or is not a package.

Changes `find_spec()` to raise `ModuleNotFoundError` instead of
`AttributeError` when parent does not exist or is not a package.
@mention-bot
Copy link

@zvyn, thanks for your PR! By analyzing the history of the files in this pull request, we identified @brettcannon, @ncoghlan and @ericsnowcurrently to be potential reviewers.

parent_path = __import__(parent_name,
fromlist=['__path__']).__path__
except AttributeError as e:
raise ModuleNotFoundError(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the name argument be passed?

Copy link
Contributor Author

@zvyn zvyn Jun 1, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it should! I'll updated the PR

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the AttributeError purely from the attempt to access __path__ on the imported module? If so then the __import__() call should go outside the try block to limit the scope of what will have an AttributeError caught. If there's another potential trigger of the AttributeError then we have a bigger problem. ;)

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a Misc/NEWS entry. The rest LGTM.

Copy link
Member

@brettcannon brettcannon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

General idea is good, just some tightening up of some things. And could you also add a .. versionchanged: note to the docs for importlib.util.find_spec() about the exception shift?

Once this is all good to go we can add the news and ACKS entries (I'm putting the former off as long as possible as the way to do it might be changing in the near future).

parent_path = __import__(parent_name,
fromlist=['__path__']).__path__
except AttributeError as e:
raise ModuleNotFoundError(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the AttributeError purely from the attempt to access __path__ on the imported module? If so then the __import__() call should go outside the try block to limit the scope of what will have an AttributeError caught. If there's another potential trigger of the AttributeError then we have a bigger problem. ;)

fromlist=['__path__']).__path__
except AttributeError as e:
raise ModuleNotFoundError(
"Error while finding module specification for '{}'."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is going to be a Python 3.7-only change so might as well use an f-string.

@@ -522,6 +522,15 @@ def test_find_relative_module_missing_package(self):
self.assertNotIn(name, sorted(sys.modules))
self.assertNotIn(fullname, sorted(sys.modules))

def test_ModuleNotFoundError_raised_for_broken_module_name(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not that the name is broken, it's that someone tried to import a submodule on something that isn't a package. So a better name might be test_find_submodule_in_module(self).

Also remove the docstring please (unittest prints that out instead of the method name in verbose mode and we prefer the former to make it easier to find the failing test, plus the formatting doesn't follow PEP 8).

@zvyn
Copy link
Contributor Author

zvyn commented Jun 7, 2017

@brettcannon thanks for the review! I committed some changes taking your comments into account

@brettcannon brettcannon self-assigned this Jun 7, 2017
@brettcannon
Copy link
Member

I made a very minor tweak, @zvyn, of dropping an unnecessary f prefix on a string, but otherwise the code changes LGTM!

Care to add an entry in Misc/NEWS and yourself to Misc/ACKS (if you're not already there)?

@zvyn
Copy link
Contributor Author

zvyn commented Jun 13, 2017

I'm travelling at the moment, but I might find some time at the airport tomorrow.

@brettcannon
Copy link
Member

@zvyn no problem. Python 3.7 isn't exactly coming out soon. 😉 Just leave a comment for me when you're done.

@zvyn
Copy link
Contributor Author

zvyn commented Jun 14, 2017

@brettcannon done! I assume Misc/NEWS will be out of date the minute I merge so I'll leave that to you :)

@brettcannon
Copy link
Member

@zvyn Yes and that issue is being worked on. 😄

I have fixed the conflict by moving your entry down a ways so it's randomly inserted in the appropriate section. I'm now just waiting for status checks to pass again.

@brettcannon brettcannon merged commit 8c3f05e into python:master Jun 14, 2017
@brettcannon
Copy link
Member

Thanks for the PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants