diff --git a/py/builtin/builtin31.py b/py/builtin/builtin31.py index 47283462e..015ddb6c1 100644 --- a/py/builtin/builtin31.py +++ b/py/builtin/builtin31.py @@ -11,9 +11,25 @@ if sys.version_info >= (3, 0): obj = obj.encode(encoding) return str(obj, encoding) + def execfile(fn, globs=None, locs=None): + if globs is None: + back = sys._getframe(1) + globs = back.f_globals + locs = back.f_locals + del back + elif locs is None: + locs = globs + fp = open(fn, "rb") + try: + source = fp.read() + finally: + fp.close() + exec_(source, globs, locs) + else: _totext = unicode _basestring = basestring + execfile = execfile import __builtin__ as builtins def print_(*args, **kwargs): diff --git a/py/builtin/testing/test_builtin.py b/py/builtin/testing/test_builtin.py index 43df15f5e..05454792e 100644 --- a/py/builtin/testing/test_builtin.py +++ b/py/builtin/testing/test_builtin.py @@ -84,6 +84,18 @@ def test_print_simple(): s = f.getvalue() assert s == "xyzabc" +def test_execfile(tmpdir): + test_file = tmpdir.join("test.py") + test_file.write("x = y") + ns = {"y" : 42} + py.builtin.execfile(str(test_file), ns) + assert ns["x"] == 42 + class A: + y = 3 + x = 4 + py.builtin.execfile(str(test_file)) + assert A.x == 3 + def test_totext(): py.builtin._totext("hello", "UTF-8")