PyAPI: free internal Python data using sys.exit(..)

Previously BPY_python_end wasn't called when scripts called sys.exit()
because BPY_python_end exited the Python interpreter.

Change this behavior to call BPY_python_end without exiting the Python
interpreter while freeing Blender/Python data.

While leaks in the context of sys.exit aren't especially important
it's generally preferable for sys.exit() to match Blender's code-paths
for exiting to avoid unexpected behavior.
This commit is contained in:
Campbell Barton
2023-09-19 15:38:55 +10:00
parent a8b6c4c826
commit e8df5cec83
5 changed files with 15 additions and 14 deletions

View File

@@ -23,7 +23,7 @@ extern "C" {
/** Call #BPY_context_set first. */
void BPY_python_start(struct bContext *C, int argc, const char **argv);
void BPY_python_end(void);
void BPY_python_end(bool do_python_exit);
void BPY_python_reset(struct bContext *C);
void BPY_python_use_system_env(void);
void BPY_python_backtrace(FILE *fp);

View File

@@ -535,7 +535,7 @@ void BPY_python_start(bContext *C, int argc, const char **argv)
#endif
}
void BPY_python_end()
void BPY_python_end(const bool do_python_exit)
{
PyGILState_STATE gilstate;
@@ -564,14 +564,16 @@ void BPY_python_end()
BPY_app_translations_end();
#ifndef WITH_PYTHON_MODULE
/* Without this we get recursive calls to #WM_exit. */
/* Without this we get recursive calls to #WM_exit_ex. */
BPY_atexit_unregister();
Py_Finalize();
if (do_python_exit) {
Py_Finalize();
}
(void)gilstate;
#else
PyGILState_Release(gilstate);
(void)do_python_exit;
#endif
#ifdef TIME_PY_RUN

View File

@@ -34,13 +34,13 @@ static PyObject *bpy_atexit(PyObject * /*self*/, PyObject * /*args*/, PyObject *
bContext *C = BPY_context_get();
/* As Python requested the exit, it handles shutting it's self down. */
const bool do_python = false;
const bool do_python_exit = false;
/* User actions such as saving the session, preferences, recent-files for e.g.
* should be skipped because an explicit call to exit is more likely to be used as part of
* automated processes shouldn't impact the users session in the future. */
const bool do_user_exit_actions = false;
WM_exit_ex(C, do_python, do_user_exit_actions);
WM_exit_ex(C, do_python_exit, do_user_exit_actions);
Py_RETURN_NONE;
}