Commits

Anonymous committed a0ec6bb

support for (a few more) builtins in storegate + handle more std::string naming madness

  • Participants
  • Parent commits 9373683

Comments (0)

Files changed (3)

+2011-05-20  Sebastien Binet  <sebastien.binet@cern.ch>
+
+	* tagging StoreGateBindings-00-05-03
+	* support for (a few more) builtins in storegate
+	* handle more std::string naming madness
+	* M src/StoreGatePyExt.cxx
+	* M src/SgPyDataModel.h
+
 2011-05-19  Sebastien Binet  <sebastien.binet@cern.ch>
 
 	* tagging StoreGateBindings-00-05-03

File src/SgPyDataModel.h

 namespace SG { struct PyProxyMgr; }
 namespace SG { struct PyProxyDict; }
 
+namespace {
+  /** @brief replace all occurences of 'from' with 'to' in 's'
+   */
+  std::string::size_type 
+  cxx_replace(std::string& s,
+              const std::string& from,
+              const std::string& to)
+  {
+    std::string::size_type cnt(std::string::npos);
+
+    if(from != to && !from.empty()) {
+      std::string::size_type pos1(0);
+      std::string::size_type pos2(0);
+      const std::string::size_type from_len(from.size());
+      const std::string::size_type to_len(to.size());
+      cnt = 0;
+
+      while((pos1 = s.find(from, pos2)) != std::string::npos) {
+        s.replace(pos1, from_len, to);
+        pos2 = pos1 + to_len;
+        ++cnt;
+      }
+    }
+    return cnt;
+  }
+}
+
 namespace SG {
   
   /**
       if (i != m_clidMap.end()) {
         return i->second.c_str();
       }
-      m_clidMap[id] = m_dictSvc->load_type(id).Name(Reflex::SCOPED);
+      // Reflex now returns std::basic_string<char> instead of std::string
+      std::string tname = m_dictSvc->load_type(id).Name(Reflex::SCOPED);
+      // handle '> >'. order MATTERS!
+      ::cxx_replace(tname, "basic_string<char> >", "string>");
+      ::cxx_replace(tname, "basic_string<char>",   "string");
+      m_clidMap[id] = tname;
       return m_clidMap[id].c_str();
     }
 
       PyObject* obj = PyDict_GetItem( m_objProxies, proxy );
       if ( NULL == obj ) {
         if ( !(obj = TPython::ObjectProxy_FromVoidPtr((void*)addr, klass)) ) {
-          std::cerr << "--error-- could not create a pyobject...\n";
           throw PyROOT::TPyException();
         }
         PyDict_SetItem(m_objProxies, proxy, obj);

File src/StoreGatePyExt.cxx

 #include <algorithm> // for stable_partition
 
 namespace {
-  static CLID int_clid = ClassID_traits<int>::ID();
+  static CLID bool_clid = ClassID_traits<bool>::ID();
+  static CLID char_clid = ClassID_traits<char>::ID();
+  static CLID int_clid =  ClassID_traits<int>::ID();
   static CLID uint_clid = ClassID_traits<unsigned int>::ID();
   static CLID long_clid = ClassID_traits<long>::ID();
   static CLID ulong_clid= ClassID_traits<unsigned long>::ID();
+  static CLID longlong_clid= ClassID_traits<long long>::ID();
   static CLID float_clid= ClassID_traits<float>::ID();
   static CLID double_clid=ClassID_traits<double>::ID();
 }
   // unlikely to happen, but checking is cheap
   if ( ! store ) {
     PyErr_SetString( PyExc_RuntimeError, 
-		     "no store available, is Athena initialized?" );
+                     "no store available, is Athena initialized?" );
     return 0;
   }
 
   if ( ! PyType_Check( tp ) ) {
     if ( ! PyString_Check( tp ) ) {
       PyErr_SetString( PyExc_TypeError, 
-		       "retrieve() argument 1 must be type or class name" );
+                       "retrieve() argument 1 must be type or class name" );
       return 0;
     } else {
       Py_INCREF( tp );
     if ( pyname && ! PyString_Check( pyname ) ) {
       PyObject* pystr = PyObject_Str( pyname );
       if ( pystr ) {
-	Py_DECREF( pyname );
-	pyname = pystr;
+        Py_DECREF( pyname );
+        pyname = pystr;
       }
     }
 
 
   if ( pykey != Py_None && ! PyString_Check( pykey ) ) {
     PyErr_SetString( PyExc_TypeError, 
-		     "retrieve() argument 2 must be string key" );
+                     "retrieve() argument 2 must be string key" );
     return 0;
   }
 
   PyObject* pyclid = s_mgr.pyclid(pyname);
   if ( ! pyclid ) {
     PyErr_Format( PyExc_NameError, 
-		  "ID of \"%s\" is unknown", PyString_AS_STRING( pyname ) );
+                  "ID of \"%s\" is unknown", PyString_AS_STRING( pyname ) );
     return 0;
   }
 
   PyObject* pyproxy = proxyDict->proxy(pyclid, pykey);
   if ( ! pyproxy ) {
     PyErr_Format( PyExc_LookupError, 
-		  "no py-proxies for (clid=%lu, type=%s, key=%s)", 
-		  PyLong_AsUnsignedLong(pyclid),
-		  PyString_AS_STRING( pyname ),
-		  (char*)( (pykey == Py_None) 
-			   ? "<None>" 
-			   : PyString_AS_STRING(pykey) )
-		  );
+                  "no py-proxies for (clid=%lu, type=%s, key=%s)", 
+                  PyLong_AsUnsignedLong(pyclid),
+                  PyString_AS_STRING( pyname ),
+                  (char*)( (pykey == Py_None) 
+                           ? "<None>" 
+                           : PyString_AS_STRING(pykey) )
+                  );
     return 0;
   }
 
   _SGPY_MSG("retrieved py-proxy [clid=" << PyLong_AsUnsignedLong(pyclid)
-	    << ", type=" << PyString_AS_STRING( pyname )
-	    << ", key=" << (char*)( (pykey == Py_None) 
-				    ? "<None>" 
-				    : PyString_AS_STRING(pykey) )
-	    << "]");
+            << ", type=" << PyString_AS_STRING( pyname )
+            << ", key=" << (char*)( (pykey == Py_None) 
+                                    ? "<None>" 
+                                    : PyString_AS_STRING(pykey) )
+            << "]");
 
   SG::DataProxy* proxy = (SG::DataProxy*)ObjectProxy_ASVOIDPTR(pyproxy);
 
   if ( ! proxy ) {
     PyErr_Format( PyExc_LookupError, 
-		  "no proxies for (clid=%lu, type=%s, key=%s)", 
-		  PyLong_AsUnsignedLong(pyclid),
-		  PyString_AS_STRING( pyname ),
-		  (char*)( (pykey == Py_None) 
-			   ? "<None>" 
-			   : PyString_AS_STRING(pykey) )
-		  );
+                  "no proxies for (clid=%lu, type=%s, key=%s)", 
+                  PyLong_AsUnsignedLong(pyclid),
+                  PyString_AS_STRING( pyname ),
+                  (char*)( (pykey == Py_None) 
+                           ? "<None>" 
+                           : PyString_AS_STRING(pykey) )
+                  );
     return 0;
   }
 
   _SGPY_MSG("retrieved cpp-proxy [clid=" << proxy->clID()
-	    << ", key=" << proxy->name() << "]");
+            << ", key=" << proxy->name() << "]");
 
   // cast proxy to pointer type if needed (setting on return type is evil hack)
   DataObject* dobj = proxy->accessData();
   if ( ! dobj ) {
     PyErr_Format( PyExc_LookupError, 
-		  "no such object \"%s\"", PyString_AS_STRING( pyname ) );
+                  "no such object \"%s\"", PyString_AS_STRING( pyname ) );
     return 0;
   }
 
   _SGPY_MSG("retrieved dobj [clID=" << dobj->clID()
-	    << ", classID=" << dobj->classID()
-	    << ", name=" << dobj->name() << "]");
+            << ", classID=" << dobj->classID()
+            << ", name=" << dobj->name() << "]");
 
   DataBucketBase* dbb = dynamic_cast< DataBucketBase* >( dobj );
   if ( ! dbb ) {
     PyErr_SetString
       ( PyExc_TypeError,
-	"unexpected kind of DataObject: can not verify final type" );
+        "unexpected kind of DataObject: can not verify final type" );
     return 0;
   }
 
   _SGPY_MSG("retrieved dbb [clID=" << dbb->clID()
-	    << ", classID=" << dbb->classID()
-	    << ", name=" << dbb->name() << "]");
+            << ", classID=" << dbb->classID()
+            << ", name=" << dbb->name() << "]");
 
   CLID id     = proxy->clID();
   CLID realID = dbb->clID();
 
   _SGPY_MSG("pyid= " << PyLong_AsUnsignedLong(pyclid)
-	    << ", id=" << id
-	    << ", realID=" << realID);
+            << ", id=" << id
+            << ", realID=" << realID);
 
   // special case of a regular PyObject
   if ( PyCLID == realID ) {
     objProxy = (PyObject*)dbb->object();
 
+  } else if (realID == bool_clid) {
+    res = dbb->cast( bool_clid );
+    bool *v = reinterpret_cast<bool*>(res);
+    objProxy = PyBool_FromLong(*v);
+    return objProxy;
+
+  } else if (realID == char_clid) {
+    res = dbb->cast( char_clid );
+    char *v = reinterpret_cast<char*>(res);
+    objProxy = PyString_FromStringAndSize(v, 1);
+    return objProxy;
+
   } else if (realID == int_clid) {
-    res = dbb->cast( uint_clid );
+    res = dbb->cast( int_clid );
     int *v = reinterpret_cast<int*>(res);
     objProxy = PyInt_FromLong(*v);
     return objProxy;
     objProxy = PyLong_FromUnsignedLong(*v);
     return objProxy;
     
+  } else if (realID == longlong_clid) {
+    res = dbb->cast( longlong_clid );
+    long long *v =reinterpret_cast<long long*>(res);
+    objProxy = PyLong_FromUnsignedLong(*v);
+    return objProxy;
+    
   } else if (realID == float_clid) {
     res = dbb->cast( float_clid );
     float *v =reinterpret_cast<float*>(res);
     res = dbb->object();
     
     _SGPY_MSG("pyid= " << PyLong_AsUnsignedLong(pyclid)
-	      << ", id=" << id
-	      << ", realID=" << realID << " => res = [" << res << "]");
+              << ", id=" << id
+              << ", realID=" << realID << " => res = [" << res << "]");
 
     if ( ! res ) {
       PyErr_Format( PyExc_RuntimeError, "found an invalid object" );
     
     const char* klass_name = s_mgr.load_type(id);
     _SGPY_MSG("retrieving objProxy(" << klass_name
-	      << ", " << res << ")...");
+              << ", " << res << ")...");
     objProxy = proxyDict->dataObject(pyproxy, klass_name, res);
     _SGPY_MSG("retrieving objProxy(" << klass_name
-	      << ", " << res << ")... [done]");
+              << ", " << res << ")... [done]");
 
   } else {
     // either use CLID BaseInfo<> or Reflex, try both as appropriate
     if ( res ) {
 
       objProxy= proxyDict->dataObject(pyproxy, 
-				      real_typename,
-				      res);
+                                      real_typename,
+                                      res);
     } else {
       // -> try Reflex...
       IClassIDSvc* clidSvc = s_mgr.m_clidSvc;
       std::string realName = "";
       if ( !clidSvc->getTypeNameOfID(realID, realName).isSuccess() ) {
-	PyErr_Format( PyExc_TypeError, "actual type of CLID %lu unknown",
-		      (long unsigned int)realID );
-	return 0;
+        PyErr_Format( PyExc_TypeError, "actual type of CLID %lu unknown",
+                      (long unsigned int)realID );
+        return 0;
       }
       
       const ROOT::Reflex::Type& fromType = ROOT::Reflex::Type::ByName(realName);
       
       if ( (bool)fromType ) {
-	ROOT::Reflex::Object realobj( fromType, dbb->object() );
+        ROOT::Reflex::Object realobj( fromType, dbb->object() );
 	
- 	const ROOT::Reflex::Type& toType = ROOT::Reflex::Type::ByName( PyString_AS_STRING( pyname ) );
- 	const ROOT::Reflex::Object& finalobj = fromType.CastObject( toType, realobj );
+        const ROOT::Reflex::Type& toType = ROOT::Reflex::Type::ByName( PyString_AS_STRING( pyname ) );
+        const ROOT::Reflex::Object& finalobj = fromType.CastObject( toType, realobj );
 	
- 	res = (void*)finalobj.Address();
- 	if ( res ) {
- 	  objProxy = proxyDict->dataObject(pyproxy, 
-					   realName.c_str(),
-					   res);
- 	}
+        res = (void*)finalobj.Address();
+        if ( res ) {
+          objProxy = proxyDict->dataObject(pyproxy, 
+                                           realName.c_str(),
+                                           res);
+        }
       }
     }
 
 
 PyObject* 
 AthenaInternal::py_sg_contains (PyObject* self,
-				PyObject* tp, PyObject* pykey)
+                                PyObject* tp, PyObject* pykey)
 {
   static SG::PyProxyMgr& s_mgr = SG::PyProxyMgr::instance();
 
   // unlikely to happen, but checking is cheap
   if ( ! store ) {
     PyErr_SetString( PyExc_RuntimeError, 
-		     "no store available, is Athena initialized?" );
+                     "no store available, is Athena initialized?" );
     return 0;
   }
 
   if ( ! PyType_Check( tp ) ) {
     if ( ! PyString_Check( tp ) ) {
       PyErr_SetString( PyExc_TypeError, 
-		       "contains() argument 1 must be type or class name" );
+                       "contains() argument 1 must be type or class name" );
       return 0;
     } else {
       Py_INCREF( tp );
     if ( pyname && ! PyString_Check( pyname ) ) {
       PyObject* pystr = PyObject_Str( pyname );
       if ( pystr ) {
-	Py_DECREF( pyname );
-	pyname = pystr;
+        Py_DECREF( pyname );
+        pyname = pystr;
       }
     }
 
 
   if ( pykey != Py_None && ! PyString_Check( pykey ) ) {
     PyErr_SetString( PyExc_TypeError, 
-		     "contains() argument 2 must be string key" );
+                     "contains() argument 2 must be string key" );
     return 0;
   }
 
   const CLID clid = s_mgr.clid (pyname);
   if ( clid == CLID_NULL ) {
     PyErr_Format( PyExc_NameError, 
-		  "ID of \"%s\" is unknown", PyString_AS_STRING( pyname ) );
+                  "ID of \"%s\" is unknown", PyString_AS_STRING( pyname ) );
     return 0;
   }
 
     store->contains (clid, PyString_AS_STRING (pykey)) ;
 
   _SGPY_MSG("sg::contains(" << clid
-	    << "/" << PyString_AS_STRING( pyname ) << ", "
-	    << PyString_AS_STRING (pykey)
-	    << ") = [" 
-	    << (sg_contains ? std::string("true") : std::string("false"))
-	    << "]");
+            << "/" << PyString_AS_STRING( pyname ) << ", "
+            << PyString_AS_STRING (pykey)
+            << ") = [" 
+            << (sg_contains ? std::string("true") : std::string("false"))
+            << "]");
 
   PyObject* o = sg_contains
     ? Py_True
   // unlikely to happen, but checking is cheap
   if ( ! store ) {
     PyErr_SetString( PyExc_RuntimeError, 
-		     "no store available, is Athena initialized?" );
+                     "no store available, is Athena initialized?" );
     return 0;
   }
 
   if ( ! PyString_Check( pykey ) ) {
     PyErr_SetString( PyExc_TypeError, 
-		     "__getitem__() argument 1 must be string key" );
+                     "__getitem__() argument 1 must be string key" );
     return 0;
   }
 
   const CLID clid = store->clid (PyString_AS_STRING (pykey));
   if ( CLID_NULL == clid ) {
     PyErr_Format (PyExc_LookupError, 
-		  "no clid for key=%s", 
-		  PyString_AS_STRING(pykey));
+                  "no clid for key=%s", 
+                  PyString_AS_STRING(pykey));
     return 0;
   }
   
   return AthenaInternal::retrieveObjectFromStore (self, 
-						  s_mgr.pytp(clid), 
-						  pykey);
+                                                  s_mgr.pytp(clid), 
+                                                  pykey);
 }
 
 void 
   // unlikely to happen, but checking is cheap
   if ( ! store ) {
     PyErr_SetString( PyExc_RuntimeError, 
-		     "no store available, is Athena initialized?" );
+                     "no store available, is Athena initialized?" );
     return 0;
   }
 
   PyObject* tp = PyObject_GetAttrString( obj, (char*)"__class__" );
   if ( ! PyType_Check( tp ) ) {
     PyErr_SetString( PyExc_RuntimeError, 
-		     "could not retrieve type of object" );
+                     "could not retrieve type of object" );
     return 0;
   }
 
     if ( pyname && ! PyString_Check( pyname ) ) {
       PyObject* pystr = PyObject_Str( pyname );
       if ( pystr ) {
-	Py_DECREF( pyname );
-	pyname = pystr;
+        Py_DECREF( pyname );
+        pyname = pystr;
       }
     }
   }
     
   if ( ! PyString_Check( pykey ) ) {
     PyErr_SetString( PyExc_TypeError, 
-		     "record() argument 2 must be string key" );
+                     "record() argument 2 must be string key" );
     return 0;
   }
   
   const CLID id = s_mgr.clid(pyname);
   if ( CLID_NULL == id ) {
     PyErr_Format( PyExc_NameError, 
-		  "ID of \"%s\" is unknown", PyString_AS_STRING( pyname ) );
+                  "ID of \"%s\" is unknown", PyString_AS_STRING( pyname ) );
     return 0;
   }
   
   s_mgr.load_type(id);
 
   _SGPY_MSG("ID=" << id
-	    << ", tp=" << PyString_AS_STRING (pyname)
-	    << ", key=" << PyString_AS_STRING (pykey));
+            << ", tp=" << PyString_AS_STRING (pyname)
+            << ", key=" << PyString_AS_STRING (pykey));
 
   PyROOT::ObjectProxy* pyRootObj = (PyROOT::ObjectProxy*)obj;
 
   SG::PyDataBucket* dbb = new SG::PyDataBucket (obj, id);
 
   _SGPY_MSG("created a pdb @" << dbb << ", clID=" << dbb->clID()
-	    << ", pdb-obj @" << dbb->object()
-	    << ", obj @" << (void*)obj
-	    << ", cc: " << ObjectProxy_ASVOIDPTR(obj)
-	    << ", isplain-pyobj: [" << (int)isPlainPyObj << "]"
-	    << ", pyrobj @" << pyRootObj->GetObject());
+            << ", pdb-obj @" << dbb->object()
+            << ", obj @" << (void*)obj
+            << ", cc: " << ObjectProxy_ASVOIDPTR(obj)
+            << ", isplain-pyobj: [" << (int)isPlainPyObj << "]"
+            << ", pyrobj @" << pyRootObj->GetObject());
   _SGPY_MSG("pdb-cast(" << dbb->clID() << "): " << dbb->cast(dbb->clID()));
 	    
   static bool allowMods = true;
   int sc = store->typeless_record( dbb, PyString_AS_STRING(pykey),
-				   isPlainPyObj 
-				   ? (void*)obj
-				   : pyRootObj->GetObject(),
-				   allowMods ).isSuccess()
+                                   isPlainPyObj 
+                                   ? (void*)obj
+                                   : pyRootObj->GetObject(),
+                                   allowMods ).isSuccess()
     ? 1
     : 0; 
   return Py_BuildValue( const_cast<char*>("i"), sc );