-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
bpo-31315: Fix an assertion failure in imp.create_dynamic(), when spec.name is not a string #3257
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-31315: Fix an assertion failure in imp.create_dynamic(), when spec.name is not a string #3257
Conversation
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.
The actual change looks good, just needs a blank line before the new decorator function in the tests
Lib/test/test_imp.py
Outdated
@@ -21,6 +21,12 @@ def requires_load_dynamic(meth): | |||
meth = support.cpython_only(meth) | |||
return unittest.skipIf(not hasattr(imp, 'load_dynamic'), | |||
'imp.load_dynamic() required')(meth) | |||
def requires_create_dynamic(meth): |
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.
Readability improvement: blank line before the function
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 for doing this, Oren. I've left a few minor comments for what I consider nits. I don't consider them blockers so at least just consider them. :) Otherwise LGTM.
Lib/test/test_imp.py
Outdated
@@ -22,6 +22,13 @@ def requires_load_dynamic(meth): | |||
return unittest.skipIf(not hasattr(imp, 'load_dynamic'), | |||
'imp.load_dynamic() required')(meth) | |||
|
|||
def requires_create_dynamic(meth): | |||
"""Decorator to skip a test if not running under CPython or lacking | |||
imp.create_dynamic().""" |
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.
Let me be clear that my recommendation here is not critical. Rather it is a reflection of my belief that little suboptimal things add up over time to become pain points. Docstrings (including in test code) are one of those things we cut corners on sometimes.
While there is precedent (unfortunately) for docstrings formatted this way, I'd rather this docstring were a bit more idiomatic. It's better to take a little time now to strengthen a healthier precedent. This is a case where a little bit of extra time spent adds up over time to make a positive difference. I don't mean to be pedantic here about a relatively insignificant style point or to waste anyone's time, but instead want to encourage a more thoughtful approach with regard to the little things. :) With that out of the way...
I'd expect a docstring here like one of the following (and including some shortening):
# one line
"""Decorator to skip a test if not running under CPython or lacking imp.create_dynamic()."""
or
# one line with separate quotation mark lines
"""
Decorator to skip a test if not running under CPython or lacking imp.create_dynamic().
"""
or
# split into multiple lines (my preference)
"""A decorator to skip tests that rely on imp.create_dynamic().
Not all Python implementations necessarily provide imp.create_dynamic().
This decorators causes decorated tests to be skipped through the normal
unittest mechanism.
"""
Lib/test/test_imp.py
Outdated
imp.create_dynamic().""" | ||
meth = support.cpython_only(meth) | ||
return unittest.skipIf(not hasattr(imp, 'create_dynamic'), | ||
'imp.create_dynamic() required')(meth) |
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.
FWIW, as a reader it's easy to miss something when more than one thing is happening on a line. It might be more clear to break this up:
deco = unittest.skipIf(not hasattr(imp, 'create_dynamic'),
'imp.create_dynamic() required')
return deco(meth)
or even
supported = hasattr(imp, 'create_dynamic')
deco = unittest.skipIf(not supported, 'imp.create_dynamic() required')
return deco(meth)
Lib/test/test_imp.py
Outdated
# There shouldn't be an assertion failure in imp.create_dynamic(), | ||
# when spec.name is not a string. | ||
class BadSpec: | ||
name = 42 |
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.
Use something more realistic. A bytes object or None
.
Lib/test/test_imp.py
Outdated
# when spec.name is not a string. | ||
class BadSpec: | ||
name = 42 | ||
origin = 'foo' |
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 origin is not a string SystemError can be raised. This should be fixed too.
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.
what code causes a SystemError?
I got a TypeError: bad argument type for built-in operation
both on my Windows and Ubuntu.
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.
Your are right. I thought _PyUnicode_AsUnicode()
raises SystemError, but it raises TypeError. No additional check is needed.
Lib/test/test_imp.py
Outdated
""" | ||
meth = support.cpython_only(meth) | ||
supported = hasattr(imp, 'create_dynamic') | ||
deco = unittest.skipIf(not supported, 'imp.create_dynamic() required') |
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 prefer skipUnless()
.
But why the requires_create_dynamic
decorator is needed? It is used just once. Why not just use two decorators directly?
@unittest.skipUnless(hasattr(imp, 'create_dynamic'), 'imp.create_dynamic() required')
@support.cpython_only
def test_issue31315(self):
...
Or use special purposed support.get_attribute()
:
@support.cpython_only
def test_issue31315(self):
create_dynamic = support.get_attribute(imp, 'create_dynamic')
...
Thanks @orenmn for the PR, and @serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 3.6. |
…en spec.name is not a string. (pythonGH-3257) (cherry picked from commit 9974e1b)
GH-3653 is a backport of this pull request to the 3.6 branch. |
importdl.c
- add a check whether spec.name is not a string.test_imp.py
- add a test to verify that the assertion failure is no more.https://bugs.python.org/issue31315