Opened 10 years ago

Closed 9 years ago

Last modified 9 years ago

#3446 closed (wontfix)

use a standard utililty function for importing a module from a string

Reported by: Gary Wilson <gary.wilson@…> Owned by: Grzegorz Ślusarek
Component: Core (Other) Version: master
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

There are several instances throughout the django code that roll their own module importing from a string. They should all use a single utility function instead.

Change History (5)

comment:1 Changed 10 years ago by Gary Wilson <gary.wilson@…>

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
Triage Stage: UnreviewedAccepted

comment:2 Changed 9 years ago by anonymous

Owner: changed from nobody to anonymous
Status: newassigned

comment:3 Changed 9 years ago by Grzegorz Ślusarek

Owner: changed from anonymous to Grzegorz Ślusarek
Status: assignednew

comment:4 Changed 9 years ago by Malcolm Tredinnick

Resolution: wontfix
Status: newclosed

I don't think this will make anything clearer. Almost everywhere we call __import__, it's only that single call. However, there are variations, because sometimes the last component is [''], sometimes it's a non-empty string in the list and sometimes it's [] (empty list). Each has special and necessary significance. So you'd be replacing a one-line call with a function call that takes more or less the same parameters -- net cost is an extra function call each time and you save having to type two sets of empty braces.

comment:5 Changed 9 years ago by Gary Wilson

I was thinking something a little bigger than replacing just the one __import__ line, like several places where there is code like:

i = path.rfind('.')
module, attr = path[:i], path[i+1:]
try:
    mod = __import__(module, globals(), locals(), [attr])
except ImportError, e:
    raise ImproperlyConfigured, 'Error importing template source loader %s: "%s"' % (module, e)
try:
    func = getattr(mod, attr)
except AttributeError:
    raise ImproperlyConfigured, 'Module "%s" does not define a "%s" callable template source loader' % (module, attr)

but you are probably right in that these can differ slightly and it might not be worth the trouble.

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