BGE Py Api
importing modules wasnt returning the error from the blender text if it failed.
This commit is contained in:
@@ -1275,7 +1275,8 @@ static int bpy_pydriver_create_dict(void)
|
||||
/* 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);
|
||||
|
||||
@@ -56,7 +56,7 @@ void bpy_import_main_set(struct Main *maggie)
|
||||
}
|
||||
|
||||
|
||||
PyObject *importText( char *name )
|
||||
PyObject *importText( char *name, int *found )
|
||||
{
|
||||
Text *text;
|
||||
char txtname[22]; /* 21+NULL */
|
||||
@@ -64,6 +64,8 @@ PyObject *importText( char *name )
|
||||
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,6 +78,8 @@ PyObject *importText( char *name )
|
||||
|
||||
if( !text )
|
||||
return NULL;
|
||||
else
|
||||
*found = 1;
|
||||
|
||||
if( !text->compiled ) {
|
||||
buf = txt_to_buf( text );
|
||||
@@ -99,7 +103,7 @@ PyObject *importText( char *name )
|
||||
* find in-memory module and recompile
|
||||
*/
|
||||
|
||||
PyObject *reimportText( PyObject *module )
|
||||
PyObject *reimportText( PyObject *module, int *found )
|
||||
{
|
||||
Text *text;
|
||||
char *txtname;
|
||||
@@ -107,6 +111,8 @@ PyObject *reimportText( PyObject *module )
|
||||
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 @@ PyObject *reimportText( PyObject *module )
|
||||
/* 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 @@ static PyObject *blender_import( PyObject * self, PyObject * args, PyObject * k
|
||||
{
|
||||
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 @@ static PyObject *blender_import( PyObject * self, PyObject * args, PyObject * k
|
||||
&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 */
|
||||
/* import existing builtin modules or modules that have been imported alredy */
|
||||
newmodule = PyImport_ImportModuleEx( name, globals, locals, fromlist );
|
||||
|
||||
m = importText( name );
|
||||
if( m ) { /* found module, ignore above exception */
|
||||
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,6 +225,7 @@ static PyObject *blender_reload( PyObject * self, PyObject * args )
|
||||
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 ) )
|
||||
@@ -216,14 +239,25 @@ static PyObject *blender_reload( PyObject * self, PyObject * args )
|
||||
/* 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;
|
||||
}
|
||||
|
||||
@@ -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[];
|
||||
|
||||
|
||||
@@ -1209,6 +1209,7 @@ PyObject *KXpy_compile(PyObject *self, PyObject *args) {
|
||||
PyObject *KXpy_import(PyObject *self, PyObject *args)
|
||||
{
|
||||
char *name;
|
||||
int found;
|
||||
PyObject *globals = NULL;
|
||||
PyObject *locals = NULL;
|
||||
PyObject *fromlist = NULL;
|
||||
@@ -1243,12 +1244,13 @@ PyObject *KXpy_import(PyObject *self, PyObject *args)
|
||||
}
|
||||
|
||||
/* 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 @@ PyObject *KXpy_reload(PyObject *self, PyObject *args) {
|
||||
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 @@ PyObject *KXpy_reload(PyObject *self, PyObject *args) {
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user