ATLAS Offline Software
Loading...
Searching...
No Matches
SG::PyProxyMgr Struct Reference

A helper class to manage accesses to PyProxies. More...

#include <SgPyDataModel.h>

Collaboration diagram for SG::PyProxyMgr:

Public Types

typedef std::unordered_map< StoreGateSvc *, SG::PyProxyDict * > PyProxyMap_t
typedef std::unordered_map< CLID, std::string > ClidMap_t
 a dictionary of CLID -> reflex typename

Public Member Functions

 PyProxyMgr ()
 ~PyProxyMgr ()
SG::PyProxyDict * pyproxy (StoreGateSvc *sg)
PyObject * pyclid (PyObject *tp)
 returns a borrowed reference
PyObject * pytp (PyObject *clid)
 returns a borrowed reference
PyObject * pytp (CLID clid)
 returns a borrowed reference
CLID clid (PyObject *tp)
CLID clid_from_tid (PyObject *tp)
const char * load_type (CLID id)
 ensure everything has been loaded for the clid id (classid, reflex dict, baseinfobase,...)

Static Public Member Functions

static PyProxyMgr & instance ()

Public Attributes

IDictLoaderSvc * m_dictSvc
IClassIDSvc * m_clidSvc
PyObject * m_aliases
 a dictionary of "typedef'ed typename" -> "typename"
PyObject * m_clids
 a dictionary of 'typename' -> CLID (and reverse CLID->'typename')
PyProxyMap_t m_proxyMap
ClidMap_t m_clidMap

Private Member Functions

PyObject * importDictAliases ()
 import the dictionary of aliases from a well known location
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 massaging.

Private Attributes

CxxUtils::ClassName::Rules m_dv_rules

Detailed Description

A helper class to manage accesses to PyProxies.

Definition at line 195 of file SgPyDataModel.h.

Member Typedef Documentation

◆ ClidMap_t

typedef std::unordered_map<CLID, std::string> SG::PyProxyMgr::ClidMap_t

a dictionary of CLID -> reflex typename

Definition at line 209 of file SgPyDataModel.h.

◆ PyProxyMap_t

Definition at line 205 of file SgPyDataModel.h.

Constructor & Destructor Documentation

◆ PyProxyMgr()

SG::PyProxyMgr::PyProxyMgr ( )

Definition at line 169 of file SgPyDataModel.cxx.

170 {
172 m_clids = PyDict_New();
173 m_clidSvc = 0;
174 {
175 ServiceHandle<IClassIDSvc> svc("ClassIDSvc", "SgPyDataModel");
176 if ( !svc.retrieve().isSuccess()) {
177 throw std::runtime_error
178 ("SG::PyProxyMgr: Could not retrieve ClassIDSvc");
179 }
180 m_clidSvc = svc.operator->();
181 }
182 m_dictSvc = 0;
183 {
184 ServiceHandle<IDictLoaderSvc> svc("AthDictLoaderSvc", "SgPyDataModel");
185 if ( !svc.retrieve().isSuccess()) {
186 throw std::runtime_error
187 ("SG::PyProxyMgr: Could not retrieve AthDictLoaderSvc");
188 }
189 m_dictSvc = svc.operator->();
190 }
191
192 m_dv_rules.add ("DataVector<$T, $B>", "DataVector<$T>");
193 }
AthROOTErrorHandlerSvc * svc
PyObject * importDictAliases()
import the dictionary of aliases from a well known location
CxxUtils::ClassName::Rules m_dv_rules
PyObject * m_aliases
a dictionary of "typedef'ed typename" -> "typename"
IDictLoaderSvc * m_dictSvc
IClassIDSvc * m_clidSvc
PyObject * m_clids
a dictionary of 'typename' -> CLID (and reverse CLID->'typename')

◆ ~PyProxyMgr()

SG::PyProxyMgr::~PyProxyMgr ( )

Definition at line 195 of file SgPyDataModel.cxx.

196 {
197 // Don't do this if don't have a valid thread state.
198 // (With py3, the interpreter gets shut down before global dtors run...)
199 if (PyThreadState_GetUnchecked())
200 {
201 Py_DECREF(m_aliases);
202 Py_DECREF(m_clids);
203 }
204 // delete the proxy dicts...
205 for ( PyProxyMap_t::iterator
206 i = m_proxyMap.begin(),
207 iEnd = m_proxyMap.end();
208 i != iEnd;
209 ++i ) {
210 delete i->second; i->second = 0;
211 }
212 }
PyProxyMap_t m_proxyMap

Member Function Documentation

◆ clid()

CLID SG::PyProxyMgr::clid ( PyObject * tp)
inline

