From c1396537e5cae88e6357931dc6efe4074cf54ced Mon Sep 17 00:00:00 2001 From: Greg Noel Date: Mon, 2 Mar 2009 03:19:29 +0000 Subject: replace execfile() by equivalent exec statement --- QMTest/TestSCons_time.py | 3 ++- bench/bench.py | 3 ++- src/engine/SCons/Variables/VariablesTests.py | 2 +- src/engine/SCons/Variables/__init__.py | 2 +- src/engine/SCons/compat/_scons_optparse.py | 2 +- src/script/scons-time.py | 32 ++++++++++++++++++---------- test/Deprecated/Options/Options.py | 2 +- test/Deprecated/Options/chdir.py | 3 ++- test/SConscript/SConscriptChdir.py | 15 ++++++++----- test/SConscriptChdir.py | 15 ++++++++----- test/Variables/Variables.py | 2 +- test/Variables/chdir.py | 3 ++- 12 files changed, 54 insertions(+), 30 deletions(-) diff --git a/QMTest/TestSCons_time.py b/QMTest/TestSCons_time.py index 869e0cb..0747393 100644 --- a/QMTest/TestSCons_time.py +++ b/QMTest/TestSCons_time.py @@ -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 = """\ diff --git a/bench/bench.py b/bench/bench.py index 07c0384..d90d75a 100644 --- a/bench/bench.py +++ b/bench/bench.py @@ -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: diff --git a/src/engine/SCons/Variables/VariablesTests.py b/src/engine/SCons/Variables/VariablesTests.py index 153b1aa..f1caff7 100644 --- a/src/engine/SCons/Variables/VariablesTests.py +++ b/src/engine/SCons/Variables/VariablesTests.py @@ -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): diff --git a/src/engine/SCons/Variables/__init__.py b/src/engine/SCons/Variables/__init__.py index e7b4316..3cc6694 100644 --- a/src/engine/SCons/Variables/__init__.py +++ b/src/engine/SCons/Variables/__init__.py @@ -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] diff --git a/src/engine/SCons/compat/_scons_optparse.py b/src/engine/SCons/compat/_scons_optparse.py index 386dfea..23f2ad3 100644 --- a/src/engine/SCons/compat/_scons_optparse.py +++ b/src/engine/SCons/compat/_scons_optparse.py @@ -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): diff --git a/src/script/scons-time.py b/src/script/scons-time.py index 134124f..019df31 100644 --- a/src/script/scons-time.py +++ b/src/script/scons-time.py @@ -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) diff --git a/test/Deprecated/Options/Options.py b/test/Deprecated/Options/Options.py index d04ad44..f411478 100644 --- a/test/Deprecated/Options/Options.py +++ b/test/Deprecated/Options/Options.py @@ -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 diff --git a/test/Deprecated/Options/chdir.py b/test/Deprecated/Options/chdir.py index 0db8223..547fe53 100644 --- a/test/Deprecated/Options/chdir.py +++ b/test/Deprecated/Options/chdir.py @@ -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'], """\ diff --git a/test/SConscript/SConscriptChdir.py b/test/SConscript/SConscriptChdir.py index 99810e3..8169f08 100644 --- a/test/SConscript/SConscriptChdir.py +++ b/test/SConscript/SConscriptChdir.py @@ -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']: diff --git a/test/SConscriptChdir.py b/test/SConscriptChdir.py index 99810e3..8169f08 100644 --- a/test/SConscriptChdir.py +++ b/test/SConscriptChdir.py @@ -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']: diff --git a/test/Variables/Variables.py b/test/Variables/Variables.py index eaac22d..0e6a152 100644 --- a/test/Variables/Variables.py +++ b/test/Variables/Variables.py @@ -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 diff --git a/test/Variables/chdir.py b/test/Variables/chdir.py index 711957c..39eccb3 100644 --- a/test/Variables/chdir.py +++ b/test/Variables/chdir.py @@ -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'], """\ -- cgit v0.12