[Python-checkins] python/dist/src/Python pythonrun.c,2.193,2.194 errors.c,2.77,2.78

mhammond@users.sourceforge.net mhammond@users.sourceforge.net
Tue, 15 Jul 2003 16:03:58 -0700


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

Modified Files:
	pythonrun.c errors.c 
Log Message:
Fix [ 771097 ] frozen programs fail due to implicit import of "warnings".

If the initial import of warnings fails, clear the error.  When the module
is actually needed, if the original import failed, see if it has managed
to find its way to sys.modules yet and if so, remember it.


Index: pythonrun.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/pythonrun.c,v
retrieving revision 2.193
retrieving revision 2.194
diff -C2 -d -r2.193 -r2.194
*** pythonrun.c	22 Apr 2003 11:18:00 -0000	2.193
--- pythonrun.c	15 Jul 2003 23:03:55 -0000	2.194
***************
*** 70,76 ****
  
  /* Reference to 'warnings' module, to avoid importing it
!    on the fly when the import lock may be held.  See 683658
  */
! PyObject *PyModule_WarningsModule = NULL;
  
  static int initialized = 0;
--- 70,105 ----
  
  /* Reference to 'warnings' module, to avoid importing it
!    on the fly when the import lock may be held.  See 683658/771097
  */
! static PyObject *warnings_module = NULL;
! 
! /* Returns a borrowed reference to the 'warnings' module, or NULL.
!    If the module is returned, it is guaranteed to have been obtained
!    without acquiring the import lock
! */
! PyObject *PyModule_GetWarningsModule()
! {
! 	PyObject *typ, *val, *tb;
! 	PyObject *all_modules;
! 	/* If we managed to get the module at init time, just use it */
! 	if (warnings_module)
! 		return warnings_module;
! 	/* If it wasn't available at init time, it may be available
! 	   now in sys.modules (common scenario is frozen apps: import
! 	   at init time fails, but the frozen init code sets up sys.path
! 	   correctly, then does an implicit import of warnings for us
! 	*/
! 	/* Save and restore any exceptions */
! 	PyErr_Fetch(&typ, &val, &tb);
! 
! 	all_modules = PySys_GetObject("__modules__");
! 	if (all_modules) {
! 		warnings_module = PyDict_GetItemString(all_modules, "warnings");
! 		/* We keep a ref in the global */
! 		Py_XINCREF(warnings_module);
! 	}
! 	PyErr_Restore(typ, val, tb);
! 	return warnings_module;
! }
  
  static int initialized = 0;
***************
*** 191,195 ****
  #endif /* WITH_THREAD */
  
! 	PyModule_WarningsModule = PyImport_ImportModule("warnings");
  
  #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
--- 220,226 ----
  #endif /* WITH_THREAD */
  
! 	warnings_module = PyImport_ImportModule("warnings");
! 	if (!warnings_module)
! 		PyErr_Clear();
  
  #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
***************
*** 263,268 ****
  
  	/* drop module references we saved */
! 	Py_XDECREF(PyModule_WarningsModule);
! 	PyModule_WarningsModule = NULL;
  
  	/* Collect garbage.  This may call finalizers; it's nice to call these
--- 294,299 ----
  
  	/* drop module references we saved */
! 	Py_XDECREF(warnings_module);
! 	warnings_module = NULL;
  
  	/* Collect garbage.  This may call finalizers; it's nice to call these

Index: errors.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/errors.c,v
retrieving revision 2.77
retrieving revision 2.78
diff -C2 -d -r2.77 -r2.78
*** errors.c	10 Apr 2003 20:29:48 -0000	2.77
--- errors.c	15 Jul 2003 23:03:55 -0000	2.78
***************
*** 600,604 ****
  }
  
! extern PyObject *PyModule_WarningsModule;
  
  /* Function to issue a warning message; may raise an exception. */
--- 600,604 ----
  }
  
! extern PyObject *PyModule_GetWarningsModule();
  
  /* Function to issue a warning message; may raise an exception. */
***************
*** 607,613 ****
  {
  	PyObject *dict, *func = NULL;
  
! 	if (PyModule_WarningsModule != NULL) {
! 		dict = PyModule_GetDict(PyModule_WarningsModule);
  		func = PyDict_GetItemString(dict, "warn");
  	}
--- 607,614 ----
  {
  	PyObject *dict, *func = NULL;
+ 	PyObject *warnings_module = PyModule_GetWarningsModule();
  
! 	if (warnings_module != NULL) {
! 		dict = PyModule_GetDict(warnings_module);
  		func = PyDict_GetItemString(dict, "warn");
  	}