Skip to content

Commit f7d13b1

Browse files
committed
[TPython] Use functions and not macros for reference counting
This is recommended by the C Python documentation for runtime dynamic embedding of Python. This prevents linker errors when building TPython against GIL-less Python.
1 parent 6eeba6b commit f7d13b1

File tree

4 files changed

+54
-54
lines changed

4 files changed

+54
-54
lines changed

bindings/tpython/src/TPyArg.cxx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ void TPyArg::CallConstructor(PyObject *&pyself, PyObject *pyclass, const std::ve
4545
for (int i = 0; i < nArgs; ++i)
4646
PyTuple_SET_ITEM(pyargs, i, (PyObject *)args[i]);
4747
pyself = PyObject_Call(pyclass, pyargs, NULL);
48-
Py_DECREF(pyargs);
48+
Py_DecRef(pyargs);
4949
}
5050

5151
////////////////////////////////////////////////////////////////////////////////
@@ -55,7 +55,7 @@ void CallConstructor(PyObject *&pyself, PyObject *pyclass)
5555

5656
PyObject *pyargs = PyTuple_New(0);
5757
pyself = PyObject_Call(pyclass, pyargs, NULL);
58-
Py_DECREF(pyargs);
58+
Py_DecRef(pyargs);
5959
}
6060

6161
//- generic dispatcher -------------------------------------------------------
@@ -68,7 +68,7 @@ PyObject *TPyArg::CallMethod(PyObject *pymeth, const std::vector<TPyArg> &args)
6868
for (int i = 0; i < nArgs; ++i)
6969
PyTuple_SET_ITEM(pyargs, i, (PyObject *)args[i]);
7070
PyObject *result = PyObject_Call(pymeth, pyargs, NULL);
71-
Py_DECREF(pyargs);
71+
Py_DecRef(pyargs);
7272
return result;
7373
}
7474

@@ -77,15 +77,15 @@ void TPyArg::CallDestructor(PyObject *&pyself, PyObject *, const std::vector<TPy
7777
{
7878
PyGILRAII gilRaii;
7979

80-
Py_XDECREF(pyself); // calls actual dtor if ref-count down to 0
80+
Py_DecRef(pyself); // calls actual dtor if ref-count down to 0
8181
}
8282

8383
////////////////////////////////////////////////////////////////////////////////
8484
void TPyArg::CallDestructor(PyObject *&pyself)
8585
{
8686
PyGILRAII gilRaii;
8787

88-
Py_XDECREF(pyself);
88+
Py_DecRef(pyself);
8989
}
9090

9191
//- constructors/destructor --------------------------------------------------
@@ -94,7 +94,7 @@ TPyArg::TPyArg(PyObject *pyobject)
9494
PyGILRAII gilRaii;
9595

9696
// Construct a TPyArg from a python object.
97-
Py_XINCREF(pyobject);
97+
Py_IncRef(pyobject);
9898
fPyObject = pyobject;
9999
}
100100

@@ -145,7 +145,7 @@ TPyArg::TPyArg(const TPyArg &s)
145145
{
146146
PyGILRAII gilRaii;
147147

148-
Py_XINCREF(s.fPyObject);
148+
Py_IncRef(s.fPyObject);
149149
fPyObject = s.fPyObject;
150150
}
151151

@@ -157,7 +157,7 @@ TPyArg &TPyArg::operator=(const TPyArg &s)
157157
PyGILRAII gilRaii;
158158

159159
if (&s != this) {
160-
Py_XINCREF(s.fPyObject);
160+
Py_IncRef(s.fPyObject);
161161
fPyObject = s.fPyObject;
162162
}
163163
return *this;
@@ -170,7 +170,7 @@ TPyArg::~TPyArg()
170170
{
171171
PyGILRAII gilRaii;
172172

173-
Py_XDECREF(fPyObject);
173+
Py_DecRef(fPyObject);
174174
fPyObject = NULL;
175175
}
176176

@@ -180,6 +180,6 @@ TPyArg::operator PyObject *() const
180180
PyGILRAII gilRaii;
181181

