def object2id(obj): """Convert object to string that can be used as an identifier. Generator objects that were never activated get a generic name... >>> def producer(): ... yield 1 >>> gobject = GeneratorObject(producer()) >>> object2id(gobject) 'generator' ...but if we have a matching definition, the name is based on it. >>> definition = Function('producer', is_generator=True) >>> gobject.activate(definition, {}, definition) >>> object2id(gobject) 'producer_instance' Accepts only SerializedObjects as arguments. >>> object2id(42) Traceback (most recent call last): ... TypeError: object2id() should be called with a SerializedObject argument, not 42 """ assert_argument_type(obj, SerializedObject) if isinstance(obj, GeneratorObject) and obj.is_activated(): return "%s_instance" % obj.definition.name else: return obj.human_readable_id
def remove_call_from_call_graph(self, call_to_remove): assert_argument_type(call_to_remove, Call) def remove(calls): try: calls.remove(call_to_remove) return True except ValueError: for call in calls: if remove(call.subcalls): return True remove(self.call_graph)
def activate(self, generator, args, callable): assert_argument_type(generator, (Function, Method)) assert_argument_type(callable, (Function, UserObject)) if self.is_activated(): raise ValueError("This generator has already been activated.") if not generator.is_generator: raise TypeError("Tried to activate GeneratorObject with %r as a generator definition." % generator) self.definition = generator self.args = args # Finally register this GeneratorObject with its callable context # (which will be a Function or an UserObject). This has to be # done only once for each GeneratorObject. callable.add_call(self)
def activate(self, generator, args, callable): assert_argument_type(generator, (Function, Method)) assert_argument_type(callable, (Function, UserObject)) if self.is_activated(): raise ValueError("This generator has already been activated.") if not generator.is_generator: raise TypeError( "Tried to activate GeneratorObject with %r as a generator definition." % generator) self.definition = generator self.args = args # Finally register this GeneratorObject with its callable context # (which will be a Function or an UserObject). This has to be # done only once for each GeneratorObject. callable.add_call(self)
def add_call(self, call): assert_argument_type(call, self.calls_type) self.calls.append(call)
def generator_object_yields(gobject): assert_argument_type(gobject, (GeneratorObject, MethodCallContext)) return [c.output for c in gobject.calls]
def generator_object_yields(gobject): assert_argument_type(gobject, GeneratorObject) return [c.output for c in gobject.calls]
def generator_object_exception(gobject): assert_argument_type(gobject, GeneratorObject) for call in gobject.calls: if call.raised_exception(): return call.exception