def from_func(obj): func = obj code = dis._get_code_object(obj) pysig = inspect.signature(obj) try: func_qualname = obj.__qualname__ except AttributeError: func_qualname = obj.__name__ self = bytecode.FunctionIdentity() self.func = func self.func_qualname = func_qualname self.func_name = func_qualname.split('.')[-1] self.code = code self.module = inspect.getmodule(func) self.modname = ('<dynamic>' if self.module is None else self.module.__name__) self.is_generator = inspect.isgeneratorfunction(func) self.pysig = pysig self.filename = code.co_filename self.firstlineno = code.co_firstlineno self.arg_names = list(pysig.parameters) uid = next(bytecode.FunctionIdentity._unique_ids) self.unique_name = '{}${}'.format(self.func_qualname, uid) return self
def check_lnotab(self, code): "Check that the lnotab byte offsets are sensible." code = dis._get_code_object(code) lnotab = list(dis.findlinestarts(code)) # Don't bother checking if the line info is sensible, because # most of the line info we can get at comes from lnotab. min_bytecode = min(t[0] for t in lnotab) max_bytecode = max(t[0] for t in lnotab) self.assertGreaterEqual(min_bytecode, 0) self.assertLess(max_bytecode, len(code.co_code))
def get_generated_code(fn): code = dis._get_code_object(fn) for name in code.co_names: if is_module(name): print(f"Found module -- {name}") else: print(f"Found non-module -- {name}") source_code = f""" {dis.code_info(fn)} {inspect.getsource(fn)} """ return source_code
def update_event(self, inp=-1): self.set_output_val(0, dis._get_code_object(self.input(0)))