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 427 of file PyROOTInspector.cxx.

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

◆ pyroot_inspect2()

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

Definition at line 570 of file PyROOTInspector.cxx.

574 {
575  PyObject *pystack = PyList_New(0);
576  ::recurse_pyinspect(pyobj, pyobj_name, pystack, persistentOnly, retvecs);
577  return pystack;
578 }

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:427
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