replace execfile() by equivalent exec statement
authorGregNoel <GregNoel@fdb21ef1-2011-0410-befe-b5e4ea1792b1>
Mon, 2 Mar 2009 03:19:29 +0000 (03:19 +0000)
committerGregNoel <GregNoel@fdb21ef1-2011-0410-befe-b5e4ea1792b1>
Mon, 2 Mar 2009 03:19:29 +0000 (03:19 +0000)
git-svn-id: http://scons.tigris.org/svn/scons/trunk@4057 fdb21ef1-2011-0410-befe-b5e4ea1792b1

12 files changed:
QMTest/TestSCons_time.py
bench/bench.py
src/engine/SCons/Variables/VariablesTests.py
src/engine/SCons/Variables/__init__.py
src/engine/SCons/compat/_scons_optparse.py
src/script/scons-time.py
test/Deprecated/Options/Options.py
test/Deprecated/Options/chdir.py
test/SConscript/SConscriptChdir.py
test/SConscriptChdir.py
test/Variables/Variables.py
test/Variables/chdir.py

index 869e0cb4824a5ac23ea97ce806dcabb8d5d0cbe2..074739346e11efe3caa1ae12e873204ae1cf9641 100644 (file)
@@ -46,6 +46,7 @@ scons_py = """\
 #!/usr/bin/env python
 import os
 import sys
+import string
 def write_args(fp, args):
     fp.write(args[0] + '\\n')
     for arg in args[1:]:
@@ -58,7 +59,7 @@ for arg in sys.argv[1:]:
         write_args(profile, sys.argv)
         break
 sys.stdout.write('SCONS_LIB_DIR = ' + os.environ['SCONS_LIB_DIR'] + '\\n')
-execfile('SConstruct')
+exec(string.replace(open('SConstruct').read(), '\\r', '\\n'))
 """
 
 aegis_py = """\
index 07c0384c54cbf69be2e9c5d2b541f7079a46c881..d90d75ad3dc2e24dabf3aa132a1c7864061df226 100644 (file)
@@ -28,6 +28,7 @@ import getopt
 import sys
 import time
 import types
+import string
 
 Usage = """\
 Usage:  bench.py OPTIONS file.py
@@ -87,7 +88,7 @@ if len(args) != 1:
     sys.exit(1)
 
 
-execfile(args[0])
+exec(string.replace(open(args[0]).read(), '\r', '\n'))
 
 
 try:
index 153b1aa6392210b73f1e74435bc47a6748e4db25..f1caff74a8f594d857cf39a1fc6495769bec5321 100644 (file)
@@ -54,7 +54,7 @@ def check(key, value, env):
 def checkSave(file, expected):
     gdict = {}
     ldict = {}
-    execfile(file, gdict, ldict)
+    exec string.replace(open(file).read(), '\r', '\n') in gdict, ldict
     assert expected == ldict, "%s\n...not equal to...\n%s" % (expected, ldict)
 
 class VariablesTestCase(unittest.TestCase):
index e7b43160219c2eec7874da8c4f597b4a3e94da60..3cc6694a30d1597081456ada1093a2c428b435ea 100644 (file)
@@ -166,7 +166,7 @@ class Variables:
                     sys.path.insert(0, dir)
                 try:
                     values['__name__'] = filename
-                    execfile(filename, {}, values)
+                    exec string.replace(open(filename).read(), '\r', '\n') in {}, values
                 finally:
                     if dir:
                         del sys.path[0]
index 386dfea67362ea72ca12ffa23d4fc6241efa1f6b..23f2ad3128883a453be3175ad42a997689e583d0 100644 (file)
@@ -920,7 +920,7 @@ class Values:
 
     def read_file(self, filename, mode="careful"):
         vars = {}
-        execfile(filename, vars)
+        exec string.replace(open(filename).read(), '\r', '\n') in vars
         self._update(vars, mode)
 
     def ensure_value(self, attr, value):
index 134124f61d041b3824b574bf6213437b4a4aa8a4..019df3127436600541575cf8e25cebf03a8377dc 100644 (file)
@@ -79,6 +79,17 @@ def make_temp_file(**kw):
             tempfile.template = save_template
     return result
 
