Commit 37fa8a96 authored by Kevin Modzelewski's avatar Kevin Modzelewski Committed by GitHub

Merge pull request #1289 from kmod/execfile_globals

Support attrwrapper as globals argument to execfile
parents 4d4d78ae 10f9ec7b
...@@ -502,7 +502,8 @@ static void pickGlobalsAndLocals(Box*& globals, Box*& locals) { ...@@ -502,7 +502,8 @@ static void pickGlobalsAndLocals(Box*& globals, Box*& locals) {
if (globals->cls == attrwrapper_cls) if (globals->cls == attrwrapper_cls)
globals = unwrapAttrWrapper(globals); globals = unwrapAttrWrapper(globals);
assert(globals && (globals->cls == module_cls || globals->cls == dict_cls)); RELEASE_ASSERT(globals && (globals->cls == module_cls || globals->cls == dict_cls), "Unspported globals type: %s",
globals ? globals->cls->tp_name : "NULL");
if (globals) { if (globals) {
// From CPython (they set it to be f->f_builtins): // From CPython (they set it to be f->f_builtins):
......
...@@ -1932,7 +1932,8 @@ static PyObject* builtin_execfile(PyObject* self, PyObject* args) noexcept { ...@@ -1932,7 +1932,8 @@ static PyObject* builtin_execfile(PyObject* self, PyObject* args) noexcept {
if (PyErr_WarnPy3k("execfile() not supported in 3.x; use exec()", 1) < 0) if (PyErr_WarnPy3k("execfile() not supported in 3.x; use exec()", 1) < 0)
return NULL; return NULL;
if (!PyArg_ParseTuple(args, "s|O!O:execfile", &filename, &PyDict_Type, &globals, &locals)) // Pyston change: allow attrwrappers here
if (!PyArg_ParseTuple(args, "s|OO:execfile", &filename, &globals, &locals))
return NULL; return NULL;
if (locals != Py_None && !PyMapping_Check(locals)) { if (locals != Py_None && !PyMapping_Check(locals)) {
PyErr_SetString(PyExc_TypeError, "locals must be a mapping"); PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
...@@ -1945,6 +1946,11 @@ static PyObject* builtin_execfile(PyObject* self, PyObject* args) noexcept { ...@@ -1945,6 +1946,11 @@ static PyObject* builtin_execfile(PyObject* self, PyObject* args) noexcept {
} else if (locals == Py_None) } else if (locals == Py_None)
locals = globals; locals = globals;
if (!PyDict_CheckExact(globals) && globals->cls != attrwrapper_cls) {
PyErr_Format(TypeError, "execfile() globals must be dict, not %s", globals->cls->tp_name);
return NULL;
}
if (PyDict_GetItemString(globals, "__builtins__") == NULL) { if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
if (PyDict_SetItemString(globals, "__builtins__", PyEval_GetBuiltins()) != 0) if (PyDict_SetItemString(globals, "__builtins__", PyEval_GetBuiltins()) != 0)
return NULL; return NULL;
......
...@@ -8,6 +8,11 @@ fn = os.path.join(os.path.dirname(__file__), 'execfile_target.py') ...@@ -8,6 +8,11 @@ fn = os.path.join(os.path.dirname(__file__), 'execfile_target.py')
execfile(fn) execfile(fn)
print "done with first execfile" print "done with first execfile"
execfile(fn) execfile(fn)
execfile(fn, globals())
try:
execfile(fn, [])
except Exception as e:
print type(e)
print test_name print test_name
print type(execfile_target) print type(execfile_target)
......
...@@ -8,7 +8,7 @@ import multiprocessing ...@@ -8,7 +8,7 @@ import multiprocessing
def worker(): def worker():
global done global done
for i in xrange(1000): for i in xrange(100):
del sys.modules["pyc_import_target"] del sys.modules["pyc_import_target"]
import pyc_import_target import pyc_import_target
...@@ -30,10 +30,10 @@ idx = 0 ...@@ -30,10 +30,10 @@ idx = 0
while l: while l:
p = l.pop() p = l.pop()
while p.is_alive(): while p.is_alive():
for i in xrange(100): for i in xrange(10):
if os.path.exists(path): if os.path.exists(path):
os.remove(path) os.remove(path)
for i in xrange(100): for i in xrange(10):
if os.path.exists(path): if os.path.exists(path):
with open(path, "rw+") as f: with open(path, "rw+") as f:
f.write(chr(i) * 100) f.write(chr(i) * 100)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment