Logo ROOT  
Reference Guide
GenericPyz.cxx
Go to the documentation of this file.
1 // Author: Stefan Wunsch, Enric Tejedor CERN 06/2018
2 // Original PyROOT code by Wim Lavrijsen, LBL
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2018, 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 #include "Python.h"
13 
14 #include "CPyCppyy.h"
15 #include "PyROOTPythonize.h"
16 #include "CPPInstance.h"
17 #include "Utility.h"
18 #include "TInterpreter.h"
19 #include "TInterpreterValue.h"
20 
21 using namespace CPyCppyy;
22 
23 static std::string GetCppName(const CPPInstance *self)
24 {
25  return Cppyy::GetScopedFinalName(self->ObjectIsA());
26 }
27 
29 {
30  auto cppObj = self->GetObject();
31  if (!cppObj)
32  // Proxied cpp object is null, use cppyy's generic __repr__
33  return PyObject_Repr((PyObject*)self);
34 
35  const std::string className = GetCppName(self);
36  auto printResult = gInterpreter->ToString(className.c_str(), cppObj);
37  if (printResult.find("@0x") == 0) {
38  // Fall back to __repr__ if we just get an address from cling
39  return PyObject_Repr((PyObject*)self);
40  } else {
41  return CPyCppyy_PyText_FromString(printResult.c_str());
42  }
43 }
44 
45 ////////////////////////////////////////////////////////////////////////////
46 /// \brief Add pretty printing pythonization
47 /// \param[in] self Always null, since this is a module function.
48 /// \param[in] args Pointer to a Python tuple object containing the arguments
49 /// received from Python.
50 ///
51 /// This function adds the following pythonizations to print the object more
52 /// user-friendly than cppyy by using the output of cling::printValue as the
53 /// return value of the special method __str__.
55 {
56  PyObject *pyclass = PyTuple_GetItem(args, 0);
57  Utility::AddToClass(pyclass, "__str__", (PyCFunction)ClingPrintValue);
59 }
CPyCppyy
Set of helper functions that are invoked from the pythonizors, on the Python side.
Definition: TPyClassGenerator.cxx:31
CPPInstance.h
PyObject
_object PyObject
Definition: PyMethodBase.h:42
PyROOT::AddPrettyPrintingPyz
PyObject * AddPrettyPrintingPyz(PyObject *self, PyObject *args)
Add pretty printing pythonization.
Definition: GenericPyz.cxx:54
Utility.h
TInterpreterValue.h
PyROOTPythonize.h
gInterpreter
#define gInterpreter
Definition: TInterpreter.h:560
CPyCppyy::CPPInstance
Definition: CPPInstance.h:26
CPyCppyy_PyText_FromString
#define CPyCppyy_PyText_FromString
Definition: CPyCppyy.h:102
ClingPrintValue
PyObject * ClingPrintValue(CPPInstance *self, PyObject *)
Definition: GenericPyz.cxx:28
CPyCppyy.h
Cppyy::GetScopedFinalName
RPY_EXPORTED std::string GetScopedFinalName(TCppType_t type)
Definition: clingwrapper.cxx:1161
TInterpreter.h
CPyCppyy::Utility::AddToClass
bool AddToClass(PyObject *pyclass, const char *label, PyCFunction cfunc, int flags=METH_VARARGS)
Definition: Utility.cxx:169
GetCppName
static std::string GetCppName(const CPPInstance *self)
Definition: GenericPyz.cxx:23
Py_RETURN_NONE
#define Py_RETURN_NONE
Definition: CPyCppyy.h:281