ATLAS Offline Software
Static Public Member Functions | List of all members
RootUtils::PyROOTInspector Class Reference

#include <PyROOTInspector.h>

Collaboration diagram for RootUtils::PyROOTInspector:

Static Public Member Functions

static PyObjectpyroot_inspect (PyObject *obj, bool persistentOnly=false)
 
static PyObjectpyroot_inspect2 (PyObject *obj, PyObject *obj_name, bool persistentOnly=false, bool retvecs=false)
 

Detailed Description

Definition at line 21 of file PyROOTInspector.h.

Member Function Documentation

◆ pyroot_inspect()

PyObject * RootUtils::PyROOTInspector::pyroot_inspect ( PyObject obj,
bool  persistentOnly = false 
)
static

Definition at line 443 of file PyROOTInspector.cxx.

445 {
446  // handle non-pyroot objects
447  if (!TPython::CPPInstance_Check(pyobj)) {
448  Py_XINCREF(pyobj);
449  return pyobj;
450  }
451 
452  TClass *tcls = RootUtils::objectIsA(pyobj);
453  if (0 == tcls) {
454  Py_INCREF(Py_None);
455  return Py_None;
456  }
457  void *obj = TPython::CPPInstance_AsVoidPtr(pyobj);
458 
459  if (!strcmp(tcls->GetName(), "string")) {
460  std::string *str = (std::string*)obj;
461  return PyUnicode_FromString(str->c_str());
462  }
463 
464  TString tstring = tcls->GetName();
465  if (tstring.BeginsWith("pair<") ||
466  tstring.BeginsWith("std::pair<")) {
467  PyObject *val = PyTuple_New(2);
468  PyObject *v0 = PyObject_GetAttrString(pyobj, "first");
469  PyObject *v1 = PyObject_GetAttrString(pyobj, "second");
470  PyTuple_SET_ITEM(val, 0, pyroot_inspect(v0, persistentOnly));
471  PyTuple_SET_ITEM(val, 1, pyroot_inspect(v1, persistentOnly));
472  Py_DECREF(v0);
473  Py_DECREF(v1);
474  return val;
475  }
476 
477  Int_t hdr = 0;
478  if (CPyCppyy::Sequence_Check(pyobj)) {
479  if (!strcmp(tcls->GetName(), "CLHEP::Hep3Vector")) {
480  hdr = 0;
481  } else {
482  hdr = 1;
483  }
484  } else {
485  hdr = 0;
486  }
487 
488  TList *members = tcls->GetListOfDataMembers();
489  const Int_t nmembers = members->GetEntries();
490 
491  PyObject *py_members = PyList_New(nmembers+hdr);
492 #if PYROOT_INSPECTOR_DBG
493  std::cerr << "==[" << tcls->GetName() << "]== (#mbrs:"
494  << nmembers
495  << " #stl:" << hdr /*PySequence_Size(pyobj)*/
496  << " #py-sz:" << PyList_Size(py_members)
497  << ")...\n";
498 #endif
499 
500  if (hdr) {
501  // handle collection
502  const Py_ssize_t nelems = PySequence_Size(pyobj);
503 #if PYROOT_INSPECTOR_DBG
504  std::cerr << "== sequence (" << nelems << ")...\n";
505 #endif
506  PyObject *py_elems = PyList_New(nelems);
507  for (Py_ssize_t i = 0; i < nelems; ++i) {
508  PyObject *itr = PySequence_GetItem(pyobj, i);
509  PyObject *itr_pyroot = pyroot_inspect(itr, persistentOnly);
510  PyList_SET_ITEM(py_elems, i, itr_pyroot);
511  Py_DECREF(itr);
512  //Py_DECREF(itr_pyroot);
513  }
514  // add the elements to the "members" list
515  PyList_SET_ITEM(py_members, 0, py_elems);
516 #if PYROOT_INSPECTOR_DBG
517  std::cerr << "== sequence (" << nelems << ")... content:\n"
518  << ::to_str(py_elems)
519  << "\n";
520  std::cerr << "== sequence (" << nelems << ")... [done]\n";
521 #endif
522  }
523 
524 
525  for (Int_t j = 0; j<nmembers; ++j) {
526  TDataMember *mbr = (TDataMember*)(members->At(j));
527  Int_t offset = mbr->GetOffset();
528  char *ptr = (char*)obj + offset;
529 
530 #if PYROOT_INSPECTOR_DBG
531  TClass *mbr_cls = TClass::GetClass(mbr->GetTypeName());
532  std::cerr << "==[" << j << "] - [" << mbr->GetTypeName() << "] "
533  << "[" << mbr->GetName()
534  << "]"
535  << "[" << (mbr_cls ? mbr_cls->GetName() : "N/A") << "]\n";
536 #endif
537 
538  PyObject *py_mbr = 0;
539 
540  if (persistentOnly && !mbr->IsPersistent())
541  continue;
542  if (mbr->IsaPointer())
543  continue;
544  if (mbr->IsBasic()) {
545  TDataType * mbr_type = mbr->GetDataType();
546  EDataType mbr_dtype = (EDataType)mbr_type->GetType();
547  py_mbr = to_pyobj(ptr, mbr_dtype);
548  } else if (mbr->IsEnum()) {
549 #if PYROOT_INSPECTOR_DBG
550  std::cerr << "==[" << mbr->GetTypeName() << "]["
551  << mbr->GetDataType()->GetType() << "][val="
552  << (*(int*)ptr) << "]["
553  << mbr->GetName() << "] is an enum !!\n";
554 #endif
555  py_mbr = to_pyobj(ptr, kInt_t);
556  } else {
557  PyObject *pyroot_obj = TPython::CPPInstance_FromVoidPtr
558  ((void*)ptr,
559  mbr->GetTypeName());
560  if (pyroot_obj) {
561  py_mbr = pyroot_inspect(pyroot_obj, persistentOnly);
562  }
563  Py_XDECREF(pyroot_obj);
564  }
565  if (!py_mbr) {
566  std::cerr << "could not create py-object of type ["
567  << mbr->GetTypeName() << "] !\n";
568  Py_DECREF(py_members);
569  throw RootUtils::PyException();
570  }
571 
572  PyObject *py_item = PyTuple_New(2);
573  PyTuple_SET_ITEM(py_item, 0,
574  PyUnicode_FromString(mbr->GetName()));
575  PyTuple_SET_ITEM(py_item, 1, py_mbr);
576  PyList_SET_ITEM(py_members, j+hdr, py_item);
577  }
578 #if PYROOT_INSPECTOR_DBG
579  std::cerr << "==[" << tcls->GetName() << "]== (#mbrs:"
580  << nmembers << ")... [done]\n";
581 #endif
582  return py_members;
583 }

