bpo-31566: Fix an assertion failure in _warnings.warn() in case of a bad __name__ global#3717
Conversation
serhiy-storchaka
left a comment
There was a problem hiding this comment.
There are many ways to handle this issue and this way looks the right way to me.
| with support.swap_item(globals(), '__name__', b'foo'), \ | ||
| support.swap_item(globals(), '__file__', None), \ | ||
| support.captured_stderr(): | ||
| self.module.warn('bar') |
There was a problem hiding this comment.
Raise an exception with -Werror.
| /* Setup module. */ | ||
| *module = PyDict_GetItemString(globals, "__name__"); | ||
| if (*module == NULL) { | ||
| if (*module == NULL || |
There was a problem hiding this comment.
This expression is slightly hard to understand. I would rewrite it as
if (*module == Py_None || (*module != NULL && PyUnicode_Check(*module))) {
Py_INCREF(*module);
}
else {
...
or
if (*module != NULL && (*module == Py_None || PyUnicode_Check(*module))) {
Py_INCREF(*module);
}
else {
...
|
Thanks @orenmn for the PR, and @serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 2.7, 3.6. |
|
GH-3730 is a backport of this pull request to the 3.6 branch. |
… of a bad __name__ global. (pythonGH-3717) (cherry picked from commit 5d3e800)
|
Thanks @orenmn for the PR, and @serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 2.7. |
|
Sorry, @orenmn and @serhiy-storchaka, I could not cleanly backport this to |
_warnings.c: add a check whether__name__isn't a string.test_warnings/__init__.py: add a test to verify that the assertion failure is no more.https://bugs.python.org/issue31566