From 2c7e223a4ced4849feb1b3209cef1faf61666eba Mon Sep 17 00:00:00 2001 From: Oren Milman Date: Sun, 24 Sep 2017 09:59:26 +0300 Subject: [PATCH 1/5] init commit --- Lib/test/test_warnings/__init__.py | 10 ++++++++++ .../2017-09-24-09-57-04.bpo-31566.OxwINs.rst | 2 ++ Python/_warnings.c | 2 +- 3 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2017-09-24-09-57-04.bpo-31566.OxwINs.rst diff --git a/Lib/test/test_warnings/__init__.py b/Lib/test/test_warnings/__init__.py index 2d0c46f409ae888..43b13a5ecd58187 100644 --- a/Lib/test/test_warnings/__init__.py +++ b/Lib/test/test_warnings/__init__.py @@ -820,6 +820,16 @@ def test_issue31416(self): self.assertRaises(TypeError): 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. + wmod = self.module + with support.swap_item(globals(), '__name__', b'foo'), \ + support.swap_item(globals(), '__file__', None), \ + support.captured_stderr(): + wmod.warn('bar') + class WarningsDisplayTests(BaseTest): diff --git a/Misc/NEWS.d/next/Core and Builtins/2017-09-24-09-57-04.bpo-31566.OxwINs.rst b/Misc/NEWS.d/next/Core and Builtins/2017-09-24-09-57-04.bpo-31566.OxwINs.rst new file mode 100644 index 000000000000000..d3ccfd7a2429438 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2017-09-24-09-57-04.bpo-31566.OxwINs.rst @@ -0,0 +1,2 @@ +Fix an assertion failure in `_warnings.warn()` in case of a bad +``__name__`` global. Patch by Oren Milman. diff --git a/Python/_warnings.c b/Python/_warnings.c index f6688b040679ad2..f4471672e1ef352 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -684,7 +684,7 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno, /* Setup module. */ *module = PyDict_GetItemString(globals, "__name__"); - if (*module == NULL) { + if (*module == NULL || !PyUnicode_Check(*module)) { *module = PyUnicode_FromString(""); if (*module == NULL) goto handle_error; From 3873b4908dd54318fd5ae6425479c255cb4d874b Mon Sep 17 00:00:00 2001 From: Oren Milman Date: Sun, 24 Sep 2017 10:09:20 +0300 Subject: [PATCH 2/5] simplify the test --- Lib/test/test_warnings/__init__.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Lib/test/test_warnings/__init__.py b/Lib/test/test_warnings/__init__.py index 43b13a5ecd58187..6ee0511bbb7616e 100644 --- a/Lib/test/test_warnings/__init__.py +++ b/Lib/test/test_warnings/__init__.py @@ -824,11 +824,10 @@ def test_issue31416(self): def test_issue31566(self): # warn() shouldn't cause an assertion failure in case of a bad # __name__ global. - wmod = self.module with support.swap_item(globals(), '__name__', b'foo'), \ support.swap_item(globals(), '__file__', None), \ support.captured_stderr(): - wmod.warn('bar') + self.module.warn('bar') class WarningsDisplayTests(BaseTest): From 34f0f03c7417aa0252342f1910ed4c5d6258f6dd Mon Sep 17 00:00:00 2001 From: Oren Milman Date: Sun, 24 Sep 2017 11:10:21 +0300 Subject: [PATCH 3/5] fix a bug in my C code --- Python/_warnings.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Python/_warnings.c b/Python/_warnings.c index f4471672e1ef352..dd3985b3673b9d7 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -684,7 +684,9 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno, /* Setup module. */ *module = PyDict_GetItemString(globals, "__name__"); - if (*module == NULL || !PyUnicode_Check(*module)) { + if (*module == NULL || + (*module != Py_None && !PyUnicode_Check(*module))) + { *module = PyUnicode_FromString(""); if (*module == NULL) goto handle_error; From 2d3505b5e06d1ad16729ab0699623ba2766eb702 Mon Sep 17 00:00:00 2001 From: Oren Milman Date: Sun, 24 Sep 2017 14:39:16 +0300 Subject: [PATCH 4/5] fix the test, and make the C code clearer --- Lib/test/test_warnings/__init__.py | 9 +++++---- Python/_warnings.c | 9 ++++----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Lib/test/test_warnings/__init__.py b/Lib/test/test_warnings/__init__.py index 6ee0511bbb7616e..02f8fd92c89e774 100644 --- a/Lib/test/test_warnings/__init__.py +++ b/Lib/test/test_warnings/__init__.py @@ -824,10 +824,11 @@ def test_issue31416(self): def test_issue31566(self): # warn() shouldn't cause an assertion failure in case of a bad # __name__ global. - with support.swap_item(globals(), '__name__', b'foo'), \ - support.swap_item(globals(), '__file__', None), \ - support.captured_stderr(): - self.module.warn('bar') + with original_warnings.catch_warnings(module=self.module): + self.module.filterwarnings('error') + with support.swap_item(globals(), '__name__', b'foo'), \ + support.swap_item(globals(), '__file__', None): + self.assertRaises(UserWarning, self.module.warn, 'bar') class WarningsDisplayTests(BaseTest): diff --git a/Python/_warnings.c b/Python/_warnings.c index dd3985b3673b9d7..2f240fd454a8d62 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -684,15 +684,14 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno, /* Setup module. */ *module = PyDict_GetItemString(globals, "__name__"); - if (*module == NULL || - (*module != Py_None && !PyUnicode_Check(*module))) - { + if (*module == Py_None || (*module != NULL && PyUnicode_Check(*module))) { + Py_INCREF(*module); + } + else { *module = PyUnicode_FromString(""); if (*module == NULL) goto handle_error; } - else - Py_INCREF(*module); /* Setup filename. */ *filename = PyDict_GetItemString(globals, "__file__"); From 692c79b6b4cf64907e6139758df3c8cc1baee29b Mon Sep 17 00:00:00 2001 From: Oren Milman Date: Sun, 24 Sep 2017 14:45:25 +0300 Subject: [PATCH 5/5] another fix to the test --- Lib/test/test_warnings/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_warnings/__init__.py b/Lib/test/test_warnings/__init__.py index 02f8fd92c89e774..b2cd4c0400d9579 100644 --- a/Lib/test/test_warnings/__init__.py +++ b/Lib/test/test_warnings/__init__.py @@ -825,7 +825,7 @@ 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') + 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')