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

Detailed Description

A helper class to manage accesses to PyProxies.

Definition at line 194 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 208 of file SgPyDataModel.h.

◆ PyProxyMap_t

Definition at line 204 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 }
AthROOTErrorHandlerSvc * svc
PyObject * importDictAliases()
import the dictionary of aliases from a well known location
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 193 of file SgPyDataModel.cxx.

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

Member Function Documentation

◆ clid()

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

Definition at line 277 of file SgPyDataModel.h.

278 {
279 CLID id = CLID_NULL;
280 // FIXME: get rid of this massaging when/if ROOT finally
281 // standardize on keeping the std:: namespace...
282 std::string tpstr = RootUtils::PyGetString(tp).first;
283 std::string tn;
284 {
285 // Protect against data race inside TClassEdit.
286 // https://github.com/root-project/root/issues/10353
287 // Should be fixed in root 6.26.02.
288 R__WRITE_LOCKGUARD(ROOT::gCoreMutex);
289 tn = TClassEdit::ShortType(tpstr.c_str(),
290 TClassEdit::kDropAllDefault);
291 }
292 m_clidSvc->getIDOfTypeName(tn, id).ignore();
293 if ( id == CLID_NULL ) {
294 // try an alias...
295 PyObject* alias = PyDict_GetItemString(m_aliases, tn.c_str());
296 if ( alias ){
297 std::string aliasstr = RootUtils::PyGetString(alias).first;
298 m_clidSvc->getIDOfTypeName(aliasstr, id).ignore();
299 }
300 }
301 if (id == CLID_NULL) {
302 // then try a type-id name...
303 return this->clid_from_tid (tp);
304 }
305 return id;
306 }
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)

◆ clid_from_tid()

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

Definition at line 309 of file SgPyDataModel.h.

310 {
311 CLID id = CLID_NULL;
312 // FIXME: get rid of this massaging when/if ROOT finally
313 // standardize on keeping the std:: namespace...
314 std::string tpstr = RootUtils::PyGetString(tp).first;
315 std::string tn;
316 {
317 // Protect against data race inside TClassEdit.
318 // https://github.com/root-project/root/issues/10353
319 // Should be fixed in root 6.26.02.
320 R__WRITE_LOCKGUARD(ROOT::gCoreMutex);
321 tn = TClassEdit::ShortType(tpstr.c_str(),
322 TClassEdit::kDropAllDefault);
323 }
324 m_clidSvc->getIDOfTypeInfoName(tn, id).ignore();
325 if ( id == CLID_NULL ) {
326 // try an alias...
327 PyObject* alias = PyDict_GetItemString(m_aliases, tn.c_str());
328 if ( alias ){
329 std::string aliasstr = RootUtils::PyGetString(alias).first;
330 m_clidSvc->getIDOfTypeInfoName(aliasstr,
331 id).ignore();
332 }
333 }
334 return id;
335 }

◆ importDictAliases()

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

import the dictionary of aliases from a well known location

Definition at line 372 of file SgPyDataModel.h.

373 {
374 const std::string moduleName = "AthenaPython.Bindings";
375 PyObject* module = PyImport_ImportModule
376 ( const_cast<char*>(moduleName.c_str()) );
377 if ( !module || !PyModule_Check(module) ) {
378 std::cerr << "SG::PyProxyDict WARNING: could not import module '"
379 << moduleName << "' !\n";
380 Py_XDECREF(module);
381 m_aliases = PyDict_New();
382 return m_aliases;
383 }
384
385 m_aliases = PyDict_GetItemString(PyModule_GetDict(module),
386 (char*)"_clid_typename_aliases");
387 // borrowed ref. so we increment it
388 Py_XINCREF(m_aliases);
389 // don't need this one anymore
390 Py_DECREF(module);
391
392 if ( !m_aliases ) {
393 std::cerr << "SG::PyProxyDict WARNING: could not retrieve the "
394 << "dictionary of aliases from '"
395 << moduleName << "' !\n";
396 Py_XDECREF(m_aliases);
397 m_aliases = PyDict_New();
398 }
399 return m_aliases;
400 }

◆ instance()

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

Definition at line 212 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 342 of file SgPyDataModel.h.