◆ pyroot_inspect2()

PyObject * RootUtils::PyROOTInspector::pyroot_inspect2 ( PyObject obj,
PyObject obj_name,
bool  persistentOnly = false,
bool  retvecs = false 
)
static

Definition at line 586 of file PyROOTInspector.cxx.

590 {
591  PyObject *pystack = PyList_New(0);
592  ::recurse_pyinspect(pyobj, pyobj_name, pystack, persistentOnly, retvecs);
593  return pystack;
594 }

The documentation for this class was generated from the following files:
RootUtils::PyROOTInspector::pyroot_inspect
static PyObject * pyroot_inspect(PyObject *obj, bool persistentOnly=false)
Definition: PyROOTInspector.cxx:443
RootUtils::PyException
CPyCppyy::PyException PyException
Definition: Utility.h:24
dbg::ptr
void * ptr(T *p)
Definition: SGImplSvc.cxx:74
parseMapping.v0
def v0
Definition: parseMapping.py:149
lumiFormat.i
int i
Definition: lumiFormat.py:85
RootUtils::objectIsA
TClass * objectIsA(PyObject *obj)
Definition: Utility.cxx:103
Pythia8_RapidityOrderMPI.val
val
Definition: Pythia8_RapidityOrderMPI.py:14
convertTimingResiduals.offset
offset
Definition: convertTimingResiduals.py:71
str
Definition: BTagTrackIpAccessor.cxx:11
python.PyAthena.obj
obj
Definition: PyAthena.py:132
PyObject
_object PyObject
Definition: IPyComponent.h:26