Store the context for python in a static variable with assessor functions - BPy_GetContext/BPy_SetContext,

Still not happy with this in the long term but its less problematic then storing the context in pythons namespace which couldn't be set before importing modules.

This might fix a crash quite a few people have reported (but I cant reproduce).
This commit is contained in:
Campbell Barton 2009-05-25 13:48:44 +00:00
parent edd783db73
commit 6d156a1bab
6 changed files with 23 additions and 25 deletions

@ -90,9 +90,7 @@ static PyObject *CreateGlobalDictionary( bContext *C )
Py_DECREF(item);
// XXX - evil, need to access context
item = PyCObject_FromVoidPtr( C, NULL );
PyDict_SetItemString( dict, "__bpy_context__", item );
Py_DECREF(item);
BPy_SetContext(C);
// XXX - put somewhere more logical
{
@ -386,6 +384,8 @@ void BPY_run_ui_scripts(bContext *C, int reload)
PySys_SetObject("path", sys_path_new);
Py_DECREF(sys_path_new);
// XXX - evil, need to access context
BPy_SetContext(C);
while((de = readdir(dir)) != NULL) {
/* We could stat the file but easier just to let python

@ -126,7 +126,7 @@ static PyObject *pyop_base_call( PyObject * self, PyObject * args, PyObject * k
PointerRNA ptr;
// XXX Todo, work out a better solution for passing on context, could make a tuple from self and pack the name and Context into it...
bContext *C = (bContext *)PyCObject_AsVoidPtr(PyDict_GetItemString(PyEval_GetGlobals(), "__bpy_context__"));
bContext *C = BPy_GetContext();
char *opname = _PyUnicode_AsString(self);

@ -1968,10 +1968,7 @@ PyObject *pyrna_basetype_register(PyObject *self, PyObject *args)
}
/* get the context, so register callback can do necessary refreshes */
item= PyDict_GetItemString(PyEval_GetGlobals(), "__bpy_context__"); /* borrow ref */
if(item)
C= (bContext*)PyCObject_AsVoidPtr(item);
C= BPy_GetContext();
/* call the register callback */
BKE_reports_init(&reports, RPT_PRINT);
@ -2031,10 +2028,8 @@ PyObject *pyrna_basetype_unregister(PyObject *self, PyObject *args)
}
/* get the context, so register callback can do necessary refreshes */
item= PyDict_GetItemString(PyEval_GetGlobals(), "__bpy_context__"); /* borrow ref */
if(item)
C= (bContext*)PyCObject_AsVoidPtr(item);
C= BPy_GetContext();
/* call unregister */
unreg(C, py_srna->ptr.data);

@ -303,17 +303,9 @@ static PyObject *Method_registerKey( PyObject * self, PyObject * args )
Py_RETURN_NONE;
}
/* internal use only */
static bContext *get_py_context__internal(void)
{
PyObject *globals = PyEval_GetGlobals();
PyObject *val= PyDict_GetItemString(globals, "__bpy_context__"); /* borrow ref */
return PyCObject_AsVoidPtr(val);
}
static PyObject *Method_getRegonPtr( PyObject * self )
{
bContext *C= get_py_context__internal();
bContext *C= BPy_GetContext();
ARegion *ar = CTX_wm_region(C);
return PyCObject_FromVoidPtr(ar, NULL);
@ -321,7 +313,7 @@ static PyObject *Method_getRegonPtr( PyObject * self )
static PyObject *Method_getAreaPtr( PyObject * self )
{
bContext *C= get_py_context__internal();
bContext *C= BPy_GetContext();
ScrArea *area = CTX_wm_area(C);
return PyCObject_FromVoidPtr(area, NULL);
@ -329,7 +321,7 @@ static PyObject *Method_getAreaPtr( PyObject * self )
static PyObject *Method_getScreenPtr( PyObject * self )
{
bContext *C= get_py_context__internal();
bContext *C= BPy_GetContext();
bScreen *screen= CTX_wm_screen(C);
return PyCObject_FromVoidPtr(screen, NULL);
@ -337,7 +329,7 @@ static PyObject *Method_getScreenPtr( PyObject * self )
static PyObject *Method_getSpacePtr( PyObject * self )
{
bContext *C= get_py_context__internal();
bContext *C= BPy_GetContext();
SpaceLink *sl= CTX_wm_space_data(C);
return PyCObject_FromVoidPtr(sl, NULL);
@ -345,7 +337,7 @@ static PyObject *Method_getSpacePtr( PyObject * self )
static PyObject *Method_getWindowPtr( PyObject * self )
{
bContext *C= get_py_context__internal();
bContext *C= BPy_GetContext();
wmWindow *window= CTX_wm_window(C);
return PyCObject_FromVoidPtr(window, NULL);

@ -29,6 +29,13 @@
#include "MEM_guardedalloc.h"
#include "BKE_report.h"
#include "BKE_context.h"
bContext* __py_context = NULL;
bContext* BPy_GetContext(void) { return __py_context; };
void BPy_SetContext(bContext *C) { __py_context= C; };
PyObject *BPY_flag_to_list(struct BPY_flag_def *flagdef, int flag)
{
PyObject *list = PyList_New(0);

@ -74,4 +74,8 @@ char *BPy_enum_as_string(struct EnumPropertyItem *item);
/* error reporting */
int BPy_reports_to_error(struct ReportList *reports);
/* TODO - find a better solution! */
struct bContext *BPy_GetContext(void);
void BPy_SetContext(struct bContext *C);
#endif