[ python-Bugs-856103 ] reload() fails with modules from zips

SourceForge.net noreply at sourceforge.net
Fri Apr 2 10:38:53 EST 2004


Bugs item #856103, was opened at 2003-12-08 02:48
Message generated for change (Comment added) made by filitov
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=856103&group_id=5470

Category: Python Interpreter Core
Group: Python 2.4
Status: Open
Resolution: None
Priority: 5
Submitted By: Tony Meyer (anadelonbrin)
Assigned to: Skip Montanaro (montanaro)
Summary: reload() fails with modules from zips

Initial Comment:
If you call reload() with a module that was imported from 
a zip, it fails with a "no such module" error.  Although 
zips are typically read-only, it is possible that a zip could 
be modified during a run, and a reload be necessary.  If 
this is considered unnecessary, then I think a more 
informative "can't reload from zip" error would be better 
than a 'no such module" one.

"""
>set PYTHONPATH=path/to/spambayes.zip
>python

>>> from spambayes import Options
>>> Options
<module &#039;spambayes.Options&#039; 
from &#039;c:\spambayes\windows\py2exe\dist\lib\spambayes
.zip\spambayes\Options.pyc&#039;>
>>> reload(Options)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ImportError: No module named Options
"""

This is with Python 2.3.2 and WinXP.

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

Comment By: Stephen Haberman (filitov)
Date: 2004-04-02 09:38

Message:
Logged In: YES 
user_id=642545

Actually, it doesn't seem to be working fully.

I've been playing with Skip's test patch and it looks like
the code it reloads uses...the same buffer size as it did
with the original read? You can play with replacing 'return
__name__' with different strings and it will change the
error you get.

E.g. with 'return __name__+"modified"', it parses fine, but
get_foo() is not found (which is added after get_name()).

E.g. with 'return "new"', it does not parse fine, it returns
'NameError: name 'de' is not defined'. My guess is that it
got part way through reading "def get_foo(): return 1" and
hit the end of a buffer.

Or something. At this point, its beyond my C/Python code
skills to track it down. Hopefully its just some minor error
in re-loading the code from the new zip file you guys can
easily find.

I can't see where to upload a patch file, so you can get
what I'm currently playing with at:

http://sh5.beachead.com:8080/~stephen/patch.txt

Note the import.c patch is the same, the new patch just adds
stuff to Skip's testReload function to try loading code from
a new zip file.


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

Comment By: Skip Montanaro (montanaro)
Date: 2004-04-02 06:23

Message:
Logged In: YES 
user_id=44345

> what doesn't work for you?

Sorry.  Pilot error.  I was adding foozip.zip to sys.path then trying

    from foozip import somemodule

instead of just

    import somemodule

(Obvious, not a feature I use a lot...)

Seems to be working for me now.  I'll try a couple more tests then 
check it in.


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

Comment By: Just van Rossum (jvr)
Date: 2004-04-02 01:00

Message:
Logged In: YES 
user_id=92689

The import.c patch looks fine (although I didn't test it yet). Skip, 
what doesn't work for you? "I can't get zip imports to work" is 
quite broad...

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

Comment By: Skip Montanaro (montanaro)
Date: 2004-04-01 21:37

Message:
Logged In: YES 
user_id=44345

attached is the patch to protect it from the vagaries of cut-n-
paste.  I also added a simple testReload test to the 
test_zipimport.py file.  I can't get zip imports to work.  Perhaps 
Just can test this out.


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

Comment By: Stephen Haberman (filitov)
Date: 2004-04-01 18:26

Message:
Logged In: YES 
user_id=642545

Here's a patch that fixes the problem so that modules from
zip files can be reloaded.

The problem was the PyImport_ReloadModulefunction not
passing a loader around. Perhaps this is naive, and the
PyImport_ReloadModule was purposefully not using a loader,
but, again, it works for me.

cvs diff -u dist\src\Python\import.c (in directory
C:\cvs\python\)
Index: dist/src/Python/import.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/import.c,v
retrieving revision 2.230
diff -u -r2.230 import.c
--- dist/src/Python/import.c	1 Apr 2004 02:45:22 -0000	2.230
+++ dist/src/Python/import.c	2 Apr 2004 00:18:46 -0000
@@ -2217,7 +2217,7 @@
 PyImport_ReloadModule(PyObject *m)
 {
 	PyObject *modules = PyImport_GetModuleDict();
-	PyObject *path = NULL;
+	PyObject *path, *loader = NULL;
 	char *name, *subname;
 	char buf[MAXPATHLEN+1];
 	struct filedescr *fdp;
@@ -2259,11 +2259,12 @@
 			PyErr_Clear();
 	}
 	buf[0] = '\0';
-	fdp = find_module(name, subname, path, buf, MAXPATHLEN+1,
&fp, NULL);
+	fdp = find_module(name, subname, path, buf, MAXPATHLEN+1,
&fp, &loader);
 	Py_XDECREF(path);
 	if (fdp == NULL)
 		return NULL;
-	m = load_module(name, fp, buf, fdp->type, NULL);
+	m = load_module(name, fp, buf, fdp->type, loader);
+    Py_XDECREF(loader);
 	if (fp)
 		fclose(fp);
 	return m;




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

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



More information about the Python-bugs-list mailing list