[Bf-blender-cvs] [ca7bd3f1c34] blender-v2.82-release: Fluid: Cleaned up functions that deal with Python objects (C-API)
Sebastián Barschkis
noreply at git.blender.org
Wed Jan 22 18:35:54 CET 2020
Commit: ca7bd3f1c341157e686038a3b159cda7e7083554
Author: Sebastián Barschkis
Date: Wed Jan 22 18:31:19 2020 +0100
Branches: blender-v2.82-release
https://developer.blender.org/rBca7bd3f1c341157e686038a3b159cda7e7083554
Fluid: Cleaned up functions that deal with Python objects (C-API)
This commit belongs to T72894. It's related to (my) previous commits on pointer exchanges (today + yesterday). It cleans up the functions by describing their usage in the comments, adds additional nullptr checks, and fixes the reference count responsibilities of newly created PyObjects.
===================================================================
M intern/mantaflow/intern/MANTA_main.cpp
===================================================================
diff --git a/intern/mantaflow/intern/MANTA_main.cpp b/intern/mantaflow/intern/MANTA_main.cpp
index 20233353698..024c562e64c 100644
--- a/intern/mantaflow/intern/MANTA_main.cpp
+++ b/intern/mantaflow/intern/MANTA_main.cpp
@@ -2008,8 +2008,13 @@ void MANTA::exportLiquidScript(FluidModifierData *mmd)
myfile.close();
}
-/* Call Mantaflow python functions through this function. Use isAttribute for object attributes,
- * e.g. s.cfl (here 's' is varname, 'cfl' functionName, and isAttribute true) */
+/* Call Mantaflow Python functions through this function. Use isAttribute for object attributes,
+ * e.g. s.cfl (here 's' is varname, 'cfl' functionName, and isAttribute true) or
+ * grid.getDataPointer (here 's' is varname, 'getDataPointer' functionName, and isAttribute
+ * false)
+ *
+ * Important! Return value: New reference or nullptr
+ * Caller of this function needs to handle reference count of returned object. */
static PyObject *callPythonFunction(std::string varName,
std::string functionName,
bool isAttribute = false)
@@ -2051,8 +2056,13 @@ static PyObject *callPythonFunction(std::string varName,
return (!isAttribute) ? returnedValue : func;
}
+/* Argument of this function may be a nullptr.
+ * If it's not function will handle the reference count decrement of that argument. */
static void *pyObjectToPointer(PyObject *inputObject)
{
+ if (!inputObject)
+ return nullptr;
+
PyGILState_STATE gilstate = PyGILState_Ensure();
PyObject *encoded = PyUnicode_AsUTF8String(inputObject);
@@ -2066,21 +2076,43 @@ static void *pyObjectToPointer(PyObject *inputObject)
in >> dataPointer;
Py_DECREF(encoded);
- PyGILState_Release(gilstate);
+ PyGILState_Release(gilstate);
return dataPointer;
}
+/* Argument of this function may be a nullptr.
+ * If it's not function will handle the reference count decrement of that argument. */
static double pyObjectToDouble(PyObject *inputObject)
{
- // Cannot use PyFloat_AsDouble() since its error check crashes - likely because of Real (aka
- // float) type in Mantaflow
- return PyFloat_AS_DOUBLE(inputObject);
+ if (!inputObject)
+ return 0.0;
+
+ PyGILState_STATE gilstate = PyGILState_Ensure();
+
+ /* Cannot use PyFloat_AsDouble() since its error check crashes.
+ * Likely because of typedef 'Real' for 'float' types in Mantaflow. */
+ double result = PyFloat_AS_DOUBLE(inputObject);
+ Py_DECREF(inputObject);
+
+ PyGILState_Release(gilstate);
+ return result;
}
+/* Argument of this function may be a nullptr.
+ * If it's not function will handle the reference count decrement of that argument. */
static long pyObjectToLong(PyObject *inputObject)
{
- return PyLong_AsLong(inputObject);
+ if (!inputObject)
+ return 0;
+
+ PyGILState_STATE gilstate = PyGILState_Ensure();
+
+ long result = PyLong_AsLong(inputObject);
+ Py_DECREF(inputObject);
+
+ PyGILState_Release(gilstate);
+ return result;
}
int MANTA::getFrame()
@@ -2104,7 +2136,7 @@ float MANTA::getTimestep()
std::string id = std::to_string(mCurrentID);
std::string solver = "s" + id;
- return pyObjectToDouble(callPythonFunction(solver, func, true));
+ return (float)pyObjectToDouble(callPythonFunction(solver, func, true));
}
bool MANTA::needsRealloc(FluidModifierData *mmd)
More information about the Bf-blender-cvs
mailing list