[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [20493] trunk/blender/source: workaround for strange python problem in the BGE and BPy API where printing warnings mistook the blender binary for a script - argv [0], Binary lines were printed into the console sometimes causing console beeps and corrupting future console output .

Campbell Barton ideasman42 at gmail.com
Fri May 29 11:22:27 CEST 2009


Revision: 20493
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=20493
Author:   campbellbarton
Date:     2009-05-29 11:22:24 +0200 (Fri, 29 May 2009)

Log Message:
-----------
workaround for strange python problem in the BGE and BPy API where printing warnings mistook the blender binary for a script - argv[0], Binary lines were printed into the console sometimes causing console beeps and corrupting future console output.

Internal texts file on disk is not used it seems python warnings dont support this (even though exceptions do).

The most common cause of this is passing a float as an argument to a method that took an int.
get around this by setting __file__ in the namespace to the script name before executing the script, the file lines are not found but at least the output is not weird and confusing.

Added read only 'mode' attribute to the python controller so there is a way to tell if its executing a module or a script. Updated docs to better explain execution methods.

Modified Paths:
--------------
    trunk/blender/source/blender/python/BPY_interface.c
    trunk/blender/source/gameengine/GameLogic/SCA_PythonController.cpp
    trunk/blender/source/gameengine/PyDoc/GameTypes.py

Modified: trunk/blender/source/blender/python/BPY_interface.c
===================================================================
--- trunk/blender/source/blender/python/BPY_interface.c	2009-05-29 08:55:14 UTC (rev 20492)
+++ trunk/blender/source/blender/python/BPY_interface.c	2009-05-29 09:22:24 UTC (rev 20493)
@@ -2780,7 +2780,11 @@
 		}
 
 	}
-
+	
+	/* Without __file__ set the sys.argv[0] is used for the filename
+	 * which ends up with lines from the blender binary being printed in the console */
+	PyDict_SetItemString(globaldict, "__file__", PyString_FromString(text->id.name+2));
+	
 	return PyEval_EvalCode( text->compiled, globaldict, globaldict );
 }
 

Modified: trunk/blender/source/gameengine/GameLogic/SCA_PythonController.cpp
===================================================================
--- trunk/blender/source/gameengine/GameLogic/SCA_PythonController.cpp	2009-05-29 08:55:14 UTC (rev 20492)
+++ trunk/blender/source/gameengine/GameLogic/SCA_PythonController.cpp	2009-05-29 09:22:24 UTC (rev 20493)
@@ -147,6 +147,11 @@
 		Py_DECREF(m_pythondictionary);
 	}
 	m_pythondictionary = PyDict_Copy(pythondictionary); /* new reference */
+	
+	/* Without __file__ set the sys.argv[0] is used for the filename
+	 * which ends up with lines from the blender binary being printed in the console */
+	PyDict_SetItemString(m_pythondictionary, "__file__", PyString_FromString(m_scriptName.Ptr()));
+	
 }
 
 int SCA_PythonController::IsTriggered(class SCA_ISensor* sensor)
@@ -266,6 +271,7 @@
 
 PyAttributeDef SCA_PythonController::Attributes[] = {
 	KX_PYATTRIBUTE_RW_FUNCTION("script", SCA_PythonController, pyattr_get_script, pyattr_set_script),
+	KX_PYATTRIBUTE_INT_RO("mode", SCA_PythonController, m_mode),
 	{ NULL }	//Sentinel
 };
 

Modified: trunk/blender/source/gameengine/PyDoc/GameTypes.py
===================================================================
--- trunk/blender/source/gameengine/PyDoc/GameTypes.py	2009-05-29 08:55:14 UTC (rev 20492)
+++ trunk/blender/source/gameengine/PyDoc/GameTypes.py	2009-05-29 09:22:24 UTC (rev 20493)
@@ -5205,8 +5205,15 @@
 	
 	Properties:
 	
-	@ivar script: the Python script this controller executes
+	@ivar script: The value of this variable depends on the execution methid.
+		- When 'Script' execution mode is set this value contains the entire python script as a single string (not the script name as you might expect) which can be modified to run different scripts.
+		- When 'Module' execution mode is set this value will contain a single line string - module name and function "module.func" or "package.modile.func" where the module names are python textblocks or external scripts.
+		note: once this is set the script name given for warnings will remain unchanged.
 	@type script: string
+	@ivar mode: the execution mode for this controller (read-only).
+		- Script: 0, Execite the L{script} as a python code.
+		- Module: 1, Execite the L{script} as a module and function.
+	@type mode: int
 	
 	@group Deprecated: getScript, setScript
 	"""
@@ -5222,17 +5229,17 @@
 		"""
 	def getScript():
 		"""
-		Gets the Python script this controller executes.
+		Gets the Python script body this controller executes.
 		
 		@deprecated: Use the L{script} attribute instead.
 		@rtype: string
 		"""
-	def setScript(script):
+	def setScript(script_body):
 		"""
-		Sets the Python script this controller executes.
+		Sets the Python script string this controller executes.
 		
 		@deprecated: Use the L{script} attribute instead.
-		@type script: string.
+		@type script_body: string.
 		"""
 
 class SCA_RandomActuator(SCA_IActuator):





More information about the Bf-blender-cvs mailing list