343 {
344 typedef ClidMap_t::iterator Itr_t;
345 Itr_t i = m_clidMap.find(id);
346 if (i != m_clidMap.end()) {
347 return i->second.c_str();
348 }
349 // Reflex now returns std::basic_string<char> instead of std::string
350 std::string tname = m_dictSvc->load_type(id).Name(Reflex::SCOPED);
351 // handle '> >'. order MATTERS!
352 ::cxx_replace(tname, "basic_string<char> >", "string>");
353 ::cxx_replace(tname, "basic_string<char>", "string");
354 m_clidMap[id] = tname;
355
356 // Try to make sure the BIB is initialized.
357 std::string bibname = "SG::BaseInfo<" + tname + ">";
358 TClass* bibcl = gROOT->GetClass (bibname.c_str());
359 if (bibcl) {
360 TMethod* m = bibcl->GetMethodAny ("baseinfo");
361 if (m) {
362 TMethodCall call (m);
363 call.Execute();
364 }
365 }
366
367 return m_clidMap[id].c_str();
368 }
@ 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 222 of file SgPyDataModel.h.

223 {
224 PyObject* clid = PyDict_GetItem(m_clids, tp);
225 if ( NULL == clid ) {
226 const CLID id = this->clid(tp);
227 if ( id == CLID_NULL ) {
228 return NULL;
229 }
230 clid = PyLong_FromLong(id);
231 PyDict_SetItem(m_clids, tp, clid);
232 // add reverse look-up entry too
233 PyDict_SetItem(m_clids, clid, tp);
234 //Py_INCREF(clid);
235 }
236 return clid;
237 }
CLID clid(PyObject *tp)

◆ pyproxy()

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

Definition at line 469 of file SgPyDataModel.h.

470 {
471 SG::PyProxyDict* p = 0;
472 PyProxyMap_t::iterator i = m_proxyMap.find(sg);
473 if ( i == m_proxyMap.end() ) {
474 m_proxyMap[sg] = p = new SG::PyProxyDict(sg);
475 } else {
476 p = i->second;
477 }
478 return p;
479 }

◆ pytp() [1/2]

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

returns a borrowed reference

Definition at line 268 of file SgPyDataModel.h.

269 {
270 PyObject* pyclid = PyLong_FromLong(clid);
271 PyObject* o = this->pytp(pyclid);
272 Py_DECREF(pyclid);
273 return o;
274 }
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 241 of file SgPyDataModel.h.

242 {
243 PyObject* tp = PyDict_GetItem(m_clids, clid);
244 if ( NULL == tp ) {
245 std::string cpp_tp;
246 if (!m_clidSvc->getTypeNameOfID(PyLong_AsUnsignedLong(clid),
247 cpp_tp).isSuccess()) {
248 return NULL;
249 }
250 PyObject* alias = PyDict_GetItemString(m_aliases, cpp_tp.c_str());
251 if ( alias ) {
252 tp = alias;
253 } else {
254 tp = PyUnicode_FromString(cpp_tp.c_str());
255 }
256 PyDict_SetItem(m_clids, clid, tp);
257 // reverse look-up
258 PyDict_SetItem(m_clids, tp, clid);
259 if (!alias) {
260 Py_DECREF(tp);
261 }
262 }
263 return tp;
264 }

Member Data Documentation

◆ m_aliases

PyObject* SG::PyProxyMgr::m_aliases

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

Definition at line 199 of file SgPyDataModel.h.

◆ m_clidMap

ClidMap_t SG::PyProxyMgr::m_clidMap

Definition at line 209 of file SgPyDataModel.h.

◆ m_clids

PyObject* SG::PyProxyMgr::m_clids

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

Definition at line 202 of file SgPyDataModel.h.

◆ m_clidSvc

IClassIDSvc* SG::PyProxyMgr::m_clidSvc

Definition at line 197 of file SgPyDataModel.h.

◆ m_dictSvc

IDictLoaderSvc* SG::PyProxyMgr::m_dictSvc

Definition at line 196 of file SgPyDataModel.h.

◆ m_proxyMap

PyProxyMap_t SG::PyProxyMgr::m_proxyMap

Definition at line 205 of file SgPyDataModel.h.


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