diff options
Diffstat (limited to 'source/blender/python/intern/bpy_interface.c')
-rw-r--r-- | source/blender/python/intern/bpy_interface.c | 74 |
1 files changed, 31 insertions, 43 deletions
diff --git a/source/blender/python/intern/bpy_interface.c b/source/blender/python/intern/bpy_interface.c index bf91b0498ed..af2b5b41961 100644 --- a/source/blender/python/intern/bpy_interface.c +++ b/source/blender/python/intern/bpy_interface.c @@ -44,6 +44,7 @@ #include "BKE_context.h" #include "BKE_text.h" +#include "BKE_font.h" /* only for utf8towchar */ #include "BKE_main.h" #include "BKE_global.h" /* only for script checking */ @@ -145,29 +146,6 @@ void BPY_update_modules( void ) bpy_context_module->ptr.data= (void *)BPy_GetContext(); } -/***************************************************************************** -* Description: This function creates a new Python dictionary object. -*****************************************************************************/ -static PyObject *CreateGlobalDictionary( bContext *C, const char *filename ) -{ - PyObject *item; - PyObject *dict = PyDict_New( ); - PyDict_SetItemString( dict, "__builtins__", PyEval_GetBuiltins( ) ); - - item = PyUnicode_FromString( "__main__" ); - PyDict_SetItemString( dict, "__name__", item ); - Py_DECREF(item); - - /* __file__ only for nice UI'ness */ - if(filename) { - PyObject *item = PyUnicode_FromString( filename ); - PyDict_SetItemString( dict, "__file__", item ); - Py_DECREF(item); - } - - return dict; -} - /* must be called before Py_Initialize */ void BPY_start_python_path(void) { @@ -204,10 +182,15 @@ void BPY_start_python_path(void) #endif { - static wchar_t py_path_bundle_wchar[FILE_MAXDIR]; + static wchar_t py_path_bundle_wchar[FILE_MAX]; + + /* cant use this, on linux gives bug: #23018, TODO: try LANG="en_US.UTF-8" /usr/bin/blender, suggested 22008 */ + /* mbstowcs(py_path_bundle_wchar, py_path_bundle, FILE_MAXDIR); */ + + utf8towchar(py_path_bundle_wchar, py_path_bundle); - mbstowcs(py_path_bundle_wchar, py_path_bundle, FILE_MAXDIR); Py_SetPythonHome(py_path_bundle_wchar); + // printf("found python (wchar_t) '%ls'\n", py_path_bundle_wchar); } } @@ -223,9 +206,12 @@ void BPY_start_python( int argc, char **argv ) { PyThreadState *py_tstate = NULL; - BPY_start_python_path(); /* allow to use our own included python */ + /* not essential but nice to set our name */ + static wchar_t bprogname_wchar[FILE_MAXDIR+FILE_MAXFILE]; /* python holds a reference */ + utf8towchar(bprogname_wchar, bprogname); + Py_SetProgramName(bprogname_wchar); - // Py_SetProgramName(); // extern char bprogname[FILE_MAXDIR+FILE_MAXFILE]; + BPY_start_python_path(); /* allow to use our own included python */ Py_Initialize( ); @@ -321,13 +307,13 @@ int BPY_run_python_script( bContext *C, const char *fn, struct Text *text, struc if (fn==NULL && text==NULL) { return 0; } - + bpy_context_set(C, &gilstate); if (text) { char fn_dummy[FILE_MAXDIR]; bpy_text_filename_get(fn_dummy, text); - py_dict = CreateGlobalDictionary(C, fn_dummy); + 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 ); @@ -348,7 +334,7 @@ int BPY_run_python_script( bContext *C, const char *fn, struct Text *text, struc else { FILE *fp= fopen(fn, "r"); - py_dict = CreateGlobalDictionary(C, fn); + py_dict = bpy_namespace_dict_new(fn); if(fp) { #ifdef _WIN32 @@ -383,7 +369,7 @@ int BPY_run_python_script( bContext *C, const char *fn, struct Text *text, struc Py_DECREF( py_result ); } - Py_DECREF(py_dict); + PyDict_SetItemString(PyThreadState_GET()->interp->modules, "__main__", Py_None); bpy_context_clear(C, &gilstate); @@ -492,7 +478,7 @@ int BPY_run_python_script_space(const char *modulename, const char *func) gilstate = PyGILState_Ensure(); - py_dict = CreateGlobalDictionary(C); + py_dict = bpy_namespace_dict_new("<dummy>"); PyObject *module = PyImport_ImportModule(scpt->script.filename); if (module==NULL) { @@ -521,7 +507,7 @@ int BPY_run_python_script_space(const char *modulename, const char *func) Py_XDECREF(module); - Py_DECREF(py_dict); + PyDict_SetItemString(PyThreadState_GET()->interp->modules, "__main__", Py_None); PyGILState_Release(gilstate); return 1; @@ -532,7 +518,7 @@ int BPY_run_python_script_space(const char *modulename, const char *func) int BPY_eval_button(bContext *C, const char *expr, double *value) { PyGILState_STATE gilstate; - PyObject *dict, *mod, *retval; + PyObject *py_dict, *mod, *retval; int error_ret = 0; if (!value || !expr) return -1; @@ -544,11 +530,11 @@ int BPY_eval_button(bContext *C, const char *expr, double *value) bpy_context_set(C, &gilstate); - dict= CreateGlobalDictionary(C, NULL); + py_dict= bpy_namespace_dict_new("<blender button>"); mod = PyImport_ImportModule("math"); if (mod) { - PyDict_Merge(dict, PyModule_GetDict(mod), 0); /* 0 - dont overwrite existing values */ + PyDict_Merge(py_dict, PyModule_GetDict(mod), 0); /* 0 - dont overwrite existing values */ Py_DECREF(mod); } else { /* highly unlikely but possibly */ @@ -556,7 +542,7 @@ int BPY_eval_button(bContext *C, const char *expr, double *value) PyErr_Clear(); } - retval = PyRun_String(expr, Py_eval_input, dict, dict); + retval = PyRun_String(expr, Py_eval_input, py_dict, py_dict); if (retval == NULL) { error_ret= -1; @@ -593,8 +579,9 @@ int BPY_eval_button(bContext *C, const char *expr, double *value) if(error_ret) { BPy_errors_to_report(CTX_wm_reports(C)); } + + PyDict_SetItemString(PyThreadState_GET()->interp->modules, "__main__", Py_None); - Py_DECREF(dict); bpy_context_clear(C, &gilstate); return error_ret; @@ -603,7 +590,7 @@ int BPY_eval_button(bContext *C, const char *expr, double *value) int BPY_eval_string(bContext *C, const char *expr) { PyGILState_STATE gilstate; - PyObject *dict, *retval; + PyObject *py_dict, *retval; int error_ret = 0; if (!expr) return -1; @@ -614,9 +601,9 @@ int BPY_eval_string(bContext *C, const char *expr) bpy_context_set(C, &gilstate); - dict= CreateGlobalDictionary(C, NULL); + py_dict= bpy_namespace_dict_new("<blender string>"); - retval = PyRun_String(expr, Py_eval_input, dict, dict); + retval = PyRun_String(expr, Py_eval_input, py_dict, py_dict); if (retval == NULL) { error_ret= -1; @@ -627,9 +614,10 @@ int BPY_eval_string(bContext *C, const char *expr) Py_DECREF(retval); } - Py_DECREF(dict); + PyDict_SetItemString(PyThreadState_GET()->interp->modules, "__main__", Py_None); + bpy_context_clear(C, &gilstate); - + return error_ret; } |