aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorSanyam Khurana <CuriousLearner@users.noreply.github.com>2017-06-13 22:41:14 +0530
committerR. David Murray <rdmurray@bitdance.com>2017-06-13 13:11:14 -0400
commitb9c3da5c89c66dcccf382e8f196746da2a06d4cc (patch)
treecc99f575ae032c7e98b7cc5d1cc707e4b52dc4a2
parent1eb6c0074d17f4fd425cacfdda893d65f5f77f0a (diff)
downloadcpython-b9c3da5c89c66dcccf382e8f196746da2a06d4cc.tar.gz
cpython-b9c3da5c89c66dcccf382e8f196746da2a06d4cc.zip
bpo-24744: Raises error in pkgutil.walk_packages if path is str (#1926)
bpo-24744: Raise error in pkgutil.walk_packages if path is str Previously an empty result list was accidentallly returned, since the code iterated over the string as if it were the expected list of paths, and of course found nothing.
-rw-r--r--Doc/whatsnew/3.7.rst4
-rw-r--r--Lib/pkgutil.py3
-rw-r--r--Lib/test/test_pkgutil.py9
-rw-r--r--Misc/NEWS3
4 files changed, 19 insertions, 0 deletions
diff --git a/Doc/whatsnew/3.7.rst b/Doc/whatsnew/3.7.rst
index 3cdc0091b39..83e1c615a8d 100644
--- a/Doc/whatsnew/3.7.rst
+++ b/Doc/whatsnew/3.7.rst
@@ -395,6 +395,10 @@ that may require changes to your code.
Changes in the Python API
-------------------------
+* :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 <positional-only_parameter>`.
Passing it as a keyword argument was deprecated in Python 3.5. (Contributed
diff --git a/Lib/pkgutil.py b/Lib/pkgutil.py
index e37ad451966..9180eaed84d 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 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 fc04dcfd7fc..2887ce6cc05 100644
--- a/Lib/test/test_pkgutil.py
+++ b/Lib/test/test_pkgutil.py
@@ -176,6 +176,15 @@ class PkgutilTests(unittest.TestCase):
continue
del sys.modules[pkg]
+ 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))
+
+ bytes_input = b'test_dir'
+ with self.assertRaises((TypeError, ValueError)):
+ list(pkgutil.walk_packages(bytes_input))
class PkgutilPEP302Tests(unittest.TestCase):
diff --git a/Misc/NEWS b/Misc/NEWS
index f91695d3350..31e8ec07385 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -362,6 +362,9 @@ Extension Modules
Library
-------
+- bpo-24744: pkgutil.walk_packages function now raises ValueError if *path*
+ is a string. Patch by Sanyam Khurana.
+
- bpo-24484: Avoid race condition in multiprocessing cleanup.
- bpo-30589: Fix multiprocessing.Process.exitcode to return the opposite