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

This commit is contained in:
2010-09-18 15:30:03 +00:00
parent a45797fe55
commit db7a4e530e
5 changed files with 37 additions and 34 deletions

View File

@@ -357,26 +357,3 @@ void bpy_text_clear_modules(int clear_all)
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);
}

View File

@@ -60,7 +60,4 @@ extern PyMethodDef bpy_reload_meth[];
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 */

View File

@@ -269,3 +269,25 @@ PyObject *PyC_UnicodeFromByte(const char *str)
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);
}

View File

@@ -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 @@ int PyC_AsArray(void *array, PyObject *value, int length, PyTypeObject *type,
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

View File

@@ -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 @@ int BPY_run_python_script( bContext *C, const char *fn, struct Text *text, struc
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 @@ int BPY_run_python_script( bContext *C, const char *fn, struct Text *text, struc
BPY_free_compiled_text( text );
}
}
if(text->compiled)
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);
#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 @@ int BPY_run_python_script_space(const char *modulename, const char *func)
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 @@ int BPY_eval_button(bContext *C, const char *expr, double *value)
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 @@ int BPY_eval_string(bContext *C, const char *expr)
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);