Пример #1
0
 def produce_name(item: ast27.ExceptHandler) -> Optional[NameExpr]:
     if item.name is None:
         return None
     elif isinstance(item.name, ast27.Name):
         return NameExpr(item.name.id)
     else:
         raise RuntimeError("'{}' has non-Name name.".format(ast27.dump(item)))
Пример #2
0
 def produce_name(item: ast27.ExceptHandler) -> Optional[NameExpr]:
     if item.name is None:
         return None
     elif isinstance(item.name, ast27.Name):
         return NameExpr(item.name.id)
     else:
         raise RuntimeError("'{}' has non-Name name.".format(ast27.dump(item)))
 def convert_arg(arg, type_comment):
     if isinstance(arg, ast27.Name):
         v = arg.id
     elif isinstance(arg, ast27.Tuple):
         v = self.visit(arg)
     else:
         raise RuntimeError("'{}' is not a valid argument.".format(ast27.dump(arg)))
     return ast3.arg(v, None, type_comment, lineno=arg.lineno, col_offset=arg.col_offset)
Пример #4
0
 def convert_arg(arg, type_comment):
     if isinstance(arg, ast27.Name):
         v = arg.id
     elif isinstance(arg, ast27.Tuple):
         v = self.visit(arg)
     else:
         raise RuntimeError("'{}' is not a valid argument.".format(ast27.dump(arg)))
     return ast3.arg(v, None, type_comment, lineno=arg.lineno, col_offset=arg.col_offset)
Пример #5
0
    def visit_ExceptHandler(self, n):
        if n.name is None:
            name = None
        elif isinstance(n.name, ast27.Name):
            name = n.name.id
        else:
            raise RuntimeError("'{}' has non-Name name.".format(ast27.dump(n)))

        return ast3.ExceptHandler(self.maybe_visit(n.type), name, self.visit(n.body))
Пример #6
0
 def visit(self, node):
     method = 'visit_' + node.__class__.__name__
     visitor = getattr(self, method, None)
     if self.verbose:
         lineno = getattr(node, 'lineno', 1)
         line = self.code[lineno - 1] if self.code else ''
         self._log(lineno, ast.dump(node), line)
     if visitor:
         visitor(node)
     return self.generic_visit(node)
Пример #7
0
    def visit_ExceptHandler(self, n):
        if n.name is None:
            name = None
        elif isinstance(n.name, ast27.Name):
            name = n.name.id
        else:
            raise RuntimeError("'{}' has non-Name name.".format(ast27.dump(n)))

        return ast3.ExceptHandler(self.maybe_visit(n.type),
                                  name,
                                  self.visit(n.body))
Пример #8
0
 def convert_arg(index: int, arg: ast27.expr) -> Var:
     if isinstance(arg, ast27.Name):
         v = arg.id
     elif isinstance(arg, ast27.Tuple):
         v = '__tuple_arg_{}'.format(index + 1)
         rvalue = NameExpr(v)
         rvalue.set_line(line)
         assignment = AssignmentStmt([self.visit(arg)], rvalue)
         assignment.set_line(line)
         decompose_stmts.append(assignment)
     else:
         raise RuntimeError("'{}' is not a valid argument.".format(ast27.dump(arg)))
     return Var(v)
Пример #9
0
 def convert_arg(index: int, arg: ast27.expr) -> Var:
     if isinstance(arg, ast27.Name):
         v = arg.id
     elif isinstance(arg, ast27.Tuple):
         v = '__tuple_arg_{}'.format(index + 1)
         rvalue = NameExpr(v)
         rvalue.set_line(line)
         assignment = AssignmentStmt([self.visit(arg)], rvalue)
         assignment.set_line(line)
         decompose_stmts.append(assignment)
     else:
         raise RuntimeError("'{}' is not a valid argument.".format(ast27.dump(arg)))
     return Var(v)
Пример #10
0
 def convert_arg(arg: ast27.expr) -> Var:
     if isinstance(arg, ast27.Name):
         v = arg.id
     elif isinstance(arg, ast27.Tuple):
         # TODO: An `arg` object may be a Tuple instead of just an identifier in the
         # case of Python 2 function definitions/lambdas that use the tuple unpacking
         # syntax. The `typed_ast.conversions` module ended up just simply passing the
         # the arg object unmodified (instead of converting it into more args, etc).
         # This isn't typesafe, since we will no longer be always passing in a string
         # to `Var`, but we'll do the same here for consistency.
         v = arg  # type: ignore
     else:
         raise RuntimeError("'{}' is not a valid argument.".format(ast27.dump(arg)))
     return Var(v)
Пример #11
0
 def convert_arg(arg: ast27.expr) -> Var:
     if isinstance(arg, ast27.Name):
         v = arg.id
     elif isinstance(arg, ast27.Tuple):
         # TODO: An `arg` object may be a Tuple instead of just an identifier in the
         # case of Python 2 function definitions/lambdas that use the tuple unpacking
         # syntax. The `typed_ast.conversions` module ended up just simply passing the
         # the arg object unmodified (instead of converting it into more args, etc).
         # This isn't typesafe, since we will no longer be always passing in a string
         # to `Var`, but we'll do the same here for consistency.
         v = arg  # type: ignore
     else:
         raise RuntimeError("'{}' is not a valid argument.".format(ast27.dump(arg)))
     return Var(v)