182182
// Extract the python object.
183-
Py_XINCREF(fPyObject);
183+
Py_IncRef(fPyObject);
184184
return fPyObject;
185185
}

bindings/tpython/src/TPyClassGenerator.cxx

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ TClass *TPyClassGenerator::GetClass(const char *name, Bool_t load, Bool_t silent
5656
PyObject *pyname = PyUnicode_FromString(name);
5757
PyObject *keys = PyDict_Keys(modules);
5858
Bool_t isModule = PySequence_Contains(keys, pyname);
59-
Py_DECREF(keys);
60-
Py_DECREF(pyname);
59+
Py_DecRef(keys);
60+
Py_DecRef(pyname);
6161

6262
if (isModule) {
6363
// the normal TClass::GetClass mechanism doesn't allow direct returns, so
@@ -77,10 +77,10 @@ TClass *TPyClassGenerator::GetClass(const char *name, Bool_t load, Bool_t silent
7777

7878
for (int i = 0; i < PyList_GET_SIZE(keys); ++i) {
7979
PyObject *key = PyList_GET_ITEM(keys, i);
80-
Py_INCREF(key);
80+
Py_IncRef(key);
8181

8282
PyObject *attr = PyDict_GetItem(dct, key);
83-
Py_INCREF(attr);
83+
Py_IncRef(attr);
8484

8585
// TODO: refactor the code below with the class method code
8686
if (PyCallable_Check(attr) && !(PyType_Check(attr) || PyObject_HasAttr(attr, bases))) {
@@ -97,8 +97,8 @@ TClass *TPyClassGenerator::GetClass(const char *name, Bool_t load, Bool_t silent
9797
int nVars = var_names ? PyTuple_GET_SIZE(var_names) : 0 /* TODO: probably large number, all default? */;
9898
if (nVars < 0)
9999
nVars = 0;
100-
Py_XDECREF(var_names);
101-
Py_XDECREF(func_code);
100+
Py_DecRef(var_names);
101+
Py_DecRef(func_code);
102102

103103
nsCode << " TPyReturn " << func_name << "(";
104104
for (int ivar = 0; ivar < nVars; ++ivar) {
@@ -117,12 +117,12 @@ TClass *TPyClassGenerator::GetClass(const char *name, Bool_t load, Bool_t silent
117117
nsCode << " return TPyReturn(TPyArg::CallMethod((PyObject*)" << std::showbase << (uintptr_t)attr << ", v)); }\n";
118118
}
119119

120-
Py_DECREF(attr);
121-
Py_DECREF(key);
120+
Py_DecRef(attr);
121+
Py_DecRef(key);
122122
}
123123

124-
Py_DECREF(keys);
125-
Py_DECREF(bases);
124+
Py_DecRef(keys);
125+
Py_DecRef(bases);
126126

127127
nsCode << " }";
128128

@@ -161,10 +161,10 @@ TClass *TPyClassGenerator::GetClass(const char *name, Bool_t load, Bool_t silent
161161
return 0; // module apparently disappeared
162162
}
163163

164-
Py_INCREF(mod);
164+
Py_IncRef(mod);
165165
PyObject *pyclass = PyDict_GetItemString(PyModule_GetDict(mod), const_cast<char *>(clName.c_str()));
166-
Py_XINCREF(pyclass);
167-
Py_DECREF(mod);
166+
Py_IncRef(pyclass);
167+
Py_DecRef(mod);
168168

169169
if (!pyclass) {
170170
PyErr_Clear(); // the class is no longer available?!
@@ -175,7 +175,7 @@ TClass *TPyClassGenerator::GetClass(const char *name, Bool_t load, Bool_t silent
175175
PyObject *attrs = PyObject_Dir(pyclass);
176176
if (!attrs) {
177177
PyErr_Clear();
178-
Py_DECREF(pyclass);
178+
Py_DecRef(pyclass);
179179
return 0;
180180
}
181181

@@ -189,7 +189,7 @@ TClass *TPyClassGenerator::GetClass(const char *name, Bool_t load, Bool_t silent
189189
Bool_t hasConstructor = kFALSE, hasDestructor = kFALSE;
190190
for (int i = 0; i < PyList_GET_SIZE(attrs); ++i) {
191191
PyObject *label = PyList_GET_ITEM(attrs, i);
192-
Py_INCREF(label);
192+
Py_IncRef(label);
193193
PyObject *attr = PyObject_GetAttr(pyclass, label);
194194

195195
// collect only member functions (i.e. callable elements in __dict__)
@@ -221,10 +221,10 @@ TClass *TPyClassGenerator::GetClass(const char *name, Bool_t load, Bool_t silent
221221
var_names ? PyTuple_GET_SIZE(var_names) - 1 /* self */ : 0 /* TODO: probably large number, all default? */;
222222
if (nVars < 0)
223223
nVars = 0;
224-
Py_XDECREF(var_names);
225-
Py_XDECREF(func_code);
224+
Py_DecRef(var_names);
225+
Py_DecRef(func_code);
226226
#if PY_VERSION_HEX < 0x03000000
227-
Py_XDECREF(im_func);
227+
Py_DecRef(im_func);
228228
#endif
229229

230230
// method declaration as appropriate
@@ -258,7 +258,7 @@ TClass *TPyClassGenerator::GetClass(const char *name, Bool_t load, Bool_t silent
258258
}
259259

260260
// no decref of attr for now (b/c of hard-wired ptr); need cleanup somehow
261-
Py_DECREF(label);
261+
Py_DecRef(label);
262262
}
263263

264264
// special case if no constructor or destructor
@@ -278,9 +278,9 @@ TClass *TPyClassGenerator::GetClass(const char *name, Bool_t load, Bool_t silent
278278
if (useNS)
279279
proxyCode << " }";
280280

281-
Py_DECREF(attrs);
281+
Py_DecRef(attrs);
282282
// done with pyclass, decref here, assuming module is kept
283-
Py_DECREF(pyclass);
283+
Py_DecRef(pyclass);
284284

285285
// body compilation
286286
if (!gInterpreter->LoadText(proxyCode.str().c_str()))

bindings/tpython/src/TPyReturn.cxx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ TPyReturn::TPyReturn()
5757
PyGILRAII gilRaii;
5858

5959
// Construct a TPyReturn object from Py_None.
60-
Py_INCREF(Py_None);
60+
Py_IncRef(Py_None);
6161
fPyObject = Py_None;
6262
}
6363

@@ -70,7 +70,7 @@ TPyReturn::TPyReturn(PyObject *pyobject)
7070
PyGILRAII gilRaii;
7171

7272
if (!pyobject) {
73-
Py_INCREF(Py_None);
73+
Py_IncRef(Py_None);
7474
fPyObject = Py_None;
7575
} else
7676
fPyObject = pyobject; // steals reference
@@ -83,7 +83,7 @@ TPyReturn::TPyReturn(const TPyReturn &other)
8383
{
8484
PyGILRAII gilRaii;
8585

86-
Py_INCREF(other.fPyObject);
86+
Py_IncRef(other.fPyObject);
8787
fPyObject = other.fPyObject;
8888
}
8989

@@ -95,8 +95,8 @@ TPyReturn &TPyReturn::operator=(const TPyReturn &other)
9595
PyGILRAII gilRaii;
9696

9797
if (this != &other) {
98-
Py_INCREF(other.fPyObject);
99-
Py_DECREF(fPyObject);
98+
Py_IncRef(other.fPyObject);
99+
Py_DecRef(fPyObject);
100100
fPyObject = other.fPyObject;
101101
}
102102

@@ -110,7 +110,7 @@ TPyReturn::~TPyReturn()
110110
{
111111
PyGILRAII gilRaii;
112112

113-
Py_DECREF(fPyObject);
113+
Py_DecRef(fPyObject);
114114
}
115115

116116
//- public members -----------------------------------------------------------
@@ -224,6 +224,6 @@ TPyReturn::operator PyObject *() const
224224
if (fPyObject == Py_None)
225225
return 0;
226226

227-
Py_INCREF(fPyObject);
227+
Py_IncRef(fPyObject);
228228
return fPyObject;
229229
}

bindings/tpython/src/TPython.cxx

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class CachedPyString {
109109
CachedPyString &operator=(CachedPyString const &) = delete;
110110
CachedPyString &operator=(CachedPyString &&) = delete;
111111

112-
~CachedPyString() { Py_DECREF(fObj); }
112+
~CachedPyString() { Py_DecRef(fObj); }
113113

114114
PyObject *obj() { return fObj; }
115115

@@ -215,7 +215,7 @@ Bool_t TPython::Initialize()
215215

216216
// retrieve the main dictionary
217217
gMainDict = PyModule_GetDict(PyImport_AddModule(const_cast<char *>("__main__")));
218-
// The gMainDict is borrowed, i.e. we are not calling Py_INCREF(gMainDict).
218+
// The gMainDict is borrowed, i.e. we are not calling Py_IncRef(gMainDict).
219219
// Like this, we avoid unexpectedly affecting how long __main__ is kept
220220
// alive. The gMainDict is only used in Exec(), ExecScript(), and Eval(),
221221
// which should not be called after __main__ is garbage collected anyway.
@@ -261,7 +261,7 @@ Bool_t TPython::Import(const char *mod_name)
261261
PyObject *values = PyDict_Values(dct);
262262
for (int i = 0; i < PyList_GET_SIZE(values); ++i) {
263263
PyObject *value = PyList_GET_ITEM(values, i);
264-
Py_INCREF(value);
264+
Py_IncRef(value);
265265

266266
// collect classes
267267
if (PyType_Check(value) || PyObject_HasAttr(value, basesStr.obj())) {
@@ -282,15 +282,15 @@ Bool_t TPython::Import(const char *mod_name)
282282
// force class creation (this will eventually call TPyClassGenerator)
283283
TClass::GetClass(fullname.c_str(), kTRUE);
284284

285-
Py_XDECREF(pyClName);
285+
Py_DecRef(pyClName);
286286
}
287287

288-
Py_DECREF(value);
288+
Py_DecRef(value);
289289
}
290290

291-
Py_DECREF(values);
292-
Py_DECREF(mod);
293-
Py_DECREF(modNameObj);
291+
Py_DecRef(values);
292+
Py_DecRef(mod);
293+
Py_DecRef(modNameObj);
294294

295295
if (PyErr_Occurred())
296296
return kFALSE;
@@ -334,7 +334,7 @@ void TPython::LoadMacro(const char *name)
334334
// create Cling classes for all new python classes
335335
for (int i = 0; i < PyList_GET_SIZE(current); ++i) {
336336
PyObject *value = PyList_GET_ITEM(current, i);
337-
Py_INCREF(value);
337+
Py_IncRef(value);
338338

339339
if (!PySequence_Contains(old, value)) {
340340
// collect classes
@@ -359,16 +359,16 @@ void TPython::LoadMacro(const char *name)
359359
TClass::GetClass(fullname.c_str(), kTRUE);
360360
}
361361

362-
Py_XDECREF(pyClName);
363-
Py_XDECREF(pyModName);
362+
Py_DecRef(pyClName);
363+
Py_DecRef(pyModName);
364364
}
365365
}
366366

367-
Py_DECREF(value);
367+
Py_DecRef(value);
368368
}
369369

370-
Py_DECREF(current);
371-
Py_DECREF(old);
370+
Py_DecRef(current);
371+
Py_DecRef(old);
372372
}
373373

374374
////////////////////////////////////////////////////////////////////////////////
@@ -445,7 +445,7 @@ Bool_t TPython::Exec(const char *cmd, std::any *result, std::string const &resul
445445

446446
// test for error
447447
if (pyObjectResult) {
448-
Py_DECREF(pyObjectResult);
448+
Py_DecRef(pyObjectResult);
449449
return kTRUE;
450450
}
451451

@@ -471,7 +471,7 @@ Bool_t TPython::Bind(TObject *object, const char *label)
471471

472472
if (bound) {
473473
Bool_t bOk = PyDict_SetItemString(gMainDict, const_cast<char *>(label), bound) == 0;
474-
Py_DECREF(bound);
474+
Py_DecRef(bound);
475475

476476
return bOk;
477477
}

0 commit comments

Comments
 (0)