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
21using namespace CPyCppyy;
22
23static std::string GetCppName(const CPPInstance *self)
24{
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}
#define Py_RETURN_NONE
Definition: CPyCppyy.h:281
#define CPyCppyy_PyText_FromString
Definition: CPyCppyy.h:102
static std::string GetCppName(const CPPInstance *self)
Definition: GenericPyz.cxx:23
PyObject * ClingPrintValue(CPPInstance *self, PyObject *)
Definition: GenericPyz.cxx:28
_object PyObject
Definition: PyMethodBase.h:41
#define gInterpreter
Definition: TInterpreter.h:556
Cppyy::TCppType_t ObjectIsA(bool check_smart=true) const
Definition: CPPInstance.h:106
bool AddToClass(PyObject *pyclass, const char *label, PyCFunction cfunc, int flags=METH_VARARGS)
Definition: Utility.cxx:169
RPY_EXPORTED std::string GetScopedFinalName(TCppType_t type)
PyObject * AddPrettyPrintingPyz(PyObject *self, PyObject *args)
Add pretty printing pythonization.
Definition: GenericPyz.cxx:54