PyAPI: use public API's for module & builtin access
D6038 by @Dormouse
This commit is contained in:
@@ -31,12 +31,6 @@
|
|||||||
#include <Python.h>
|
#include <Python.h>
|
||||||
#include <frameobject.h>
|
#include <frameobject.h>
|
||||||
|
|
||||||
/* Needed for 'PyInterpreterState', we should remove this dependency. */
|
|
||||||
#if PY_VERSION_HEX >= 0x03080000
|
|
||||||
# define Py_BUILD_CORE
|
|
||||||
# include <internal/pycore_pystate.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include "BLI_utildefines.h" /* for bool */
|
#include "BLI_utildefines.h" /* for bool */
|
||||||
|
|
||||||
#include "py_capi_utils.h"
|
#include "py_capi_utils.h"
|
||||||
@@ -757,9 +751,10 @@ PyObject *PyC_UnicodeFromByte(const char *str)
|
|||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
PyObject *PyC_DefaultNameSpace(const char *filename)
|
PyObject *PyC_DefaultNameSpace(const char *filename)
|
||||||
{
|
{
|
||||||
PyInterpreterState *interp = PyThreadState_GET()->interp;
|
PyObject *modules = PyImport_GetModuleDict();
|
||||||
|
PyObject *builtins = PyEval_GetBuiltins();
|
||||||
PyObject *mod_main = PyModule_New("__main__");
|
PyObject *mod_main = PyModule_New("__main__");
|
||||||
PyDict_SetItemString(interp->modules, "__main__", mod_main);
|
PyDict_SetItemString(modules, "__main__", mod_main);
|
||||||
Py_DECREF(mod_main); /* sys.modules owns now */
|
Py_DECREF(mod_main); /* sys.modules owns now */
|
||||||
PyModule_AddStringConstant(mod_main, "__name__", "__main__");
|
PyModule_AddStringConstant(mod_main, "__name__", "__main__");
|
||||||
if (filename) {
|
if (filename) {
|
||||||
@@ -767,8 +762,8 @@ PyObject *PyC_DefaultNameSpace(const char *filename)
|
|||||||
* note: this wont map to a real file when executing text-blocks and buttons. */
|
* note: this wont map to a real file when executing text-blocks and buttons. */
|
||||||
PyModule_AddObject(mod_main, "__file__", PyC_UnicodeFromByte(filename));
|
PyModule_AddObject(mod_main, "__file__", PyC_UnicodeFromByte(filename));
|
||||||
}
|
}
|
||||||
PyModule_AddObject(mod_main, "__builtins__", interp->builtins);
|
PyModule_AddObject(mod_main, "__builtins__", builtins);
|
||||||
Py_INCREF(interp->builtins); /* AddObject steals a reference */
|
Py_INCREF(builtins); /* AddObject steals a reference */
|
||||||
return PyModule_GetDict(mod_main);
|
return PyModule_GetDict(mod_main);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -795,15 +790,15 @@ bool PyC_NameSpace_ImportArray(PyObject *py_dict, const char *imports[])
|
|||||||
/* restore MUST be called after this */
|
/* restore MUST be called after this */
|
||||||
void PyC_MainModule_Backup(PyObject **main_mod)
|
void PyC_MainModule_Backup(PyObject **main_mod)
|
||||||
{
|
{
|
||||||
PyInterpreterState *interp = PyThreadState_GET()->interp;
|
PyObject *modules = PyImport_GetModuleDict();
|
||||||
*main_mod = PyDict_GetItemString(interp->modules, "__main__");
|
*main_mod = PyDict_GetItemString(modules, "__main__");
|
||||||
Py_XINCREF(*main_mod); /* don't free */
|
Py_XINCREF(*main_mod); /* don't free */
|
||||||
}
|
}
|
||||||
|
|
||||||
void PyC_MainModule_Restore(PyObject *main_mod)
|
void PyC_MainModule_Restore(PyObject *main_mod)
|
||||||
{
|
{
|
||||||
PyInterpreterState *interp = PyThreadState_GET()->interp;
|
PyObject *modules = PyImport_GetModuleDict();
|
||||||
PyDict_SetItemString(interp->modules, "__main__", main_mod);
|
PyDict_SetItemString(modules, "__main__", main_mod);
|
||||||
Py_XDECREF(main_mod);
|
Py_XDECREF(main_mod);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user