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

SourceForge.net noreply@sourceforge.net
Mon, 31 Mar 2003 11:38:38 -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-31 21:38

Message:
Logged In: YES 
user_id=89016

A use case can be found at
http://www.livinglogic.de/viewcvs/index.cgi/LivingLogic/xist/_xist/xsc.py?rev=2.235
(Look for the classmethod makemod() in the class Namespace).
This puts a class object into sys.modules instead of the
module that defines this class. This makes it possible to
derive from "modules". 

Of course the patch does not fully fix the problem, because
reload() does not repopulate the class object. Unfortunately
that's impossible to fix with Python code, as it's
impossible for Python code to distinguish the first import
from subsequent ones. If this was possible (and Python code
had access to the old "module"), a real reload could be
coded in pure Python for this specific case.

But with the patch at least it's possible to use the return
value of reload() afterwards to use the new "module".

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

Comment By: Martin v. Löwis (loewis)
Date: 2003-03-30 18:34

Message:
Logged In: YES 
user_id=21627

The patch looks fine now as far as it goes. I'm unsure what
the use case is, though: What object do you have in
sys.modules for which reload() would be meaningful? Can you
attach an example where reloading fails now but succeeds
with your patch applied?

As for reload modifying the module object: It needs to, or
else all clients would have to run reload; this would
include things like function default arguments. I guess it
returns a result for historical reasons.


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

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