ROOT
master
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
(
bool
borrowed
)
const
12
{
13
PyGILState_STATE state =
PyGILState_Ensure
();
14
PyObject
*
result
=
nullptr
;
15
if
(
fPyHardRef
) {
16
if
(!
borrowed
)
Py_INCREF
(
fPyHardRef
);
17
result
=
fPyHardRef
;
18
}
else
if
(
fPyWeakRef
) {
19
result
=
CPyCppyy_GetWeakRef
(
fPyWeakRef
);
20
if
(
result
) {
// dispatcher object disappeared?
21
if
(
borrowed
)
Py_DECREF
(
result
);
22
}
23
}
24
PyGILState_Release
(state);
25
return
result
;
26
}
27
28
//-----------------------------------------------------------------------------
29
CPyCppyy::DispatchPtr::DispatchPtr
(
PyObject
*
pyobj
,
bool
strong
) : fPyHardRef(nullptr)
30
{
31
PyGILState_STATE state =
PyGILState_Ensure
();
32
if
(
strong
) {
33
Py_INCREF
(
pyobj
);
34
fPyHardRef
=
pyobj
;
35
fPyWeakRef
=
nullptr
;
36
}
else
{
37
fPyHardRef
=
nullptr
;
38
fPyWeakRef
=
PyWeakref_NewRef
(
pyobj
,
nullptr
);
39
}
40
((
CPPInstance
*)
pyobj
)->SetDispatchPtr(
this
);
41
PyGILState_Release
(state);
42
}
43
44
//-----------------------------------------------------------------------------
45
CPyCppyy::DispatchPtr::DispatchPtr
(
const
DispatchPtr
&
other
,
void
*
cppinst
) : fPyWeakRef(nullptr)
46
{
47
PyGILState_STATE state =
PyGILState_Ensure
();
48
PyObject
*
pyobj
=
other
.Get(
false
/* not borrowed */
);
49
fPyHardRef
=
pyobj
? (
PyObject
*)((
CPPInstance
*)
pyobj
)->Copy(
cppinst
) :
nullptr
;
50
if
(
fPyHardRef
) ((
CPPInstance
*)
fPyHardRef
)->SetDispatchPtr(
this
);
51
Py_XDECREF
(
pyobj
);
52
PyGILState_Release
(state);
53
}
54
55
//-----------------------------------------------------------------------------
56
CPyCppyy::DispatchPtr::~DispatchPtr
() {
57
// if we're holding a hard reference, or holding weak reference while being part
58
// of a dispatcher intermediate, then this delete is from the C++ side, and Python
59
// is "notified" by nulling out the reference and an exception will be raised on
60
// continued access
61
PyGILState_STATE state =
PyGILState_Ensure
();
62
if
(fPyWeakRef) {
63
PyObject
*
pyobj
=
CPyCppyy_GetWeakRef
(fPyWeakRef);
64
if
(
pyobj
&& ((
CPPScope
*)
Py_TYPE
(
pyobj
))->
fFlags
&
CPPScope::kIsPython
)
65
((
CPPInstance
*)
pyobj
)->GetObjectRaw() =
nullptr
;
66
Py_XDECREF
(
pyobj
);
67
Py_DECREF
(fPyWeakRef);
68
}
else
if
(fPyHardRef) {
69
((
CPPInstance
*)fPyHardRef)->GetObjectRaw() =
nullptr
;
70
Py_DECREF
(fPyHardRef);
71
}
72
PyGILState_Release
(state);
73
}
74
75
//-----------------------------------------------------------------------------
76
CPyCppyy::DispatchPtr
&
CPyCppyy::DispatchPtr::assign
(
const
DispatchPtr
&
other
,
void
*
cppinst
)
77
{
78
PyGILState_STATE state =
PyGILState_Ensure
();
79
if
(
this
!= &
other
) {
80
Py_XDECREF
(fPyWeakRef); fPyWeakRef =
nullptr
;
81
Py_XDECREF
(fPyHardRef);
82
PyObject
*
pyobj
=
other
.Get(
false
/* not borrowed */
);
83
fPyHardRef =
pyobj
? (
PyObject
*)((
CPPInstance
*)
pyobj
)->Copy(
cppinst
) :
nullptr
;
84
if
(fPyHardRef) ((
CPPInstance
*)fPyHardRef)->SetDispatchPtr(
this
);
85
Py_XDECREF
(
pyobj
);
86
}
87
PyGILState_Release
(state);
88
return
*
this
;
89
}
90
91
//-----------------------------------------------------------------------------
92
void
CPyCppyy::DispatchPtr::PythonOwns
()
93
{
94
// Python maintains the hardref, so only allowed a weakref here
95
if
(fPyHardRef) {
96
fPyWeakRef =
PyWeakref_NewRef
(fPyHardRef,
nullptr
);
97
Py_DECREF
(fPyHardRef); fPyHardRef =
nullptr
;
98
}
99
}
100
101
//-----------------------------------------------------------------------------
102
void
CPyCppyy::DispatchPtr::CppOwns
()
103
{
104
// C++ maintains the hardref, keeping the PyObject alive w/o outstanding ref
105
PyGILState_STATE state =
PyGILState_Ensure
();
106
if
(fPyWeakRef) {
107
fPyHardRef =
CPyCppyy_GetWeakRef
(fPyWeakRef);
108
Py_DECREF
(fPyWeakRef); fPyWeakRef =
nullptr
;
109
}
110
PyGILState_Release
(state);
111
}
CPPInstance.h
CPPScope.h
CPyCppyy.h
CPyCppyy_GetWeakRef
static PyObject * CPyCppyy_GetWeakRef(PyObject *ref)
Definition
CPyCppyy.h:356
Py_TYPE
#define Py_TYPE(ob)
Definition
CPyCppyy.h:196
DispatchPtr.h
PyObject
_object PyObject
Definition
PyMethodBase.h:36
fFlags
std::ios_base::fmtflags fFlags
Definition
RDFHelpers.cxx:218
TRangeDynCast
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Definition
TCollection.h:358
result
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
Definition
TGWin32VirtualXProxy.cxx:174
CPyCppyy::CPPInstance
Definition
CPPInstance.h:27
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:102
CPyCppyy::DispatchPtr::assign
DispatchPtr & assign(const DispatchPtr &other, void *cppinst)
Definition
DispatchPtr.cxx:76
CPyCppyy::DispatchPtr::DispatchPtr
DispatchPtr()
Definition
DispatchPtr.h:29
CPyCppyy::DispatchPtr::fPyWeakRef
PyObject * fPyWeakRef
Definition
DispatchPtr.h:71
CPyCppyy::DispatchPtr::Get
PyObject * Get(bool borrowed=true) const
Definition
DispatchPtr.cxx:11
CPyCppyy::DispatchPtr::~DispatchPtr
~DispatchPtr()
Definition
DispatchPtr.cxx:56
CPyCppyy::DispatchPtr::fPyHardRef
PyObject * fPyHardRef
Definition
DispatchPtr.h:70
CPyCppyy::DispatchPtr::PythonOwns
void PythonOwns()
Definition
DispatchPtr.cxx:92
ROOT::Detail::TRangeCast
Definition
TCollection.h:311
bindings
pyroot
cppyy
CPyCppyy
src
DispatchPtr.cxx
ROOT master - Reference Guide Generated on Mon Sep 1 2025 04:34:50 (GVA Time) using Doxygen 1.10.0