diff options
| author | Benjamin Peterson <benjamin@python.org> | 2010-03-22 22:40:06 (GMT) |
|---|---|---|
| committer | Benjamin Peterson <benjamin@python.org> | 2010-03-22 22:40:06 (GMT) |
| commit | 798e54004f47b8173ef70e6df0132bedceb13311 (patch) | |
| tree | 2bb9eb5c744e21e28c44e0c00bb48e15343a5a7d /Lib/lib2to3/fixes/fix_exitfunc.py | |
| parent | 513d9aeadb09b460e968054c7ed78c385835dcd4 (diff) | |
| download | cpython-798e54004f47b8173ef70e6df0132bedceb13311.zip cpython-798e54004f47b8173ef70e6df0132bedceb13311.tar.gz cpython-798e54004f47b8173ef70e6df0132bedceb13311.tar.bz2 | |
Merged revisions 79077,79137,79304-79305 via svnmerge from
svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3
........
r79077 | benjamin.peterson | 2010-03-18 18:05:29 -0500 (Thu, 18 Mar 2010) | 1 line
port detect_encoding improvements from py3k
........
r79137 | benjamin.peterson | 2010-03-20 11:12:53 -0500 (Sat, 20 Mar 2010) | 1 line
add a fixer for setting sys.exitfunc #2356
........
r79304 | benjamin.peterson | 2010-03-22 17:20:22 -0500 (Mon, 22 Mar 2010) | 1 line
fix test_parser when it's run in a path with spaces #7666
........
r79305 | benjamin.peterson | 2010-03-22 17:27:07 -0500 (Mon, 22 Mar 2010) | 1 line
normalize whitespace
........
Diffstat (limited to 'Lib/lib2to3/fixes/fix_exitfunc.py')
| -rw-r--r-- | Lib/lib2to3/fixes/fix_exitfunc.py | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/Lib/lib2to3/fixes/fix_exitfunc.py b/Lib/lib2to3/fixes/fix_exitfunc.py new file mode 100644 index 0000000..b29b464 --- /dev/null +++ b/Lib/lib2to3/fixes/fix_exitfunc.py @@ -0,0 +1,70 @@ +""" +Convert use of sys.exitfunc to use the atexit module. +""" + +# Author: Benjamin Peterson + +from lib2to3 import pytree, fixer_base +from lib2to3.fixer_util import Name, Attr, Call, Comma, Newline, syms + + +class FixExitfunc(fixer_base.BaseFix): + + PATTERN = """ + ( + sys_import=import_name<'import' + ('sys' + | + dotted_as_names< (any ',')* 'sys' (',' any)* > + ) + > + | + expr_stmt< + power< 'sys' trailer< '.' 'exitfunc' > > + '=' func=any > + ) + """ + + def __init__(self, *args): + super(FixExitfunc, self).__init__(*args) + + def start_tree(self, tree, filename): + super(FixExitfunc, self).start_tree(tree, filename) + self.sys_import = None + + 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 = "" + register = pytree.Node(syms.power, + Attr(Name("atexit"), Name("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("atexit", " ")) + else: + containing_stmt = self.sys_import.parent + position = containing_stmt.children.index(self.sys_import) + stmt_container = containing_stmt.parent + new_import = pytree.Node("import_name", + [Name("import"), Name("atexit", " ")] + ) + new = pytree.Node("simple_stmt", [new_import]) + containing_stmt.insert_child(position + 1, Newline()) + containing_stmt.insert_child(position + 2, new) |
