[Python-Dev] Strange import behaviour, recently introduced

Guido van Rossum guido@digicool.com
Wed, 21 Feb 2001 21:24:31 -0500


> Jack Jansen wrote:
> > This week I noticed that these resource imports have suddenly
> > become very very slow. Whereas startup time of my application used
> > to be around 2 seconds (where the non-frozen version took 6
> > seconds) it now takes almost 20 times as long. The non-frozen
> > version still takes 6 seconds.

[Thomas Heller]
> The most recent version calls PyImport_ImportModuleEx() for
> '__builtin__' for every import of __builtin__ without caching the
> result in a static variable.
> 
> Can this be the cause?

Would this help?

*** import.c	2001/02/20 21:43:24	2.162
--- import.c	2001/02/22 02:24:55
***************
*** 1873,1878 ****
--- 1873,1879 ----
  {
  	static PyObject *silly_list = NULL;
  	static PyObject *builtins_str = NULL;
+ 	static PyObject *builtin_str = NULL;
  	static PyObject *import_str = NULL;
  	PyObject *globals = NULL;
  	PyObject *import = NULL;
***************
*** 1887,1892 ****
--- 1888,1896 ----
  		builtins_str = PyString_InternFromString("__builtins__");
  		if (builtins_str == NULL)
  			return NULL;
+ 		builtin_str = PyString_InternFromString("__builtin__");
+ 		if (builtin_str == NULL)
+ 			return NULL;
  		silly_list = Py_BuildValue("[s]", "__doc__");
  		if (silly_list == NULL)
  			return NULL;
***************
*** 1902,1913 ****
  	}
  	else {
  		/* No globals -- use standard builtins, and fake globals */
  		PyErr_Clear();
  
! 		builtins = PyImport_ImportModuleEx("__builtin__",
! 						   NULL, NULL, NULL);
  		if (builtins == NULL)
  			return NULL;
  		globals = Py_BuildValue("{OO}", builtins_str, builtins);
  		if (globals == NULL)
  			goto err;
--- 1906,1918 ----
  	}
  	else {
  		/* No globals -- use standard builtins, and fake globals */
+ 		PyInterpreterState *interp = PyThreadState_Get()->interp;
  		PyErr_Clear();
  
! 		builtins = PyDict_GetItem(interp->modules, builtin_str);
  		if (builtins == NULL)
  			return NULL;
+ 		Py_INCREF(builtins);
  		globals = Py_BuildValue("{OO}", builtins_str, builtins);
  		if (globals == NULL)
  			goto err;

--Guido van Rossum (home page: http://www.python.org/~guido/)