From 6777c420dbb92523ca9c2acd1fa1f8abce98f88d Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Wed, 29 Jun 2022 09:55:47 +1000 Subject: Mantaflow: call MANTA::terminateMantaflow on exit terminateMantaflow was never called, this leak is more of a technicality since it's only called on exit. Also make Py_Initialize/Py_Finalize optional in Pd:setup/finalize as it caused Blender to crash, finalizing Python twice. Add a patch to extern/mantaflow to keep track of changes in Blender from up-stream. --- extern/mantaflow/README.blender | 3 +- extern/mantaflow/helper/pwrapper/registry.cpp | 15 +++-- extern/mantaflow/helper/pwrapper/registry.h | 7 ++- extern/mantaflow/patches/local_namespace.diff | 86 +++++++++++++++++++++++++++ 4 files changed, 104 insertions(+), 7 deletions(-) create mode 100644 extern/mantaflow/patches/local_namespace.diff (limited to 'extern') diff --git a/extern/mantaflow/README.blender b/extern/mantaflow/README.blender index bc1e2a164dc..b0b010d54f4 100644 --- a/extern/mantaflow/README.blender +++ b/extern/mantaflow/README.blender @@ -2,4 +2,5 @@ Project: Mantaflow URL: http://mantaflow.com/ License: Apache 2.0 Upstream version: 0.13 -Local modifications: None +Local modifications: +* ./patches/local_namespace.diff to support loading MANTA variables into an isolated __main__ name-space. diff --git a/extern/mantaflow/helper/pwrapper/registry.cpp b/extern/mantaflow/helper/pwrapper/registry.cpp index b20651f3baa..b4206a41dea 100644 --- a/extern/mantaflow/helper/pwrapper/registry.cpp +++ b/extern/mantaflow/helper/pwrapper/registry.cpp @@ -706,16 +706,23 @@ PyObject *WrapperRegistry::initModule() //****************************************************** // Register members and exposed functions -void setup(const std::string &filename, const std::vector &args, PyObject *name_space) +void setup(const bool python_lifecycle, + const std::string &filename, + const std::vector &args, + PyObject *name_space) { WrapperRegistry::instance().construct(filename, args); - Py_Initialize(); + if (python_lifecycle) { + Py_Initialize(); + } WrapperRegistry::instance().runPreInit(name_space); } -void finalize() +void finalize(const bool python_lifecycle) { - Py_Finalize(); + if (python_lifecycle) { + Py_Finalize(); + } WrapperRegistry::instance().cleanup(); } diff --git a/extern/mantaflow/helper/pwrapper/registry.h b/extern/mantaflow/helper/pwrapper/registry.h index aa63383515f..2273d0b9bb1 100644 --- a/extern/mantaflow/helper/pwrapper/registry.h +++ b/extern/mantaflow/helper/pwrapper/registry.h @@ -48,8 +48,11 @@ template struct Namify { namespace Pb { // internal registry access -void setup(const std::string &filename, const std::vector &args, PyObject *name_space); -void finalize(); +void setup(bool python_lifecycle, + const std::string &filename, + const std::vector &args, + PyObject *name_space); +void finalize(bool python_lifecycle); bool canConvert(PyObject *obj, const std::string &to); Manta::PbClass *objFromPy(PyObject *obj); Manta::PbClass *createPy(const std::string &classname, diff --git a/extern/mantaflow/patches/local_namespace.diff b/extern/mantaflow/patches/local_namespace.diff new file mode 100644 index 00000000000..41bc1696772 --- /dev/null +++ b/extern/mantaflow/patches/local_namespace.diff @@ -0,0 +1,86 @@ +diff --git a/extern/mantaflow/helper/pwrapper/registry.cpp b/extern/mantaflow/helper/pwrapper/registry.cpp +index 5196c0409f8..b4206a41dea 100644 +--- a/extern/mantaflow/helper/pwrapper/registry.cpp ++++ b/extern/mantaflow/helper/pwrapper/registry.cpp +@@ -115,7 +115,7 @@ class WrapperRegistry { + void construct(const std::string &scriptname, const vector &args); + void cleanup(); + void renameObjects(); +- void runPreInit(); ++ void runPreInit(PyObject *name_space); + PyObject *initModule(); + ClassData *lookup(const std::string &name); + bool canConvert(ClassData *from, ClassData *to); +@@ -505,7 +505,7 @@ void WrapperRegistry::addConstants(PyObject *module) + } + } + +-void WrapperRegistry::runPreInit() ++void WrapperRegistry::runPreInit(PyObject *name_space) + { + // add python directories to path + PyObject *sys_path = PySys_GetObject((char *)"path"); +@@ -518,7 +518,15 @@ void WrapperRegistry::runPreInit() + } + if (!mCode.empty()) { + mCode = "from manta import *\n" + mCode; +- PyRun_SimpleString(mCode.c_str()); ++ PyObject *return_value = PyRun_String(mCode.c_str(), Py_file_input, name_space, name_space); ++ if (return_value == nullptr) { ++ if (PyErr_Occurred()) { ++ PyErr_Print(); ++ } ++ } ++ else { ++ Py_DECREF(return_value); ++ } + } + } + +@@ -698,16 +706,23 @@ PyObject *WrapperRegistry::initModule() + //****************************************************** + // Register members and exposed functions + +-void setup(const std::string &filename, const std::vector &args) ++void setup(const bool python_lifecycle, ++ const std::string &filename, ++ const std::vector &args, ++ PyObject *name_space) + { + WrapperRegistry::instance().construct(filename, args); +- Py_Initialize(); +- WrapperRegistry::instance().runPreInit(); ++ if (python_lifecycle) { ++ Py_Initialize(); ++ } ++ WrapperRegistry::instance().runPreInit(name_space); + } + +-void finalize() ++void finalize(const bool python_lifecycle) + { +- Py_Finalize(); ++ if (python_lifecycle) { ++ Py_Finalize(); ++ } + WrapperRegistry::instance().cleanup(); + } + +diff --git a/extern/mantaflow/helper/pwrapper/registry.h b/extern/mantaflow/helper/pwrapper/registry.h +index d9d2bbb624b..2273d0b9bb1 100644 +--- a/extern/mantaflow/helper/pwrapper/registry.h ++++ b/extern/mantaflow/helper/pwrapper/registry.h +@@ -48,8 +48,11 @@ template struct Namify { + namespace Pb { + + // internal registry access +-void setup(const std::string &filename, const std::vector &args); +-void finalize(); ++void setup(bool python_lifecycle, ++ const std::string &filename, ++ const std::vector &args, ++ PyObject *name_space); ++void finalize(bool python_lifecycle); + bool canConvert(PyObject *obj, const std::string &to); + Manta::PbClass *objFromPy(PyObject *obj); + Manta::PbClass *createPy(const std::string &classname, -- cgit v1.2.3