Skip to content

bpo-21720: Improve exception message of __import__()#4113

Closed
berkerpeksag wants to merge 3 commits into
python:masterfrom
berkerpeksag:21720-python3
Closed

bpo-21720: Improve exception message of __import__()#4113
berkerpeksag wants to merge 3 commits into
python:masterfrom
berkerpeksag:21720-python3

Conversation

@berkerpeksag

@berkerpeksag berkerpeksag commented Oct 25, 2017

Copy link
Copy Markdown
Member

Exception message was unclear when the type of
fromlist parameter is not str:

TypeError: hasattr(): attribute name must be string

https://bugs.python.org/issue21720

Exception message was unclear when the type of
fromlist parameter is not str:

    TypeError: hasattr(): attribute name must be string
@ncoghlan

Copy link
Copy Markdown
Contributor

CI runs under -bb, so the tests fail with BytesWarning rather than the expected exception. To avoid that, the type checking loop will need to be a separate loop before the if statement that checks for "*".

@berkerpeksag

Copy link
Copy Markdown
Member Author

Thank you for the review, @ncoghlan. I've fixed the BytesWarning issue.

self.assertEqual(cm.exception.name, SUBMOD_NAME)

def test_fromlist_invalid_type(self):
with self.assertRaises(TypeError) as cm:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Add an 'error' filter for BytesWarning.

"Sequence item 0 in 'from list' must be str, not 'bytes'"
)
with self.assertRaises(TypeError) as cm:
self.__import__('encodings', fromlist=['aliases', b'codecs'])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think it is better to use 'utf_8' instead of 'codecs'. 'codecs' is just an outer module imported in 'encodings'. It could be imported as '_codecs' for example.

@ncoghlan

Copy link
Copy Markdown
Contributor

@serhiy-storchaka proposed an alternate solution in #4118 (adjusting the importlib behaviour back to more closely matching the behaviour of the old import system), and that seems like a better option to me since it minimises the overhead of the new check, while still avoiding the cryptic bytes warning.

@ncoghlan ncoghlan closed this Oct 26, 2017
@berkerpeksag berkerpeksag deleted the 21720-python3 branch October 26, 2017 02:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants