diff options
author | Joerg Mueller <nexyon@gmail.com> | 2010-07-31 02:34:27 +0400 |
---|---|---|
committer | Joerg Mueller <nexyon@gmail.com> | 2010-07-31 02:34:27 +0400 |
commit | 61c9e46aad2cf679331341c7d38f10bec19203a9 (patch) | |
tree | 84f7231156c8c35cc8acd785fc48a96cd1d2d820 /source/blender/python/intern | |
parent | c59b930d135ce7527542da96d1984842cf5e42ff (diff) | |
parent | e4a16c8010f8bdf4bed4e2661929cd0858649d25 (diff) |
svn merge -r 30718:30912 https://svn.blender.org/svnroot/bf-blender/trunk/blender
Diffstat (limited to 'source/blender/python/intern')
-rw-r--r-- | source/blender/python/intern/bpy_interface.c | 62 | ||||
-rw-r--r-- | source/blender/python/intern/bpy_rna.c | 56 |
2 files changed, 88 insertions, 30 deletions
diff --git a/source/blender/python/intern/bpy_interface.c b/source/blender/python/intern/bpy_interface.c index bf91b0498ed..83f407bab67 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 */ @@ -148,11 +149,28 @@ void BPY_update_modules( void ) /***************************************************************************** * Description: This function creates a new Python dictionary object. *****************************************************************************/ -static PyObject *CreateGlobalDictionary( bContext *C, const char *filename ) +static PyObject *CreateGlobalDictionary(bContext *C, const char *filename, PyObject *main_ns_orig) { PyObject *item; - PyObject *dict = PyDict_New( ); - PyDict_SetItemString( dict, "__builtins__", PyEval_GetBuiltins( ) ); + PyObject *dict; + + + /* 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) + */ + dict= PyModule_GetDict(PyImport_AddModule("__main__")); + PyDict_Merge(main_ns_orig, dict, 1); + PyDict_Clear(dict); + Py_INCREF(dict); + + /* using builtins rather then PyEval_GetBuiltins() + * print's many less items when printing, the modules __dict__ + * this is how python works so better follow. */ + PyDict_SetItemString(dict, "__builtins__", PyImport_AddModule("builtins")); + item = PyUnicode_FromString( "__main__" ); PyDict_SetItemString( dict, "__name__", item ); @@ -168,6 +186,13 @@ static PyObject *CreateGlobalDictionary( bContext *C, const char *filename ) return dict; } +static void CreateGlobalDictionary_Restore(PyObject *main_ns_orig) +{ + PyObject *main_ns= PyModule_GetDict(PyImport_AddModule("__main__")); + PyDict_Clear(main_ns); + PyDict_Merge(main_ns, main_ns_orig, 1); +} + /* must be called before Py_Initialize */ void BPY_start_python_path(void) { @@ -204,10 +229,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); } } @@ -316,6 +346,7 @@ void BPY_end_python( void ) int BPY_run_python_script( bContext *C, const char *fn, struct Text *text, struct ReportList *reports) { PyObject *py_dict, *py_result= NULL; + PyObject *main_ns_orig= PyDict_New(); PyGILState_STATE gilstate; if (fn==NULL && text==NULL) { @@ -327,7 +358,7 @@ 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 = CreateGlobalDictionary(C, fn_dummy); + py_dict = CreateGlobalDictionary(C, fn_dummy, main_ns_orig); if( !text->compiled ) { /* if it wasn't already compiled, do it now */ char *buf = txt_to_buf( text ); @@ -348,7 +379,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 = CreateGlobalDictionary(C, fn, main_ns_orig); if(fp) { #ifdef _WIN32 @@ -382,7 +413,10 @@ int BPY_run_python_script( bContext *C, const char *fn, struct Text *text, struc } else { Py_DECREF( py_result ); } - + + CreateGlobalDictionary_Restore(main_ns_orig); + Py_DECREF(main_ns_orig); + Py_DECREF(py_dict); bpy_context_clear(C, &gilstate); @@ -533,6 +567,7 @@ int BPY_eval_button(bContext *C, const char *expr, double *value) { PyGILState_STATE gilstate; PyObject *dict, *mod, *retval; + PyObject *main_ns_orig= PyDict_New(); int error_ret = 0; if (!value || !expr) return -1; @@ -544,7 +579,7 @@ int BPY_eval_button(bContext *C, const char *expr, double *value) bpy_context_set(C, &gilstate); - dict= CreateGlobalDictionary(C, NULL); + dict= CreateGlobalDictionary(C, NULL, main_ns_orig); mod = PyImport_ImportModule("math"); if (mod) { @@ -594,6 +629,9 @@ int BPY_eval_button(bContext *C, const char *expr, double *value) BPy_errors_to_report(CTX_wm_reports(C)); } + CreateGlobalDictionary_Restore(main_ns_orig); + Py_DECREF(main_ns_orig); + Py_DECREF(dict); bpy_context_clear(C, &gilstate); @@ -604,6 +642,7 @@ int BPY_eval_string(bContext *C, const char *expr) { PyGILState_STATE gilstate; PyObject *dict, *retval; + PyObject *main_ns_orig= PyDict_New(); int error_ret = 0; if (!expr) return -1; @@ -614,7 +653,7 @@ int BPY_eval_string(bContext *C, const char *expr) bpy_context_set(C, &gilstate); - dict= CreateGlobalDictionary(C, NULL); + dict= CreateGlobalDictionary(C, NULL, main_ns_orig); retval = PyRun_String(expr, Py_eval_input, dict, dict); @@ -627,6 +666,9 @@ int BPY_eval_string(bContext *C, const char *expr) Py_DECREF(retval); } + CreateGlobalDictionary_Restore(main_ns_orig); + Py_DECREF(main_ns_orig); + Py_DECREF(dict); bpy_context_clear(C, &gilstate); diff --git a/source/blender/python/intern/bpy_rna.c b/source/blender/python/intern/bpy_rna.c index 69caee898a0..2a708c82697 100644 --- a/source/blender/python/intern/bpy_rna.c +++ b/source/blender/python/intern/bpy_rna.c @@ -3086,37 +3086,47 @@ static struct PyMethodDef pyrna_prop_collection_methods[] = { * todo - also accept useful args */ static PyObject * pyrna_struct_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { - BPy_StructRNA *base = NULL; + BPy_StructRNA *base; - if (!PyArg_ParseTuple(args, "O!:bpy_struct.__new__", &pyrna_struct_Type, &base)) + if (!PyArg_ParseTuple(args, "|O!:bpy_struct.__new__", &pyrna_struct_Type, &base)) return NULL; - - if (type == &pyrna_struct_Type) { - return pyrna_struct_CreatePyObject(&base->ptr); - } else { + + if (type == Py_TYPE(base)) { + Py_INCREF(base); + return (PyObject *)base; + } else if (PyType_IsSubtype(type, &pyrna_struct_Type)) { BPy_StructRNA *ret = (BPy_StructRNA *) type->tp_alloc(type, 0); ret->ptr = base->ptr; return (PyObject *)ret; } + else { + PyErr_Format(PyExc_TypeError, "bpy_struct.__new__(type): type '%.200s' is not a subtype of bpy_struct.", type->tp_name); + return NULL; + } } /* only needed for subtyping, so a new class gets a valid BPy_StructRNA * todo - also accept useful args */ static PyObject * pyrna_prop_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { - BPy_PropertyRNA *base = NULL; + BPy_PropertyRNA *base; - if (!PyArg_ParseTuple(args, "O!:Base BPy_PropertyRNA", &pyrna_prop_Type, &base)) + if (!PyArg_ParseTuple(args, "O!:bpy_prop.__new__", &pyrna_prop_Type, &base)) return NULL; - if (ELEM3(type, &pyrna_prop_Type, &pyrna_prop_array_Type, &pyrna_prop_collection_Type)) { - return pyrna_prop_CreatePyObject(&base->ptr, base->prop); - } else { + if (type == Py_TYPE(base)) { + Py_INCREF(base); + return (PyObject *)base; + } else if (PyType_IsSubtype(type, &pyrna_prop_Type)) { BPy_PropertyRNA *ret = (BPy_PropertyRNA *) type->tp_alloc(type, 0); ret->ptr = base->ptr; ret->prop = base->prop; return (PyObject *)ret; } + else { + PyErr_Format(PyExc_TypeError, "bpy_prop.__new__(type): type '%.200s' is not a subtype of bpy_prop.", type->tp_name); + return NULL; + } } PyObject *pyrna_param_to_py(PointerRNA *ptr, ParameterList *parms, PropertyRNA *prop, void *data) @@ -4235,18 +4245,18 @@ StructRNA *pyrna_struct_as_srna(PyObject *self, int parent, const char *error_pr } if(py_srna==NULL) { - PyErr_Format(PyExc_SystemError, "%.200s internal error, self of type '%.200s' had no bl_rna attribute, should never happen", error_prefix, Py_TYPE(self)->tp_name); + PyErr_Format(PyExc_SystemError, "%.200s, missing bl_rna attribute from '%.200s' instance (may not be registered)", error_prefix, Py_TYPE(self)->tp_name); return NULL; } if(!BPy_StructRNA_Check(py_srna)) { - PyErr_Format(PyExc_SystemError, "%.200s internal error, bl_rna was of type '%.200s', instead of %.200s instance", error_prefix, Py_TYPE(py_srna)->tp_name, pyrna_struct_Type.tp_name); + PyErr_Format(PyExc_SystemError, "%.200s, bl_rna attribute wrong type '%.200s' on '%.200s'' instance", error_prefix, Py_TYPE(py_srna)->tp_name, Py_TYPE(self)->tp_name); Py_DECREF(py_srna); return NULL; } if(py_srna->ptr.type != &RNA_Struct) { - PyErr_Format(PyExc_SystemError, "%.200s internal error, bl_rna was not a RNA_Struct type of rna struct", error_prefix); + PyErr_Format(PyExc_SystemError, "%.200s, bl_rna attribute not a RNA_Struct, on '%.200s'' instance", error_prefix, Py_TYPE(self)->tp_name); Py_DECREF(py_srna); return NULL; } @@ -4570,10 +4580,13 @@ static int bpy_class_call(PointerRNA *ptr, FunctionRNA *func, ParameterList *par else { args = PyTuple_New(1); PyTuple_SET_ITEM(args, 0, py_srna); - py_class_instance = PyObject_Call(py_class, args, NULL); + py_class_instance= PyObject_Call(py_class, args, NULL); Py_DECREF(args); - - if(py_class_instance_store) { + + if(py_class_instance == NULL) { + err= -1; /* so the error is not overridden below */ + } + else if(py_class_instance_store) { *py_class_instance_store = py_class_instance; Py_INCREF(py_class_instance); } @@ -4626,8 +4639,11 @@ static int bpy_class_call(PointerRNA *ptr, FunctionRNA *func, ParameterList *par } } else { - PyErr_Format(PyExc_RuntimeError, "could not create instance of %.200s to call callback function %.200s.", RNA_struct_identifier(ptr->type), RNA_function_identifier(func)); - err= -1; + /* the error may be alredy set if the class instance couldnt be created */ + if(err != -1) { + PyErr_Format(PyExc_RuntimeError, "could not create instance of %.200s to call callback function %.200s.", RNA_struct_identifier(ptr->type), RNA_function_identifier(func)); + err= -1; + } } if (ret == NULL) { /* covers py_class_instance failing too */ @@ -4787,7 +4803,7 @@ static PyObject *pyrna_basetype_register(PyObject *self, PyObject *py_class) reg= RNA_struct_register(srna); if(!reg) { - PyErr_SetString(PyExc_ValueError, "bpy.types.register(...): expected a Type subclassed from a registerable rna type (no register supported)."); + PyErr_Format(PyExc_ValueError, "bpy.types.register(...): expected a subclass of a registerable rna type (%.200s does not support registration).", RNA_struct_identifier(srna)); return NULL; } |