Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TPyArg.cxx
Go to the documentation of this file.
1// Author: Enric Tejedor CERN 08/2019
2// Original PyROOT code by Wim Lavrijsen, LBL
3//
4// /*************************************************************************
5// * Copyright (C) 1995-2019, Rene Brun and Fons Rademakers. *
6// * All rights reserved. *
7// * *
8// * For the licensing terms see $ROOTSYS/LICENSE. *
9// * For the list of contributors see $ROOTSYS/README/CREDITS. *
10// *************************************************************************/
11
12// Bindings
13#include "Python.h"
14
15#include "TPyArg.h"
16
17//______________________________________________________________________________
18// Generic wrapper for arguments
19// =============================
20//
21// Transport class for bringing C++ values and objects from Cling to Python. It
22// provides, from the selected constructor, the proper conversion to a PyObject.
23// In principle, there should be no need to use this class directly: it relies
24// on implicit conversions.
25
26//- data ---------------------------------------------------------------------
28
29namespace {
30 class PyGILRAII {
31 PyGILState_STATE m_GILState;
32 public:
33 PyGILRAII() : m_GILState(PyGILState_Ensure()) { }
34 ~PyGILRAII() { PyGILState_Release(m_GILState); }
35 };
36}
37
38//- constructor dispatcher ---------------------------------------------------
39void TPyArg::CallConstructor(PyObject *&pyself, PyObject *pyclass, const std::vector<TPyArg> &args)
40{
41 PyGILRAII gilRaii;
42
43 int nArgs = args.size();
44 PyObject *pyargs = PyTuple_New(nArgs);
45 for (int i = 0; i < nArgs; ++i)
46 PyTuple_SET_ITEM(pyargs, i, (PyObject *)args[i]);
47 pyself = PyObject_Call(pyclass, pyargs, NULL);
48 Py_DECREF(pyargs);
49}
50
51////////////////////////////////////////////////////////////////////////////////
52void CallConstructor(PyObject *&pyself, PyObject *pyclass)
53{
54 PyGILRAII gilRaii;
55
56 PyObject *pyargs = PyTuple_New(0);
57 pyself = PyObject_Call(pyclass, pyargs, NULL);
58 Py_DECREF(pyargs);
59}
60
61//- generic dispatcher -------------------------------------------------------
62PyObject *TPyArg::CallMethod(PyObject *pymeth, const std::vector<TPyArg> &args)
63{
64 PyGILRAII gilRaii;
65
66 int nArgs = args.size();
67 PyObject *pyargs = PyTuple_New(nArgs);
68 for (int i = 0; i < nArgs; ++i)
69 PyTuple_SET_ITEM(pyargs, i, (PyObject *)args[i]);
70 PyObject *result = PyObject_Call(pymeth, pyargs, NULL);
71 Py_DECREF(pyargs);
72 return result;
73}
74
75//- denstructor dispatcher ----------------------------------------------------
76void TPyArg::CallDestructor(PyObject *&pyself, PyObject *, const std::vector<TPyArg> &)
77{
78 PyGILRAII gilRaii;
79
80 Py_XDECREF(pyself); // calls actual dtor if ref-count down to 0
81}
82
83////////////////////////////////////////////////////////////////////////////////
85{
86 PyGILRAII gilRaii;
87
88 Py_XDECREF(pyself);
89}
90
91//- constructors/destructor --------------------------------------------------
93{
94 PyGILRAII gilRaii;
95
96 // Construct a TPyArg from a python object.
97 Py_XINCREF(pyobject);
98 fPyObject = pyobject;
99}
100
101////////////////////////////////////////////////////////////////////////////////
102/// Construct a TPyArg from an integer value.
103
105{
106 PyGILRAII gilRaii;
107
108 fPyObject = PyLong_FromLong(value);
109}
110
111////////////////////////////////////////////////////////////////////////////////
112/// Construct a TPyArg from an integer value.
113
115{
116 PyGILRAII gilRaii;
117
118 fPyObject = PyLong_FromLong(value);
119}
120
121////////////////////////////////////////////////////////////////////////////////
122/// Construct a TPyArg from a double value.
123
125{
126 PyGILRAII gilRaii;
127
128 fPyObject = PyFloat_FromDouble(value);
129}
130
131////////////////////////////////////////////////////////////////////////////////
132/// Construct a TPyArg from a C-string.
133
135{
136 PyGILRAII gilRaii;
137
138 fPyObject = PyUnicode_FromString(value);
139}
140
141////////////////////////////////////////////////////////////////////////////////
142/// Copy constructor.
143
145{
146 PyGILRAII gilRaii;
147
148 Py_XINCREF(s.fPyObject);
150}
151
152////////////////////////////////////////////////////////////////////////////////
153/// Assignment operator.
154
156{
157 PyGILRAII gilRaii;
158
159 if (&s != this) {
160 Py_XINCREF(s.fPyObject);
162 }
163 return *this;
164}
165
166////////////////////////////////////////////////////////////////////////////////
167/// Done with held PyObject.
168
170{
171 PyGILRAII gilRaii;
172
173 Py_XDECREF(fPyObject);
174 fPyObject = NULL;
175}
176
177//- public members -----------------------------------------------------------
178TPyArg::operator PyObject *() const
179{
180 PyGILRAII gilRaii;
181
182 // Extract the python object.
183 Py_XINCREF(fPyObject);
184 return fPyObject;
185}
_object PyObject
long Long_t
Definition RtypesCore.h:54
#define ClassImp(name)
Definition Rtypes.h:382
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
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
static PyObject * CallMethod(PyObject *pymeth, const std::vector< TPyArg > &args)
Definition TPyArg.cxx:62
static void CallDestructor(PyObject *&pyself, PyObject *pymeth, const std::vector< TPyArg > &args)
Definition TPyArg.cxx:76
PyObject * fPyObject
Definition TPyArg.h:59
TPyArg(PyObject *)
Definition TPyArg.cxx:92
TPyArg & operator=(const TPyArg &)
Assignment operator.
Definition TPyArg.cxx:155
virtual ~TPyArg()
Done with held PyObject.
Definition TPyArg.cxx:169
static void CallConstructor(PyObject *&pyself, PyObject *pyclass, const std::vector< TPyArg > &args)
Definition TPyArg.cxx:39
_object PyObject
Definition TPyArg.h:14
void CallConstructor(PyObject *&pyself, PyObject *pyclass)
Definition TPyArg.cxx:31