From 0b63c1514ec739c276d716fcc3a7a18d645768d9 Mon Sep 17 00:00:00 2001 From: CuriousLearner Date: Sat, 3 Jun 2017 13:25:02 +0530 Subject: [PATCH 01/10] bpo-24744: Raises error in pkgutil.walk_packages if path is str --- Lib/pkgutil.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Lib/pkgutil.py b/Lib/pkgutil.py index e37ad4519666c1..8d8391de93564a 100644 --- a/Lib/pkgutil.py +++ b/Lib/pkgutil.py @@ -119,6 +119,9 @@ def iter_modules(path=None, prefix=''): """ if path is None: importers = iter_importers() + elif isinstance(path, str): + raise TypeError("path must be None or list of paths to look for " + "modules in") else: importers = map(get_importer, path) From 01d03edddc41225d2ed8fe003b888abe07890981 Mon Sep 17 00:00:00 2001 From: CuriousLearner Date: Sun, 4 Jun 2017 01:10:04 +0530 Subject: [PATCH 02/10] bpo-24744: Add bad input test for pkgutil.walk_packages() function --- Lib/pkgutil.py | 4 ++-- Lib/test/test_pkgutil.py | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/Lib/pkgutil.py b/Lib/pkgutil.py index 8d8391de93564a..0f982bdbe5bf04 100644 --- a/Lib/pkgutil.py +++ b/Lib/pkgutil.py @@ -119,8 +119,8 @@ def iter_modules(path=None, prefix=''): """ if path is None: importers = iter_importers() - elif isinstance(path, str): - raise TypeError("path must be None or list of paths to look for " + elif isinstance(path, str) or isinstance(path, bytes): + raise ValueError("path must be None or list of paths to look for " "modules in") else: importers = map(get_importer, path) diff --git a/Lib/test/test_pkgutil.py b/Lib/test/test_pkgutil.py index fc04dcfd7fc414..58184ceb06c3b8 100644 --- a/Lib/test/test_pkgutil.py +++ b/Lib/test/test_pkgutil.py @@ -176,6 +176,21 @@ def test_walkpackages_zipfile(self): continue del sys.modules[pkg] + def test_walkpackages_on_bad_input(self): + """Test that walk_packages() throws ValueError if path is str or bytes + object. + """ + str_input = 'test_dir' + with self.assertRaises(ValueError) as context: + list(pkgutil.walk_packages(str_input)) + + self.assertTrue('path must be None or list of paths' in str(context.exception)) + + bytes_input = b'test_dir' + with self.assertRaises(ValueError) as context: + list(pkgutil.walk_packages(bytes_input)) + + self.assertTrue('path must be None or list of paths' in str(context.exception)) class PkgutilPEP302Tests(unittest.TestCase): From 675d644d66ff424dff42cecbb72cdbc0b448a3a5 Mon Sep 17 00:00:00 2001 From: CuriousLearner Date: Mon, 5 Jun 2017 11:19:52 +0530 Subject: [PATCH 03/10] bpo-24744: Address review comments --- Lib/pkgutil.py | 2 +- Lib/test/test_pkgutil.py | 8 ++------ 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/Lib/pkgutil.py b/Lib/pkgutil.py index 0f982bdbe5bf04..9180eaed84dfd6 100644 --- a/Lib/pkgutil.py +++ b/Lib/pkgutil.py @@ -119,7 +119,7 @@ def iter_modules(path=None, prefix=''): """ if path is None: importers = iter_importers() - elif isinstance(path, str) or isinstance(path, bytes): + elif isinstance(path, str): raise ValueError("path must be None or list of paths to look for " "modules in") else: diff --git a/Lib/test/test_pkgutil.py b/Lib/test/test_pkgutil.py index 58184ceb06c3b8..61f4ab5b738761 100644 --- a/Lib/test/test_pkgutil.py +++ b/Lib/test/test_pkgutil.py @@ -181,17 +181,13 @@ def test_walkpackages_on_bad_input(self): object. """ str_input = 'test_dir' - with self.assertRaises(ValueError) as context: + with self.assertRaises(ValueError): list(pkgutil.walk_packages(str_input)) - self.assertTrue('path must be None or list of paths' in str(context.exception)) - bytes_input = b'test_dir' - with self.assertRaises(ValueError) as context: + with self.assertRaises(TypeError): list(pkgutil.walk_packages(bytes_input)) - self.assertTrue('path must be None or list of paths' in str(context.exception)) - class PkgutilPEP302Tests(unittest.TestCase): From b3f8f628daf19fa95f30cd632ae729a2700f33d9 Mon Sep 17 00:00:00 2001 From: CuriousLearner Date: Tue, 6 Jun 2017 10:20:43 +0530 Subject: [PATCH 04/10] bpo-24744: Address review comments in pkgutil tests --- Lib/test/test_pkgutil.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_pkgutil.py b/Lib/test/test_pkgutil.py index 61f4ab5b738761..c00dc7f5f00c09 100644 --- a/Lib/test/test_pkgutil.py +++ b/Lib/test/test_pkgutil.py @@ -181,11 +181,11 @@ def test_walkpackages_on_bad_input(self): object. """ str_input = 'test_dir' - with self.assertRaises(ValueError): + with self.assertRaises((TypeError, ValueError)): list(pkgutil.walk_packages(str_input)) bytes_input = b'test_dir' - with self.assertRaises(TypeError): + with self.assertRaises((TypeError, ValueError)): list(pkgutil.walk_packages(bytes_input)) From 9fb5eac369370c726ab40d07900d7e1ef8c1d74b Mon Sep 17 00:00:00 2001 From: CuriousLearner Date: Tue, 6 Jun 2017 10:45:33 +0530 Subject: [PATCH 05/10] bpo-24744: Fixes docstring of test_walkpackages_on_bad_input method --- Lib/test/test_pkgutil.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_pkgutil.py b/Lib/test/test_pkgutil.py index c00dc7f5f00c09..9012d16d340f5e 100644 --- a/Lib/test/test_pkgutil.py +++ b/Lib/test/test_pkgutil.py @@ -177,8 +177,8 @@ def test_walkpackages_zipfile(self): del sys.modules[pkg] def test_walkpackages_on_bad_input(self): - """Test that walk_packages() throws ValueError if path is str or bytes - object. + """Test that walk_packages() throws TypeError / ValueError if path is + str or bytes object. """ str_input = 'test_dir' with self.assertRaises((TypeError, ValueError)): From d9dd7a30c7bd581552af642b5bd84cce534ea749 Mon Sep 17 00:00:00 2001 From: CuriousLearner Date: Tue, 6 Jun 2017 18:15:23 +0530 Subject: [PATCH 06/10] bpo-24744: Address review comments --- Lib/test/test_pkgutil.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_pkgutil.py b/Lib/test/test_pkgutil.py index 9012d16d340f5e..2887ce6cc055da 100644 --- a/Lib/test/test_pkgutil.py +++ b/Lib/test/test_pkgutil.py @@ -176,10 +176,8 @@ def test_walkpackages_zipfile(self): continue del sys.modules[pkg] - def test_walkpackages_on_bad_input(self): - """Test that walk_packages() throws TypeError / ValueError if path is - str or bytes object. - """ + def test_walk_packages_raises_on_string_or_bytes_input(self): + str_input = 'test_dir' with self.assertRaises((TypeError, ValueError)): list(pkgutil.walk_packages(str_input)) From 082449df090bbe609da592853e55de70d9a382b8 Mon Sep 17 00:00:00 2001 From: CuriousLearner Date: Tue, 6 Jun 2017 19:09:36 +0530 Subject: [PATCH 07/10] bpo-24744: Adds entry in Whatsnew/3.7.rst and NEWS section --- Doc/whatsnew/3.7.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Doc/whatsnew/3.7.rst b/Doc/whatsnew/3.7.rst index 761c85fd22084b..868dc86274e66d 100644 --- a/Doc/whatsnew/3.7.rst +++ b/Doc/whatsnew/3.7.rst @@ -330,6 +330,11 @@ that may require changes to your code. Changes in the Python API ------------------------- +* :meth:`pkgutil.walk_packages` now raises ValueError/TypeError if path is + string/bytes object. + Previously an empty list was returned. (Contributed by Sanyam Khurana in + :issue:`24744`.) + * A format string argument for :meth:`string.Formatter.format` is now :ref:`positional-only `. Passing it as a keyword argument was deprecated in Python 3.5. (Contributed From 626599b2b41ce24e4014c4dd9e995d6e4e66a14f Mon Sep 17 00:00:00 2001 From: CuriousLearner Date: Tue, 6 Jun 2017 21:10:27 +0530 Subject: [PATCH 08/10] bpo-24744: Adds entry in Whatsnew/3.7.rst and NEWS section --- Doc/whatsnew/3.7.rst | 7 +++---- Misc/NEWS | 4 +++- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Doc/whatsnew/3.7.rst b/Doc/whatsnew/3.7.rst index 868dc86274e66d..bd6632e5e1cee2 100644 --- a/Doc/whatsnew/3.7.rst +++ b/Doc/whatsnew/3.7.rst @@ -330,10 +330,9 @@ that may require changes to your code. Changes in the Python API ------------------------- -* :meth:`pkgutil.walk_packages` now raises ValueError/TypeError if path is - string/bytes object. - Previously an empty list was returned. (Contributed by Sanyam Khurana in - :issue:`24744`.) +* :meth:`pkgutil.walk_packages` now raises ValueError if path is a string + object. Previously an empty list was returned. (Contributed by Sanyam + Khurana in :issue:`24744`.) * A format string argument for :meth:`string.Formatter.format` is now :ref:`positional-only `. diff --git a/Misc/NEWS b/Misc/NEWS index 902b102ac72d5c..1b315439fc7f22 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -345,9 +345,11 @@ Extension Modules Library ------- +- bpo-24744: pkgutil.walk_packages function raises ValueError if path is of + str object type. Patch by Sanyam Khurana. + - bpo-30245: Fix possible overflow when organize struct.pack_into error message. Patch by Yuan Liu. - - bpo-30378: Fix the problem that logging.handlers.SysLogHandler cannot handle IPv6 addresses. From e61e323e9fceff2e8911bdc4e658a014129c6785 Mon Sep 17 00:00:00 2001 From: CuriousLearner Date: Tue, 6 Jun 2017 21:13:27 +0530 Subject: [PATCH 09/10] bpo-24744: Minor fix in Misc/NEWS --- Misc/NEWS | 1 + 1 file changed, 1 insertion(+) diff --git a/Misc/NEWS b/Misc/NEWS index 1b315439fc7f22..a6c1e92a12b90c 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -350,6 +350,7 @@ Library - bpo-30245: Fix possible overflow when organize struct.pack_into error message. Patch by Yuan Liu. + - bpo-30378: Fix the problem that logging.handlers.SysLogHandler cannot handle IPv6 addresses. From bb26556104f4bd98e20690340df0d810751d9a4f Mon Sep 17 00:00:00 2001 From: CuriousLearner Date: Tue, 6 Jun 2017 23:19:22 +0530 Subject: [PATCH 10/10] bpo-24744: Minor fix in NEWS and whatsnew/3.7.rst --- Doc/whatsnew/3.7.rst | 6 +++--- Misc/NEWS | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Doc/whatsnew/3.7.rst b/Doc/whatsnew/3.7.rst index bd6632e5e1cee2..2288aee99091f8 100644 --- a/Doc/whatsnew/3.7.rst +++ b/Doc/whatsnew/3.7.rst @@ -330,9 +330,9 @@ that may require changes to your code. Changes in the Python API ------------------------- -* :meth:`pkgutil.walk_packages` now raises ValueError if path is a string - object. Previously an empty list was returned. (Contributed by Sanyam - Khurana in :issue:`24744`.) +* :meth:`pkgutil.walk_packages` now raises ValueError if *path* is a string. + Previously an empty list was returned. (Contributed by Sanyam Khurana in + :issue:`24744`.) * A format string argument for :meth:`string.Formatter.format` is now :ref:`positional-only `. diff --git a/Misc/NEWS b/Misc/NEWS index a6c1e92a12b90c..c8bc874f7b4b6a 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -345,8 +345,8 @@ Extension Modules Library ------- -- bpo-24744: pkgutil.walk_packages function raises ValueError if path is of - str object type. Patch by Sanyam Khurana. +- bpo-24744: pkgutil.walk_packages function now raises ValueError if *path* + is a string. Patch by Sanyam Khurana. - bpo-30245: Fix possible overflow when organize struct.pack_into error message. Patch by Yuan Liu.