The trouble with __import__( 'pkg', {}, {}, [''] )

32 views
Skip to first unread message

mrts

unread,
Aug 8, 2008, 9:51:37 PM8/8/08
to Django developers
...is http://bugs.python.org/issue2090 . This is a common idiom for
importing modules dynamically in Django.

To verify that this really is the case, create e.g. a custom
middleware as follows:

---
middleware/__init__.py:

print "EmptyMiddleware imported."

class EmptyMiddleware(object):
def process_request(self, request)
pass
---

which results in:

Django version 1.0-alpha_2-SVN-8255, using settings 'foo.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
EmptyMiddleware imported.
EmptyMiddleware imported.
[08/Aug/2008 21:40:10] "GET / HTTP/1.1" 200 3396

Ramiro Morales

unread,
Aug 9, 2008, 8:34:19 AM8/9/08
to django-d...@googlegroups.com
On Fri, Aug 8, 2008 at 10:51 PM, mrts <mr...@mrts.pri.ee> wrote:
>
> ...is http://bugs.python.org/issue2090 . This is a common idiom for
> importing modules dynamically in Django.
>

Oh __import__ with [''] as the fouth parameter, I had the following message in
draft for the "post_save signal triggered twice" [1]thread a couple of week ago
and never dare to send it (I've update the grepping over the tree to current
trunk status):

Regarding the use of __import__, there are some notes in [2]ticket 6579 about
calling it with a empty string as the fourth parameter having the effect of
initializing the module twice (there is also a [3]link to a ticket in the
Python bug tracker.)

When [4]pointed to it in a comment to #6587, Øyvind Saltvik
implemented another loading strategy in the latest patch for that ticket.

All this in the context of (the maybe 1.0 feature) of removing the hacks in the
template tag loading code, but could this also be related to the repeated
triggering of signals?

Searching over the Django code as of r8255 [5]finds 28 occurrences of such a
pattern, some of them when loading user project or application code from the
Django core.

Regards,

--
Ramiro Morales

1. http://groups.google.com/group/django-developers/browse_frm/thread/c1cf43fddb2c8cdd/8131a56097b61fc8?hl=en&lnk=gst8131a56097b61fc8
2. http://code.djangoproject.com/ticket/6579
3. http://code.djangoproject.com/ticket/6579#comment:6
4. http://code.djangoproject.com/ticket/6587#comment:10
5. http://dpaste.com/hold/70347/

Ivan Illarionov

unread,
Aug 9, 2008, 3:17:22 PM8/9/08
to Django developers
I submitted a patch[1] that fixes this problem.

[1] http://code.djangoproject.com/attachment/ticket/8193/import_fixes_8272.diff

On Aug 9, 5:51 am, mrts <m...@mrts.pri.ee> wrote:
> ...ishttp://bugs.python.org/issue2090. This is a common idiom for
> importing modules dynamically in Django.
>
> To verify that this really is the case, create e.g. a custom
> middleware as follows:
>
> ---
> middleware/__init__.py:
>
> print "EmptyMiddleware imported."
>
> class EmptyMiddleware(object):
>   def process_request(self, request)
>      pass
> ---
>
> which results in:
>
> Django version 1.0-alpha_2-SVN-8255, using settings 'foo.settings'
> Development server is running athttp://127.0.0.1:8000/
Reply all
Reply to author
Forward
0 new messages