+def HACK_for_exec(cmd, *args):
+    '''
+    For some reason, Python won't allow an exec() within a function
+    that also declares an internal function (including lambda functions).
+    This function is a hack that calls exec() in a function with no
+    internal functions.
+    '''
+    if not args:          exec(cmd)
+    elif len(args) == 1:  exec cmd in args[0]
+    else:                 exec cmd in args[0], args[1]
+
 class Plotter:
     def increment_size(self, largest):
         """
@@ -830,7 +841,7 @@ class SConsTimer:
                 self.title = a
 
         if self.config_file:
-            execfile(self.config_file, self.__dict__)
+            exec string.replace(open(self.config_file).read(), '\r', '\n') in self.__dict__
 
         if self.chdir:
             os.chdir(self.chdir)
@@ -859,19 +870,18 @@ class SConsTimer:
 
         if format == 'ascii':
 
-            def print_function_timing(file, func):
+            for file in args:
                 try:
-                    f, line, func, time = self.get_function_profile(file, func)
+                    f, line, func, time = \
+                            self.get_function_profile(file, function_name)
                 except ValueError, e:
-                    sys.stderr.write("%s: func: %s: %s\n" % (self.name, file, e))
+                    sys.stderr.write("%s: func: %s: %s\n" %
+                                     (self.name, file, e))
                 else:
                     if f.startswith(cwd_):
                         f = f[len(cwd_):]
                     print "%.3f %s:%d(%s)" % (time, f, line, func)
 
-            for file in args:
-                print_function_timing(file, function_name)
-
         elif format == 'gnuplot':
 
             results = self.collect_results(args, self.get_function_time,
@@ -950,7 +960,7 @@ class SConsTimer:
                 self.title = a
 
         if self.config_file:
-            execfile(self.config_file, self.__dict__)
+            HACK_for_exec(string.replace(open(self.config_file).read(), '\r', '\n'), self.__dict__)
 
         if self.chdir:
             os.chdir(self.chdir)
@@ -1070,7 +1080,7 @@ class SConsTimer:
         object_name = args.pop(0)
 
         if self.config_file:
-            execfile(self.config_file, self.__dict__)
+            HACK_for_exec(string.replace(open(self.config_file).read(), '\r', '\n'), self.__dict__)
 
         if self.chdir:
             os.chdir(self.chdir)
@@ -1208,7 +1218,7 @@ class SConsTimer:
             sys.exit(1)
 
         if self.config_file:
-            execfile(self.config_file, self.__dict__)
+            exec string.replace(open(self.config_file).read(), '\r', '\n') in self.__dict__
 
         if args:
             self.archive_list = args
@@ -1448,7 +1458,7 @@ class SConsTimer:
                 which = a
 
         if self.config_file:
-            execfile(self.config_file, self.__dict__)
+            HACK_for_exec(string.replace(open(self.config_file).read(), '\r', '\n'), self.__dict__)
 
         if self.chdir:
             os.chdir(self.chdir)
index d04ad443324498e63ac0665f57fc8dd33a551cca..f411478904bcb0c23077ffc59e881f36059922cf 100644 (file)
@@ -241,7 +241,7 @@ opts.Save('options.saved', env)
 def checkSave(file, expected):
     gdict = {}
     ldict = {}
-    execfile(file, gdict, ldict)
+    exec string.replace(open(file).read(), '\r', '\n') in gdict, ldict
     assert expected == ldict, "%s\n...not equal to...\n%s" % (expected, ldict)
 
 # First test with no command line options
index 0db8223af4548c2e03222aa9dc015b5a93480d65..547fe534049285f347f9d7973d6f9c0ec8b38866 100644 (file)
@@ -52,7 +52,8 @@ print "VARIABLE =", repr(env['VARIABLE'])
 test.write(['bin', 'opts.cfg'], """\
 import os
 os.chdir(os.path.split(__name__)[0])
