Opened 6 years ago

Closed 6 years ago

#15662 closed Bug (fixed)

module_has_submodule incorrectly uses the Importer Protocol

Reported by: Bradley Ayers <bradley.ayers@…> Owned by: nobody
Component: Core (Other) Version: 1.2
Severity: Normal Keywords:
Cc: lrekucki@… Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

django.utils.module_loading.module_has_submodule has the code:

for finder in sys.meta_path:
    if finder.find_module(name):
        return True

where name is the absolute path to the module that should be imported. However PEP302 that defines the Importer Protocol says that a second argument must be supplied if the module isn't directly on sys.path.

Thus, the code should be:

for finder in sys.meta_path:
    if finder.find_module(name, package):
        return True

I came across this problem with using Attest, Django, and loading a template tag that had the same name as a package on my PATH.

Attachments (3)

module_loading.py.patch (491 bytes) - added by Bradley Ayers <bradley.ayers@…> 6 years ago.
module_loading.py.2.patch (492 bytes) - added by Bradley Ayers <bradley.ayers@…> 6 years ago.
Fixed typo
module_has_submodule.patch (2.7 KB) - added by Bradley Ayers <bradley.ayers@…> 6 years ago.
Added tests

Download all attachments as: .zip

Change History (10)

Changed 6 years ago by Bradley Ayers <bradley.ayers@…>

Attachment: module_loading.py.patch added

comment:1 Changed 6 years ago by Łukasz Rekucki

Cc: lrekucki@… added

comment:2 Changed 6 years ago by Alex Gaynor

a) There's a typo in the patch, b) could use tests, c) <redacted> :)

Changed 6 years ago by Bradley Ayers <bradley.ayers@…>

Attachment: module_loading.py.2.patch added

Fixed typo

Changed 6 years ago by Bradley Ayers <bradley.ayers@…>

Attachment: module_has_submodule.patch added

Added tests

comment:3 Changed 6 years ago by Bradley Ayers <bradley.ayers@…>

Component: UncategorizedCore framework

comment:4 Changed 6 years ago by Luke Plant

Type: Bug

comment:5 Changed 6 years ago by Luke Plant

Severity: Normal

comment:6 Changed 6 years ago by Luke Plant

milestone: 1.3
Triage Stage: UnreviewedReady for checkin

comment:7 Changed 6 years ago by Jannis Leidel

Resolution: fixed
Status: newclosed

In [16075]:

Fixed #15662 -- Made sure the module_has_submodule utility function follow correct PEP 302, passing the package as the second argument to the find_module method of the importer. Thanks, Bradley Ayers.

Note: See TracTickets for help on using tickets.
Back to Top