def test_unsignedID(self): """ L{util.unsignedID} uses the function passed to L{util.setIDFunction} to determine the unique integer id of an object and then adjusts it to be positive if necessary. """ foo = object() bar = object() # A fake object identity mapping objects = {foo: 17, bar: -73} def fakeId(obj): return objects[obj] util.setIDFunction(fakeId) self.assertEqual(util.unsignedID(foo), 17) self.assertEqual(util.unsignedID(bar), (sys.maxsize + 1) * 2 - 73)
def _determineClassName(x): c = _determineClass(x) try: return c.__name__ except: try: return str(c) except: return '<BROKEN CLASS AT 0x%x>' % unsignedID(c)
def test_defaultIDFunction(self): """ L{util.unsignedID} uses the built in L{id} by default. """ obj = object() idValue = id(obj) if idValue < 0: idValue += (sys.maxsize + 1) * 2 self.assertEqual(util.unsignedID(obj), idValue)
def _safeFormat(formatter, o): """ Helper function for L{safe_repr} and L{safe_str}. """ try: return formatter(o) except: io = NativeStringIO() traceback.print_exc(file=io) className = _determineClassName(o) tbValue = io.getvalue() return "<%s instance at 0x%x with %s error:\n %s>" % ( className, unsignedID(o), formatter.__name__, tbValue)
def __str__(self): if self._str is not None: return self._str if hasattr(self, "func"): # This code should be replaced by a utility function in reflect; # see ticket #6066: if hasattr(self.func, "__qualname__"): func = self.func.__qualname__ elif hasattr(self.func, "__name__"): func = self.func.func_name if hasattr(self.func, "im_class"): func = self.func.im_class.__name__ + "." + func else: func = reflect.safe_repr(self.func) else: func = None now = self.seconds() L = [ "<DelayedCall 0x%x [%ss] called=%s cancelled=%s" % (unsignedID(self), self.time - now, self.called, self.cancelled) ] if func is not None: L.extend((" ", func, "(")) if self.args: L.append(", ".join([reflect.safe_repr(e) for e in self.args])) if self.kw: L.append(", ") if self.kw: L.append(", ".join(["%s=%s" % (k, reflect.safe_repr(v)) for (k, v) in self.kw.items()])) L.append(")") if self.debug: L.append("\n\ntraceback at creation: \n\n%s" % (" ".join(self.creator))) L.append(">") return "".join(L)
def __str__(self): if self._str is not None: return self._str if hasattr(self, 'func'): # This code should be replaced by a utility function in reflect; # see ticket #6066: if hasattr(self.func, '__qualname__'): func = self.func.__qualname__ elif hasattr(self.func, '__name__'): func = self.func.func_name if hasattr(self.func, 'im_class'): func = self.func.im_class.__name__ + '.' + func else: func = reflect.safe_repr(self.func) else: func = None now = self.seconds() L = ["<DelayedCall 0x%x [%ss] called=%s cancelled=%s" % ( unsignedID(self), self.time - now, self.called, self.cancelled)] if func is not None: L.extend((" ", func, "(")) if self.args: L.append(", ".join([reflect.safe_repr(e) for e in self.args])) if self.kw: L.append(", ") if self.kw: L.append(", ".join(['%s=%s' % (k, reflect.safe_repr(v)) for (k, v) in self.kw.items()])) L.append(")") if self.debug: L.append("\n\ntraceback at creation: \n\n%s" % (' '.join(self.creator))) L.append('>') return "".join(L)
def __repr__(self): s = '<%s to %s at %x>' % (self.__class__, self.addr, unsignedID(self)) return s