Logo ROOT  
Reference Guide
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.h"
14#include "CPPInstance.h"
15#include "Utility.h"
16#include "PyROOTPythonize.h"
17#include "PyzCppHelpers.hxx"
18
19// ROOT
20#include "TObject.h"
21
22using namespace CPyCppyy;
23
24// Implement Python's __eq__ with TObject::IsEqual
26{
27 if (!CPPInstance_Check(obj) || !((CPPInstance *)obj)->fObject)
28 return CPPInstance_Type.tp_richcompare(self, obj, Py_EQ);
29
30 return CallPyObjMethod(self, "IsEqual", obj);
31}
32
33// Implement Python's __ne__ with TObject::IsEqual
35{
36 if (!CPPInstance_Check(obj) || !((CPPInstance *)obj)->fObject)
37 return CPPInstance_Type.tp_richcompare(self, obj, Py_NE);
38
39 return BoolNot(CallPyObjMethod(self, "IsEqual", obj));
40}
41
42////////////////////////////////////////////////////////////////////////////
43/// \brief Add pythonization for equality and inequality operators in
44/// TObject
45/// \param[in] self Always null, since this is a module function.
46/// \param[in] args Pointer to a Python tuple object containing the arguments
47/// received from Python.
48///
49/// The equality and inequality operators are better implemented in C++,
50/// since we need to need to rely on Cppyy's rich comparison if the object
51/// we are comparing ourselves with is not a Python proxy or if it contains
52/// a null pointer. For example, we need to support the comparison to None.
53///
54/// The rest of comparison operators (i.e. those that define order)
55/// can be implemented in Python, throwing a NotImplemented exception
56/// if we are not comparing two proxies to TObject or derivate.
58{
59 PyObject *pyclass = PyTuple_GetItem(args, 0);
60 Utility::AddToClass(pyclass, "__eq__", (PyCFunction)TObjectIsEqual, METH_O);
61 Utility::AddToClass(pyclass, "__ne__", (PyCFunction)TObjectIsNotEqual, METH_O);
63}
#define Py_RETURN_NONE
Definition: CPyCppyy.h:281
_object PyObject
Definition: PyMethodBase.h:41
PyObject * BoolNot(PyObject *value)
PyObject * CallPyObjMethod(PyObject *obj, const char *meth)
Set of helper functions that are invoked from the C++ implementation of pythonizations.
PyObject * TObjectIsEqual(PyObject *self, PyObject *obj)
Definition: TObjectPyz.cxx:25
PyObject * TObjectIsNotEqual(PyObject *self, PyObject *obj)
Definition: TObjectPyz.cxx:34
bool AddToClass(PyObject *pyclass, const char *label, PyCFunction cfunc, int flags=METH_VARARGS)
Definition: Utility.cxx:169
PyTypeObject CPPInstance_Type
bool CPPInstance_Check(T *object)
Definition: CPPInstance.h:118
PyObject * AddTObjectEqNePyz(PyObject *self, PyObject *args)
Add pythonization for equality and inequality operators in TObject.
Definition: TObjectPyz.cxx:57