[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [19726] trunk/blender/source: BGE Py Api
Campbell Barton
ideasman42 at gmail.com
Wed Apr 15 10:08:46 CEST 2009
Revision: 19726
http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=19726
Author: campbellbarton
Date: 2009-04-15 10:08:42 +0200 (Wed, 15 Apr 2009)
Log Message:
-----------
BGE Py Api
importing modules wasnt returning the error from the blender text if it failed.
Modified Paths:
--------------
trunk/blender/source/blender/python/BPY_interface.c
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/Ketsji/KX_PythonInit.cpp
Modified: trunk/blender/source/blender/python/BPY_interface.c
===================================================================
--- trunk/blender/source/blender/python/BPY_interface.c 2009-04-15 07:00:11 UTC (rev 19725)
+++ trunk/blender/source/blender/python/BPY_interface.c 2009-04-15 08:08:42 UTC (rev 19726)
@@ -1275,7 +1275,8 @@
/* If there's a Blender text called pydrivers.py, import it.
* Users can add their own functions to this module. */
if (G.f&G_DOSCRIPTLINKS) {
- mod = importText("pydrivers"); /* can also use PyImport_Import() */
+ int found; /* not used but needed as an arg */
+ mod = importText("pydrivers", &found); /* can also use PyImport_Import() */
if (mod) {
PyDict_SetItemString(d, "pydrivers", mod);
PyDict_SetItemString(d, "p", mod);
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-15 07:00:11 UTC (rev 19725)
+++ trunk/blender/source/blender/python/api2_2x/bpy_internal_import.c 2009-04-15 08:08:42 UTC (rev 19726)
@@ -56,7 +56,7 @@
}
-PyObject *importText( char *name )
+PyObject *importText( char *name, int *found )
{
Text *text;
char txtname[22]; /* 21+NULL */
@@ -64,6 +64,8 @@
int namelen = strlen( name );
Main *maggie= bpy_import_main ? bpy_import_main:G.main;
+ *found= 0;
+
if (namelen>21-3) return NULL; /* we know this cant be importable, the name is too long for blender! */
memcpy( txtname, name, namelen );
@@ -76,7 +78,9 @@
if( !text )
return NULL;
-
+ else
+ *found = 1;
+
if( !text->compiled ) {
buf = txt_to_buf( text );
text->compiled = Py_CompileString( buf, text->id.name+2, Py_file_input );
@@ -99,7 +103,7 @@
* find in-memory module and recompile
*/
-PyObject *reimportText( PyObject *module )
+PyObject *reimportText( PyObject *module, int *found )
{
Text *text;
char *txtname;
@@ -107,6 +111,8 @@
char *buf = NULL;
Main *maggie= bpy_import_main ? bpy_import_main:G.main;
+ *found= 0;
+
/* get name, filename from the module itself */
txtname = PyModule_GetFilename( module );
@@ -125,6 +131,8 @@
/* uh-oh.... didn't find it */
if( !text )
return NULL;
+ else
+ *found = 1;
/* if previously compiled, free the object */
/* (can't see how could be NULL, but check just in case) */
@@ -155,8 +163,9 @@
{
PyObject *exception, *err, *tb;
char *name;
+ int found= 0;
PyObject *globals = NULL, *locals = NULL, *fromlist = NULL;
- PyObject *m;
+ PyObject *newmodule;
//PyObject_Print(args, stderr, 0);
#if (PY_VERSION_HEX >= 0x02060000)
@@ -173,24 +182,37 @@
&name, &globals, &locals, &fromlist ) )
return NULL;
#endif
- m = PyImport_ImportModuleEx( name, globals, locals, fromlist );
- if( m )
- return m;
- else
- PyErr_Fetch( &exception, &err, &tb ); /*restore for probable later use */
-
- m = importText( name );
- if( m ) { /* found module, ignore above exception */
+ /* import existing builtin modules or modules that have been imported alredy */
+ newmodule = PyImport_ImportModuleEx( name, globals, locals, fromlist );
+
+ if(newmodule)
+ return newmodule;
+
+ PyErr_Fetch( &exception, &err, &tb ); /* get the python error incase we cant import as blender text either */
+
+ /* importing from existing modules failed, see if we have this module as blender text */
+ newmodule = importText( name, &found );
+
+ if( newmodule ) {/* found module as blender text, ignore above exception */
PyErr_Clear( );
Py_XDECREF( exception );
Py_XDECREF( err );
Py_XDECREF( tb );
/* printf( "imported from text buffer...\n" ); */
- } else {
+ }
+ else if (found==1) { /* blender text module failed to execute but was found, use its error message */
+ Py_XDECREF( exception );
+ Py_XDECREF( err );
+ Py_XDECREF( tb );
+ return NULL;
+ }
+ else {
+ /* no blender text was found that could import the module
+ * rause the original error from PyImport_ImportModuleEx */
PyErr_Restore( exception, err, tb );
}
- return m;
+ return newmodule;
}
@@ -203,7 +225,8 @@
PyObject *exception, *err, *tb;
PyObject *module = NULL;
PyObject *newmodule = NULL;
-
+ int found= 0;
+
/* check for a module arg */
if( !PyArg_ParseTuple( args, "O:bpy_reload", &module ) )
return NULL;
@@ -216,14 +239,25 @@
/* no file, try importing from memory */
PyErr_Fetch( &exception, &err, &tb ); /*restore for probable later use */
- newmodule = reimportText( module );
- if( newmodule ) { /* found module, ignore above exception */
+ newmodule = reimportText( module, &found );
+ if( newmodule ) {/* found module as blender text, ignore above exception */
PyErr_Clear( );
Py_XDECREF( exception );
Py_XDECREF( err );
Py_XDECREF( tb );
- } else
+ /* printf( "imported from text buffer...\n" ); */
+ }
+ else if (found==1) { /* blender text module failed to execute but was found, use its error message */
+ Py_XDECREF( exception );
+ Py_XDECREF( err );
+ Py_XDECREF( tb );
+ return NULL;
+ }
+ else {
+ /* no blender text was found that could import the module
+ * rause the original error from PyImport_ImportModuleEx */
PyErr_Restore( exception, err, tb );
+ }
return newmodule;
}
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-15 07:00:11 UTC (rev 19725)
+++ trunk/blender/source/blender/python/api2_2x/bpy_internal_import.h 2009-04-15 08:08:42 UTC (rev 19726)
@@ -35,8 +35,8 @@
#include "compile.h" /* for the PyCodeObject */
#include "eval.h" /* for PyEval_EvalCode */
-PyObject *importText( char *name );
-PyObject *reimportText( PyObject *module );
+PyObject *importText( char *name, int *found );
+PyObject *reimportText( PyObject *module, int *found );
extern PyMethodDef bpy_import[];
extern PyMethodDef bpy_reload[];
Modified: trunk/blender/source/gameengine/Ketsji/KX_PythonInit.cpp
===================================================================
--- trunk/blender/source/gameengine/Ketsji/KX_PythonInit.cpp 2009-04-15 07:00:11 UTC (rev 19725)
+++ trunk/blender/source/gameengine/Ketsji/KX_PythonInit.cpp 2009-04-15 08:08:42 UTC (rev 19726)
@@ -1209,6 +1209,7 @@
PyObject *KXpy_import(PyObject *self, PyObject *args)
{
char *name;
+ int found;
PyObject *globals = NULL;
PyObject *locals = NULL;
PyObject *fromlist = NULL;
@@ -1243,12 +1244,13 @@
}
/* Import blender texts as python modules */
- m= importText(name);
+ m= importText(name, &found);
if (m)
return m;
-
- PyErr_Format(PyExc_ImportError,
- "Import of external Module %.20s not allowed.", name);
+
+ if(found==0) /* if its found but could not import then it has its own error */
+ PyErr_Format(PyExc_ImportError, "Import of external Module %.20s not allowed.", name);
+
return NULL;
}
@@ -1260,7 +1262,7 @@
PyErr_SetString(PyExc_RuntimeError, "Sandbox: reload() function disabled!\nGame Scripts should not use this function.");
return NULL;
#endif
-
+ int found;
PyObject *module = NULL;
PyObject *newmodule = NULL;
@@ -1268,9 +1270,11 @@
if( !PyArg_ParseTuple( args, "O:bpy_reload", &module ) )
return NULL;
- newmodule= reimportText( module );
+ newmodule= reimportText( module, &found );
+ if (newmodule)
+ return newmodule;
- if (newmodule==NULL)
+ if (found==0) /* if its found but could not import then it has its own error */
PyErr_SetString(PyExc_ImportError, "failed to reload from blenders internal text");
return newmodule;
More information about the Bf-blender-cvs
mailing list