Python AST模块无法检测“if”或“for”

2024-09-27 00:21:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试限制用户提供的脚本,并使用以下访问者:

class SyntaxChecker(ast.NodeVisitor):

    def check(self, syntax):
        tree = ast.parse(syntax)
        print(ast.dump(tree), syntax)
        self.visit(tree)

    def visit_Call(self, node):
        print('Called for Call', ast.dump(node))
        if isinstance(node.func, ast.Call) and node.func.id not in allowed_functions:
            raise CodeError("%s is not an allowed function!"%node.func.id)
        elif isinstance(node.func, ast.Attribute) and node.func.value.id not in allowed_classes:
            raise CodeError('{0} is not calling an allowed class'.format(node.func.value.id))
        elif isinstance(node.func, ast.Name) and node.func.id in allowed_classes:
            raise CodeError('You are not allowed to instantiate any class, {0}'.format(node.func.id))
        else:
            ast.NodeVisitor.generic_visit(self, node)

    def visit_Assign(self, node):
        print('Called for Assign', ast.dump(node))
        ast.NodeVisitor.generic_visit(self, node)

    def visit_Attribute(self, node):
        print('Called for Attribute', ast.dump(node))
        if node.value.id not in allowed_classes:
            raise CodeError('"{0}" is not an allowed class'.format(node.value.id))
        elif node.value.id in allowed_classes and isinstance(node.ctx, ast.Store):
            raise CodeError('Trying to change something in a pre-defined class, "{0}" in "{1}"'.format(node.attr, node.value.id))
        else:
            ast.NodeVisitor.generic_visit(self, node)

    def visit_Expr(self, node):
        print('Called for Expr', ast.dump(node))
        ast.NodeVisitor.generic_visit(self, node)

    def visit_Name(self, node):
        print('Called for Name', ast.dump(node))
        if isinstance(node.ctx, ast.Store) and node.id in allowed_classes:
            raise CodeError('Trying to change a pre-defined class, {0}'.format(node.id))
        elif isinstance(node.ctx, ast.Load) and node.id not in safe_names and node.id not in allowed_functions and node.id not in allowed_classes:
            raise CodeError('"{0}" function is not allowed'.format(node.id))
        else:
            ast.NodeVisitor.generic_visit(self, node)

    def generic_visit(self, node):
        print('Called for generic', ast.dump(node))        
        if type(node).__name__ not in allowed_node_types:
            raise CodeError("%s is not allowed!"%type(node).__name__)
        else:
            ast.NodeVisitor.generic_visit(self, node)

if __name__ == '__main__':
    # Check whole file
    x = SyntaxChecker()
    code = open(sys.argv[1], 'r').read()
    try:
        x.check(code)
    except CodeError as e:
        print(repr(e))

    # Or check line by line, considering multiline statements
    code = ''
    for line in open(sys.argv[1], 'r'):
        line = line.strip()
        if line:
            code += line
            try:
                print('[{0}]'.format(code))
                x.check(code)
                code = ''
            except CodeError as e:
                print(repr(e))
                break
            except SyntaxError as e:
                print('********Feeding next line', repr(e))

它目前运行良好,我将对其进行更多的调优,但问题是,在解析类似这样的东西时,它总是抛出SyntaxError('unexpected EOF while parsing', ('<unknown>', 1, 15, 'for j in A.b():'))

^{pr2}$

因此,不会解析for或{}。在

编辑:我添加了一个代码来检查整个代码,或者检查多行语句。在


Tags: andinselfidnodefornotvisit
2条回答

您正在逐行分析代码,但是for循环并不是独立的。没有套件的for循环是语法错误。Python希望找到一个套件,却找到了EOF(文件结尾)。在

换句话说,解析器只能在一个物理行上处理Simple Statements和独立的{a2},如果在同一行上直接跟一个简单的语句或表达式,Compound Statements。在

您的代码也将失败:

  • 多行字符串

    somestring = """Containing more
    than one
    line"""
    
  • 线路延续

    if the_line == 'too long' and \
       a_backslash_was_used in (True, 'true'):
        # your code fails
    
    somevar = (you_are_allowed_to_use_newlines,
        "inside parentheses and brackets and braces")
    

使用ast.parse()逐行检查代码在这里行不通;它只适用于整个套件;在逐个文件的基础上,我只传递整个文件。在

要逐行检查代码,您需要自己标记它。您可以使用^{} library;它将报告SyntaxError异常或tokenize.TokenError语法错误。在

如果要限制脚本,请查看^{};项目本身或其源代码。它们解析整个脚本,然后根据生成的AST节点执行(限制它们接受哪些节点)。在

可以使用is instance(iterator,Ast,if,过去))与ast.解析. 在

相关问题 更多 >

    热门问题