From 04f619d8af6ddc7de541488f77713818e18a886e Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Sat, 14 Aug 2010 05:33:20 +0000 Subject: [PATCH] - PyLineSpit() - used to print the filename and line number for internal errors now works when executing class functions in a module. - replaced PySys_GetObject("modules") with PyImport_GetModuleDict() - use defaults for keymap import/export rather then setting the same value every time from the UI scripts. --- release/scripts/ui/space_userpref.py | 11 +++----- release/scripts/ui/space_userpref_keymap.py | 8 +++--- source/blender/python/generic/bgl.c | 2 +- source/blender/python/generic/blf_api.c | 2 +- .../python/generic/bpy_internal_import.c | 2 +- source/blender/python/generic/geometry.c | 2 +- source/blender/python/generic/mathutils.c | 2 +- source/blender/python/generic/noise.c | 2 +- source/blender/python/intern/bpy.c | 2 +- source/blender/python/intern/bpy_operator.c | 2 +- source/blender/python/intern/bpy_props.c | 2 +- source/blender/python/intern/bpy_util.c | 26 ++++++++++++++++--- source/blender/python/intern/bpy_util.h | 2 +- 13 files changed, 41 insertions(+), 24 deletions(-) diff --git a/release/scripts/ui/space_userpref.py b/release/scripts/ui/space_userpref.py index 049d22a44ed..5f9514c5885 100644 --- a/release/scripts/ui/space_userpref.py +++ b/release/scripts/ui/space_userpref.py @@ -91,15 +91,12 @@ class USERPREF_HT_header(bpy.types.Header): layout.operator_context = 'INVOKE_DEFAULT' if userpref.active_section == 'INPUT': - op = layout.operator("wm.keyconfig_export") - op.filepath = "keymap.py" - op = layout.operator("wm.keyconfig_import") - op.filepath = "keymap.py" + layout.operator("wm.keyconfig_export") + layout.operator("wm.keyconfig_import") elif userpref.active_section == 'ADDONS': - op = layout.operator("wm.addon_install") - op.filepath = "*.py" + layout.operator("wm.addon_install") elif userpref.active_section == 'THEMES': - op = layout.operator("ui.reset_default_theme") + layout.operator("ui.reset_default_theme") class USERPREF_PT_tabs(bpy.types.Panel): diff --git a/release/scripts/ui/space_userpref_keymap.py b/release/scripts/ui/space_userpref_keymap.py index c9bae47bd39..24d77dbed32 100644 --- a/release/scripts/ui/space_userpref_keymap.py +++ b/release/scripts/ui/space_userpref_keymap.py @@ -513,7 +513,7 @@ class WM_OT_keyconfig_import(bpy.types.Operator): bl_idname = "wm.keyconfig_import" bl_label = "Import Key Configuration..." - filepath = StringProperty(name="File Path", description="Filepath to write file to") + filepath = StringProperty(name="File Path", description="Filepath to write file to", default="keymap.py") filter_folder = BoolProperty(name="Filter folders", description="", default=True, options={'HIDDEN'}) filter_text = BoolProperty(name="Filter text", description="", default=True, options={'HIDDEN'}) filter_python = BoolProperty(name="Filter python", description="", default=True, options={'HIDDEN'}) @@ -522,7 +522,7 @@ class WM_OT_keyconfig_import(bpy.types.Operator): def execute(self, context): import shutil - if not self.properties.filepath: + if not self.properties.is_property_set("filepath"): raise Exception("Filepath not set") f = open(self.properties.filepath, "r") @@ -582,14 +582,14 @@ class WM_OT_keyconfig_export(bpy.types.Operator): bl_idname = "wm.keyconfig_export" bl_label = "Export Key Configuration..." - filepath = StringProperty(name="File Path", description="Filepath to write file to") + filepath = StringProperty(name="File Path", description="Filepath to write file to", default="keymap.py") filter_folder = BoolProperty(name="Filter folders", description="", default=True, options={'HIDDEN'}) filter_text = BoolProperty(name="Filter text", description="", default=True, options={'HIDDEN'}) filter_python = BoolProperty(name="Filter python", description="", default=True, options={'HIDDEN'}) kc_name = StringProperty(name="KeyConfig Name", description="Name to save the key config as") def execute(self, context): - if not self.properties.filepath: + if not self.properties.is_property_set("filepath"): raise Exception("Filepath not set") f = open(self.properties.filepath, "w") diff --git a/source/blender/python/generic/bgl.c b/source/blender/python/generic/bgl.c index bb0b3a43186..8ac2107f8d2 100644 --- a/source/blender/python/generic/bgl.c +++ b/source/blender/python/generic/bgl.c @@ -1117,7 +1117,7 @@ PyObject *BGL_Init(void) { PyObject *mod, *dict, *item; mod = PyModule_Create(&BGL_module_def); - PyDict_SetItemString(PySys_GetObject("modules"), BGL_module_def.m_name, mod); + PyDict_SetItemString(PyImport_GetModuleDict(), BGL_module_def.m_name, mod); dict= PyModule_GetDict(mod); if( PyType_Ready( &BGL_bufferType) < 0) diff --git a/source/blender/python/generic/blf_api.c b/source/blender/python/generic/blf_api.c index 9e4ce1f057d..762153b8349 100644 --- a/source/blender/python/generic/blf_api.c +++ b/source/blender/python/generic/blf_api.c @@ -394,7 +394,7 @@ PyObject *BLF_Init(void) PyObject *submodule; submodule = PyModule_Create(&BLF_module_def); - PyDict_SetItemString(PySys_GetObject("modules"), BLF_module_def.m_name, submodule); + PyDict_SetItemString(PyImport_GetModuleDict(), BLF_module_def.m_name, submodule); PyModule_AddIntConstant(submodule, "ROTATION", BLF_ROTATION); PyModule_AddIntConstant(submodule, "CLIPPING", BLF_CLIPPING); diff --git a/source/blender/python/generic/bpy_internal_import.c b/source/blender/python/generic/bpy_internal_import.c index 0bcecafd23c..1951e72567c 100644 --- a/source/blender/python/generic/bpy_internal_import.c +++ b/source/blender/python/generic/bpy_internal_import.c @@ -304,7 +304,7 @@ PyMethodDef bpy_reload_meth[] = { {"bpy_reload_meth", (PyCFunction)blender_reloa void bpy_text_clear_modules(int clear_all) { - PyObject *modules= PySys_GetObject("modules"); + PyObject *modules= PyImport_GetModuleDict(); char *fname; char *file_extension; diff --git a/source/blender/python/generic/geometry.c b/source/blender/python/generic/geometry.c index 1e8436ed5ae..0e98760314d 100644 --- a/source/blender/python/generic/geometry.c +++ b/source/blender/python/generic/geometry.c @@ -835,7 +835,7 @@ PyObject *Geometry_Init(void) PyObject *submodule; submodule = PyModule_Create(&M_Geometry_module_def); - PyDict_SetItemString(PySys_GetObject("modules"), M_Geometry_module_def.m_name, submodule); + PyDict_SetItemString(PyImport_GetModuleDict(), M_Geometry_module_def.m_name, submodule); return (submodule); } diff --git a/source/blender/python/generic/mathutils.c b/source/blender/python/generic/mathutils.c index ada5bac8c2a..a643e6621b2 100644 --- a/source/blender/python/generic/mathutils.c +++ b/source/blender/python/generic/mathutils.c @@ -258,7 +258,7 @@ PyObject *Mathutils_Init(void) return NULL; submodule = PyModule_Create(&M_Mathutils_module_def); - PyDict_SetItemString(PySys_GetObject("modules"), M_Mathutils_module_def.m_name, submodule); + PyDict_SetItemString(PyImport_GetModuleDict(), M_Mathutils_module_def.m_name, submodule); /* each type has its own new() function */ PyModule_AddObject( submodule, "Vector", (PyObject *)&vector_Type ); diff --git a/source/blender/python/generic/noise.c b/source/blender/python/generic/noise.c index b07950099a3..4a09cbb58d8 100644 --- a/source/blender/python/generic/noise.c +++ b/source/blender/python/generic/noise.c @@ -658,7 +658,7 @@ static struct PyModuleDef noise_module_def = { PyObject *Noise_Init(void) { PyObject *submodule = PyModule_Create(&noise_module_def); - PyDict_SetItemString(PySys_GetObject("modules"), noise_module_def.m_name, submodule); + PyDict_SetItemString(PyImport_GetModuleDict(), noise_module_def.m_name, submodule); /* use current time as seed for random number generator by default */ setRndSeed(0); diff --git a/source/blender/python/intern/bpy.c b/source/blender/python/intern/bpy.c index 7ca1b365c6f..c86ebc7f691 100644 --- a/source/blender/python/intern/bpy.c +++ b/source/blender/python/intern/bpy.c @@ -158,7 +158,7 @@ void BPy_init_modules( void ) mod = PyModule_New("_bpy"); /* add the module so we can import it */ - PyDict_SetItemString(PySys_GetObject("modules"), "_bpy", mod); + PyDict_SetItemString(PyImport_GetModuleDict(), "_bpy", mod); Py_DECREF(mod); /* run first, initializes rna types */ diff --git a/source/blender/python/intern/bpy_operator.c b/source/blender/python/intern/bpy_operator.c index d92044b027e..c6c34fbcaf5 100644 --- a/source/blender/python/intern/bpy_operator.c +++ b/source/blender/python/intern/bpy_operator.c @@ -259,7 +259,7 @@ PyObject *BPY_operator_module( void ) static PyMethodDef pyop_macro_def_meth ={"macro_define", (PyCFunction) PYOP_wrap_macro_define, METH_VARARGS, NULL}; PyObject *submodule = PyModule_New("_bpy.ops"); - PyDict_SetItemString(PySys_GetObject("modules"), "_bpy.ops", submodule); + PyDict_SetItemString(PyImport_GetModuleDict(), "_bpy.ops", submodule); PyModule_AddObject( submodule, "call", PyCFunction_New(&pyop_call_meth, NULL) ); PyModule_AddObject( submodule, "as_string",PyCFunction_New(&pyop_as_string_meth,NULL) ); diff --git a/source/blender/python/intern/bpy_props.c b/source/blender/python/intern/bpy_props.c index c278ad56ab9..9afe638908c 100644 --- a/source/blender/python/intern/bpy_props.c +++ b/source/blender/python/intern/bpy_props.c @@ -919,7 +919,7 @@ PyObject *BPY_rna_props( void ) { PyObject *submodule; submodule= PyModule_Create(&props_module); - PyDict_SetItemString(PySys_GetObject("modules"), props_module.m_name, submodule); + PyDict_SetItemString(PyImport_GetModuleDict(), props_module.m_name, submodule); /* INCREF since its its assumed that all these functions return the * module with a new ref like PyDict_New, since they are passed to diff --git a/source/blender/python/intern/bpy_util.c b/source/blender/python/intern/bpy_util.c index 38462d1b176..1d14ab67510 100644 --- a/source/blender/python/intern/bpy_util.c +++ b/source/blender/python/intern/bpy_util.c @@ -54,7 +54,7 @@ void PyObSpit(char *name, PyObject *var) { } void PyLineSpit(void) { - char *filename; + const char *filename; int lineno; PyErr_Clear(); @@ -63,7 +63,7 @@ void PyLineSpit(void) { fprintf(stderr, "%s:%d\n", filename, lineno); } -void BPY_getFileAndNum(char **filename, int *lineno) +void BPY_getFileAndNum(const char **filename, int *lineno) { PyObject *getframe, *frame; PyObject *f_lineno= NULL, *co_filename= NULL; @@ -83,6 +83,7 @@ void BPY_getFileAndNum(char **filename, int *lineno) return; } + /* when executing a script */ if (filename) { co_filename= PyObject_GetAttrStringArgs(frame, 1, "f_code", "co_filename"); if (co_filename==NULL) { @@ -95,6 +96,25 @@ void BPY_getFileAndNum(char **filename, int *lineno) Py_DECREF(co_filename); } + /* when executing a module */ + if(filename && *filename == NULL) { + /* try an alternative method to get the filename - module based + * references below are all borrowed (double checked) */ + PyObject *mod_name= PyDict_GetItemString(PyEval_GetGlobals(), "__name__"); + if(mod_name) { + PyObject *mod= PyDict_GetItem(PyImport_GetModuleDict(), mod_name); + if(mod) { + *filename= PyModule_GetFilename(mod); + } + + /* unlikely, fallback */ + if(*filename == NULL) { + *filename= _PyUnicode_AsString(mod_name); + } + } + } + + if (lineno) { f_lineno= PyObject_GetAttrString(frame, "f_lineno"); if (f_lineno==NULL) { @@ -330,7 +350,7 @@ int BPy_errors_to_report(ReportList *reports) PyObject *pystring_format= NULL; // workaround, see below char *cstring; - char *filename; + const char *filename; int lineno; if (!PyErr_Occurred()) diff --git a/source/blender/python/intern/bpy_util.h b/source/blender/python/intern/bpy_util.h index e7e7bb09419..cfe820b53b0 100644 --- a/source/blender/python/intern/bpy_util.h +++ b/source/blender/python/intern/bpy_util.h @@ -38,7 +38,7 @@ struct ReportList; void PyObSpit(char *name, PyObject *var); void PyLineSpit(void); -void BPY_getFileAndNum(char **filename, int *lineno); +void BPY_getFileAndNum(const char **filename, int *lineno); PyObject *BPY_exception_buffer(void);