-execfile('opts2.cfg')
+import string
+exec(string.replace(open('opts2.cfg').read(), '\\r', '\\n'))
 """)
 
 test.write(['bin', 'opts2.cfg'], """\
index 99810e3c33c58b55500750eeb3a5f0d5ff4c9abf..8169f08fdee686abb1267109d48a0469291bda7c 100644 (file)
@@ -44,27 +44,32 @@ SConscript('dir5/SConscript')
 """)
 
 test.write(['dir1', 'SConscript'], """
-execfile("create_test.py")
+import string
+exec(string.replace(open("create_test.py").read(), '\\r', '\\n'))
 """)
 
 test.write(['dir2', 'SConscript'], """
-execfile("create_test.py")
+import string
+exec(string.replace(open("create_test.py").read(), '\\r', '\\n'))
 """)
 
 test.write(['dir3', 'SConscript'], """
 import os.path
 name = os.path.join('dir3', 'create_test.py')
-execfile(name)
+import string
+exec(string.replace(open(name).read(), '\\r', '\\n'))
 """)
 
 test.write(['dir4', 'SConscript'], """
-execfile("create_test.py")
+import string
+exec(string.replace(open("create_test.py").read(), '\\r', '\\n'))
 """)
 
 test.write(['dir5', 'SConscript'], """
 import os.path
 name = os.path.join('dir5', 'create_test.py')
-execfile(name)
+import string
+exec(string.replace(open(name).read(), '\\r', '\\n'))
 """)
 
 for dir in ['dir1', 'dir2', 'dir3','dir4', 'dir5']:
index 99810e3c33c58b55500750eeb3a5f0d5ff4c9abf..8169f08fdee686abb1267109d48a0469291bda7c 100644 (file)
@@ -44,27 +44,32 @@ SConscript('dir5/SConscript')
 """)
 
 test.write(['dir1', 'SConscript'], """
-execfile("create_test.py")
+import string
+exec(string.replace(open("create_test.py").read(), '\\r', '\\n'))
 """)
 
 test.write(['dir2', 'SConscript'], """
-execfile("create_test.py")
+import string
+exec(string.replace(open("create_test.py").read(), '\\r', '\\n'))
 """)
 
 test.write(['dir3', 'SConscript'], """
 import os.path
 name = os.path.join('dir3', 'create_test.py')
-execfile(name)
+import string
+exec(string.replace(open(name).read(), '\\r', '\\n'))
 """)
 
 test.write(['dir4', 'SConscript'], """
-execfile("create_test.py")
+import string
+exec(string.replace(open("create_test.py").read(), '\\r', '\\n'))
 """)
 
 test.write(['dir5', 'SConscript'], """
 import os.path
 name = os.path.join('dir5', 'create_test.py')
-execfile(name)
+import string
+exec(string.replace(open(name).read(), '\\r', '\\n'))
 """)
 
 for dir in ['dir1', 'dir2', 'dir3','dir4', 'dir5']:
index eaac22dd81a11b2120f65a951812d03ca29bed43..0e6a152c8fc881f9cf0b7fff062cd9da0fbddb5e 100644 (file)
@@ -235,7 +235,7 @@ opts.Save('variables.saved', env)
 def checkSave(file, expected):
     gdict = {}
     ldict = {}
-    execfile(file, gdict, ldict)
+    exec string.replace(open(file).read(), '\r', '\n') in gdict, ldict
     assert expected == ldict, "%s\n...not equal to...\n%s" % (expected, ldict)
 
 # First test with no command line variables
index 711957c31023a7137d8cf93bf55d1c9fa6b6c5f7..39eccb3eede844efb2c4465d85b87b9a85f89a15 100644 (file)
@@ -52,7 +52,8 @@ print "VARIABLE =", repr(env['VARIABLE'])
 test.write(['bin', 'opts.cfg'], """\
 import os
 os.chdir(os.path.split(__name__)[0])
-execfile('opts2.cfg')
+import string
+exec(string.replace(open('opts2.cfg').read(), '\\r', '\\n'))
 """)
 
 test.write(['bin', 'opts2.cfg'], """\