Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TObjectPyz.cxx
Go to the documentation of this file.
1// Author: Enric Tejedor CERN 05/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 "CPyCppyy/API.h"
14
15#include "../../cppyy/CPyCppyy/src/CPyCppyy.h"
16#include "../../cppyy/CPyCppyy/src/CPPInstance.h"
17#include "../../cppyy/CPyCppyy/src/Utility.h"
18
19#include "PyROOTPythonize.h"
20
21// ROOT
22#include "TObject.h"
23
24namespace {
25
26// Convert generic python object into a boolean value
27PyObject *BoolNot(PyObject *value)
28{
29 if (PyObject_IsTrue(value) == 1) {
30 Py_DECREF(value);
32 } else {
33 Py_XDECREF(value);
35 }
36}
37
38// Call method with signature: obj->meth(arg1)
39PyObject *CallPyObjMethod(PyObject *obj, const char *meth, PyObject *arg1)
40{
41 return PyObject_CallMethod(obj, meth, "O", arg1);
42}
43
44} // namespace
45
46using namespace CPyCppyy;
47
48// Implement Python's __eq__ with TObject::IsEqual
50{
51 if (!CPyCppyy::Instance_Check(obj) || !((CPPInstance *)obj)->fObject)
52 return CPPInstance_Type.tp_richcompare(self, obj, Py_EQ);
53
54 return CallPyObjMethod(self, "IsEqual", obj);
55}
56
57// Implement Python's __ne__ with TObject::IsEqual
59{
60 if (!CPyCppyy::Instance_Check(obj) || !((CPPInstance *)obj)->fObject)
61 return CPPInstance_Type.tp_richcompare(self, obj, Py_NE);
62
63 return BoolNot(CallPyObjMethod(self, "IsEqual", obj));
64}
65
66////////////////////////////////////////////////////////////////////////////
67/// \brief Add pythonization for equality and inequality operators in
68/// TObject
69/// \param[in] self Always null, since this is a module function.
70/// \param[in] args Pointer to a Python tuple object containing the arguments
71/// received from Python.
72///
73/// The equality and inequality operators are better implemented in C++,
74/// since we need to need to rely on Cppyy's rich comparison if the object
75/// we are comparing ourselves with is not a Python proxy or if it contains
76/// a null pointer. For example, we need to support the comparison to None.
77///
78/// The rest of comparison operators (i.e. those that define order)
79/// can be implemented in Python, throwing a NotImplemented exception
80/// if we are not comparing two proxies to TObject or derivate.
82{
83 PyObject *pyclass = PyTuple_GetItem(args, 0);
84 Utility::AddToClass(pyclass, "__eq__", (PyCFunction)TObjectIsEqual, METH_O);
85 Utility::AddToClass(pyclass, "__ne__", (PyCFunction)TObjectIsNotEqual, METH_O);
87}
#define Py_RETURN_TRUE
Definition CPyCppyy.h:272
#define Py_RETURN_FALSE
Definition CPyCppyy.h:276
#define Py_RETURN_NONE
Definition CPyCppyy.h:268
_object PyObject
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
PyObject * TObjectIsEqual(PyObject *self, PyObject *obj)
PyObject * TObjectIsNotEqual(PyObject *self, PyObject *obj)
bool AddToClass(PyObject *pyclass, const char *label, PyCFunction cfunc, int flags=METH_VARARGS)
Definition Utility.cxx:182
PyTypeObject CPPInstance_Type
CPYCPPYY_EXTERN bool Instance_Check(PyObject *pyobject)
Definition API.cxx:166
PyObject * AddTObjectEqNePyz(PyObject *self, PyObject *args)
Add pythonization for equality and inequality operators in TObject.