Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8d88604

Browse files
committed
#14798: pyclbr now raises ImportError instead of KeyError for missing packages
1 parent 1033b31 commit 8d88604

4 files changed

Lines changed: 12 additions & 0 deletions

File tree

Lib/pyclbr.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ def _readmodule(module, path, inpackage=None):
128128
parent = _readmodule(package, path, inpackage)
129129
if inpackage is not None:
130130
package = "%s.%s" % (inpackage, package)
131+
if not '__path__' in parent:
132+
raise ImportError('No package named {}'.format(package))
131133
return _readmodule(submodule, parent['__path__'], package)
132134

133135
# Search the path for the module

Lib/test/test_pyclbr.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,11 @@ def test_others(self):
167167
cm('email.parser')
168168
cm('test.test_pyclbr')
169169

170+
def test_issue_14798(self):
171+
# test ImportError is raised when the first part of a dotted name is
172+
# not a package
173+
self.assertRaises(ImportError, pyclbr.readmodule_ex, 'asyncore.foo')
174+
170175

171176
def test_main():
172177
run_unittest(PyclbrTest)

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ Lars Marius Garshol
332332
Dan Gass
333333
Andrew Gaul
334334
Stephen M. Gava
335+
Xavier de Gaye
335336
Harry Henry Gebel
336337
Marius Gedminas
337338
Thomas Gellekum

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ Core and Builtins
6363
Library
6464
-------
6565

66+
- Issue #14798: Fix the functions in pyclbr to raise an ImportError
67+
when the first part of a dotted name is not a package. Patch by
68+
Xavier de Gaye.
69+
6670
- Issue #14829: Fix bisect and range() indexing with large indices
6771
(>= 2 ** 32) under 64-bit Windows.
6872

0 commit comments

Comments
 (0)