Skip to content

Commit

Permalink
[PyROOT] WriteObject pythonization recognizes TObject-derived objects
Browse files Browse the repository at this point in the history
Similarly to what is done in TDirectory::WriteObject. The cppyy
pythonization of this method had to rely only on WriteObjectAny since
the wrapped C++ type of the object is obfuscated.
  • Loading branch information
vepadulano committed Mar 2, 2022
1 parent db6a9d6 commit fb74d2b
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions bindings/pyroot/pythonizations/src/TDirectoryPyz.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "Utility.h"
#include "PyzCppHelpers.hxx"
#include "TClass.h"
#include "TNamed.h"
#include "TDirectory.h"
#include "TKey.h"
#include "Python.h"
Expand Down Expand Up @@ -47,13 +48,34 @@ PyObject *TDirectoryWriteObject(CPPInstance *self, PyObject *args)
"TDirectory::WriteObject must be called with a TDirectory instance as first argument");
return nullptr;
}

// Implement a check on whether the object is derived from TObject or not. Similarly to what is done in
// TDirectory::WriteObject with SFINAE. Unfortunately, 'wrt' is a void* in this scope and can't be casted to its
// concrete type.
auto *wrtclass = GetTClass(wrt);
void *wrtobj = wrt->GetObject();
Int_t result = 0;
if (option != nullptr) {
result = dir->WriteObjectAny(wrt->GetObject(), GetTClass(wrt), CPyCppyy_PyText_AsString(name),
CPyCppyy_PyText_AsString(option), bufsize);

if (wrtclass->IsTObject()) {
// If the found TClass is derived from TObject, cast the object to a TNamed since we are just interested in the
// object title for the purposes of the WriteTObject function.
auto objtowrite = static_cast<TNamed *>(wrtclass->DynamicCast(TNamed::Class(), wrtobj));

if (option != nullptr) {
result =
dir->WriteTObject(objtowrite, CPyCppyy_PyText_AsString(name), CPyCppyy_PyText_AsString(option), bufsize);
} else {
result = dir->WriteTObject(objtowrite, CPyCppyy_PyText_AsString(name));
}
} else {
result = dir->WriteObjectAny(wrt->GetObject(), GetTClass(wrt), CPyCppyy_PyText_AsString(name));
if (option != nullptr) {
result = dir->WriteObjectAny(wrtobj, wrtclass, CPyCppyy_PyText_AsString(name),
CPyCppyy_PyText_AsString(option), bufsize);
} else {
result = dir->WriteObjectAny(wrtobj, wrtclass, CPyCppyy_PyText_AsString(name));
}
}

return PyInt_FromLong((Long_t)result);
}

Expand Down

0 comments on commit fb74d2b

Please sign in to comment.