[Python-checkins] python/dist/src/Python import.c,2.220,2.221

nascheme@users.sourceforge.net nascheme@users.sourceforge.net
Mon, 16 Jun 2003 14:03:09 -0700


Update of /cvsroot/python/python/dist/src/Python
In directory sc8-pr-cvs1:/tmp/cvs-serv29262/Python

Modified Files:
	import.c 
Log Message:
Don't use the module object setattr when importing submodules.  Instead,
operate on the module dictionary directly.  This prevents spurious
depreciation warnings from being raised if a submodule name shadows
a builtin name.


Index: import.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/import.c,v
retrieving revision 2.220
retrieving revision 2.221
diff -C2 -d -r2.220 -r2.221
*** import.c	23 Mar 2003 14:31:01 -0000	2.220
--- import.c	16 Jun 2003 21:03:07 -0000	2.221
***************
*** 2212,2220 ****
  }
  
  static PyObject *
  import_submodule(PyObject *mod, char *subname, char *fullname)
  {
  	PyObject *modules = PyImport_GetModuleDict();
! 	PyObject *m, *res = NULL;
  
  	/* Require:
--- 2212,2254 ----
  }
  
+ static int
+ add_submodule(PyObject *mod, PyObject *submod, char *fullname, char *subname,
+ 	      PyObject *modules)
+ {
+ 	if (mod == Py_None)
+ 		return 1;
+ 	/* 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.  (The exception is when the
+ 	   load failed with a SyntaxError -- then there's no trace in
+ 	   sys.modules.  In that case, of course, do nothing extra.) */
+ 	if (submod == NULL) {
+ 		submod = PyDict_GetItemString(modules, fullname);
+ 		if (submod == NULL)
+ 			return 1;
+ 	}
+ 	if (PyModule_Check(mod)) {
+ 		/* We can't use setattr here since it can give a
+ 		 * spurious warning if the submodule name shadows a
+ 		 * builtin name */
+ 		PyObject *dict = PyModule_GetDict(mod);
+ 		if (!dict)
+ 			return 0;
+ 		if (PyDict_SetItemString(dict, subname, submod) < 0)
+ 			return 0;
+ 	}
+ 	else {
+ 		if (PyObject_SetAttrString(mod, subname, submod) < 0)
+ 			return 0;
+ 	}
+ 	return 1;
+ }
+ 
  static PyObject *
  import_submodule(PyObject *mod, char *subname, char *fullname)
  {
  	PyObject *modules = PyImport_GetModuleDict();
! 	PyObject *m = NULL;
  
  	/* Require:
***************
*** 2258,2278 ****
  		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.  (The exception is when
! 			   the load failed with a SyntaxError -- then
! 			   there's no trace in sys.modules.  In that case,
! 			   of course, do nothing extra.) */
! 			res = m;
! 			if (res == NULL)
! 				res = PyDict_GetItemString(modules, fullname);
! 			if (res != NULL &&
! 			    PyObject_SetAttrString(mod, subname, res) < 0) {
! 				Py_XDECREF(m);
! 				m = NULL;
! 			}
  		}
  	}
--- 2292,2298 ----
  		if (fp)
  			fclose(fp);
! 		if (!add_submodule(mod, m, fullname, subname, modules)) {
! 			Py_XDECREF(m);
! 			m = NULL;
  		}
  	}