mocker's use of __import__ causes double module initialization

Bug #529675 reported by David Glick
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Mocker
Fix Released
Undecided
Gustavo Niemeyer

Bug Description

Mocker uses the idiom object = __import__(module_path, {}, {}, [""]) to import a module and get a reference to it. This has the unwanted side effect of causing the module to be re-initialized if it was already imported, as described in http://bugs.python.org/issue2090 ... that's bad if the module being imported has import-time side effects.

The Python 2.6 documentation now recommends a different idiom for use in this situation:

"If you simply want to import a module (potentially within a package) by
name, you can get it from sys.modules:
>>> import sys
>>> name = 'foo.bar.baz'
>>> __import__(name)
<module 'foo' from ...>
>>> baz = sys.modules[name]
>>> baz
<module 'foo.bar.baz' from ...>
"

I'm attaching a patch (against mocker 0.10.1) which makes mocker use this approach.

Revision history for this message
David Glick (davisagli) wrote :
Revision history for this message
Gustavo Niemeyer (niemeyer) wrote :

Thanks for the patch David. I've applied the change in revision 58.

In comparison to your patch, I've just reused the module_path variable.

This will be in Mocker 0.15 in a moment.

Changed in mocker:
assignee: nobody → Gustavo Niemeyer (niemeyer)
status: New → Fix Committed
Changed in mocker:
status: Fix Committed → Fix Released
Revision history for this message
David Glick (davisagli) wrote :

Thanks for merging and releasing this. Will the 1.0 release be posted to PyPI?

Revision history for this message
Gustavo Niemeyer (niemeyer) wrote :

You're welcome, David. That's been done as well.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.