def build_param(ctx, py_arg, self_name, kwarg_only): # NB: In Python3 py_arg is a pair of (str arg, expr? annotation) name = py_arg.arg r = ctx.make_range(py_arg.lineno, py_arg.col_offset, py_arg.col_offset + len(name)) if getattr(py_arg, 'annotation', None) is not None: annotation_expr = build_expr(ctx, py_arg.annotation) elif self_name is not None and name == 'self': annotation_expr = Var(Ident(r, self_name)) else: annotation_expr = EmptyTypeAnnotation(r) return Param(annotation_expr, Ident(r, name), kwarg_only)
def get_class_properties(cls, self_name): """ Get a list of Property objects representing the properties of a class. Args: cls: The class to get properties of. self_name: The name of the class that the properties should belong to. Returns: A list of Property objects corresponding to the properties of cls. Property here refers to the subclass of TreeView. """ props = inspect.getmembers(cls, predicate=lambda m: isinstance(m, property)) # Any property that should not compiled must be in this list on the Module. unused_properties = getattr(cls, "__jit_unused_properties__", []) # Create Property TreeView objects from inspected property objects. properties = [] for prop in props: if prop[0] not in unused_properties and not should_drop(prop[1].fget): getter = get_jit_def(prop[1].fget, f"__{prop[0]}_getter", self_name=self_name) setter = get_jit_def(prop[1].fset, f"__{prop[0]}_setter", self_name=self_name) if prop[1].fset else None properties.append( Property(getter.range(), Ident(getter.range(), prop[0]), getter, setter)) return properties
def get_class_properties(cls, self_name): """ Get a list of Property objects representing the properties of a class. Arguments: cls: The class to get properties of. self_name: The name of the class that the properties should belong to. Returns: A list of Property objects corresponding to the properties of cls. Property here refers to the subclass of TreeView. """ props = inspect.getmembers(cls, predicate=lambda m: isinstance(m, property)) # Create Property TreeView objects from inspected property objects. properties = [] for prop in props: getter = get_jit_def(prop[1].fget, f"__{prop[0]}_getter", self_name=self_name) setter = get_jit_def(prop[1].fset, f"__{prop[0]}_setter", self_name=self_name) if prop[1].fset else None properties.append( Property(getter.range(), Ident(getter.range(), prop[0]), getter, setter)) return properties
def build_Print(ctx, stmt): r = ctx.make_range(stmt.lineno, stmt.col_offset, stmt.col_offset + len("print")) if stmt.dest: raise NotSupportedError( r, "print statements with non-default destinations aren't supported" ) args = [build_expr(ctx, val) for val in stmt.values] return ExprStmt(Apply(Var(Ident(r, "print")), args, []))
def build_Name(ctx, expr): r = ctx.make_range(expr.lineno, expr.col_offset, expr.col_offset + len(expr.id)) if expr.id.startswith(_reserved_prefix): raise NotSupportedError(r, "names of variables used in JIT-ed functions " "can't start with " + _reserved_prefix) if expr.id == "True": return TrueLiteral(r) elif expr.id == "False": return FalseLiteral(r) elif expr.id == "None": return NoneLiteral(r) return Var(Ident(r, expr.id))
def build_Call(ctx, expr): func = build_expr(ctx, expr.func) args = [build_expr(ctx, py_arg) for py_arg in expr.args] if hasattr(expr, 'starargs') and expr.starargs: stararg_expr = build_expr(ctx, expr.starargs) args += [Starred(stararg_expr.range(), stararg_expr)] kwargs = [] for kw in expr.keywords: kw_expr = build_expr(ctx, kw.value) # XXX: we could do a better job at figuring out the range for the name here if not kw.arg: raise NotSupportedError(kw_expr.range(), 'keyword-arg expansion is not supported') kwargs.append(Attribute(Ident(kw_expr.range(), kw.arg), kw_expr)) return Apply(func, args, kwargs)
def build_Attribute(ctx, expr): base = build_expr(ctx, expr.value) # expr.attr is just a string, so it's not annotated in any way, so we have # to build the range manually source = ctx.source.encode('utf-8') def get_char(index): return chr(source[index]) start_pos = base.range().end + 1 while get_char(start_pos) in string.whitespace: # Skip whitespace start_pos += 1 end_pos = start_pos + len(expr.attr) name_range = ctx.make_raw_range(start_pos, end_pos) return Select(base, Ident(name_range, expr.attr))
def build_def(ctx, py_def, type_line, self_name=None): body = py_def.body r = ctx.make_range(py_def.lineno + len(py_def.decorator_list), py_def.col_offset, py_def.col_offset + len("def")) param_list = build_param_list(ctx, py_def.args, self_name) return_type = None if getattr(py_def, 'returns', None) is not None: return_type = build_expr(ctx, py_def.returns) decl = Decl(r, param_list, return_type) is_method = self_name is not None if type_line is not None: type_comment_decl = torch._C.parse_type_comment(type_line) decl = torch._C.merge_type_from_type_comment(decl, type_comment_decl, is_method) return Def(Ident(r, py_def.name), decl, build_stmts(ctx, body))
def build_JoinedStr(ctx, expr): s = '' args = [] for value in expr.values: r = ctx.make_range(value.lineno, value.col_offset, value.col_offset + 1) if isinstance(value, ast.FormattedValue): if value.conversion != -1: raise NotSupportedError(r, 'Don\'t support conversion in JoinedStr') if value.format_spec is not None: raise NotSupportedError(r, 'Don\'t support formatting in JoinedStr') s += '{}' args.append(build_expr(ctx, value.value)) elif isinstance(value, ast.Str): s += value.s else: raise NotSupportedError(r, 'Unsupported value in JoinedStr') r = ctx.make_range(expr.lineno, expr.col_offset, expr.col_offset + 1) return Apply(Select(StringLiteral(r, s), Ident(r, 'format')), args, [])
def build_class_def(ctx, py_def, methods, properties, self_name, assigns): r = ctx.make_range(py_def.lineno, py_def.col_offset, py_def.col_offset + len("class")) return ClassDef(Ident(r, self_name), [Stmt(method) for method in methods], properties, assigns)