[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [32001] trunk/blender/source/blender/ python: move namespace creation function into py_capi_utils.c, to be used in more general cases, not just the blender/rna api.

Campbell Barton ideasman42 at gmail.com
Sat Sep 18 17:30:03 CEST 2010


Revision: 32001
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=32001
Author:   campbellbarton
Date:     2010-09-18 17:30:03 +0200 (Sat, 18 Sep 2010)

Log Message:
-----------
move namespace creation function into py_capi_utils.c, to be used in more general cases, not just the blender/rna api.

Modified Paths:
--------------
    trunk/blender/source/blender/python/generic/bpy_internal_import.c
    trunk/blender/source/blender/python/generic/bpy_internal_import.h
    trunk/blender/source/blender/python/generic/py_capi_utils.c
    trunk/blender/source/blender/python/generic/py_capi_utils.h
    trunk/blender/source/blender/python/intern/bpy_interface.c

Modified: trunk/blender/source/blender/python/generic/bpy_internal_import.c
===================================================================
--- trunk/blender/source/blender/python/generic/bpy_internal_import.c	2010-09-18 14:47:59 UTC (rev 32000)
+++ trunk/blender/source/blender/python/generic/bpy_internal_import.c	2010-09-18 15:30:03 UTC (rev 32001)
@@ -357,26 +357,3 @@
 	Py_DECREF(list); /* removes all references from append */
 }
 #endif
-
-
-/*****************************************************************************
-* Description: This function creates a new Python dictionary object.
-* note: dict is owned by sys.modules["__main__"] module, reference is borrowed
-* note: important we use the dict from __main__, this is what python expects
-  for 'pickle' to work as well as strings like this...
- >> foo = 10
- >> print(__import__("__main__").foo)
-*****************************************************************************/
-PyObject *bpy_namespace_dict_new(const char *filename)
-{
-	PyInterpreterState *interp= PyThreadState_GET()->interp;
-	PyObject *mod_main= PyModule_New("__main__");	
-	PyDict_SetItemString(interp->modules, "__main__", mod_main);
-	Py_DECREF(mod_main); /* sys.modules owns now */
-	PyModule_AddStringConstant(mod_main, "__name__", "__main__");
-	if(filename)
-		PyModule_AddStringConstant(mod_main, "__file__", filename); /* __file__ only for nice UI'ness */
-	PyModule_AddObject(mod_main, "__builtins__", interp->builtins);
-	Py_INCREF(interp->builtins); /* AddObject steals a reference */
-	return PyModule_GetDict(mod_main);
-}

Modified: trunk/blender/source/blender/python/generic/bpy_internal_import.h
===================================================================
--- trunk/blender/source/blender/python/generic/bpy_internal_import.h	2010-09-18 14:47:59 UTC (rev 32000)
+++ trunk/blender/source/blender/python/generic/bpy_internal_import.h	2010-09-18 15:30:03 UTC (rev 32001)
@@ -60,7 +60,4 @@
 struct Main *bpy_import_main_get(void);
 void bpy_import_main_set(struct Main *maggie);
 
-/* name namespace function for bpy & bge */
-PyObject *bpy_namespace_dict_new(const char *filename);
-
 #endif				/* EXPP_bpy_import_h */

Modified: trunk/blender/source/blender/python/generic/py_capi_utils.c
===================================================================
--- trunk/blender/source/blender/python/generic/py_capi_utils.c	2010-09-18 14:47:59 UTC (rev 32000)
+++ trunk/blender/source/blender/python/generic/py_capi_utils.c	2010-09-18 15:30:03 UTC (rev 32001)
@@ -269,3 +269,25 @@
 		return result;
 	}
 }
+
+/*****************************************************************************
+* Description: This function creates a new Python dictionary object.
+* note: dict is owned by sys.modules["__main__"] module, reference is borrowed
+* note: important we use the dict from __main__, this is what python expects
+  for 'pickle' to work as well as strings like this...
+ >> foo = 10
+ >> print(__import__("__main__").foo)
+*****************************************************************************/
+PyObject *PyC_DefaultNameSpace(const char *filename)
+{
+	PyInterpreterState *interp= PyThreadState_GET()->interp;
+	PyObject *mod_main= PyModule_New("__main__");	
+	PyDict_SetItemString(interp->modules, "__main__", mod_main);
+	Py_DECREF(mod_main); /* sys.modules owns now */
+	PyModule_AddStringConstant(mod_main, "__name__", "__main__");
+	if(filename)
+		PyModule_AddStringConstant(mod_main, "__file__", filename); /* __file__ only for nice UI'ness */
+	PyModule_AddObject(mod_main, "__builtins__", interp->builtins);
+	Py_INCREF(interp->builtins); /* AddObject steals a reference */
+	return PyModule_GetDict(mod_main);
+}

