ATLAS Offline Software
Loading...
Searching...
No Matches
SgPyDataModel.h
Go to the documentation of this file.
1// -*- C++ -*-
2
3/*
4 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
5*/
6
7#ifndef STOREGATEBINDINGS_SGPYDATAMODEL_H
8#define STOREGATEBINDINGS_SGPYDATAMODEL_H 1
9
10#include "Python.h"
11
13#include "SgPyMsg.h"
14
15// Framework includes
16#include "GaudiKernel/Bootstrap.h"
17#include "GaudiKernel/ClassID.h"
18#include "GaudiKernel/DataObject.h"
19#include "GaudiKernel/IClassIDSvc.h"
23#include "CxxUtils/ClassName.h"
24
26
27extern const CLID PyCLID;
28
29
30// ROOT includes
31#include "TClass.h"
32#include "TClassEdit.h"
33#include "TClassRef.h"
34#include "TROOT.h"
35#include "TMethod.h"
36#include "TMethodCall.h"
37
38// PyROOT includes
39#include "AthenaPyRoot.h"
40#ifdef ATLAS_HAVE_CPYCPPYY
41# include "CPyCppyy/PyException.h"
42#endif // ATLAS_HAVE_CPYCPPYY
43
44#include <unordered_map>
45#include <stdexcept>
46
47// Called from python, so only excuted single-threaded (GIL).
50
51// fwd declares
52namespace SG { struct PyProxyMgr; }
53namespace SG { struct PyProxyDict; }
54
55namespace {
58 std::string::size_type
59 cxx_replace(std::string& s,
60 const std::string& from,
61 const std::string& to)
62 {
63 std::string::size_type cnt(std::string::npos);
64
65 if(from != to && !from.empty()) {
66 std::string::size_type pos1(0);
67 std::string::size_type pos2(0);
68 const std::string::size_type from_len(from.size());
69 const std::string::size_type to_len(to.size());
70 cnt = 0;
71
72 while((pos1 = s.find(from, pos2)) != std::string::npos) {
73 s.replace(pos1, from_len, to);
74 pos2 = pos1 + to_len;
75 ++cnt;
76 }
77 }
78 return cnt;
79 }
80}
81
82namespace SG {
83
97 {
98 public:
105 CLID clid );
106
107
111 virtual ~PyDataBucket() override {
113 Py_DECREF( m_pyObj );
114 }
115
119 virtual void* object() override
120 {
121 return m_clid != PyCLID
123 : m_pyObj;
124 }
125
126
128
129
138 virtual void* cast (CLID clid, IRegisterTransient* itr = 0,
139 bool isConst = true) override;
140
141
150 virtual void* cast (const std::type_info& tinfo,
151 IRegisterTransient* itr = 0,
152 bool isConst = true) override;
153
157 virtual const CLID& clID() const override { return m_clid; }
158
162 virtual const std::type_info& tinfo() const override
163 {
164 return m_bib->typeinfo();
165 }
166
170 virtual void relinquish() override
171 {
172 Py_DECREF( m_pyObj );
173 }
174
178 virtual void lock() override;
179
180 private:
183
186
190 };
191
196 {
198 IClassIDSvc* m_clidSvc;
201
204
205 typedef std::unordered_map<StoreGateSvc*,SG::PyProxyDict*> PyProxyMap_t;
207
209 typedef std::unordered_map<CLID, std::string> ClidMap_t;
211
212 static
214 { static PyProxyMgr mgr; return mgr; }
215
216 PyProxyMgr();
217 ~PyProxyMgr();
218
220
222 inline
224 {
225 PyObject* clid = PyDict_GetItem(m_clids, tp);
226 if ( NULL == clid ) {
227 const CLID id = this->clid(tp);
228 if ( id == CLID_NULL ) {
229 return NULL;
230 }
231 clid = PyLong_FromLong(id);
232 PyDict_SetItem(m_clids, tp, clid);
233 // add reverse look-up entry too
234 PyDict_SetItem(m_clids, clid, tp);
235 //Py_INCREF(clid);
236 }
237 return clid;
238 }
239
241 inline
243 {
244 PyObject* tp = PyDict_GetItem(m_clids, clid);
245 if ( NULL == tp ) {
246 std::string cpp_tp;
247 if (!m_clidSvc->getTypeNameOfID(PyLong_AsUnsignedLong(clid),
248 cpp_tp).isSuccess()) {
249 return NULL;
250 }
251 PyObject* alias = PyDict_GetItemString(m_aliases, cpp_tp.c_str());
252 if ( alias ) {
253 tp = alias;
254 } else {
255 tp = PyUnicode_FromString(cpp_tp.c_str());
256 }
257 PyDict_SetItem(m_clids, clid, tp);
258 // reverse look-up
259 PyDict_SetItem(m_clids, tp, clid);
260 if (!alias) {
261 Py_DECREF(tp);
262 }
263 }
264 return tp;
265 }
266
268 inline
270 {
271 PyObject* pyclid = PyLong_FromLong(clid);
272 PyObject* o = this->pytp(pyclid);
273 Py_DECREF(pyclid);
274 return o;
275 }
276
277 inline
279 {
280 CLID id = CLID_NULL;
281 std::string tn = typename_from_python (tp);
282 m_clidSvc->getIDOfTypeName(tn, id).ignore();
283 if ( id == CLID_NULL ) {
284 // try an alias...
285 PyObject* alias = PyDict_GetItemString(m_aliases, tn.c_str());
286 if ( alias ){
287 std::string aliasstr = RootUtils::PyGetString(alias).first;
288 m_clidSvc->getIDOfTypeName(aliasstr, id).ignore();
289 }
290 }
291 if (id == CLID_NULL) {
292 // then try a type-id name...
293 return this->clid_from_tid (tp);
294 }
295 return id;
296 }
297
298 inline
300 {
301 CLID id = CLID_NULL;
302 std::string tn = typename_from_python (tp);
303 m_clidSvc->getIDOfTypeInfoName(tn, id).ignore();
304 if ( id == CLID_NULL ) {
305 // try an alias...
306 PyObject* alias = PyDict_GetItemString(m_aliases, tn.c_str());
307 if ( alias ){
308 std::string aliasstr = RootUtils::PyGetString(alias).first;
309 m_clidSvc->getIDOfTypeInfoName(aliasstr,
310 id).ignore();
311 }
312 }
313 return id;
314 }
315
320 inline
321 const char* load_type(CLID id)
322 {
323 typedef ClidMap_t::iterator Itr_t;
324 Itr_t i = m_clidMap.find(id);
325 if (i != m_clidMap.end()) {
326 return i->second.c_str();
327 }
328 // Reflex now returns std::basic_string<char> instead of std::string
329 std::string tname = m_dictSvc->load_type(id).Name(Reflex::SCOPED);
330 // handle '> >'. order MATTERS!
331 ::cxx_replace(tname, "basic_string<char> >", "string>");
332 ::cxx_replace(tname, "basic_string<char>", "string");
333 m_clidMap[id] = tname;
334
335 // Try to make sure the BIB is initialized.
336 std::string bibname = "SG::BaseInfo<" + tname + ">";
337 TClass* bibcl = gROOT->GetClass (bibname.c_str());
338 if (bibcl) {
339 TMethod* m = bibcl->GetMethodAny ("baseinfo");
340 if (m) {
341 TMethodCall call (m);
342 call.Execute();
343 }
344 }
345
346 return m_clidMap[id].c_str();
347 }
348
349 private:
352 {
353 const std::string moduleName = "AthenaPython.Bindings";
354 PyObject* module = PyImport_ImportModule
355 ( const_cast<char*>(moduleName.c_str()) );
356 if ( !module || !PyModule_Check(module) ) {
357 std::cerr << "SG::PyProxyDict WARNING: could not import module '"
358 << moduleName << "' !\n";
359 Py_XDECREF(module);
360 m_aliases = PyDict_New();
361 return m_aliases;
362 }
363
364 m_aliases = PyDict_GetItemString(PyModule_GetDict(module),
365 (char*)"_clid_typename_aliases");
366 // borrowed ref. so we increment it
367 Py_XINCREF(m_aliases);
368 // don't need this one anymore
369 Py_DECREF(module);
370
371 if ( !m_aliases ) {
372 std::cerr << "SG::PyProxyDict WARNING: could not retrieve the "
373 << "dictionary of aliases from '"
374 << moduleName << "' !\n";
375 Py_XDECREF(m_aliases);
376 m_aliases = PyDict_New();
377 }
378 return m_aliases;
379 }
380
381 std::string typename_from_python (PyObject* tp) const;
382
383 CxxUtils::ClassName::Rules m_dv_rules;
384
385 }; //> class PyProxyMgr
386
392 {
394
396 m_sgSvc( sgSvc )
397 {}
398
400 {}
401
402 inline
403 PyObject* proxy( PyObject* pyclid, PyObject* pykey )
404 {
405 _SGPY_MSG("PyProxyDict::proxy(...)...");
406
407 PyObject* pyproxy = NULL;
408
409 unsigned int id_tmp = 0;
410 if (!PyArg_Parse( pyclid, "I", &id_tmp )) {
411 return nullptr;
412 }
413
414 CLID id = id_tmp;
415 std::string skey;
416 if (pykey != Py_None) {
417 skey = RootUtils::PyGetString (pykey).first;
418 }
419 SG::DataProxy* proxy = skey.empty()
420 ? m_sgSvc->proxy(id)
421 : m_sgSvc->proxy(id, skey);
422 _SGPY_MSG("PyProxyDict::proxy("
423 << (proxy ? proxy->clID() : id) << ", "
424 << (proxy ? proxy->name() : skey) << ")...");
425 pyproxy = TPython::CPPInstance_FromVoidPtr((void*)proxy,
426 "SG::DataProxy");
427
428 _SGPY_MSG("PyProxyDict::proxy(...)... [done]");
429 return pyproxy;
430 }
431
432 inline
433 PyObject* newPyDataObject( const char* klass, void* addr=0 )
434 {
435 PyObject* obj = NULL;
436 if ( !(obj = TPython::CPPInstance_FromVoidPtr((void*)addr, klass)) ) {
437#ifdef ATLAS_HAVE_CPYCPPYY
438 throw CPyCppyy::PyException();
439#else
440 throw std::runtime_error ("Python exception");
441#endif // ATLAS_HAVE_CPYCPPYY
442 }
443 return obj;
444 }
445 }; //> struct PyProxyDict
446
447
448
449 // -------- inlines ----------------------
450 inline
451 PyProxyDict*
453 {
454 SG::PyProxyDict* p = 0;
455 PyProxyMap_t::iterator i = m_proxyMap.find(sg);
456 if ( i == m_proxyMap.end() ) {
457 m_proxyMap[sg] = p = new SG::PyProxyDict(sg);
458 } else {
459 p = i->second;
460 }
461 return p;
462 }
463
464
465} //> end namespace SG
466
467#endif // !STOREGATEBINDINGS_SGPYDATAMODEL_H
#define CPPInstance_ASVOIDPTR(o)
Provide an interface for finding inheritance information at run time.
Recursively separate out template arguments in a C++ class name.
uint32_t CLID
The Class ID type.
_object PyObject
const CLID PyCLID
#define _SGPY_MSG(x)
Definition SgPyMsg.h:22
Define macros for attributes used to control the static checker.
#define ATLAS_NO_CHECK_FILE_THREAD_SAFETY
T * cast(SG::IRegisterTransient *irt=0, bool isConst=true)
Return the contents of the DataBucket, converted to type T.
a simple interface to interact with the Reflex dictionaries and abstract/workaround a few ROOT bugs.
The non-template portion of the BaseInfo implementation.
Interface for registering a transient object in t2p map.
virtual ~PyDataBucket() override
Destructor.
PyDataBucket(PyObject *obj, CLID clid)
Constructor.
virtual void * object() override
Return the held object.
PyObject * m_pyObj
Pointer to the held pyroot object (or 'regular' PyObject).
virtual const CLID & clID() const override
Retrieve reference to class definition structure.
virtual void * cast(CLID clid, IRegisterTransient *itr=0, bool isConst=true) override
Return the contents of the DataBucket, converted to type given by clid.
virtual void lock() override
If the held object derives from ILockable, call lock() on it.
CLID m_clid
The class ID of the wrapped object.
virtual void relinquish() override
Give up ownership of the DataBucket contents.
virtual const std::type_info & tinfo() const override
Return the type_info for the stored object.
const SG::BaseInfoBase * m_bib
pointer to the SG::BaseInfoBase structure holding the converter functions for objects held by StoreGa...
The Athena Transient Store API.
@ SCOPED
Definition RootType.h:27
std::pair< std::string, bool > PyGetString(PyObject *s)
Convert python string -> C++ string for py2 and py3.
Definition PyGetString.h:40
Forward declaration.
a python front-end to the IProxyDict interface PyProxyDict encapsulates getting python objects from t...
PyObject * proxy(PyObject *pyclid, PyObject *pykey)
PyObject * newPyDataObject(const char *klass, void *addr=0)
PyProxyDict(StoreGateSvc *sgSvc)
StoreGateSvc * m_sgSvc
CLID clid_from_tid(PyObject *tp)
PyObject * importDictAliases()
import the dictionary of aliases from a well known location
static PyProxyMgr & instance()
PyObject * pytp(CLID clid)
returns a borrowed reference
std::unordered_map< CLID, std::string > ClidMap_t
a dictionary of CLID -> reflex typename
CxxUtils::ClassName::Rules m_dv_rules
PyObject * m_aliases
a dictionary of "typedef'ed typename" -> "typename"
PyProxyMap_t m_proxyMap
CLID clid(PyObject *tp)
ClidMap_t m_clidMap
const char * load_type(CLID id)
ensure everything has been loaded for the clid id (classid, reflex dict, baseinfobase,...
SG::PyProxyDict * pyproxy(StoreGateSvc *sg)
IDictLoaderSvc * m_dictSvc
std::unordered_map< StoreGateSvc *, SG::PyProxyDict * > PyProxyMap_t
PyObject * pyclid(PyObject *tp)
returns a borrowed reference
IClassIDSvc * m_clidSvc
PyObject * pytp(PyObject *clid)
returns a borrowed reference
std::string typename_from_python(PyObject *tp) const
Extract a string from a python object and interpret it as a type name, performing any needed massagin...
PyObject * m_clids
a dictionary of 'typename' -> CLID (and reverse CLID->'typename')