mirror of
https://github.com/python/cpython.git
synced 2025-10-06 07:02:33 +00:00
[3.6] bpo-31566: Fix an assertion failure in _warnings.warn() in case of a bad __name__ global. (GH-3717) (#3730)
(cherry picked from commit 5d3e80021a
)
This commit is contained in:
parent
ce418bf822
commit
415cc1fa57
3 changed files with 16 additions and 3 deletions
|
@ -820,6 +820,16 @@ class _WarningsTests(BaseTest, unittest.TestCase):
|
||||||
self.assertRaises(TypeError):
|
self.assertRaises(TypeError):
|
||||||
wmod.warn_explicit('foo', Warning, 'bar', 1)
|
wmod.warn_explicit('foo', Warning, 'bar', 1)
|
||||||
|
|
||||||
|
@support.cpython_only
|
||||||
|
def test_issue31566(self):
|
||||||
|
# warn() shouldn't cause an assertion failure in case of a bad
|
||||||
|
# __name__ global.
|
||||||
|
with original_warnings.catch_warnings(module=self.module):
|
||||||
|
self.module.filterwarnings('error', category=UserWarning)
|
||||||
|
with support.swap_item(globals(), '__name__', b'foo'), \
|
||||||
|
support.swap_item(globals(), '__file__', None):
|
||||||
|
self.assertRaises(UserWarning, self.module.warn, 'bar')
|
||||||
|
|
||||||
|
|
||||||
class WarningsDisplayTests(BaseTest):
|
class WarningsDisplayTests(BaseTest):
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Fix an assertion failure in `_warnings.warn()` in case of a bad
|
||||||
|
``__name__`` global. Patch by Oren Milman.
|
|
@ -694,13 +694,14 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
|
||||||
|
|
||||||
/* Setup module. */
|
/* Setup module. */
|
||||||
*module = PyDict_GetItemString(globals, "__name__");
|
*module = PyDict_GetItemString(globals, "__name__");
|
||||||
if (*module == NULL) {
|
if (*module == Py_None || (*module != NULL && PyUnicode_Check(*module))) {
|
||||||
|
Py_INCREF(*module);
|
||||||
|
}
|
||||||
|
else {
|
||||||
*module = PyUnicode_FromString("<string>");
|
*module = PyUnicode_FromString("<string>");
|
||||||
if (*module == NULL)
|
if (*module == NULL)
|
||||||
goto handle_error;
|
goto handle_error;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
Py_INCREF(*module);
|
|
||||||
|
|
||||||
/* Setup filename. */
|
/* Setup filename. */
|
||||||
*filename = PyDict_GetItemString(globals, "__file__");
|
*filename = PyDict_GetItemString(globals, "__file__");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue