Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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 "TClass.h"
19#include "TInterpreter.h"
20#include "TInterpreterValue.h"
21
22#include <map>
23
24using namespace CPyCppyy;
25
26static std::string GetCppName(const CPPInstance *self)
27{
29}
30
31// We take as unique identifier the declId of the class to
32// treat the case where a class is loaded, an instance printed,
33// the class unloaded and reloaded with changes.
34static ULong64_t GetClassID(const char *clName)
35{
36 if (auto cl = TClass::GetClass(clName)) {
37 if (auto clInfo = cl->GetClassInfo()) {
38 return reinterpret_cast<ULong64_t>(gInterpreter->GetDeclId(clInfo));
39 }
40 }
41 return 0;
42}
43
45{
46 // Map holding the classID of the classes and the pointer
47 // to the printer function.
48 static std::map<ULong64_t, void *> declIDPrinterMap;
49
50 auto cppObj = self->GetObject();
51 if (!cppObj)
52 // Proxied cpp object is null, use cppyy's generic __repr__
53 return PyObject_Repr((PyObject*)self);
54
55 // We jit the helper only once, at the first invocation of any
56 // printer. The integer parameter is there to make sure we have
57 // different instances of the printing function in presence of
58 // unload-reload events.
59 if (0 == declIDPrinterMap.size()) {
60 std::string printerCode = "namespace ROOT::Internal::Pythonizations::ValuePrinters"
61 "{"
62 " template<class T, ULong64_t> std::string ValuePrinter(void *obj)"
63 " {"
64 " return cling::printValue((T *)obj);"
65 " }"
66 "}";
67 gInterpreter->Declare(printerCode.c_str());
68 }
69
70 const std::string className = GetCppName(self);
71
72 std::string printResult;
73
74 if (const auto classID = GetClassID(className.c_str())) {
75 // If we never encountered this class, we jit the function which
76 // is necessary to print it and store it in the map instantiated
77 // above. Otherwise, we just use the pointer to the previously
78 // jitted function. This allows to jit the printer only once per
79 // type, at the modest price of a typename and pointer stored in
80 // memory.
81 auto &printerFuncrPtr = declIDPrinterMap[classID];
82
83 if (!printerFuncrPtr) {
84 std::string printFuncName = "ROOT::Internal::Pythonizations::ValuePrinters::ValuePrinter<" + className + ", " +
85 std::to_string(classID) + ">";
86 printerFuncrPtr = (void *)gInterpreter->Calc(printFuncName.c_str());
87 }
88 printResult = ((std::string(*)(void *))printerFuncrPtr)(cppObj);
89 } else {
90 // If something went wrong, we use the slow method
91 printResult = gInterpreter->ToString(className.c_str(), cppObj);
92 }
93
94 if (printResult.find("@0x") == 0) {
95 // Fall back to __repr__ if we just get an address from cling
96 return PyObject_Repr((PyObject*)self);
97 } else {
98 return CPyCppyy_PyText_FromString(printResult.c_str());
99 }
100}
101
102////////////////////////////////////////////////////////////////////////////
103/// \brief Add pretty printing pythonization
104/// \param[in] self Always null, since this is a module function.
105/// \param[in] args Pointer to a Python tuple object containing the arguments
106/// received from Python.
107///
108/// This function adds the following pythonizations to print the object more
109/// user-friendly than cppyy by using the output of cling::printValue as the
110/// return value of the special method __str__.
112{
113 PyObject *pyclass = PyTuple_GetItem(args, 0);
114 Utility::AddToClass(pyclass, "__str__", (PyCFunction)ClingPrintValue);
116}
#define Py_RETURN_NONE
Definition CPyCppyy.h:289
#define CPyCppyy_PyText_FromString
Definition CPyCppyy.h:102
static ULong64_t GetClassID(const char *clName)
static std::string GetCppName(const CPPInstance *self)
PyObject * ClingPrintValue(CPPInstance *self, PyObject *)
_object PyObject
unsigned long long ULong64_t
Definition RtypesCore.h:81
#define gInterpreter
Cppyy::TCppType_t ObjectIsA(bool check_smart=true) const
static TClass * GetClass(const char *name, Bool_t load=kTRUE, Bool_t silent=kFALSE)
Static method returning pointer to TClass of the specified class name.
Definition TClass.cxx:2968
bool AddToClass(PyObject *pyclass, const char *label, PyCFunction cfunc, int flags=METH_VARARGS)
Definition Utility.cxx:170
Set of helper functions that are invoked from the pythonizors, on the Python side.
RPY_EXPORTED std::string GetScopedFinalName(TCppType_t type)
PyObject * AddPrettyPrintingPyz(PyObject *self, PyObject *args)
Add pretty printing pythonization.