[Patches] [ python-Patches-701743 ] Reloading pseudo modules

SourceForge.net noreply@sourceforge.net
Mon, 17 Mar 2003 06:25:14 -0800


Patches item #701743, was opened at 2003-03-11 19:59
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=701743&group_id=5470

Category: Core (C code)
Group: Python 2.3
Status: Open
Resolution: None
Priority: 5
Submitted By: Walter Dörwald (doerwalter)
Assigned to: Nobody/Anonymous (nobody)
Summary: Reloading pseudo modules

Initial Comment:
Python allows to put something that is not a module in
sys.modules. Unfortunately reload() does not work wth
such a pseudo module ("TypeError: reload() argument
must be module" is raised). This patch changes
Python/import.c::PyImport_ReloadModule() so that it
works with anything that has a __name__ attribute that
can be found in sys.modules.keys().

----------------------------------------------------------------------

>Comment By: Walter Dörwald (doerwalter)
Date: 2003-03-17 15:25

Message:
Logged In: YES 
user_id=89016

PyImport_ReloadModule() is only called by the implementation
of the reload builtin, so it seems that m==NULL can only
happen with broken extension modules. I've updated the patch
accordingly (raising a SystemError) and changed the error
case for a missing __name__ attribute to raise a TypeError
when an AttributeError is detected. Unfortunately this might
mask exceptions (e.g. when __name__ is implemented as a
property.)

Another problem is that reload() seems to repopulate the
existing module object when reloading real modules. Example:
Write a simple foo.py which contains "x = 1" and then:
>>> import foo
>>> foo.x
1
[ Now open your editor and change foo.py to "x = 2" ]
>>> foo2 = reload(foo)
>>> foo.x
2
>>> foo2.x
2
>>> print id(foo), id(foo2)
1077466884 1077466884
>>> 

Of course this can't work with pseudo modules. I wonder why
reload() has a return value at all, as it always modifies
its parameter for real modules.

----------------------------------------------------------------------

Comment By: Martin v. Löwis (loewis)
Date: 2003-03-15 14:51

Message:
Logged In: YES 
user_id=21627

I think the exceptions need to be reworked: "must be a
module" now only occurs if m is NULL. Under what
circumstances could that happen? Failure to provide __name__
is passed through; shouldn't this get diagnosed in a better way?

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=701743&group_id=5470