def BUILD_CLASS(self, instr): call_func = self.ast_stack.pop() assert isinstance(call_func, _ast.Call) func = call_func.func assert isinstance(func, _ast.FunctionDef) code = func.body pop_assignment(code, '__module__') doc = pop_doc(code) ret = code.pop() assert isinstance(ret, _ast.Return) and ret.value == 'LOAD_LOCALS' bases = self.ast_stack.pop() assert isinstance(bases, _ast.Tuple) bases = bases.elts name = self.ast_stack.pop() class_ = _ast.ClassDef(name=name, bases=bases, body=code, decorator_list=[], lineno=instr.lineno, col_offset=0) self.ast_stack.append(class_)
def inherited_class_structure(self, items): (name, bases, body) = items return _ast.ClassDef(name=name, body=body, decorator_list=[], bases=[bases])
def class_def_stmt(name: str, bases: List[_ast.expr], keywords: List[_ast.keyword], body: List[_ast.stmt], decorator_list: List[_ast.expr]) -> _ast.ClassDef: return _ast.ClassDef(name=name, bases=bases, keywords=keywords, body=body, decorator_list=decorator_list)
def LOAD_BUILD_CLASS(self, instr): class_body = [] body_instr = instr while body_instr.opname not in function_ops: body_instr = self.ilst.pop(0) class_body.append(body_instr) call_func = self.decompile_block(class_body, stack_items=[None]).stmnt() assert len(call_func) == 1 call_func = call_func[0] func_def = call_func.args[0] code = func_def.body name = call_func.args[1].s bases = call_func.args[2:] keywords = call_func.keywords kwargs = call_func.kwargs starargs = call_func.starargs if isinstance(code[0], _ast.Expr): _name = code.pop(1) _doc = code.pop(1) elif isinstance(code[0], _ast.Assign): _name = code.pop(0) else: assert False ret = code.pop(-1) assert isinstance(ret, _ast.Return) class_ = _ast.ClassDef( name=name, bases=bases, body=code, decorator_list=[], kwargs=kwargs, keywords=keywords, starargs=starargs, lineno=instr.lineno, col_offset=0, ) self.ast_stack.append(class_)
def ClassDef(name, bases=(), body=None, decorator_list=()): """Creates an _ast.ClassDef node. Args: name: The name of the class. bases: The base classes of the class body: A list of _ast.stmt nodes that go in the body of the class. decorator_list: A list of decorator nodes. Raises: ValueError: If some body element is not an _ast.stmt node. Returns: An _ast.ClassDef node. """ body = FormatAndValidateBody(body) bases = [_WrapWithName(base, ctx_type=CtxEnum.LOAD) for base in bases] return _ast.ClassDef(name=name, bases=bases, body=body, decorator_list=list(decorator_list))
class ConstructorFixture: constructor_without_params = _ast.ClassDef( name='Test', body=[ _ast.FunctionDef( name='__init__', body=[_ast.Pass()], ), ], bases=None, ) empty_class = _ast.ClassDef( name='Test', body=[], bases=None, ) class_without_constructor = _ast.ClassDef( name='Test', body=[ _ast.FunctionDef( name='some_function', body=[_ast.Pass()], ), ], bases=None, ) class_with_constructor_and_additional_method = _ast.ClassDef( name='Test', body=[ _ast.FunctionDef( name='some_function', body=[_ast.Pass()], ), _ast.FunctionDef( name='__init__', body=[_ast.Pass()], ), ], bases=None, ) class_with_inheritance = _ast.ClassDef( name='Test', body=[], bases=[_ast.ClassDef(name='TestParent')], )