Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
PyzPythonHelpers.cxx
Go to the documentation of this file.
1// Author: Stefan Wunsch CERN 08/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/**
13
14Set of helper functions that are invoked from the pythonizors, on the
15Python side. For that purpose, they are included in the interface of the
16PyROOT extension module.
17
18*/
19
20#include "CPyCppyy/API.h"
21
22#include "PyROOTPythonize.h"
23
24#include "ROOT/RConfig.hxx"
25#include "TInterpreter.h"
26
27#include <sstream>
28
29////////////////////////////////////////////////////////////////////////////
30/// \brief Get size of C++ data-type
31/// \param[in] self Always null, since this is a module function.
32/// \param[in] args C++ data-type as Python string
33///
34/// This function returns the length of a C++ data-type in bytes
35/// as a Python integer.
37{
38 // Get name of data-type
39 PyObject *pydtype = PyTuple_GetItem(args, 0);
40 std::string dtype = PyUnicode_AsUTF8(pydtype);
41
42 // Call interpreter to get size of data-type using `sizeof`
43 size_t size = 0;
44 std::stringstream code;
45 code << "*((size_t*)" << std::showbase << (uintptr_t)&size << ") = (size_t)sizeof(" << dtype << ")";
46 gInterpreter->Calc(code.str().c_str());
47
48 // Return size of data-type as integer
49 PyObject *pysize = PyLong_FromLong(size);
50 return pysize;
51}
52
53////////////////////////////////////////////////////////////////////////////
54/// \brief Get pointer to the data of an object
55/// \param[in] self Always null, since this is a module function.
56/// \param[in] args [0] Python representation of the C++ object.
57/// [1] Data-type of the C++ object as Python string.
58/// [2] Method to be called on the C++ object to get the data pointer as Python string
59///
60/// This function returns the pointer to the data of an object as an Python
61/// integer retrieved by the given method.
63{
64 // Get pointer of C++ object
65 PyObject *pyobj = PyTuple_GetItem(args, 0);
66 void* cppobj = CPyCppyy::Instance_AsVoidPtr(pyobj);
67
68 // Get name of C++ object as string
69 PyObject *pycppname = PyTuple_GetItem(args, 1);
70 std::string cppname = PyUnicode_AsUTF8(pycppname);
71
72 // Get name of method to be called to get the data pointer
73 PyObject *pymethodname = PyTuple_GetItem(args, 2);
74 std::string methodname = PyUnicode_AsUTF8(pymethodname);
75
76 // Call interpreter to get pointer to data
77 uintptr_t pointer = 0;
78 std::stringstream code;
79 code << "*((intptr_t*)" << std::showbase << (uintptr_t)&pointer << ") = reinterpret_cast<uintptr_t>(reinterpret_cast<"
80 << cppname << "*>(" << std::showbase << (uintptr_t)cppobj << ")->" << methodname << "())";
81 gInterpreter->Calc(code.str().c_str());
82
83 // Return pointer as integer
84 PyObject *pypointer = PyLong_FromUnsignedLongLong(pointer);
85 return pypointer;
86}
87
88////////////////////////////////////////////////////////////////////////////
89/// \brief Get endianess of the system
90/// \param[in] self Always null, since this is a module function.
91/// \param[in] args Pointer to an empty Python tuple.
92/// \return Endianess as Python string
93///
94/// This function returns endianess of the system as a Python integer. The
95/// return value is either '<' or '>' for little or big endian, respectively.
96PyObject *PyROOT::GetEndianess(PyObject * /* self */, PyObject * /* args */)
97{
98#ifdef R__BYTESWAP
99 return PyUnicode_FromString("<");
100#else
101 return PyUnicode_FromString(">");
102#endif
103}
_object PyObject
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
#define gInterpreter
CPYCPPYY_EXTERN void * Instance_AsVoidPtr(PyObject *pyobject)
Definition API.cxx:102
PyObject * GetEndianess(PyObject *self, PyObject *args)
Get endianess of the system.
PyObject * GetDataPointer(PyObject *self, PyObject *args)
Get pointer to the data of an object.
PyObject * GetSizeOfType(PyObject *self, PyObject *args)
Set of helper functions that are invoked from the pythonizors, on the Python side.