diff options
Diffstat (limited to 'source/blender/python/intern/bpy_interface.c')
-rw-r--r-- | source/blender/python/intern/bpy_interface.c | 164 |
1 files changed, 84 insertions, 80 deletions
diff --git a/source/blender/python/intern/bpy_interface.c b/source/blender/python/intern/bpy_interface.c index 4f59467a9d6..83c52464c9c 100644 --- a/source/blender/python/intern/bpy_interface.c +++ b/source/blender/python/intern/bpy_interface.c @@ -22,6 +22,11 @@ * * ***** END GPL LICENSE BLOCK ***** */ + +/** \file blender/python/intern/bpy_interface.c + * \ingroup pythonintern + */ + /* grr, python redefines */ #ifdef _POSIX_C_SOURCE @@ -32,6 +37,8 @@ #include "MEM_guardedalloc.h" +#include "RNA_types.h" + #include "bpy.h" #include "bpy_rna.h" #include "bpy_util.h" @@ -73,7 +80,7 @@ BPy_StructRNA *bpy_context_module= NULL; /* for fast access */ #ifdef TIME_PY_RUN #include "PIL_time.h" -static int bpy_timer_count = 0; +static int bpy_timer_count= 0; static double bpy_timer; /* time since python starts */ static double bpy_timer_run; /* time for each python script run */ static double bpy_timer_run_tot; /* accumulate python runs */ @@ -84,7 +91,7 @@ void bpy_context_set(bContext *C, PyGILState_STATE *gilstate) py_call_level++; if(gilstate) - *gilstate = PyGILState_Ensure(); + *gilstate= PyGILState_Ensure(); if(py_call_level==1) { @@ -102,7 +109,7 @@ void bpy_context_set(bContext *C, PyGILState_STATE *gilstate) if(bpy_timer_count==0) { /* record time from the beginning */ bpy_timer= PIL_check_seconds_timer(); - bpy_timer_run = bpy_timer_run_tot = 0.0; + bpy_timer_run= bpy_timer_run_tot= 0.0; } bpy_timer_run= PIL_check_seconds_timer(); @@ -138,9 +145,9 @@ void bpy_context_clear(bContext *UNUSED(C), PyGILState_STATE *gilstate) void BPY_text_free_code(Text *text) { - if( text->compiled ) { - Py_DECREF( ( PyObject * ) text->compiled ); - text->compiled = NULL; + if(text->compiled) { + Py_DECREF((PyObject *)text->compiled); + text->compiled= NULL; } } @@ -148,8 +155,8 @@ void BPY_modules_update(bContext *C) { #if 0 // slow, this runs all the time poll, draw etc 100's of time a sec. PyObject *mod= PyImport_ImportModuleLevel("bpy", NULL, NULL, NULL, 0); - PyModule_AddObject( mod, "data", BPY_rna_module() ); - PyModule_AddObject( mod, "types", BPY_rna_types() ); // atm this does not need updating + PyModule_AddObject(mod, "data", BPY_rna_module()); + PyModule_AddObject(mod, "types", BPY_rna_types()); // atm this does not need updating #endif /* refreshes the main struct */ @@ -163,9 +170,13 @@ static void bpy_python_start_path(void) { char *py_path_bundle= BLI_get_folder(BLENDER_PYTHON, NULL); - if(py_path_bundle==NULL) + if(py_path_bundle==NULL) { + /* Common enough to have bundled *nix python but complain on OSX/Win */ +#if defined(__APPLE__) || defined(_WIN32) + fprintf(stderr, "Warning! bundled python not found and is expected on this platform. (if you built with CMake: 'install' target may have not been built)\n"); +#endif return; - + } /* set the environment path */ printf("found bundled python: %s\n", py_path_bundle); @@ -221,26 +232,26 @@ static struct _inittab bpy_internal_modules[]= { void BPY_python_start(int argc, const char **argv) { #ifndef WITH_PYTHON_MODULE - PyThreadState *py_tstate = NULL; + PyThreadState *py_tstate= NULL; /* 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); - /* builtin modules */ + /* must run before python initializes */ PyImport_ExtendInittab(bpy_internal_modules); bpy_python_start_path(); /* allow to use our own included python */ - /* Python 3.2 now looks for '2.56/python/include/python3.2d/pyconfig.h' to parse + /* Python 3.2 now looks for '2.57/python/include/python3.2d/pyconfig.h' to parse * from the 'sysconfig' module which is used by 'site', so for now disable site. * alternatively we could copy the file. */ Py_NoSiteFlag= 1; - Py_Initialize( ); + Py_Initialize(); - // PySys_SetArgv( argc, argv); // broken in py3, not a huge deal + // PySys_SetArgv(argc, argv); // broken in py3, not a huge deal /* sigh, why do python guys not have a char** version anymore? :( */ { int i; @@ -257,39 +268,20 @@ void BPY_python_start(int argc, const char **argv) #else (void)argc; (void)argv; - + + /* must run before python initializes */ PyImport_ExtendInittab(bpy_internal_modules); #endif /* bpy.* and lets us import it */ BPy_init_modules(); - { /* our own import and reload functions */ - PyObject *item; - PyObject *mod; - //PyObject *m = PyImport_AddModule("__builtin__"); - //PyObject *d = PyModule_GetDict(m); - PyObject *d = PyEval_GetBuiltins( ); -// PyDict_SetItemString(d, "reload", item=PyCFunction_New(&bpy_reload_meth, NULL)); Py_DECREF(item); - PyDict_SetItemString(d, "__import__", item=PyCFunction_New(&bpy_import_meth, NULL)); Py_DECREF(item); - - /* move reload here - * XXX, use import hooks */ - mod= PyImport_ImportModuleLevel((char *)"imp", NULL, NULL, NULL, 0); - if(mod) { - PyDict_SetItemString(PyModule_GetDict(mod), "reload", item=PyCFunction_New(&bpy_reload_meth, NULL)); Py_DECREF(item); - Py_DECREF(mod); - } - else { - BLI_assert(!"unable to load 'imp' module."); - } - - } + bpy_import_init(PyEval_GetBuiltins()); pyrna_alloc_types(); #ifndef WITH_PYTHON_MODULE - py_tstate = PyGILState_GetThisThreadState(); + py_tstate= PyGILState_GetThisThreadState(); PyEval_ReleaseThread(py_tstate); #endif } @@ -305,11 +297,11 @@ void BPY_python_end(void) /* clear all python data from structs */ - Py_Finalize( ); + Py_Finalize(); #ifdef TIME_PY_RUN // measure time since py started - bpy_timer = PIL_check_seconds_timer() - bpy_timer; + bpy_timer= PIL_check_seconds_timer() - bpy_timer; printf("*bpy stats* - "); printf("tot exec: %d, ", bpy_timer_count); @@ -353,7 +345,7 @@ typedef struct { } PyModuleObject; #endif -static int python_script_exec(bContext *C, const char *fn, struct Text *text, struct ReportList *reports) +static int python_script_exec(bContext *C, const char *fn, struct Text *text, struct ReportList *reports, const short do_jump) { PyObject *main_mod= NULL; PyObject *py_dict= NULL, *py_result= NULL; @@ -371,25 +363,26 @@ static int python_script_exec(bContext *C, const char *fn, struct Text *text, st if (text) { char fn_dummy[FILE_MAXDIR]; - bpy_text_filename_get(fn_dummy, text); + bpy_text_filename_get(fn_dummy, sizeof(fn_dummy), text); - if( !text->compiled ) { /* if it wasn't already compiled, do it now */ - char *buf = txt_to_buf( text ); + if(text->compiled == NULL) { /* if it wasn't already compiled, do it now */ + char *buf= txt_to_buf(text); - text->compiled = - Py_CompileString( buf, fn_dummy, Py_file_input ); + text->compiled= Py_CompileString(buf, fn_dummy, Py_file_input); - MEM_freeN( buf ); + MEM_freeN(buf); if(PyErr_Occurred()) { - python_script_error_jump_text(text); + if(do_jump) { + python_script_error_jump_text(text); + } BPY_text_free_code(text); } } if(text->compiled) { - py_dict = PyC_DefaultNameSpace(fn_dummy); - py_result = PyEval_EvalCode(text->compiled, py_dict, py_dict); + py_dict= PyC_DefaultNameSpace(fn_dummy); + py_result= PyEval_EvalCode(text->compiled, py_dict, py_dict); } } @@ -397,7 +390,7 @@ static int python_script_exec(bContext *C, const char *fn, struct Text *text, st FILE *fp= fopen(fn, "r"); if(fp) { - py_dict = PyC_DefaultNameSpace(fn); + py_dict= PyC_DefaultNameSpace(fn); #ifdef _WIN32 /* Previously we used PyRun_File to run directly the code on a FILE @@ -413,11 +406,11 @@ static int python_script_exec(bContext *C, const char *fn, struct Text *text, st pystring= MEM_mallocN(strlen(fn) + 32, "pystring"); pystring[0]= '\0'; sprintf(pystring, "exec(open(r'%s').read())", fn); - py_result = PyRun_String( pystring, Py_file_input, py_dict, py_dict ); + py_result= PyRun_String(pystring, Py_file_input, py_dict, py_dict); MEM_freeN(pystring); } #else - py_result = PyRun_File(fp, fn, Py_file_input, py_dict, py_dict); + py_result= PyRun_File(fp, fn, Py_file_input, py_dict, py_dict); fclose(fp); #endif } @@ -429,17 +422,20 @@ static int python_script_exec(bContext *C, const char *fn, struct Text *text, st if (!py_result) { if(text) { - python_script_error_jump_text(text); + if(do_jump) { + python_script_error_jump_text(text); + } } BPy_errors_to_report(reports); - } else { - Py_DECREF( py_result ); + } + else { + Py_DECREF(py_result); } if(py_dict) { #ifdef PYMODULE_CLEAR_WORKAROUND PyModuleObject *mmod= (PyModuleObject *)PyDict_GetItemString(PyThreadState_GET()->interp->modules, "__main__"); - PyObject *dict_back = mmod->md_dict; + PyObject *dict_back= mmod->md_dict; /* freeing the module will clear the namespace, * gives problems running classes defined in this namespace being used later. */ mmod->md_dict= NULL; @@ -459,18 +455,18 @@ static int python_script_exec(bContext *C, const char *fn, struct Text *text, st /* Can run a file or text block */ int BPY_filepath_exec(bContext *C, const char *filepath, struct ReportList *reports) { - return python_script_exec(C, filepath, NULL, reports); + return python_script_exec(C, filepath, NULL, reports, FALSE); } -int BPY_text_exec(bContext *C, struct Text *text, struct ReportList *reports) +int BPY_text_exec(bContext *C, struct Text *text, struct ReportList *reports, const short do_jump) { - return python_script_exec(C, NULL, text, reports); + return python_script_exec(C, NULL, text, reports, do_jump); } void BPY_DECREF(void *pyob_ptr) { - PyGILState_STATE gilstate = PyGILState_Ensure(); + PyGILState_STATE gilstate= PyGILState_Ensure(); Py_DECREF((PyObject *)pyob_ptr); PyGILState_Release(gilstate); } @@ -479,7 +475,7 @@ int BPY_button_exec(bContext *C, const char *expr, double *value) { PyGILState_STATE gilstate; PyObject *py_dict, *mod, *retval; - int error_ret = 0; + int error_ret= 0; PyObject *main_mod= NULL; if (!value || !expr) return -1; @@ -495,7 +491,7 @@ int BPY_button_exec(bContext *C, const char *expr, double *value) py_dict= PyC_DefaultNameSpace("<blender button>"); - mod = PyImport_ImportModule("math"); + mod= PyImport_ImportModule("math"); if (mod) { PyDict_Merge(py_dict, PyModule_GetDict(mod), 0); /* 0 - dont overwrite existing values */ Py_DECREF(mod); @@ -505,7 +501,7 @@ int BPY_button_exec(bContext *C, const char *expr, double *value) PyErr_Clear(); } - retval = PyRun_String(expr, Py_eval_input, py_dict, py_dict); + retval= PyRun_String(expr, Py_eval_input, py_dict, py_dict); if (retval == NULL) { error_ret= -1; @@ -524,7 +520,7 @@ int BPY_button_exec(bContext *C, const char *expr, double *value) } } else { - val = PyFloat_AsDouble(retval); + val= PyFloat_AsDouble(retval); } Py_DECREF(retval); @@ -555,7 +551,8 @@ int BPY_string_exec(bContext *C, const char *expr) PyGILState_STATE gilstate; PyObject *main_mod= NULL; PyObject *py_dict, *retval; - int error_ret = 0; + int error_ret= 0; + Main *bmain_back; /* XXX, quick fix for release (Copy Settings crash), needs further investigation */ if (!expr) return -1; @@ -569,7 +566,12 @@ int BPY_string_exec(bContext *C, const char *expr) py_dict= PyC_DefaultNameSpace("<blender string>"); - retval = PyRun_String(expr, Py_eval_input, py_dict, py_dict); + bmain_back= bpy_import_main_get(); + bpy_import_main_set(CTX_data_main(C)); + + retval= PyRun_String(expr, Py_eval_input, py_dict, py_dict); + + bpy_import_main_set(bmain_back); if (retval == NULL) { error_ret= -1; @@ -650,7 +652,7 @@ int BPY_context_member_get(bContext *C, const char *member, bContextDataResult * else { int len= PySequence_Fast_GET_SIZE(seq_fast); int i; - for(i = 0; i < len; i++) { + for(i= 0; i < len; i++) { PyObject *list_item= PySequence_Fast_GET_ITEM(seq_fast, i); if(BPy_StructRNA_Check(list_item)) { @@ -674,11 +676,13 @@ int BPY_context_member_get(bContext *C, const char *member, bContextDataResult * } if(done==0) { - if (item) printf("Context '%s' not a valid type\n", member); - else printf("Context '%s' not found\n", member); + if (item) printf("PyContext '%s' not a valid type\n", member); + else printf("PyContext '%s' not found\n", member); } else { - printf("Context '%s' found\n", member); + if(G.f & G_DEBUG) { + printf("PyContext '%s' found\n", member); + } } return done; @@ -690,7 +694,7 @@ int BPY_context_member_get(bContext *C, const char *member, bContextDataResult * /* TODO, reloading the module isnt functional at the moment. */ extern int main_python(int argc, const char **argv); -static struct PyModuleDef bpy_proxy_def = { +static struct PyModuleDef bpy_proxy_def= { PyModuleDef_HEAD_INIT, "bpy", /* m_name */ NULL, /* m_doc */ @@ -713,13 +717,13 @@ void bpy_module_delay_init(PyObject *bpy_proxy) { const int argc= 1; const char *argv[2]; - - const char *filename_rel= PyModule_GetFilename(bpy_proxy); /* can be relative */ + PyObject *filename_obj= PyModule_GetFilenameObject(bpy_proxy); /* updating the module dict below will loose the reference to __file__ */ + const char *filename_rel= _PyUnicode_AsString(filename_obj); /* can be relative */ char filename_abs[1024]; BLI_strncpy(filename_abs, filename_rel, sizeof(filename_abs)); BLI_path_cwd(filename_abs); - + argv[0]= filename_abs; argv[1]= NULL; @@ -733,7 +737,7 @@ void bpy_module_delay_init(PyObject *bpy_proxy) static void dealloc_obj_dealloc(PyObject *self); -static PyTypeObject dealloc_obj_Type = {{{0}}}; +static PyTypeObject dealloc_obj_Type= {{{0}}}; /* use our own dealloc so we can free a property if we use one */ static void dealloc_obj_dealloc(PyObject *self) @@ -767,10 +771,10 @@ PyInit_bpy(void) dealloc_obj *dob; /* assign dummy type */ - dealloc_obj_Type.tp_name = "dealloc_obj"; - dealloc_obj_Type.tp_basicsize = sizeof(dealloc_obj); - dealloc_obj_Type.tp_dealloc = dealloc_obj_dealloc; - dealloc_obj_Type.tp_flags = Py_TPFLAGS_DEFAULT; + dealloc_obj_Type.tp_name= "dealloc_obj"; + dealloc_obj_Type.tp_basicsize= sizeof(dealloc_obj); + dealloc_obj_Type.tp_dealloc= dealloc_obj_dealloc; + dealloc_obj_Type.tp_flags= Py_TPFLAGS_DEFAULT; if(PyType_Ready(&dealloc_obj_Type) < 0) return NULL; |