1 /* GemRB - Infinity Engine Emulator
2 * Copyright (C) 2003 The GemRB Project
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "PythonHelpers.h"
23 static bool CallPython(PyObject
*Function
, PyObject
*args
= NULL
)
28 PyObject
*ret
= PyObject_CallObject(Function
, args
);
31 if (PyErr_Occurred()) {
40 static bool CallPython(PyObject
*Function
, int arg
)
45 PyObject
*args
= Py_BuildValue("(i)", arg
);
46 return CallPython(Function
, args
);
49 StringCallback::StringCallback(const char *str
)
50 : Name(PyString_FromString(const_cast<char*>(str
)))
54 StringCallback::StringCallback(PyObject
*Name
)
60 StringCallback::~StringCallback()
65 PyObject
*StringCallback::GetFunction()
67 if (!Name
|| !Py_IsInitialized()) {
70 /* Borrowed reference */
71 PyObject
*Function
= PyDict_GetItem(gs
->pDict
, Name
);
72 if (!Function
|| !PyCallable_Check(Function
)) {
73 printMessage("GUIScript", "Missing callback function:", LIGHT_RED
);
74 printf("%s\n", PyString_AsString(Name
));
80 bool StringCallback::call()
82 return CallPython(GetFunction());
85 bool StringCallback::call(int arg
)
87 return CallPython(GetFunction(), arg
);
90 PythonCallback::PythonCallback(PyObject
*Function
)
93 if (Function
&& PyCallable_Check(Function
)) {
100 PythonCallback::~PythonCallback()
102 if (Py_IsInitialized()) {
103 Py_XDECREF(Function
);
107 bool PythonCallback::call ()
109 if (!Function
|| !Py_IsInitialized()) {
112 return CallPython(Function
);
115 bool PythonCallback::call (int arg
)
117 if (!Function
|| !Py_IsInitialized()) {
120 return CallPython(Function
, arg
);