Modified: trunk/blender/source/blender/python/generic/py_capi_utils.h
===================================================================
--- trunk/blender/source/blender/python/generic/py_capi_utils.h	2010-09-18 14:47:59 UTC (rev 32000)
+++ trunk/blender/source/blender/python/generic/py_capi_utils.h	2010-09-18 15:30:03 UTC (rev 32001)
@@ -26,6 +26,7 @@
 #define PY_CAPI_UTILS_H
 
 struct PyObject;
+struct PyTypeObject;
 
 void			PyC_ObSpit(char *name, PyObject *var);
 void			PyC_LineSpit(void);
@@ -38,4 +39,7 @@
 PyObject *		PyC_UnicodeFromByte(const char *str);
 const char *	PuC_UnicodeAsByte(PyObject *py_str, PyObject **coerce); /* coerce must be NULL */
 
+/* name namespace function for bpy & bge */
+PyObject *		PyC_DefaultNameSpace(const char *filename);
+
 #endif // PY_CAPI_UTILS_H

Modified: trunk/blender/source/blender/python/intern/bpy_interface.c
===================================================================
--- trunk/blender/source/blender/python/intern/bpy_interface.c	2010-09-18 14:47:59 UTC (rev 32000)
+++ trunk/blender/source/blender/python/intern/bpy_interface.c	2010-09-18 15:30:03 UTC (rev 32001)
@@ -52,6 +52,7 @@
 #include "BPY_extern.h"
 
 #include "../generic/bpy_internal_import.h" // our own imports
+#include "../generic/py_capi_utils.h"
 
 /* for internal use, when starting and ending python scripts */
 
@@ -306,7 +307,6 @@
 	if (text) {
 		char fn_dummy[FILE_MAXDIR];
 		bpy_text_filename_get(fn_dummy, text);
-		py_dict = bpy_namespace_dict_new(fn_dummy);
 		
 		if( !text->compiled ) {	/* if it wasn't already compiled, do it now */
 			char *buf = txt_to_buf( text );
@@ -320,16 +320,19 @@
 				BPY_free_compiled_text( text );
 			}
 		}
-		if(text->compiled)
-			py_result =  PyEval_EvalCode( text->compiled, py_dict, py_dict );
+
+		if(text->compiled) {
+			py_dict = PyC_DefaultNameSpace(fn_dummy);
+			py_result =  PyEval_EvalCode(text->compiled, py_dict, py_dict);
+		}
 		
 	}
 	else {
 		FILE *fp= fopen(fn, "r");
 
-		py_dict = bpy_namespace_dict_new(fn);
+		if(fp) {
+			py_dict = PyC_DefaultNameSpace(fn);
 
-		if(fp) {
 #ifdef _WIN32
 			/* Previously we used PyRun_File to run directly the code on a FILE 
 			 * object, but as written in the Python/C API Ref Manual, chapter 2,
@@ -471,7 +474,7 @@
 	
 	gilstate = PyGILState_Ensure();
 	
-	py_dict = bpy_namespace_dict_new("<dummy>");
+	py_dict = PyC_DefaultNameSpace("<dummy>");
 	
 	PyObject *module = PyImport_ImportModule(scpt->script.filename);
 	if (module==NULL) {
@@ -523,7 +526,7 @@
 
 	bpy_context_set(C, &gilstate);
 	
-	py_dict= bpy_namespace_dict_new("<blender button>");
+	py_dict= PyC_DefaultNameSpace("<blender button>");
 
 	mod = PyImport_ImportModule("math");
 	if (mod) {
@@ -594,7 +597,7 @@
 
 	bpy_context_set(C, &gilstate);
 
-	py_dict= bpy_namespace_dict_new("<blender string>");
+	py_dict= PyC_DefaultNameSpace("<blender string>");
 
 	retval = PyRun_String(expr, Py_eval_input, py_dict, py_dict);
 





More information about the Bf-blender-cvs mailing list