Definition at line 278 of file SgPyDataModel.h.

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 }
uint32_t CLID
The Class ID type.
_object PyObject
std::pair< std::string, bool > PyGetString(PyObject *s)
Convert python string -> C++ string for py2 and py3.
Definition PyGetString.h:40
CLID clid_from_tid(PyObject *tp)
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...

◆ clid_from_tid()

CLID SG::PyProxyMgr::clid_from_tid ( PyObject * tp)
inline

Definition at line 299 of file SgPyDataModel.h.

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 }

◆ importDictAliases()

PyObject * SG::PyProxyMgr::importDictAliases ( )
inlineprivate

import the dictionary of aliases from a well known location

Definition at line 351 of file SgPyDataModel.h.

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 }

◆ instance()

PyProxyMgr & SG::PyProxyMgr::instance ( )
inlinestatic

Definition at line 213 of file SgPyDataModel.h.

◆ load_type()

const char * SG::PyProxyMgr::load_type ( CLID id)
inline

ensure everything has been loaded for the clid id (classid, reflex dict, baseinfobase,...)

Returns
the reflex typename associated with that clid

Definition at line 321 of file SgPyDataModel.h.

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 }
@ SCOPED
Definition RootType.h:27
call(args, bufsize=0, executable=None, stdin=None, preexec_fn=None, close_fds=False, shell=False, cwd=None, env=None, universal_newlines=False, startupinfo=None, creationflags=0, message="", logger=msg, loglevel=None, timeout=None, retry=2, timefactor=1.5, sleeptime=10)
Definition trfUtils.py:157
ClidMap_t m_clidMap

◆ pyclid()

PyObject * SG::PyProxyMgr::pyclid ( PyObject * tp)
inline

returns a borrowed reference

Definition at line 223 of file SgPyDataModel.h.

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 }
CLID clid(PyObject *tp)

◆ pyproxy()

PyProxyDict * SG::PyProxyMgr::pyproxy ( StoreGateSvc * sg)
inline

Definition at line 452 of file SgPyDataModel.h.

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 }

◆ pytp() [1/2]

PyObject * SG::PyProxyMgr::pytp ( CLID clid)
inline

returns a borrowed reference

Definition at line 269 of file SgPyDataModel.h.

270 {
271 PyObject* pyclid = PyLong_FromLong(clid);
272 PyObject* o = this->pytp(pyclid);
273 Py_DECREF(pyclid);
274 return o;
275 }
PyObject * pyclid(PyObject *tp)
returns a borrowed reference
PyObject * pytp(PyObject *clid)
returns a borrowed reference

◆ pytp() [2/2]

PyObject * SG::PyProxyMgr::pytp ( PyObject * clid)
inline

returns a borrowed reference

Definition at line 242 of file SgPyDataModel.h.

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 }

◆ typename_from_python()

std::string SG::PyProxyMgr::typename_from_python ( PyObject * tp) const
private

Extract a string from a python object and interpret it as a type name, performing any needed massaging.

Definition at line 217 of file SgPyDataModel.cxx.

218 {
219 std::string tpstr = RootUtils::PyGetString(tp).first;
220
221 // Remove defaulted template arguments; i.e., std::allocator from
222 // STL containers.
223 std::string tn = TClassEdit::ShortType(tpstr.c_str(),
224 TClassEdit::kDropAllDefault);
225
226 // With cppjit, the second template argument of DataVector doesn't
227 // get removed. Remove it here, else we won't be able to find the clid.
228 if (tn.starts_with ("DataVector<")) {
229 tn = CxxUtils::ClassName::applyRules (tn, m_dv_rules);
230 }
231 return tn;
232 }

Member Data Documentation

◆ m_aliases

PyObject* SG::PyProxyMgr::m_aliases

a dictionary of "typedef'ed typename" -> "typename"

Definition at line 200 of file SgPyDataModel.h.

◆ m_clidMap

ClidMap_t SG::PyProxyMgr::m_clidMap

Definition at line 210 of file SgPyDataModel.h.

◆ m_clids

PyObject* SG::PyProxyMgr::m_clids

a dictionary of 'typename' -> CLID (and reverse CLID->'typename')

Definition at line 203 of file SgPyDataModel.h.

◆ m_clidSvc

IClassIDSvc* SG::PyProxyMgr::m_clidSvc

Definition at line 198 of file SgPyDataModel.h.

◆ m_dictSvc

IDictLoaderSvc* SG::PyProxyMgr::m_dictSvc

Definition at line 197 of file SgPyDataModel.h.

◆ m_dv_rules

CxxUtils::ClassName::Rules SG::PyProxyMgr::m_dv_rules
private

Definition at line 383 of file SgPyDataModel.h.

◆ m_proxyMap

PyProxyMap_t SG::PyProxyMgr::m_proxyMap

Definition at line 206 of file SgPyDataModel.h.


The documentation for this struct was generated from the following files: