Cleanup: remove duplicate context variable (__py_context)

The context was stored both in __py_context & bpy_context_module.

This avoids duplicate functions to update them too.
This commit is contained in:
Campbell Barton 2020-10-15 18:12:03 +11:00
parent 1cc3abca70
commit 5531697f6d
11 changed files with 33 additions and 35 deletions

View File

@ -330,7 +330,9 @@ static void setup_app_data(bContext *C,
#ifdef WITH_PYTHON
/* let python know about new main */
BPY_context_update(C);
if (CTX_py_init_get(C)) {
BPY_context_update(C);
}
#endif
/* FIXME: this version patching should really be part of the file-reading code,

View File

@ -68,8 +68,7 @@ void BPY_thread_restore(BPy_ThreadStatePtr tstate);
(void)0
void BPY_text_free_code(struct Text *text);
void BPY_modules_update(
struct bContext *C); /* XXX - annoying, need this for pointers that get out of date */
void BPY_modules_update(void);
void BPY_modules_load_user(struct bContext *C);
void BPY_app_handlers_reset(const short do_all);

View File

@ -32,7 +32,7 @@ extern "C" {
#include <stdio.h>
/* bpy_interface.c */
void BPY_python_start(int argc, const char **argv);
void BPY_python_start(struct bContext *C, int argc, const char **argv);
void BPY_python_end(void);
void BPY_python_reset(struct bContext *C);
void BPY_python_use_system_env(void);

View File

@ -349,7 +349,7 @@ static PyObject *bpy_import_test(const char *modname)
/******************************************************************************
* Description: Creates the bpy module and adds it to sys.modules for importing
******************************************************************************/
void BPy_init_modules(void)
void BPy_init_modules(struct bContext *C)
{
PointerRNA ctx_ptr;
PyObject *mod;
@ -400,8 +400,7 @@ void BPy_init_modules(void)
PyModule_AddObject(mod, "_utils_previews", BPY_utils_previews_module());
PyModule_AddObject(mod, "msgbus", BPY_msgbus_module());
/* bpy context */
RNA_pointer_create(NULL, &RNA_Context, (void *)BPy_GetContext(), &ctx_ptr);
RNA_pointer_create(NULL, &RNA_Context, C, &ctx_ptr);
bpy_context_module = (BPy_StructRNA *)pyrna_struct_CreatePyObject(&ctx_ptr);
/* odd that this is needed, 1 ref on creation and another for the module
* but without we get a crash on exit */

View File

@ -24,7 +24,9 @@
extern "C" {
#endif
void BPy_init_modules(void);
struct bContext;
void BPy_init_modules(struct bContext *C);
extern PyObject *bpy_package_py;
/* bpy_interface_atexit.c */

View File

@ -38,16 +38,6 @@
#include "../generic/py_capi_utils.h"
static bContext *__py_context = NULL;
bContext *BPy_GetContext(void)
{
return __py_context;
}
void BPy_SetContext(bContext *C)
{
__py_context = C;
}
char *BPy_enum_as_string(const EnumPropertyItem *item)
{
DynStr *dynstr = BLI_dynstr_new();

View File

@ -48,9 +48,7 @@ bool BPy_errors_to_report_ex(struct ReportList *reports,
bool BPy_errors_to_report_brief_with_prefix(struct ReportList *reports, const char *error_prefix);
bool BPy_errors_to_report(struct ReportList *reports);
/* TODO - find a better solution! */
struct bContext *BPy_GetContext(void);
void BPy_SetContext(struct bContext *C);
struct bContext *BPY_context_get(void);
extern void bpy_context_set(struct bContext *C, PyGILState_STATE *gilstate);
extern void bpy_context_clear(struct bContext *C, const PyGILState_STATE *gilstate);

View File

@ -111,8 +111,8 @@ void BPY_context_update(bContext *C)
return;
}
BPy_SetContext(C);
BPY_modules_update(C); /* can give really bad results if this isn't here */
BPY_context_set(C);
BPY_modules_update(); /* can give really bad results if this isn't here */
}
void bpy_context_set(bContext *C, PyGILState_STATE *gilstate)
@ -155,7 +155,7 @@ void bpy_context_clear(bContext *UNUSED(C), const PyGILState_STATE *gilstate)
/* XXX - Calling classes currently wont store the context :\,
* cant set NULL because of this. but this is very flakey still. */
#if 0
BPy_SetContext(NULL);
BPY_context_set(NULL);
#endif
#ifdef TIME_PY_RUN
@ -224,7 +224,10 @@ void BPY_text_free_code(Text *text)
}
}
void BPY_modules_update(bContext *C)
/**
* Needed so the #Main pointer in `bpy.data` doesn't become out of date.
*/
void BPY_modules_update(void)
{
#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);
@ -234,14 +237,16 @@ void BPY_modules_update(bContext *C)
/* refreshes the main struct */
BPY_update_rna_module();
if (bpy_context_module) {
bpy_context_module->ptr.data = (void *)C;
}
}
bContext *BPy_GetContext(void)
{
return bpy_context_module->ptr.data;
}
void BPY_context_set(bContext *C)
{
BPy_SetContext(C);
bpy_context_module->ptr.data = (void *)C;
}
#ifdef WITH_FLUID
@ -295,7 +300,7 @@ static struct _inittab bpy_internal_modules[] = {
};
/* call BPY_context_set first */
void BPY_python_start(int argc, const char **argv)
void BPY_python_start(bContext *C, int argc, const char **argv)
{
#ifndef WITH_PYTHON_MODULE
PyThreadState *py_tstate = NULL;
@ -387,7 +392,7 @@ void BPY_python_start(int argc, const char **argv)
#endif
/* bpy.* and lets us import it */
BPy_init_modules();
BPy_init_modules(C);
pyrna_alloc_types();

View File

@ -339,7 +339,7 @@ static PyObject *pyop_call(PyObject *UNUSED(self), PyObject *args)
* is freed by clear_globals(), further access will crash blender.
* Setting context is not needed in this case, only calling because this
* function corrects bpy.data (internal Main pointer) */
BPY_modules_update(C);
BPY_modules_update();
/* return operator_ret as a bpy enum */
return pyrna_enum_bitfield_to_py(rna_enum_operator_return_items, operator_ret);

View File

@ -80,6 +80,11 @@
#define USE_MATHUTILS
#define USE_STRING_COERCE
/* Unfortunately Python needs to hold a global reference to the context.
* If we remove this is means `bpy.context` won't be usable from some parts of the code:
* `bpy.app.handler` callbacks for example.
* Even though this is arguably "correct", it's going to cause problems for existing scripts,
* so accept having this for the time being. */
BPy_StructRNA *bpy_context_module = NULL; /* for fast access */
static PyObject *pyrna_struct_Subtype(PointerRNA *ptr);

View File

@ -334,9 +334,7 @@ void WM_init(bContext *C, int argc, const char **argv)
* Will try fix when the crash can be repeated. - campbell. */
#ifdef WITH_PYTHON
BPY_context_set(C); /* necessary evil */
BPY_python_start(argc, argv);
BPY_python_start(C, argc, argv);
BPY_python_reset(C);
#else
(void)argc; /* unused */