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#include "PyzCppHelpers.hxx"
21
22// ROOT
23#include "TObject.h"
24
25using namespace CPyCppyy;
26
27// Implement Python's __eq__ with TObject::IsEqual
29{
30 if (!CPyCppyy::Instance_Check(obj) || !((CPPInstance *)obj)->fObject)
31 return CPPInstance_Type.tp_richcompare(self, obj, Py_EQ);
32
33 return CallPyObjMethod(self, "IsEqual", obj);
34}
35
36// Implement Python's __ne__ with TObject::IsEqual
38{
39 if (!CPyCppyy::Instance_Check(obj) || !((CPPInstance *)obj)->fObject)
40 return CPPInstance_Type.tp_richcompare(self, obj, Py_NE);
41
42 return BoolNot(CallPyObjMethod(self, "IsEqual", obj));
43}
44
45////////////////////////////////////////////////////////////////////////////
46/// \brief Add pythonization for equality and inequality operators in
47/// TObject
48/// \param[in] self Always null, since this is a module function.
49/// \param[in] args Pointer to a Python tuple object containing the arguments
50/// received from Python.
51///
52/// The equality and inequality operators are better implemented in C++,
53/// since we need to need to rely on Cppyy's rich comparison if the object
54/// we are comparing ourselves with is not a Python proxy or if it contains
55/// a null pointer. For example, we need to support the comparison to None.
56///
57/// The rest of comparison operators (i.e. those that define order)
58/// can be implemented in Python, throwing a NotImplemented exception
59/// if we are not comparing two proxies to TObject or derivate.
61{
62 PyObject *pyclass = PyTuple_GetItem(args, 0);
63 Utility::AddToClass(pyclass, "__eq__", (PyCFunction)TObjectIsEqual, METH_O);
64 Utility::AddToClass(pyclass, "__ne__", (PyCFunction)TObjectIsNotEqual, METH_O);
66}
#define Py_RETURN_NONE
Definition CPyCppyy.h:268
_object PyObject
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)
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:163
PyObject * AddTObjectEqNePyz(PyObject *self, PyObject *args)
Add pythonization for equality and inequality operators in TObject.