ROOT
master
Reference Guide
Loading...
Searching...
No Matches
DispatchPtr.cxx
Go to the documentation of this file.
1
// Bindings
2
#include "
cpyrt.h
"
3
4
using 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
//-----------------------------------------------------------------------------
12
PyObject
*
cppjit::cpyrt::DispatchPtr::Get
(
bool
borrowed)
const
{
13
PythonGILRAII
python_gil_raii;
14
if
(
fPyHardRef
) {
15
if
(!borrowed)
16
Py_INCREF(
fPyHardRef
);
17
return
fPyHardRef
;
18
}
19
if
(
fPyWeakRef
) {
20
PyObject
* disp =
cpyrt_GetWeakRef
(
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
//-----------------------------------------------------------------------------
31
cppjit::cpyrt::DispatchPtr::DispatchPtr
(
PyObject
* pyobj,
bool
strong)
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
//-----------------------------------------------------------------------------
46
cppjit::cpyrt::DispatchPtr::DispatchPtr
(
const
DispatchPtr
& other,
void
* cppinst)
47
: fPyWeakRef(nullptr) {
48
PythonGILRAII
python_gil_raii;
49
PyObject
* pyobj = other.
Get
(
false
/* not borrowed */
);
50
fPyHardRef
=
51
pyobj ? (
PyObject
*)((
CPPInstance
*)pyobj)->Copy(cppinst) :
nullptr
;
52
if
(
fPyHardRef
)
53
((
CPPInstance
*)
fPyHardRef
)->SetDispatchPtr(
this
);
54
Py_XDECREF(pyobj);
55
}
56
57
//-----------------------------------------------------------------------------
58
cppjit::cpyrt::DispatchPtr::~DispatchPtr
() {
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
//-----------------------------------------------------------------------------
77
cppjit::cpyrt::DispatchPtr
&
78
cppjit::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
//-----------------------------------------------------------------------------
95
void
cppjit::cpyrt::DispatchPtr::PythonOwns
() {
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
//-----------------------------------------------------------------------------
106
void
cppjit::cpyrt::DispatchPtr::CppOwns
() {
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
}
CPPInstance.h
CPPScope.h
fFlags
uint32_t fFlags
Definition
DeclareExecutors.h:115
DispatchPtr.h
PyObject
_object PyObject
Definition
PyMethodBase.h:36
cppjit::cpyrt::CPPInstance
Definition
CPPInstance.h:28
cppjit::cpyrt::CPPScope
Definition
CPPScope.h:43
cppjit::cpyrt::CPPScope::kIsPython
@ kIsPython
Definition
CPPScope.h:51
cppjit::cpyrt::DispatchPtr
Definition
DispatchPtr.h:30
cppjit::cpyrt::DispatchPtr::CppOwns
void CppOwns()
Definition
DispatchPtr.cxx:106
cppjit::cpyrt::DispatchPtr::DispatchPtr
DispatchPtr()
Definition
DispatchPtr.h:36
cppjit::cpyrt::DispatchPtr::PythonOwns
void PythonOwns()
Definition
DispatchPtr.cxx:95
cppjit::cpyrt::DispatchPtr::~DispatchPtr
~DispatchPtr()
Definition
DispatchPtr.cxx:58
cppjit::cpyrt::DispatchPtr::fPyWeakRef
PyObject * fPyWeakRef
Definition
DispatchPtr.h:75
cppjit::cpyrt::DispatchPtr::fPyHardRef
PyObject * fPyHardRef
Definition
DispatchPtr.h:74
cppjit::cpyrt::DispatchPtr::Get
PyObject * Get(bool borrowed=true) const
Definition
DispatchPtr.cxx:12
cppjit::cpyrt::DispatchPtr::assign
DispatchPtr & assign(const DispatchPtr &other, void *cppinst)
Definition
DispatchPtr.cxx:78
cppjit::cpyrt::PythonGILRAII
Definition
DispatchPtr.h:22
cpyrt.h
cpyrt_GetWeakRef
static PyObject * cpyrt_GetWeakRef(PyObject *ref)
Definition
cpyrt.h:113
cppjit
Definition
API.cxx:34
bindings
pyroot
cppjit
src
cpyrt
DispatchPtr.cxx
ROOTmaster - Reference Guide Generated on Fri Oct 16 2026 04:42:57 (GVA Time) using Doxygen 1.10.0