Panda3D
Loading...
Searching...
No Matches
pythonCallbackObject.cxx
Go to the documentation of this file.
1/**
2 * PANDA 3D SOFTWARE
3 * Copyright (c) Carnegie Mellon University. All rights reserved.
4 *
5 * All use of this software is subject to the terms of the revised BSD
6 * license. You should have received a copy of this license along
7 * with this source code in a file named "LICENSE."
8 *
9 * @file pythonCallbackObject.cxx
10 * @author drose
11 * @date 2009-03-13
12 */
13
15
16#ifdef HAVE_PYTHON
17
18#include "py_panda.h"
19#include "pythonThread.h"
20#include "callbackData.h"
21#include "config_putil.h"
22
23TypeHandle PythonCallbackObject::_type_handle;
24
25Configure(config_pythonCallbackObject);
26ConfigureFn(config_pythonCallbackObject) {
27 PythonCallbackObject::init_type();
28}
29
30#ifndef CPPPARSER
31extern struct Dtool_PyTypedObject Dtool_TypedObject;
32#endif
33
34/**
35 *
36 */
37PythonCallbackObject::
38PythonCallbackObject(PyObject *function) {
39 _function = Py_None;
40 Py_INCREF(_function);
41
42 set_function(function);
43
44#if !defined(SIMPLE_THREADS) && defined(WITH_THREAD)
45 // Ensure that the Python threading system is initialized and ready to go.
46
47#if PY_VERSION_HEX >= 0x03020000
48 Py_Initialize();
49#endif
50
51#if PY_VERSION_HEX < 0x03090000
52 // PyEval_InitThreads is now a deprecated no-op in Python 3.9+
53 PyEval_InitThreads();
54#endif // PY_VERSION_HEX
55#endif // WITH_THREAD
56}
57
58/**
59 *
60 */
61PythonCallbackObject::
62~PythonCallbackObject() {
63 Py_DECREF(_function);
64}
65
66/**
67 * Replaces the function that is called for the callback. runs. The
68 * parameter should be a Python callable object.
69 */
70void PythonCallbackObject::
71set_function(PyObject *function) {
72 Py_DECREF(_function);
73 _function = function;
74 Py_INCREF(_function);
75 if (_function != Py_None && !PyCallable_Check(_function)) {
76 nassert_raise("Invalid function passed to PythonCallbackObject");
77 }
78}
79
80/**
81 * Returns the function that is called for the callback.
82 */
83PyObject *PythonCallbackObject::
84get_function() {
85 Py_INCREF(_function);
86 return _function;
87}
88
89/**
90 * This method called when the callback is triggered; it *replaces* the
91 * original function. To continue performing the original function, you must
92 * call cbdata->upcall() during the callback.
93 */
94void PythonCallbackObject::
95do_callback(CallbackData *cbdata) {
96#if defined(HAVE_THREADS) && !defined(SIMPLE_THREADS)
97 // Use PyGILState to protect this asynchronous call.
98 PyGILState_STATE gstate;
99 gstate = PyGILState_Ensure();
100#endif
101
102 do_python_callback(cbdata);
103
104#if defined(HAVE_THREADS) && !defined(SIMPLE_THREADS)
105 PyGILState_Release(gstate);
106#endif
107}
108
109/**
110 * The Python calls that implement do_callback(). This function is separate
111 * so we can acquire the Python interpretor lock while it runs.
112 */
113void PythonCallbackObject::
114do_python_callback(CallbackData *cbdata) {
115 nassertv(cbdata != nullptr);
116
117 // Wrap the cbdata up in a Python object, then put it in a tuple, for the
118 // argument list.
119 PyObject *pycbdata =
120 DTool_CreatePyInstanceTyped(cbdata, Dtool_TypedObject,
121 false, false, cbdata->get_type_index());
122 PyObject *args = Py_BuildValue("(O)", pycbdata);
123 Py_DECREF(pycbdata);
124
125 PyObject *result = PythonThread::call_python_func(_function, args);
126 Py_DECREF(args);
127
128 if (result == nullptr) {
129 if (PyErr_Occurred() != PyExc_SystemExit) {
130 util_cat.error()
131 << "Exception occurred in " << *this << "\n";
132 }
133 } else {
134 Py_DECREF(result);
135 }
136}
137
138#endif // HAVE_PYTHON
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is a generic data block that is passed along to a CallbackObject when a callback is made.
TypeHandle is the identifier used to differentiate C++ class types.
Definition typeHandle.h:81
int get_type_index() const
Returns the internal index number associated with this object's TypeHandle, a unique number for each ...
Definition typedObject.I:20
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.