[Python-checkins] r56939 - sandbox/trunk/2to3/fixes/fix_execfile.py

neal.norwitz python-checkins at python.org
Sun Aug 12 02:18:51 CEST 2007


Author: neal.norwitz
Date: Sun Aug 12 02:18:21 2007
New Revision: 56939

Added:
   sandbox/trunk/2to3/fixes/fix_execfile.py
Log:
Add fixer to convert exec() to exec()

Added: sandbox/trunk/2to3/fixes/fix_execfile.py
==============================================================================
--- (empty file)
+++ sandbox/trunk/2to3/fixes/fix_execfile.py	Sun Aug 12 02:18:21 2007
@@ -0,0 +1,37 @@
+# Copyright 2006 Google, Inc. All Rights Reserved.
+# Licensed to PSF under a Contributor Agreement.
+
+"""Fixer for execfile.
+
+This converts usages of the execfile function into calls to the built-in
+exec() function.
+"""
+
+import pytree
+from fixes import basefix
+from fixes.util import Comma, Name, Call, LParen, RParen, Dot
+
+
+class FixExecfile(basefix.BaseFix):
+
+    PATTERN = """
+    power< 'execfile' trailer< '(' arglist< filename=any [',' globals=any [',' locals=any ] ] > ')' > >
+    |
+    power< 'execfile' trailer< '(' filename=any ')' > >
+    """
+
+    def transform(self, node, results):
+        assert results
+        syms = self.syms
+        filename = results["filename"]
+        globals = results.get("globals")
+        locals = results.get("locals")
+        args = [Name('open'), LParen(), filename.clone(), RParen(), Dot(),
+                Name('read'), LParen(), RParen()]
+        args[0].set_prefix("")
+        if globals is not None:
+            args.extend([Comma(), globals.clone()])
+        if locals is not None:
+            args.extend([Comma(), locals.clone()])
+
+        return Call(Name("exec"), args, prefix=node.get_prefix())


More information about the Python-checkins mailing list