[Python-Dev] python package

M.-A. Lemburg mal@lemburg.com
Sat, 13 Jul 2002 21:07:05 +0200


Gordon McMillan wrote:
> On 13 Jul 2002 at 19:58, M.-A. Lemburg wrote:
> 
> 
>>Gordon McMillan wrote:
> 
> 
>>>Getting
>>> from <toplevelname> import urllib
>>>and 
>>> import urllib
>>>
>>>to return the same (is, not equals) object will
>>>require very delicate surgery on some very difficult
>>>code. And without it, most non-trivial scripts will
>>>break in very mysterious ways.
>>
>>Not really. The following code does all it takes to
>>make this work for e.g. having 'import DateTime' and
>>'from mx import DateTime' provide the same symbols:
> 
> 
> [snip hackery]
> 
> 
>>The module objects would be different, but that's
>>just about it. 
> 
> 
> Which was exactly my point. Much code that does
> *not* use "from ... import ..." in fact relies on
> having the same module object.

You mean for e.g. hacking the module's globals ? To solve
that, you'd probably need to manipulate sys.modules as
well... I'm just not sure whether this is possible from
within the module implementing the redirection.

Hmm, running this:

testmodload.py:
import sys, os
sys.modules['testmodload'] = os
print 'worked'


Python 2.1.3 (#1, May 16 2002, 18:59:26)
 >>> import testmodload
worked
 >>> testmodload
<module 'os' from '/usr/local/lib/python2.1/os.pyc'>
 >>>

Looks like this is possible, so you probably don't even
need the 'from mx.DateTime import *' in the code I posted.
A simple 'sys.modules['DateTime'] = mx.DateTime' would
give you an even better solution.

-- 
Marc-Andre Lemburg
CEO eGenix.com Software GmbH
_______________________________________________________________________
eGenix.com -- Makers of the Python mx Extensions: mxDateTime,mxODBC,...
Python Consulting:                               http://www.egenix.com/
Python Software:                    http://www.egenix.com/files/python/