def __setitem__(self,key,arg): SHMOBJ.protect(self) if not type(key) in [str, SHMSTR]: raise TypeError("** error: SHMDCT.__setitem__ only takes SHMSTR or str for key") if not isinstance(arg, SHMOBJ): raise TypeError("** invalid type being set in shmdict") datatype, datalen = get_type_and_len(arg) np = py2shmobj.shmdict_lookup(self.addr, str(key)) if np: py2shmobj.shmdnode_put(np,arg.get_ptr(),datatype,datalen) else: if not isinstance(key, SHMSTR): raise TypeError("** error: SHMDCT.__setitem__ only takes SHMSTR when key is new") dnode = py2shmobj.shmdnode_alloc() if dnode is None: raise Exception('shmdnode_alloc failed: out of memory') py2shmobj.shmdnode_init(dnode, arg.get_ptr(), datatype, datalen) py2shmobj.shmdict_insert(self.addr, dnode, key.get_ptr())
def __init__(self,arg,internal=False): SHMOBJ.__init__(self, arg, internal) if type(arg) in [dict, SHMDCT]: self.addr = py2shmobj.shmdict_init() for (k,e) in arg.items(): e = arg[k] if not isinstance(k,SHMSTR): raise TypeError("**error: keys may only be SHMSTR at this time") if not isinstance(e, SHMOBJ): raise TypeError("** invalid type being inserted in shmdict") datatype, datalen = get_type_and_len(e) dnode = py2shmobj.shmdnode_alloc() if dnode is None: raise Exception('shmdnode_alloc failed: out of memory') py2shmobj.shmdnode_init(dnode, e.get_ptr(), datatype, datalen) py2shmobj.shmdict_insert(self.addr, dnode, k.get_ptr()) elif self.addr is None: raise TypeError("invalid type %s assigned to SHMDCT" % type(arg))