[Patches] [ python-Patches-441791 ] Improvment to package import semantics

noreply@sourceforge.net noreply@sourceforge.net
Thu, 02 Aug 2001 07:14:49 -0700


Patches item #441791, was opened at 2001-07-16 12:34
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=305470&aid=441791&group_id=5470

Category: core (C code)
Group: None
>Status: Closed
Resolution: None
Priority: 5
Submitted By: Alex Coventry (alex_coventry)
Assigned to: Guido van Rossum (gvanrossum)
Summary: Improvment to package import semantics

Initial Comment:
I think it's nice to be able to do this:

>>> import sys
>>> sys.path.append('/l/alex_c/')
>>> import mouse.foo
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/l/alex_c/mouse/foo.py", line 1, in ?
    bar
NameError: name 'bar' is not defined
>>> import mouse.foo
>>> reload(mouse.foo) # Works now that foo.py is
corrected
<module 'mouse.foo' from '/l/alex_c/mouse/foo.py'>
>>> from mouse import foo
>>> foo.bar
1

However, at the moment, that's not possible, because if
an error occurs
in the loading of foo, foo is not added to mouse's
dictionary, even
though the module added to sys.modules under the key
'mouse.foo'.  This
has been driving me nuts, because I have large datasets
that I need to
load in each time I start python up, so I tend to test
my scripts in a
single interactive interpreter as I write them, so it's
important for me
to be able to reliably reload modules.  I think it's
also a sensible way
for things to work in general, too.  If there's enough
controversy about
this, I can write a PEP about it, or something.  At any
rate, here's a
patch against the current CVS repository that changes
the semantics as
I've described.

Alex.

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

>Comment By: Guido van Rossum (gvanrossum)
Date: 2001-08-02 07:14

Message:
Logged In: YES 
user_id=6380

I've converted your unit test to the standard regression
test suite and checked it in.  Thanks!


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

Comment By: Guido van Rossum (gvanrossum)
Date: 2001-07-23 06:30

Message:
Logged In: YES 
user_id=6380

Alex, I've checked in my version of the patch.

Would you mind converting the unittest to something that I
can drop into the test package?


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

Comment By: Alex Coventry (alex_coventry)
Date: 2001-07-20 14:54

Message:
Logged In: YES 
user_id=49686

Thanks for the corrections.  For what it's worth, here's the
unit test I have in my
code for this change, along with something to catch the
SyntaxError error, not 
that anyone else is likely to make that mistake:)

Alex

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

Comment By: Guido van Rossum (gvanrossum)
Date: 2001-07-20 12:47

Message:
Logged In: YES 
user_id=6380

Notice that when the module fails with a SyntaxError, my
patch does nothing. propagating the SyntaxError normally,
while the original error raised a SystemError, masking the
SyntaxError. Clearly that was wrong.

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

Comment By: Guido van Rossum (gvanrossum)
Date: 2001-07-20 12:41

Message:
Logged In: YES 
user_id=6380

I've added a streamlined version that deals with the error
handling a bit differently, and conforms to the C style
guide (PEP 7).


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

Comment By: Alex Coventry (alex_coventry)
Date: 2001-07-19 15:21

Message:
Logged In: YES 
user_id=49686

Hi, I've looked for pertinent thread in the python-dev
archive, but I've
been unable to find one.  I've searched for keywords
"import.c",
"import_submodule", "reload", and lastly "package import",
although I
might have missed a thread matching that, as there were over
a hundred
hits.  Could you point me at a thread?

Alex.


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

Comment By: Thomas Wouters (twouters)
Date: 2001-07-17 02:00

Message:
Logged In: YES 
user_id=34209

This is not a 2.0.1 bugfix candidate, or any bugfix
candidate. Group changed. This is also a subject of
occasional discussion on python-dev, see the archives ;P


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

Comment By: Alex Coventry (alex_coventry)
Date: 2001-07-16 12:42

Message:
Logged In: YES 
user_id=49686

a) Sorry about the duplicate submission

b) I attached the patch I'm proposing to it, but can't see
it on this page.
Here it is, just in case:

Index: Python/import.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/import.c,v
retrieving revision 2.178
diff -c -r2.178 import.c
*** Python/import.c	2001/07/05 03:47:53	2.178
--- Python/import.c	2001/07/16 19:04:05
***************
*** 1789,1795 ****
  import_submodule(PyObject *mod, char *subname, char
*fullname)
  {
  	PyObject *modules = PyImport_GetModuleDict();
! 	PyObject *m;
  
  	/* Require:
  	   if mod == None: subname == fullname
--- 1789,1795 ----
  import_submodule(PyObject *mod, char *subname, char
*fullname)
  {
  	PyObject *modules = PyImport_GetModuleDict();
! 	PyObject *m, *resulting_module=NULL;
  
  	/* Require:
  	   if mod == None: subname == fullname
***************
*** 1829,1839 ****
  		m = load_module(fullname, fp, buf, fdp->type);
  		if (fp)
  			fclose(fp);
! 		if (m != NULL && mod != Py_None) {
! 			if (PyObject_SetAttrString(mod, subname, m) < 0) {
! 				Py_DECREF(m);
! 				m = NULL;
! 			}
  		}
  	}
  
--- 1829,1864 ----
  		m = load_module(fullname, fp, buf, fdp->type);
  		if (fp)
  			fclose(fp);
! 		if (mod != Py_None) {
! 
! 		  /* Irrespective of the success of this load, make a
! 		     reference to it in the parent package module. */
! 
! 		  /* ...a copy gets saved in the modules dictionary
! 		     under the full name, so get a reference from
! 		     there, if need be. */
! 		  if (m == NULL) {
! 		    resulting_module = PyDict_GetItemString(modules,
! 							    fullname);
! 		    if (resulting_module == NULL) {
! 
! 		      /* ...failed to find the module under its full
!                          name; oops */
! 		      PyErr_Format(PyExc_SystemError,
! 				   "Failed to create a sys.modules " \
! 				   "entry under %.200s", fullname);
! 		      return NULL;
! 		    }
! 		  } else {
! 		    resulting_module = m;
! 		  }
! 
! 		  if (PyObject_SetAttrString(mod, subname,
resulting_module) < 0) {
! 		    if (m != NULL) {
! 		      Py_DECREF(m);
! 		      m = NULL;
! 		    }
! 		  }
  		}
  	}
  

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

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