Logo ROOT  
Reference Guide
TDirectoryFilePyz.cxx
Go to the documentation of this file.
1// Author: Massimiliano Galli CERN 08/2019
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 "CPyCppyy.h"
13#include "CPPInstance.h"
14#include "CPPOverload.h"
15#include "PyROOTPythonize.h"
16#include "ProxyWrappers.h"
17#include "Python.h"
18#include "Utility.h"
19#include "PyzCppHelpers.hxx"
20#include "TClass.h"
21#include "TKey.h"
22#include "TDirectoryFile.h"
23
24using namespace CPyCppyy;
25
26////////////////////////////////////////////////////////////////////////////
27/// \brief Allow access to objects through the method Get()
28/// This concerns both TDirectoryFile and TFile, since the latter
29/// inherits the Get method from the former.
30/// We decided not to inject this behavior directly in TDirectory
31/// because this one already has a templated method Get which, when
32/// invoked from Python, returns an object of the derived class (e.g. TH1F)
33/// and not a generic TObject.
34/// In case the object is not found, a null pointer is returned.
36{
37 // Pythonization of TDirectoryFile::Get that handles non-TObject deriveds
38 if (!CPPInstance_Check(self)) {
39 PyErr_SetString(PyExc_TypeError,
40 "T(Directory)File::Get must be called with a T(Directory)File instance as first argument");
41 return nullptr;
42 }
43 auto dirf = (TDirectoryFile *)GetTClass(self)->DynamicCast(TDirectoryFile::Class(), self->GetObject());
44 if (!dirf) {
45 PyErr_SetString(PyExc_ReferenceError, "attempt to access a null-pointer");
46 return nullptr;
47 }
48 const char *namecycle = CPyCppyy_PyText_AsString(pynamecycle);
49 if (!namecycle)
50 return nullptr; // TypeError already set
51 auto key = dirf->GetKey(namecycle);
52 if (key) {
53 void *addr = dirf->GetObjectChecked(namecycle, key->GetClassName());
54 return BindCppObjectNoCast(addr, (Cppyy::TCppType_t)Cppyy::GetScope(key->GetClassName()), kFALSE);
55 }
56 // no key? for better or worse, call normal Get()
57 void *addr = dirf->Get(namecycle);
58 return BindCppObject(addr, (Cppyy::TCppType_t)Cppyy::GetScope("TObject"), kFALSE);
59}
60
61////////////////////////////////////////////////////////////////////////////
62/// \brief Add pythonisation of TDirectoryFile::Get()
63/// \param[in] self Always null, since this is a module function.
64/// \param[in] args Pointer to a Python tuple object containing the arguments
65/// received from Python.
66///
67/// Allow access to objects through the Get() method.
68/// (e.g. dirfile.Get(key))
70{
71 PyObject *pyclass = PyTuple_GetItem(args, 0);
72 Utility::AddToClass(pyclass, "Get", (PyCFunction)TDirectoryFileGetPyz, METH_O);
74}
#define CPyCppyy_PyText_AsString
Definition: CPyCppyy.h:97
#define Py_RETURN_NONE
Definition: CPyCppyy.h:281
void Class()
Definition: Class.C:29
_object PyObject
Definition: PyMethodBase.h:41
TClass * GetTClass(const CPyCppyy::CPPInstance *pyobj)
const Bool_t kFALSE
Definition: RtypesCore.h:90
PyObject * TDirectoryFileGetPyz(CPPInstance *self, PyObject *pynamecycle)
Allow access to objects through the method Get() This concerns both TDirectoryFile and TFile,...
void * DynamicCast(const TClass *base, void *obj, Bool_t up=kTRUE)
Cast obj of this class type up to baseclass cl if up is true.
Definition: TClass.cxx:4878
A ROOT file is structured in Directories (like a file system).
bool AddToClass(PyObject *pyclass, const char *label, PyCFunction cfunc, int flags=METH_VARARGS)
Definition: Utility.cxx:169
PyObject * BindCppObjectNoCast(Cppyy::TCppObject_t object, Cppyy::TCppType_t klass, const unsigned flags=0)
bool CPPInstance_Check(T *object)
Definition: CPPInstance.h:118
PyObject * BindCppObject(Cppyy::TCppObject_t object, Cppyy::TCppType_t klass, const unsigned flags=0)
TCppScope_t TCppType_t
Definition: cpp_cppyy.h:19
RPY_EXPORTED TCppScope_t GetScope(const std::string &scope_name)
PyObject * AddTDirectoryFileGetPyz(PyObject *self, PyObject *args)
Add pythonisation of TDirectoryFile::Get()