def test_annotate_weakref(self): a = set() ref = weakref.ref(a) self.assertEqual( object_annotation(ref), "weakref to id 0x{:x}".format(id(a)), )
def test_annotate_weakref_to_dead_referent(self): a = set() ref = weakref.ref(a) del a self.assertEqual( object_annotation(ref), "weakref (dead referent)", )
def test_annotate_frame(self): def some_function(x, y): z = 27 pow(z, z) return inspect.currentframe() frame = some_function("a string", 97.8) annotation = object_annotation(frame) self.assertTrue(annotation.startswith("frame\\n")) self.assertIn("test_annotations", annotation)
def test_annotate_instancemethod_without_class(self): # In Python 2, it's possible to create bound methods # without an im_class attribute. def my_method(self): return 42 method = types.MethodType(my_method, NewStyle()) self.assertEqual( object_annotation(method), "instancemethod\\n<None>.my_method", )
def annotated(self): """ Annotate this graph, returning an AnnotatedGraph object with the same structure. """ # Build up dictionary of edge annotations. edge_annotations = {} for edge in self.edges: if edge not in edge_annotations: # We annotate all edges from a given object at once. referrer = self._tail[edge] known_refs = annotated_references(referrer) for out_edge in self._out_edges[referrer]: referent = self._head[out_edge] if known_refs[referent]: annotation = known_refs[referent].pop() else: annotation = None edge_annotations[out_edge] = annotation annotated_vertices = [ AnnotatedVertex( id=id(vertex), annotation=object_annotation(vertex), ) for vertex in self.vertices ] annotated_edges = [ AnnotatedEdge( id=edge, annotation=edge_annotations[edge], head=id(self._head[edge]), tail=id(self._tail[edge]), ) for edge in self.edges ] return AnnotatedGraph( vertices=annotated_vertices, edges=annotated_edges, )
def test_annotate_instancemethod_with_nameless_function(self): # Regression test for mdickinson/refcycle#25. # Create a nameless function: comparison_function.__name__ # raise AttributeError. comparison_function_type = ctypes.CFUNCTYPE( ctypes.c_int, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int), ) comparison_function = comparison_function_type(lambda a, b: 0) method = types.MethodType(comparison_function, NewStyle()) if six.PY2: expected = "instancemethod\\n<None>.<anonymous>" else: expected = "instancemethod\\n<anonymous>" self.assertEqual(object_annotation(method), expected)
def test_annotate_dict(self): d = {1: 2, 3: 4, 5: 6} self.assertEqual( object_annotation(d), "dict[3]", )
def test_bool(self): x = True self.assertEqual( object_annotation(x), "True", )
def test_annotate_bound_method(self): method = NewStyle().foo self.assertEqual( object_annotation(method), "instancemethod\\nNewStyle.foo", )
def test_int(self): x = 12345 self.assertEqual( object_annotation(x), "12345", )
def test_annotate_old_style_object(self): obj = OldStyle() self.assertEqual( object_annotation(obj), "instance\\nOldStyle", )
def test_annotate_new_style_class(self): self.assertEqual( object_annotation(NewStyle), "type\\nNewStyle", )
def test_none(self): x = None self.assertEqual( object_annotation(x), "None", )
def test_annotate_object(self): obj = NewStyle() self.assertEqual( object_annotation(obj), "object\\nrefcycle.test.test_annotations.NewStyle", )
def test_float(self): x = 12345.0 self.assertEqual( object_annotation(x), "12345.0", )
def test_annotate_tuple(self): t = (1, 2, 3) self.assertEqual( object_annotation(t), "tuple[3]", )
def test_annotate_list(self): l = [1, 2] self.assertEqual( object_annotation(l), "list[2]", )
def test_str(self): x = "hello world" self.assertEqual( object_annotation(x), repr(x), )
def test_complex(self): x = 2j self.assertEqual( object_annotation(x), repr(x), )
def test_annotate_module(self): annotation = object_annotation(weakref) self.assertTrue(annotation.startswith("module\\n")) self.assertIn("weakref", annotation)
def test_annotate_function(self): self.assertEqual( object_annotation(f), "function\\nf", )