[Python-checkins] python/dist/src/Python import.c,2.235,2.236

tim_one at users.sourceforge.net tim_one at users.sourceforge.net
Mon Aug 2 05:52:15 CEST 2004


Update of /cvsroot/python/python/dist/src/Python
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv22639/Python

Modified Files:
	import.c 
Log Message:
PyImport_ExecCodeModuleEx():  remove module from sys.modules in error cases.
PyImport_ReloadModule():  restore the module to sys.modules in error cases.
load_package():  semantic-neutral refactoring from an earlier stab at
                 this patch; giving it a common error exit made the code
                 easier to follow, so retaining that part.
_RemoveModule():  new little utility to delete a key from sys.modules.


Index: import.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/import.c,v
retrieving revision 2.235
retrieving revision 2.236
diff -C2 -d -r2.235 -r2.236
*** import.c	1 Aug 2004 23:26:05 -0000	2.235
--- import.c	2 Aug 2004 03:52:12 -0000	2.236
***************
*** 558,565 ****
  }
  
  
  /* Execute a code object in a module and return the module object
!    WITH INCREMENTED REFERENCE COUNT */
! 
  PyObject *
  PyImport_ExecCodeModule(char *name, PyObject *co)
--- 558,580 ----
  }
  
+ /* Remove name from sys.modules, if it's there. */
+ static void
+ _RemoveModule(const char *name)
+ {
+ 	PyObject *modules = PyImport_GetModuleDict();
+ 	if (PyDict_GetItemString(modules, name) == NULL)
+ 		return;
+ 	if (PyDict_DelItemString(modules, name) < 0)
+ 		Py_FatalError("import:  deleting existing key in"
+ 			      "sys.modules failed");
+ }
  
  /* Execute a code object in a module and return the module object
!  * WITH INCREMENTED REFERENCE COUNT.  If an error occurs, name is
!  * removed from sys.modules, to avoid leaving damaged module objects
!  * in sys.modules.  The caller may wish to restore the original
!  * module object (if any) in this case; PyImport_ReloadModule is an
!  * example.
!  */
  PyObject *
  PyImport_ExecCodeModule(char *name, PyObject *co)
***************
*** 583,587 ****
  		if (PyDict_SetItemString(d, "__builtins__",
  					 PyEval_GetBuiltins()) != 0)
! 			return NULL;
  	}
  	/* Remember the filename as the __file__ attribute */
--- 598,602 ----
  		if (PyDict_SetItemString(d, "__builtins__",
  					 PyEval_GetBuiltins()) != 0)
! 			goto error;
  	}
  	/* Remember the filename as the __file__ attribute */
***************
*** 602,606 ****
  	v = PyEval_EvalCode((PyCodeObject *)co, d, d);
  	if (v == NULL)
! 		return NULL;
  	Py_DECREF(v);
  
--- 617,621 ----
  	v = PyEval_EvalCode((PyCodeObject *)co, d, d);
  	if (v == NULL)
! 		goto error;
  	Py_DECREF(v);
  
***************
*** 615,618 ****
--- 630,637 ----
  
  	return m;
+ 
+   error:
+ 	_RemoveModule(name);
+ 	return NULL;
  }
  
***************
*** 889,893 ****
  load_package(char *name, char *pathname)
  {
! 	PyObject *m, *d, *file, *path;
  	int err;
  	char buf[MAXPATHLEN+1];
--- 908,914 ----
  load_package(char *name, char *pathname)
  {
! 	PyObject *m, *d;
! 	PyObject *file = NULL;
! 	PyObject *path = NULL;
  	int err;
  	char buf[MAXPATHLEN+1];
***************
*** 904,920 ****
  	file = PyString_FromString(pathname);
  	if (file == NULL)
! 		return NULL;
  	path = Py_BuildValue("[O]", file);
! 	if (path == NULL) {
! 		Py_DECREF(file);
! 		return NULL;
! 	}
  	err = PyDict_SetItemString(d, "__file__", file);
  	if (err == 0)
  		err = PyDict_SetItemString(d, "__path__", path);
! 	if (err != 0) {
! 		m = NULL;
! 		goto cleanup;
! 	}
  	buf[0] = '\0';
  	fdp = find_module(name, "__init__", path, buf, sizeof(buf), &fp, NULL);
--- 925,937 ----
  	file = PyString_FromString(pathname);
  	if (file == NULL)
! 		goto error;
  	path = Py_BuildValue("[O]", file);
! 	if (path == NULL)
! 		goto error;
  	err = PyDict_SetItemString(d, "__file__", file);
  	if (err == 0)
  		err = PyDict_SetItemString(d, "__path__", path);
! 	if (err != 0)
! 		goto error;
  	buf[0] = '\0';
  	fdp = find_module(name, "__init__", path, buf, sizeof(buf), &fp, NULL);
***************
*** 931,934 ****
--- 948,955 ----
  	if (fp != NULL)
  		fclose(fp);
+ 	goto cleanup;
+ 
+   error:
+   	m = NULL;
    cleanup:
  	Py_XDECREF(path);
***************
*** 2235,2238 ****
--- 2256,2260 ----
  	struct filedescr *fdp;
  	FILE *fp = NULL;
+ 	PyObject *newm;
  
  	if (m == NULL || !PyModule_Check(m)) {
***************
*** 2276,2283 ****
  	if (fdp == NULL)
  		return NULL;
! 	m = load_module(name, fp, buf, fdp->type, NULL);
  	if (fp)
  		fclose(fp);
! 	return m;
  }
  
--- 2298,2313 ----
  	if (fdp == NULL)
  		return NULL;
! 	newm = load_module(name, fp, buf, fdp->type, NULL);
  	if (fp)
  		fclose(fp);
! 	if (newm == NULL) {
! 		/* load_module probably removed name from modules because of
! 		 * the error.  Put back the original module object.  We're
! 		 * going to return NULL in this case regardless of whether
! 		 * replacing name succeeds, so the return value is ignored.
! 		 */
! 		PyDict_SetItemString(modules, name, m);
! 	}
! 	return newm;
  }
  



More information about the Python-checkins mailing list