Code

Opened 3 years ago

Closed 3 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@…> 3 years ago.
module_loading.py.2.patch (492 bytes) - added by Bradley Ayers <bradley.ayers@…> 3 years ago.
Fixed typo
module_has_submodule.patch (2.7 KB) - added by Bradley Ayers <bradley.ayers@…> 3 years ago.
Added tests

Download all attachments as: .zip

Change History (10)

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

comment:1 Changed 3 years ago by lrekucki

  • Cc lrekucki@… added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 3 years ago by Alex

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

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

Fixed typo

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

Added tests

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

  • Component changed from Uncategorized to Core framework

comment:4 Changed 3 years ago by lukeplant

  • Type set to Bug

comment:5 Changed 3 years ago by lukeplant

  • Severity set to Normal

comment:6 Changed 3 years ago by lukeplant

  • milestone 1.3 deleted
  • Triage Stage changed from Unreviewed to Ready for checkin

comment:7 Changed 3 years ago by jezdez

  • Resolution set to fixed
  • Status changed from new to closed

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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.