def transform(self, node, results): if 'sys_import' in results: if self.sys_import is None: self.sys_import = results['sys_import'] return else: func = results['func'].clone() func.prefix = u'' register = pytree.Node(syms.power, Attr(Name(u'atexit'), Name(u'register'))) call = Call(register, [func], node.prefix) node.replace(call) if self.sys_import is None: self.warning(node, "Can't find sys import; Please add an atexit import at the top of your file.") return names = self.sys_import.children[1] if names.type == syms.dotted_as_names: names.append_child(Comma()) names.append_child(Name(u'atexit', u' ')) else: containing_stmt = self.sys_import.parent position = containing_stmt.children.index(self.sys_import) stmt_container = containing_stmt.parent new_import = pytree.Node(syms.import_name, [Name(u'import'), Name(u'atexit', u' ')]) new = pytree.Node(syms.simple_stmt, [new_import]) containing_stmt.insert_child(position + 1, Newline()) containing_stmt.insert_child(position + 2, new) return
def transform(self, node, results): u""" Call __builtins__.long() with the value and the base of the value. This works because 0b10 is int("10", 2), 0o10 is int("10", 8), etc. """ val = node.value base_ = base(val) if base_ == 8: assert val.strip().startswith(u"0o") or \ val.strip().startswith( u"0O"), u"Invalid format for octal literal" node.changed() node.value = u"".join((u"0", val[2:])) elif base_ == 2: assert val.startswith(u"0") and val[1] in u"bB", \ u"Invalid format for binary literal" # __builtins__.long func_name = Node(syms.power, Attr(Name(u"__builtins__"), Name(u"long"))) # ("...", 2) func_args = [ String(u"".join((u"\"", val.strip()[2:], u"\""))), Comma(), Number(2, prefix=u" ") ] new_node = Call(func_name, func_args, node.prefix) return new_node
def transform(self, node, results): func = results['func'] touch_import(None, 'collections', node=node) args = [func.clone(), String(', ')] args.extend(Attr(Name('collections'), Name('Callable'))) return Call(Name('isinstance'), args, prefix=node.prefix)
def transform(self, node, results): tb = results['tb'].clone() tb.prefix = '' with_tb = Attr(results['val'].clone(), Name('with_traceback')) + \ [ArgList([tb])] new = pytree.Node(self.syms.simple_stmt, [Name("raise")] + with_tb) new.prefix = node.prefix return new
def transform(self, node, results): assert results base = results.get(u"base") attr = results.get(u"attr") head = results.get(u"head") arg_ = results.get(u"arg") if arg_: arg = arg_.clone() head.replace(Attr(Name(unicode(arg), prefix=head.prefix), Name(u"next"))) arg_.remove() elif base: attr.replace(Name(u"next", prefix=attr.prefix))
def transform(self, node, results): # First, find a the sys import. We'll just hope it's global scope. if "sys_import" in results: if self.sys_import is None: self.sys_import = results["sys_import"] return func = results["func"].clone() func.prefix = u"" register = pytree.Node(syms.power, Attr(Name(u"atexit"), Name(u"register"))) call = Call(register, [func], node.prefix) node.replace(call) if self.sys_import is None: # That's interesting. self.warning( node, "Can't find sys import; Please add an atexit " "import at the top of your file.") return # Now add an atexit import after the sys import. names = self.sys_import.children[1] if names.type == syms.dotted_as_names: names.append_child(Comma()) names.append_child(Name(u"atexit", u" ")) else: containing_stmt = self.sys_import.parent position = containing_stmt.children.index(self.sys_import) stmt_container = containing_stmt.parent new_import = pytree.Node( syms.import_name, [Name(u"import"), Name(u"atexit", u" ")]) new = pytree.Node(syms.simple_stmt, [new_import]) containing_stmt.insert_child(position + 1, Newline()) containing_stmt.insert_child(position + 2, new)
def build_attr(prefix, suffix): prefix = prefix if isinstance(prefix, Node) else Name(prefix) suffix = suffix if isinstance(suffix, Node) else Name(suffix) return Node(syms.power, Attr(prefix, suffix))
def test_returns(self): attr = Attr(Name("a"), Name("b")) self.assertEqual(type(attr), list)
def test(self): call = parse("foo()", strip_levels=2) self.assertStr(Attr(Name("a"), Name("b")), "a.b") self.assertStr(Attr(call, Name("b")), "foo().b")
def transform(self, node, results): syms = self.syms exc = results["exc"].clone() if exc.type == token.STRING: msg = "Python 3 does not support string exceptions" self.cannot_convert(node, msg) return # Python 2 supports # raise ((((E1, E2), E3), E4), E5), V # as a synonym for # raise E1, V # Since Python 3 will not support this, we recurse down any tuple # literals, always taking the first element. if is_tuple(exc): while is_tuple(exc): # exc.children[1:-1] is the unparenthesized tuple # exc.children[1].children[0] is the first element of the tuple exc = exc.children[1].children[0].clone() exc.prefix = u" " if "tb" in results: tb = results["tb"].clone() else: tb = None if "val" in results: val = results["val"].clone() if is_tuple(val): # Assume that exc is a subclass of Exception and call exc(*val). args = [c.clone() for c in val.children[1:-1]] exc = Call(exc, args) elif val.type in (token.NUMBER, token.STRING): # Handle numeric and string literals specially, e.g. # "raise Exception, 5" -> "raise Exception(5)". val.prefix = u"" exc = Call(exc, [val]) elif val.type == token.NAME and val.value == u"None": # Handle None specially, e.g. # "raise Exception, None" -> "raise Exception". pass else: # val is some other expression. If val evaluates to an instance # of exc, it should just be raised. If val evaluates to None, # a default instance of exc should be raised (as above). If val # evaluates to a tuple, exc(*val) should be called (as # above). Otherwise, exc(val) should be called. We can only # tell what to do at runtime, so defer to future.utils.raise_(), # which handles all of these cases. touch_import_top(u"future.utils", u"raise_", node) exc.prefix = u"" args = [exc, Comma(), val] if tb is not None: args += [Comma(), tb] return Call(Name(u"raise_"), args) if tb is not None: tb.prefix = "" exc_list = Attr(exc, Name('with_traceback')) + [ArgList([tb])] else: exc_list = [exc] return pytree.Node(syms.raise_stmt, [Name(u"raise")] + exc_list, prefix=node.prefix)
def test_returns(self): attr = Attr(Name('a'), Name('b')) self.assertEqual(type(attr), list)
def test(self): call = parse('foo()', strip_levels=2) self.assertStr(Attr(Name('a'), Name('b')), 'a.b') self.assertStr(Attr(call, Name('b')), 'foo().b')