[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [19989] trunk/blender/source: python modules in the game engine could point to builtin modules like GameLogic that was cleared .

Campbell Barton ideasman42 at gmail.com
Thu Apr 30 01:39:27 CEST 2009


Revision: 19989
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=19989
Author:   campbellbarton
Date:     2009-04-30 01:39:27 +0200 (Thu, 30 Apr 2009)

Log Message:
-----------
python modules in the game engine could point to builtin modules like GameLogic that was cleared.
I added module clearing before there was checks for invalid python objects, so now its not needed for BGE Builtin types at least.

also made the builtin modules get re-used if they already exist and clear all user modules when the game engine finishes so with Module-Py-Controllers the referenced modules are at least up to date when pressing Pkey.

Modified Paths:
--------------
    trunk/blender/source/blender/python/api2_2x/bpy_internal_import.c
    trunk/blender/source/blender/python/api2_2x/bpy_internal_import.h
    trunk/blender/source/gameengine/Expressions/InputParser.cpp
    trunk/blender/source/gameengine/Expressions/Value.cpp
    trunk/blender/source/gameengine/Ketsji/KX_PyConstraintBinding.cpp
    trunk/blender/source/gameengine/Ketsji/KX_PythonInit.cpp
    trunk/blender/source/gameengine/VideoTexture/blendVideoTex.cpp

Modified: trunk/blender/source/blender/python/api2_2x/bpy_internal_import.c
===================================================================
--- trunk/blender/source/blender/python/api2_2x/bpy_internal_import.c	2009-04-29 23:33:04 UTC (rev 19988)
+++ trunk/blender/source/blender/python/api2_2x/bpy_internal_import.c	2009-04-29 23:39:27 UTC (rev 19989)
@@ -272,7 +272,12 @@
  * Its also needed for the BGE Python api so imported scripts are not used between levels
  * 
  * This clears every modules that has a __file__ attribute (is not a builtin)
- * and is a filename only (no path). since pythons bultins include a full path even for win32.
+ *
+ * Note that clearing external python modules is important for the BGE otherwise
+ * it wont reload scripts between loading different blend files or while making the game.
+ * - use 'clear_all' arg in this case.
+ *
+ * Since pythons bultins include a full path even for win32.
  * even if we remove a python module a reimport will bring it back again.
  */
 
@@ -284,7 +289,7 @@
 #endif
 
 
-void bpy_text_clear_modules(void)
+void bpy_text_clear_modules(int clear_all)
 {
 	PyObject *modules= PySys_GetObject("modules");
 	
@@ -309,7 +314,7 @@
 	while (PyDict_Next(modules, &pos, &key, &value)) {
 		fname= PyModule_GetFilename(value);
 		if(fname) {
-			if ((strstr(fname, SEPSTR))==0) { /* no path ? */
+			if (clear_all || ((strstr(fname, SEPSTR))==0)) { /* no path ? */
 				file_extension = strstr(fname, ".py");
 				if(file_extension && *(file_extension + 3) == '\0') { /* .py extension ? */
 					/* now we can be fairly sure its a python import from the blendfile */

Modified: trunk/blender/source/blender/python/api2_2x/bpy_internal_import.h
===================================================================
--- trunk/blender/source/blender/python/api2_2x/bpy_internal_import.h	2009-04-29 23:33:04 UTC (rev 19988)
+++ trunk/blender/source/blender/python/api2_2x/bpy_internal_import.h	2009-04-29 23:39:27 UTC (rev 19989)
@@ -37,7 +37,7 @@
 
 PyObject*	bpy_text_import( char *name, int *found );
 PyObject*	bpy_text_reimport( PyObject *module, int *found );
-void		bpy_text_clear_modules( void ); /* Clear user modules */
+void		bpy_text_clear_modules( int clear_all ); /* Clear user modules */
 extern PyMethodDef bpy_import_meth[];
 extern PyMethodDef bpy_reload_meth[];
 

Modified: trunk/blender/source/gameengine/Expressions/InputParser.cpp
===================================================================
--- trunk/blender/source/gameengine/Expressions/InputParser.cpp	2009-04-29 23:33:04 UTC (rev 19988)
+++ trunk/blender/source/gameengine/Expressions/InputParser.cpp	2009-04-29 23:39:27 UTC (rev 19989)
@@ -676,11 +676,23 @@
 extern "C" {
 	void initExpressionModule(void)
 	{
+		PyObject *m;
+		/* Use existing module where possible
+		 * be careful not to init any runtime vars after this */
+		m = PyImport_ImportModule( "Expression" );
+		if(m) {
+			Py_DECREF(m);
+			return m;
+		}
+		else {
+			PyErr_Clear();
+		
 #if (PY_VERSION_HEX >= 0x03000000)
-		PyModule_Create(&Expression_module_def);
+			PyModule_Create(&Expression_module_def);
 #else
-		Py_InitModule("Expression",CParserMethods);
+			Py_InitModule("Expression",CParserMethods);
 #endif
+		}
 	}
 }
 

Modified: trunk/blender/source/gameengine/Expressions/Value.cpp
===================================================================
--- trunk/blender/source/gameengine/Expressions/Value.cpp	2009-04-29 23:33:04 UTC (rev 19988)
+++ trunk/blender/source/gameengine/Expressions/Value.cpp	2009-04-29 23:39:27 UTC (rev 19989)
@@ -773,11 +773,23 @@
 extern "C" {
 	void initCValue(void)
 	{
+		PyObject *m;
+		/* Use existing module where possible
+		 * be careful not to init any runtime vars after this */
+		m = PyImport_ImportModule( "CValue" );
+		if(m) {
+			Py_DECREF(m);
+			return m;
+		}
+		else {
+			PyErr_Clear();
+		
 #if (PY_VERSION_HEX >= 0x03000000)
-		PyModule_Create(&CValue_module_def);
+			PyModule_Create(&CValue_module_def);
 #else
-		Py_InitModule("CValue",CValueMethods);
+			Py_InitModule("CValue",CValueMethods);
 #endif
+		}
 	}
 }
 

Modified: trunk/blender/source/gameengine/Ketsji/KX_PyConstraintBinding.cpp
===================================================================
--- trunk/blender/source/gameengine/Ketsji/KX_PyConstraintBinding.cpp	2009-04-29 23:33:04 UTC (rev 19988)
+++ trunk/blender/source/gameengine/Ketsji/KX_PyConstraintBinding.cpp	2009-04-29 23:39:27 UTC (rev 19989)
@@ -586,13 +586,24 @@
   PyObject* m;
   PyObject* d;
 
+	/* Use existing module where possible
+	 * be careful not to init any runtime vars after this */
+	m = PyImport_ImportModule( "PhysicsConstraints" );
+	if(m) {
+		Py_DECREF(m);
+		return m;
+	}
+	else {
+		PyErr_Clear();
+	
 #if (PY_VERSION_HEX >= 0x03000000)
-  m = PyModule_Create(&PhysicsConstraints_module_def);
+		m = PyModule_Create(&PhysicsConstraints_module_def);
 #else
-  m = Py_InitModule4("PhysicsConstraints", physicsconstraints_methods,
+		m = Py_InitModule4("PhysicsConstraints", physicsconstraints_methods,
 		     PhysicsConstraints_module_documentation,
 		     (PyObject*)NULL,PYTHON_API_VERSION);
 #endif
+	}
 
   // Add some symbolic constants to the module
   d = PyModule_GetDict(m);

Modified: trunk/blender/source/gameengine/Ketsji/KX_PythonInit.cpp
===================================================================
--- trunk/blender/source/gameengine/Ketsji/KX_PythonInit.cpp	2009-04-29 23:33:04 UTC (rev 19988)
+++ trunk/blender/source/gameengine/Ketsji/KX_PythonInit.cpp	2009-04-29 23:39:27 UTC (rev 19989)
@@ -1098,16 +1098,28 @@
 
 	gUseVisibilityTemp=false;
 
-	// Create the module and add the functions
+
 	
+	/* Use existing module where possible
+	 * be careful not to init any runtime vars after this */
+	m = PyImport_ImportModule( "GameLogic" );
+	if(m) {
+		Py_DECREF(m);
+		return m;
+	}
+	else {
+		PyErr_Clear();
+		
+		// Create the module and add the functions	
 #if (PY_VERSION_HEX >= 0x03000000)
-	m = PyModule_Create(&GameLogic_module_def);
+		m = PyModule_Create(&GameLogic_module_def);
 #else
-	m = Py_InitModule4("GameLogic", game_methods,
-					   GameLogic_module_documentation,
-					   (PyObject*)NULL,PYTHON_API_VERSION);
+		m = Py_InitModule4("GameLogic", game_methods,
+						   GameLogic_module_documentation,
+						   (PyObject*)NULL,PYTHON_API_VERSION);
 #endif
-
+	}
+	
 	// Add some symbolic constants to the module
 	d = PyModule_GetDict(m);
 	
@@ -1562,8 +1574,7 @@
 	
 	bpy_import_main_set(maggie);
 	
-	/* run this to clear game modules and user modules which
-	 * may contain references to in game data */
+	/* clear user defined modules that may contain data from the last run */
 	clearGameModules();
 
 	PyObject* moduleobj = PyImport_AddModule("__main__");
@@ -1583,6 +1594,8 @@
 
 static void clearGameModules()
 {
+	/* references to invalid BGE data is better supported in 2.49+ so dont clear dicts */
+#if 0
 	/* Note, user modules could still reference these modules
 	 * but since the dict's are cleared their members wont be accessible */
 	
@@ -1597,9 +1610,10 @@
 	clearModule(modules, "Mathutils");	
 	clearModule(modules, "BGL");	
 	PyErr_Clear(); // incase some of these were alredy removed.
+#endif
 	
-	/* clear user defined modules */
-	bpy_text_clear_modules();
+	/* clear user defined modules, arg '1' for clear external py modules too */
+	bpy_text_clear_modules(1);
 }
 
 void exitGamePythonScripting()
@@ -1633,14 +1647,25 @@
   PyObject* d;
   PyObject* item;
 
-  // Create the module and add the functions
+	/* Use existing module where possible
+	 * be careful not to init any runtime vars after this */
+	m = PyImport_ImportModule( "Rasterizer" );
+	if(m) {
+		Py_DECREF(m);
+		return m;
+	}
+	else {
+		PyErr_Clear();
+	
+		// Create the module and add the functions
 #if (PY_VERSION_HEX >= 0x03000000)
-  m = PyModule_Create(&Rasterizer_module_def);
+		m = PyModule_Create(&Rasterizer_module_def);
 #else
-  m = Py_InitModule4("Rasterizer", rasterizer_methods,
+		m = Py_InitModule4("Rasterizer", rasterizer_methods,
 		     Rasterizer_module_documentation,
 		     (PyObject*)NULL,PYTHON_API_VERSION);
 #endif
+	}
 
   // Add some symbolic constants to the module
   d = PyModule_GetDict(m);
@@ -1756,15 +1781,25 @@
 	PyObject* m;
 	PyObject* d;
 	PyObject* item;
-
-	// Create the module and add the functions
+	
+	/* Use existing module where possible */
+	m = PyImport_ImportModule( "GameKeys" );
+	if(m) {
+		Py_DECREF(m);
+		return m;
+	}
+	else {
+		PyErr_Clear();
+	
+		// Create the module and add the functions
 #if (PY_VERSION_HEX >= 0x03000000)
-	m = PyModule_Create(&GameKeys_module_def);
+		m = PyModule_Create(&GameKeys_module_def);
 #else
-	m = Py_InitModule4("GameKeys", gamekeys_methods,
+		m = Py_InitModule4("GameKeys", gamekeys_methods,
 					   GameKeys_module_documentation,
 					   (PyObject*)NULL,PYTHON_API_VERSION);
 #endif
+	}
 
 	// Add some symbolic constants to the module
 	d = PyModule_GetDict(m);

Modified: trunk/blender/source/gameengine/VideoTexture/blendVideoTex.cpp
===================================================================
--- trunk/blender/source/gameengine/VideoTexture/blendVideoTex.cpp	2009-04-29 23:33:04 UTC (rev 19988)
+++ trunk/blender/source/gameengine/VideoTexture/blendVideoTex.cpp	2009-04-29 23:39:27 UTC (rev 19989)
@@ -192,13 +192,24 @@
 	if (PyType_Ready(&TextureType) < 0) 
 		return NULL;
 
+	/* Use existing module where possible
+	 * be careful not to init any runtime vars after this */
+	m = PyImport_ImportModule( "VideoTexture" );
+	if(m) {
+		Py_DECREF(m);
+		return m;
+	}
+	else {
+		PyErr_Clear();
+	
 #if (PY_VERSION_HEX >= 0x03000000)
-	m = PyModule_Create(&VideoTexture_module_def);
+		m = PyModule_Create(&VideoTexture_module_def);
 #else
-	m = Py_InitModule4("VideoTexture", moduleMethods,
-		"Module that allows to play video files on textures in GameBlender.",
-		(PyObject*)NULL,PYTHON_API_VERSION);
+		m = Py_InitModule4("VideoTexture", moduleMethods,
+			"Module that allows to play video files on textures in GameBlender.",
+			(PyObject*)NULL,PYTHON_API_VERSION);
 #endif
+	}
 	
 	if (m == NULL) 
 		return NULL;
@@ -209,9 +220,10 @@
 
 	Py_INCREF(&TextureType);
 	PyModule_AddObject(m, (char*)"Texture", (PyObject*)&TextureType);
-
+	
 	// init last error description
 	Exception::m_lastError[0] = '\0';
+	
 	return m;
 }
 





More information about the Bf-blender-cvs mailing list