From: Petri Lehtinen Date: Fri, 18 May 2012 18:51:11 +0000 (+0300) Subject: #14798: pyclbr now raises ImportError instead of KeyError for missing packages X-Git-Tag: v3.3.0a4~127^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=8d886046821f1cd43147c340b04ee0f067157749;p=python #14798: pyclbr now raises ImportError instead of KeyError for missing packages --- diff --git a/Lib/pyclbr.py b/Lib/pyclbr.py index 52cbdd53ab..65e9fbedfd 100644 --- a/Lib/pyclbr.py +++ b/Lib/pyclbr.py @@ -128,6 +128,8 @@ def _readmodule(module, path, inpackage=None): parent = _readmodule(package, path, inpackage) if inpackage is not None: package = "%s.%s" % (inpackage, package) + if not '__path__' in parent: + raise ImportError('No package named {}'.format(package)) return _readmodule(submodule, parent['__path__'], package) # Search the path for the module diff --git a/Lib/test/test_pyclbr.py b/Lib/test/test_pyclbr.py index 3082b29dbc..e83989e2d8 100644 --- a/Lib/test/test_pyclbr.py +++ b/Lib/test/test_pyclbr.py @@ -167,6 +167,11 @@ class PyclbrTest(TestCase): cm('email.parser') cm('test.test_pyclbr') + def test_issue_14798(self): + # test ImportError is raised when the first part of a dotted name is + # not a package + self.assertRaises(ImportError, pyclbr.readmodule_ex, 'asyncore.foo') + def test_main(): run_unittest(PyclbrTest) diff --git a/Misc/ACKS b/Misc/ACKS index 8aab2bed11..d8ffe35ea2 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -332,6 +332,7 @@ Lars Marius Garshol Dan Gass Andrew Gaul Stephen M. Gava +Xavier de Gaye Harry Henry Gebel Marius Gedminas Thomas Gellekum diff --git a/Misc/NEWS b/Misc/NEWS index 565db1bd24..1e7153d080 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -63,6 +63,10 @@ Core and Builtins Library ------- +- Issue #14798: Fix the functions in pyclbr to raise an ImportError + when the first part of a dotted name is not a package. Patch by + Xavier de Gaye. + - Issue #14829: Fix bisect and range() indexing with large indices (>= 2 ** 32) under 64-bit Windows.