Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
DispatchPtr.cxx
Go to the documentation of this file.
1// Bindings
2#include "cpyrt.h"
3
4using namespace cppjit;
5#define CPYRT_INTERNAL 1
6#include "cpyrt/DispatchPtr.h"
7#undef CPYRT_INTERNAL
8#include "CPPInstance.h"
9#include "CPPScope.h"
10
11//-----------------------------------------------------------------------------
13 PythonGILRAII python_gil_raii;
14 if (fPyHardRef) {
15 if (!borrowed)
16 Py_INCREF(fPyHardRef);
17 return fPyHardRef;
18 }
19 if (fPyWeakRef) {
21 if (disp) { // dispatcher object disappeared?
22 if (borrowed)
23 Py_DECREF(disp);
24 return disp;
25 }
26 }
27 return nullptr;
28}
29
30//-----------------------------------------------------------------------------
32 : fPyHardRef(nullptr) {
33 PythonGILRAII python_gil_raii;
34 if (strong) {
35 Py_INCREF(pyobj);
36 fPyHardRef = pyobj;
37 fPyWeakRef = nullptr;
38 } else {
39 fPyHardRef = nullptr;
40 fPyWeakRef = PyWeakref_NewRef(pyobj, nullptr);
41 }
42 ((CPPInstance*)pyobj)->SetDispatchPtr(this);
43}
44
45//-----------------------------------------------------------------------------
47 : fPyWeakRef(nullptr) {
48 PythonGILRAII python_gil_raii;
49 PyObject* pyobj = other.Get(false /* not borrowed */);
51 pyobj ? (PyObject*)((CPPInstance*)pyobj)->Copy(cppinst) : nullptr;
52 if (fPyHardRef)
53 ((CPPInstance*)fPyHardRef)->SetDispatchPtr(this);
54 Py_XDECREF(pyobj);
55}
56
57//-----------------------------------------------------------------------------
59 // if we're holding a hard reference, or holding weak reference while being
60 // part of a dispatcher intermediate, then this delete is from the C++ side,
61 // and Python is "notified" by nulling out the reference and an exception will
62 // be raised on continued access
63 PythonGILRAII python_gil_raii;
64 if (fPyWeakRef) {
65 PyObject* pyobj = cpyrt_GetWeakRef(fPyWeakRef);
66 if (pyobj && ((CPPScope*)Py_TYPE(pyobj))->fFlags & CPPScope::kIsPython)
67 ((CPPInstance*)pyobj)->GetObjectRaw() = nullptr;
68 Py_XDECREF(pyobj);
69 Py_DECREF(fPyWeakRef);
70 } else if (fPyHardRef) {
71 ((CPPInstance*)fPyHardRef)->GetObjectRaw() = nullptr;
72 Py_DECREF(fPyHardRef);
73 }
74}
75
76//-----------------------------------------------------------------------------
78cppjit::cpyrt::DispatchPtr::assign(const DispatchPtr& other, void* cppinst) {
79 PythonGILRAII python_gil_raii;
80 if (this != &other) {
81 Py_XDECREF(fPyWeakRef);
82 fPyWeakRef = nullptr;
83 Py_XDECREF(fPyHardRef);
84 PyObject* pyobj = other.Get(false /* not borrowed */);
85 fPyHardRef =
86 pyobj ? (PyObject*)((CPPInstance*)pyobj)->Copy(cppinst) : nullptr;
87 if (fPyHardRef)
88 ((CPPInstance*)fPyHardRef)->SetDispatchPtr(this);
89 Py_XDECREF(pyobj);
90 }
91 return *this;
92}
93
94//-----------------------------------------------------------------------------
96 PythonGILRAII python_gil_raii;
97 // Python maintains the hardref, so only allowed a weakref here
98 if (fPyHardRef) {
99 fPyWeakRef = PyWeakref_NewRef(fPyHardRef, nullptr);
100 Py_DECREF(fPyHardRef);
101 fPyHardRef = nullptr;
102 }
103}
104
105//-----------------------------------------------------------------------------
107 PythonGILRAII python_gil_raii;
108 // C++ maintains the hardref, keeping the PyObject alive w/o outstanding ref
109 if (fPyWeakRef) {
110 fPyHardRef = cpyrt_GetWeakRef(fPyWeakRef);
111 Py_DECREF(fPyWeakRef);
112 fPyWeakRef = nullptr;
113 }
114}
uint32_t fFlags
_object PyObject
PyObject * Get(bool borrowed=true) const
DispatchPtr & assign(const DispatchPtr &other, void *cppinst)
static PyObject * cpyrt_GetWeakRef(PyObject *ref)
Definition cpyrt.h:113
Definition API.cxx:34