Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'source/gameengine/Expressions')
-rw-r--r--source/gameengine/Expressions/CMakeLists.txt2
-rw-r--r--source/gameengine/Expressions/KX_PythonCallBack.cpp119
-rw-r--r--source/gameengine/Expressions/KX_PythonCallBack.h40
-rw-r--r--source/gameengine/Expressions/PyObjectPlus.cpp15
-rw-r--r--source/gameengine/Expressions/PyObjectPlus.h13
5 files changed, 185 insertions, 4 deletions
diff --git a/source/gameengine/Expressions/CMakeLists.txt b/source/gameengine/Expressions/CMakeLists.txt
index 6907f314503..48c10d75a17 100644
--- a/source/gameengine/Expressions/CMakeLists.txt
+++ b/source/gameengine/Expressions/CMakeLists.txt
@@ -55,6 +55,7 @@ set(SRC
StringValue.cpp
Value.cpp
VectorValue.cpp
+ KX_PythonCallBack.cpp
BoolValue.h
ConstExpr.h
@@ -77,6 +78,7 @@ set(SRC
Value.h
VectorValue.h
VoidValue.h
+ KX_PythonCallBack.h
)
blender_add_lib(ge_logic_expressions "${SRC}" "${INC}" "${INC_SYS}")
diff --git a/source/gameengine/Expressions/KX_PythonCallBack.cpp b/source/gameengine/Expressions/KX_PythonCallBack.cpp
new file mode 100644
index 00000000000..fbc250a1b3d
--- /dev/null
+++ b/source/gameengine/Expressions/KX_PythonCallBack.cpp
@@ -0,0 +1,119 @@
+/*
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Contributor(s): Porteries Tristan.
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+/** \file gameengine/Expressions/KX_PythonCallBack.cpp
+ * \ingroup expressions
+ */
+
+#include "KX_PythonCallBack.h"
+#include <iostream>
+#include <stdarg.h>
+
+#include "BLI_alloca.h"
+
+/** Check if a python value is a function and have the correct number of arguments.
+ * \param value The python value to check.
+ * \param minargcount The minimum of arguments possible.
+ * \param maxargcount The maximum of arguments possible.
+ * \param r_argcount The number of argument of this function, this variable will be
+ * changed in the function.
+ */
+static PyObject *CheckPythonFunction(PyObject *value, unsigned int minargcount, unsigned int maxargcount, unsigned int &r_argcount)
+{
+ if (PyMethod_Check(value)) {
+ PyCodeObject *code = ((PyCodeObject *)PyFunction_GET_CODE(PyMethod_GET_FUNCTION(value)));
+ // *args support
+ r_argcount = (code->co_flags & CO_VARARGS) ? maxargcount : (code->co_argcount - 1);
+ }
+ else if (PyFunction_Check(value)) {
+ PyCodeObject *code = ((PyCodeObject *)PyFunction_GET_CODE(value));
+ // *args support
+ r_argcount = (code->co_flags & CO_VARARGS) ? maxargcount : code->co_argcount;
+ }
+ else { // is not a methode or a function
+ PyErr_Format(PyExc_TypeError, "items must be functions or methodes, not %s",
+ Py_TYPE(value)->tp_name);
+ return NULL;
+ }
+
+ if (r_argcount < minargcount || r_argcount > maxargcount) {
+ // wrong number of arguments
+ PyErr_Format(PyExc_TypeError, "methode or function (%s) has invalid number of arguments (%i) must be between %i and %i",
+ Py_TYPE(value)->tp_name, r_argcount, minargcount, maxargcount);
+ return NULL;
+ }
+
+ return value;
+}
+
+/** Create a python tuple to call a python function
+ * \param argcount The lenght of the tuple.
+ * \param arglist The fully list of python arguments [size >= argcount].
+ */
+static PyObject *CreatePythonTuple(unsigned int argcount, PyObject **arglist)
+{
+ PyObject *tuple = PyTuple_New(argcount);
+
+ for (unsigned int i = 0; i < argcount; ++i) {
+ PyObject *item = arglist[i];
+ // increment reference and copy it in a new tuple
+ Py_INCREF(item);
+ PyTuple_SET_ITEM(tuple, i, item);
+ }
+
+ return tuple;
+}
+
+void RunPythonCallBackList(PyObject *functionlist, PyObject **arglist, unsigned int minargcount, unsigned int maxargcount)
+{
+ unsigned int size = PyList_Size(functionlist);
+ PyObject **argTuples = (PyObject **)BLI_array_alloca(argTuples, maxargcount - minargcount + 1);
+ memset(argTuples, 0, sizeof(PyObject *) * (maxargcount - minargcount + 1));
+
+ for (unsigned int i = 0; i < size; ++i) {
+ unsigned int funcargcount = 0;
+
+ PyObject *item = PyList_GET_ITEM(functionlist, i);
+ PyObject *func = CheckPythonFunction(item, minargcount, maxargcount, funcargcount);
+ if (!func) { // this item fails the check
+ PyErr_Print();
+ PyErr_Clear();
+ continue;
+ }
+
+ // get correct argument tuple.
+ PyObject *tuple = argTuples[funcargcount - minargcount];
+ if (!tuple)
+ argTuples[funcargcount - minargcount] = tuple = CreatePythonTuple(funcargcount, arglist);
+
+ PyObject *ret = PyObject_Call(func, tuple, NULL);
+ if (!ret) { // if ret is NULL this seems that the function doesn't work !
+ PyErr_Print();
+ PyErr_Clear();
+ }
+ else
+ Py_DECREF(ret);
+ }
+
+ for (unsigned int i = 0; i <= (maxargcount - minargcount); ++i)
+ Py_XDECREF(argTuples[i]);
+}
diff --git a/source/gameengine/Expressions/KX_PythonCallBack.h b/source/gameengine/Expressions/KX_PythonCallBack.h
new file mode 100644
index 00000000000..2ff6e305d67
--- /dev/null
+++ b/source/gameengine/Expressions/KX_PythonCallBack.h
@@ -0,0 +1,40 @@
+/*
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Contributor(s): Porteries Tristan.
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+/** \file KX_PythonCallBack.h
+ * \ingroup expressions
+ */
+
+#ifndef __KX_PYTHON_CALLBACK_H__
+#define __KX_PYTHON_CALLBACK_H__
+
+#include "KX_Python.h"
+
+/** Execute each functions with at least one argument
+ * \param functionlist The python list which contains callbacks.
+ * \param arglist The first item in the tuple to execute callbacks (can be NULL for no arguments).
+ * \param minargcount The minimum of quantity of arguments possible.
+ * \param maxargcount The maximum of quantity of arguments possible.
+ */
+void RunPythonCallBackList(PyObject *functionlist, PyObject **arglist, unsigned int minargcount, unsigned int maxargcount);
+
+#endif // __KX_PYTHON_CALLBACK_H__
diff --git a/source/gameengine/Expressions/PyObjectPlus.cpp b/source/gameengine/Expressions/PyObjectPlus.cpp
index a65d61bc98b..31a17bd0cbf 100644
--- a/source/gameengine/Expressions/PyObjectPlus.cpp
+++ b/source/gameengine/Expressions/PyObjectPlus.cpp
@@ -148,12 +148,18 @@ PyObject *PyObjectPlus::py_base_repr(PyObject *self) // This should be the ent
PyObject *PyObjectPlus::py_base_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
PyTypeObject *base_type;
- PyObjectPlus_Proxy *base = NULL;
- if (!PyArg_ParseTuple(args, "O:Base PyObjectPlus", &base))
+ /* one or more args is needed */
+ if (!PyTuple_GET_SIZE(args)) {
+ PyErr_SetString(PyExc_TypeError,
+ "Expected at least one argument");
return NULL;
+ }
+
+ PyObjectPlus_Proxy *base = (PyObjectPlus_Proxy *)PyTuple_GET_ITEM(args, 0);
- /* the 'base' PyObject may be subclassed (multiple times even)
+ /**
+ * the 'base' PyObject may be subclassed (multiple times even)
* we need to find the first C++ defined class to check 'type'
* is a subclass of the base arguments type.
*
@@ -162,12 +168,13 @@ PyObject *PyObjectPlus::py_base_new(PyTypeObject *type, PyObject *args, PyObject
* eg.
*
* # CustomOb is called 'type' in this C code
+ * \code{.py}
* class CustomOb(GameTypes.KX_GameObject):
* pass
*
* # this calls py_base_new(...), the type of 'CustomOb' is checked to be a subclass of the 'cont.owner' type
* ob = CustomOb(cont.owner)
- *
+ * \endcode
* */
base_type= Py_TYPE(base);
while (base_type && !BGE_PROXY_CHECK_TYPE(base_type))
diff --git a/source/gameengine/Expressions/PyObjectPlus.h b/source/gameengine/Expressions/PyObjectPlus.h
index 34b814d7416..c3ce98ed27a 100644
--- a/source/gameengine/Expressions/PyObjectPlus.h
+++ b/source/gameengine/Expressions/PyObjectPlus.h
@@ -111,6 +111,8 @@ typedef struct PyObjectPlus_Proxy {
/* Opposite of BGE_PROXY_REF */
#define BGE_PROXY_FROM_REF(_self) (((PyObjectPlus *)_self)->GetProxy())
+/* Same as 'BGE_PROXY_REF' but doesn't incref. */
+#define BGE_PROXY_FROM_REF_BORROW(_self) _bge_proxy_from_ref_borrow((void *)_self)
// This must be the first line of each
@@ -631,6 +633,17 @@ public:
#ifdef WITH_PYTHON
PyObject *PyUnicode_From_STR_String(const STR_String& str);
+
+inline PyObject *_bge_proxy_from_ref_borrow(void *self_v)
+{
+ PyObject *self_proxy = BGE_PROXY_FROM_REF(self_v);
+ /* this is typically _very_ bad practice,
+ * however we know the proxy is owned by 'self_v' */
+ self_proxy->ob_refcnt--;
+ return self_proxy;
+}
+
#endif
+
#endif /* __PYOBJECTPLUS_H__ */