ROOT
v6-32
Reference Guide
Loading...
Searching...
No Matches
DispatchPtr.cxx
Go to the documentation of this file.
1
// Bindings
2
#include "
CPyCppyy.h
"
3
#define CPYCPPYY_INTERNAL 1
4
#include "
CPyCppyy/DispatchPtr.h
"
5
#undef CPYCPPYY_INTERNAL
6
#include "
CPPInstance.h
"
7
#include "
CPPScope.h
"
8
9
10
//-----------------------------------------------------------------------------
11
PyObject
*
CPyCppyy::DispatchPtr::Get
()
const
12
{
13
if
(
fPyHardRef
)
return
fPyHardRef
;
14
if
(
fPyWeakRef
) {
15
PyObject
* disp = PyWeakref_GetObject(
fPyWeakRef
);
16
if
(disp != Py_None)
// dispatcher object disappeared?
17
return
disp;
18
}
19
return
nullptr
;
20
}
21
22
//-----------------------------------------------------------------------------
23
CPyCppyy::DispatchPtr::DispatchPtr
(
PyObject
* pyobj,
bool
strong) :
fPyHardRef
(nullptr)
24
{
25
if
(strong) {
26
Py_INCREF(pyobj);
27
fPyHardRef
= pyobj;
28
fPyWeakRef
=
nullptr
;
29
}
else
{
30
fPyHardRef
=
nullptr
;
31
fPyWeakRef
= PyWeakref_NewRef(pyobj,
nullptr
);
32
}
33
((
CPPInstance
*)pyobj)->SetDispatchPtr(
this
);
34
}
35
36
//-----------------------------------------------------------------------------
37
CPyCppyy::DispatchPtr::DispatchPtr
(
const
DispatchPtr
& other,
void
* cppinst) :
fPyWeakRef
(nullptr)
38
{
39
PyObject
* pyobj = other.
Get
();
40
fPyHardRef
= pyobj ? (
PyObject
*)((
CPPInstance
*)pyobj)->Copy(cppinst) :
nullptr
;
41
if
(
fPyHardRef
) ((
CPPInstance
*)
fPyHardRef
)->SetDispatchPtr(
this
);
42
}
43
44
//-----------------------------------------------------------------------------
45
CPyCppyy::DispatchPtr::~DispatchPtr
() {
46
// if we're holding a hard reference, or holding weak reference while being part
47
// of a dispatcher intermediate, then this delete is from the C++ side, and Python
48
// is "notified" by nulling out the reference and an exception will be raised on
49
// continued access
50
if
(
fPyWeakRef
) {
51
PyObject
* pyobj = PyWeakref_GetObject(
fPyWeakRef
);
52
if
(pyobj && pyobj != Py_None && ((
CPPScope
*)
Py_TYPE
(pyobj))->fFlags &
CPPScope::kIsPython
)
53
((
CPPInstance
*)pyobj)->GetObjectRaw() =
nullptr
;
54
Py_DECREF(
fPyWeakRef
);
55
}
else
if
(
fPyHardRef
) {
56
((
CPPInstance
*)
fPyHardRef
)->GetObjectRaw() =
nullptr
;
57
Py_DECREF(
fPyHardRef
);
58
}
59
}
60
61
//-----------------------------------------------------------------------------
62
CPyCppyy::DispatchPtr
&
CPyCppyy::DispatchPtr::assign
(
const
DispatchPtr
& other,
void
* cppinst)
63
{
64
if
(
this
!= &other) {
65
Py_XDECREF(
fPyWeakRef
);
fPyWeakRef
=
nullptr
;
66
Py_XDECREF(
fPyHardRef
);
67
PyObject
* pyobj = other.
Get
();
68
fPyHardRef
= pyobj ? (
PyObject
*)((
CPPInstance
*)pyobj)->Copy(cppinst) :
nullptr
;
69
if
(
fPyHardRef
) ((
CPPInstance
*)
fPyHardRef
)->SetDispatchPtr(
this
);
70
}
71
return
*
this
;
72
}
73
74
//-----------------------------------------------------------------------------
75
void
CPyCppyy::DispatchPtr::PythonOwns
()
76
{
77
// Python maintains the hardref, so only allowed a weakref here
78
if
(
fPyHardRef
) {
79
fPyWeakRef
= PyWeakref_NewRef(
fPyHardRef
,
nullptr
);
80
Py_DECREF(
fPyHardRef
);
fPyHardRef
=
nullptr
;
81
}
82
}
83
84
//-----------------------------------------------------------------------------
85
void
CPyCppyy::DispatchPtr::CppOwns
()
86
{
87
// C++ maintains the hardref, keeping the PyObject alive w/o outstanding ref
88
if
(
fPyWeakRef
) {
89
fPyHardRef
= PyWeakref_GetObject(
fPyWeakRef
);
90
if
(
fPyHardRef
== Py_None)
fPyHardRef
=
nullptr
;
91
Py_XINCREF(
fPyHardRef
);
92
Py_DECREF(
fPyWeakRef
);
fPyWeakRef
=
nullptr
;
93
}
94
}
CPPInstance.h
CPPScope.h
CPyCppyy.h
Py_TYPE
#define Py_TYPE(ob)
Definition
CPyCppyy.h:196
DispatchPtr.h
PyObject
_object PyObject
Definition
PyMethodBase.h:43
CPyCppyy::CPPInstance
Definition
CPPInstance.h:26
CPyCppyy::CPPScope
Definition
CPPScope.h:37
CPyCppyy::CPPScope::kIsPython
@ kIsPython
Definition
CPPScope.h:45
CPyCppyy::DispatchPtr
Definition
DispatchPtr.h:23
CPyCppyy::DispatchPtr::CppOwns
void CppOwns()
Definition
DispatchPtr.cxx:85
CPyCppyy::DispatchPtr::assign
DispatchPtr & assign(const DispatchPtr &other, void *cppinst)
Definition
DispatchPtr.cxx:62
CPyCppyy::DispatchPtr::DispatchPtr
DispatchPtr()
Definition
DispatchPtr.h:29
CPyCppyy::DispatchPtr::fPyWeakRef
PyObject * fPyWeakRef
Definition
DispatchPtr.h:71
CPyCppyy::DispatchPtr::Get
PyObject * Get() const
Definition
DispatchPtr.cxx:11
CPyCppyy::DispatchPtr::~DispatchPtr
~DispatchPtr()
Definition
DispatchPtr.cxx:45
CPyCppyy::DispatchPtr::fPyHardRef
PyObject * fPyHardRef
Definition
DispatchPtr.h:70
CPyCppyy::DispatchPtr::PythonOwns
void PythonOwns()
Definition
DispatchPtr.cxx:75
bindings
pyroot
cppyy
CPyCppyy
src
DispatchPtr.cxx
ROOT v6-32 - Reference Guide Generated on Tue May 19 2026 02:46:44 (GVA Time) using Doxygen 1.13.2