ROOT
v6-32
Reference Guide
Loading...
Searching...
No Matches
PyResult.cxx
Go to the documentation of this file.
1
// Bindings
2
#include "
CPyCppyy.h
"
3
#define CPYCPPYY_INTERNAL 1
4
#include "
CPyCppyy/PyResult.h
"
5
#undef CPYCPPYY_INTERNAL
6
7
#include "
CPPInstance.h
"
8
9
// Standard
10
#include <stdexcept>
11
12
13
//______________________________________________________________________________
14
// Python expression eval result
15
// =============================
16
//
17
// Transport class for bringing objects from python (dynamically typed) to Cling
18
// (statically typed). It is best to immediately cast a PyResult to the real
19
// type, either implicitly (for builtin types) or explicitly (through a void*
20
// cast for pointers to C++ objects).
21
22
23
//- constructors/destructor --------------------------------------------------
24
CPyCppyy::PyResult::PyResult
()
25
{
26
// Construct a PyResult object from Py_None.
27
Py_INCREF
(
Py_None
);
28
fPyObject
=
Py_None
;
29
}
30
31
//----------------------------------------------------------------------------
32
CPyCppyy::PyResult::PyResult
(
PyObject
*
pyobject
)
33
{
34
// Construct a PyResult from a python object. The python object may represent
35
// a C++ object. Steals reference to given python object.
36
if
(!
pyobject
) {
37
Py_INCREF
(
Py_None
);
38
fPyObject =
Py_None
;
39
}
else
40
fPyObject =
pyobject
;
// steals reference
41
}
42
43
//----------------------------------------------------------------------------
44
CPyCppyy::PyResult::PyResult
(
const
PyResult
&
other
)
45
{
46
// Copy constructor. Applies python object reference counting.
47
Py_INCREF
(
other
.fPyObject);
48
fPyObject =
other
.fPyObject;
49
}
50
51
//----------------------------------------------------------------------------
52
CPyCppyy::PyResult
&
CPyCppyy::PyResult::operator=
(
const
PyResult
&
other
)
53
{
54
// Assignment operator. Applies python object reference counting.
55
if
(
this
!= &
other
) {
56
Py_INCREF
(
other
.fPyObject);
57
Py_DECREF
(fPyObject);
58
fPyObject =
other
.fPyObject;
59
}
60
61
return
*
this
;
62
}
63
64
//----------------------------------------------------------------------------
65
CPyCppyy::PyResult::~PyResult
()
66
{
67
// Destructor. Reference counting for the held python object is in effect.
68
Py_DECREF
(fPyObject);
69
}
70
71
72
//- public members -----------------------------------------------------------
73
CPyCppyy::PyResult::operator
char
*()
const
74
{
75
// Cast python return value to C-style string (may fail).
76
return
(
char
*)((
const
char
*)*
this
);
77
}
78
79
//----------------------------------------------------------------------------
80
CPyCppyy::PyResult::operator
const
char
*()
const
81
{
82
// Cast python return value to C-style string (may fail).
83
if
(fPyObject ==
Py_None
)
// for void returns
84
return
nullptr
;
85
86
const
char
* s =
CPyCppyy_PyText_AsString
(fPyObject);
87
if
(
PyErr_Occurred
()) {
88
PyErr_Print
();
89
return
nullptr
;
90
}
91
92
return
s;
93
}
94
95
//----------------------------------------------------------------------------
96
CPyCppyy::PyResult::operator char()
const
97
{
98
// Cast python return value to C++ char (may fail).
99
std::string s =
operator
const
char
*();
100
if
(s.size())
101
return
s[0];
102
103
return
'\0'
;
104
}
105
106
//----------------------------------------------------------------------------
107
CPyCppyy::PyResult::operator long()
const
108
{
109
// Cast python return value to C++ long (may fail).
110
long
l
=
PyLong_AsLong
(fPyObject);
111
112
if
(
PyErr_Occurred
())
113
PyErr_Print
();
114
115
return
l
;
116
}
117
118
//----------------------------------------------------------------------------
119
CPyCppyy::PyResult::operator
unsigned
long()
const
120
{
121
// Cast python return value to C++ unsigned long (may fail).
122
unsigned
long
ul
=
PyLong_AsUnsignedLong
(fPyObject);
123
124
if
(
PyErr_Occurred
())
125
PyErr_Print
();
126
127
return
ul
;
128
}
129
130
//----------------------------------------------------------------------------
131
CPyCppyy::PyResult::operator
double
()
const
132
{
133
// Cast python return value to C++ double (may fail).
134
double
d
=
PyFloat_AsDouble
(fPyObject);
135
136
if
(
PyErr_Occurred
())
137
PyErr_Print
();
138
139
return
d
;
140
}
141
142
//----------------------------------------------------------------------------
143
CPyCppyy::PyResult::operator
void
*()
const
144
{
145
// Cast python return value to C++ object with dictionary (may fail; note that
146
// you have to use the void* converter, as CINT will not call any other).
147
if
(fPyObject ==
Py_None
)
148
return
nullptr
;
149
150
if
(
CPyCppyy::CPPInstance_Check
(fPyObject)) {
151
((
CPyCppyy::CPPInstance
*)fPyObject)->CppOwns();
152
return
((
CPyCppyy::CPPInstance
*)fPyObject)->GetObject();
153
}
else
154
return
fPyObject;
// borrows reference
155
}
156
157
//----------------------------------------------------------------------------
158
CPyCppyy::PyResult::operator
PyObject
*()
const
159
{
160
// Direct return of the held PyObject; note the new reference.
161
if
(fPyObject ==
Py_None
)
162
return
nullptr
;
163
164
Py_INCREF
(fPyObject);
165
return
fPyObject;
166
}
CPPInstance.h
CPyCppyy.h
CPyCppyy_PyText_AsString
#define CPyCppyy_PyText_AsString
Definition
CPyCppyy.h:76
PyObject
_object PyObject
Definition
PyMethodBase.h:43
PyResult.h
d
#define d(i)
Definition
RSha256.hxx:102
TRangeDynCast
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Definition
TCollection.h:358
CPyCppyy::CPPInstance
Definition
CPPInstance.h:26
CPyCppyy::PyResult
Definition
PyResult.h:22
CPyCppyy::PyResult::~PyResult
virtual ~PyResult()
Definition
PyResult.cxx:65
CPyCppyy::PyResult::fPyObject
PyObject * fPyObject
Definition
PyResult.h:60
CPyCppyy::PyResult::operator=
PyResult & operator=(const PyResult &)
Definition
PyResult.cxx:52
CPyCppyy::PyResult::PyResult
PyResult()
Definition
PyResult.cxx:24
ROOT::Detail::TRangeCast
Definition
TCollection.h:311
double
CPyCppyy::CPPInstance_Check
bool CPPInstance_Check(T *object)
Definition
CPPInstance.h:125
l
TLine l
Definition
textangle.C:4
bindings
pyroot
cppyy
CPyCppyy
src
PyResult.cxx
ROOT v6-32 - Reference Guide Generated on Mon Feb 17 2025 14:16:45 (GVA Time) using Doxygen 1.10.0