Skip to content

Commit c38e32a

Browse files
commodobrettcannon
authored andcommitted
bpo-30645: don't append to an inner loop path in imp.load_package() (GH-2268)
Bug didn't manifest itself when importing a module with source as .py files are always the first on the search path. The issue only showed up in bytecode-only packages where the calculated file path would be ``__init__.py/__init__.pyc``. Patch by Alexandru Ardelean.
1 parent d174d24 commit c38e32a

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

Lib/imp.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,9 @@ def load_package(name, path):
203203
extensions = (machinery.SOURCE_SUFFIXES[:] +
204204
machinery.BYTECODE_SUFFIXES[:])
205205
for extension in extensions:
206-
path = os.path.join(path, '__init__'+extension)
207-
if os.path.exists(path):
206+
init_path = os.path.join(path, '__init__' + extension)
207+
if os.path.exists(init_path):
208+
path = init_path
208209
break
209210
else:
210211
raise ValueError('{!r} is not a package'.format(path))

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Ankur Ankan
5757
Heidi Annexstad
5858
Ramchandra Apte
5959
Éric Araujo
60+
Alexandru Ardelean
6061
Alicia Arlen
6162
Jeffrey Armstrong
6263
Jason Asbahr

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,10 @@ Library
428428
- bpo-30508: Don't log exceptions if Task/Future "cancel()" method was
429429
called.
430430

431+
- bpo-30645: Fix path calculation in `imp.load_package()`, fixing it for
432+
cases when a package is only shipped with bytecodes. Patch by
433+
Alexandru Ardelean.
434+
431435
- bpo-11822: The dis.dis() function now is able to disassemble nested
432436
code objects.
433437

0 commit comments

Comments
 (0)