Commit 0db6c63b authored by Kevin Modzelewski's avatar Kevin Modzelewski

Support frame.f_globals in frames with custom globals

parent 524b9284
......@@ -346,6 +346,16 @@ public:
abort();
}
Box* getGlobalsDict() {
Box* globals = getGlobals();
if (!globals)
return NULL;
if (isSubclass(globals->cls, module_cls))
return globals->getAttrWrapper();
return globals;
}
FrameInfo* getFrameInfo() {
if (id.type == PythonFrameId::COMPILED) {
CompiledFunction* cf = getCF();
......@@ -591,13 +601,7 @@ Box* getGlobals() {
}
Box* getGlobalsDict() {
Box* globals = getGlobals();
if (!globals)
return NULL;
if (isSubclass(globals->cls, module_cls))
return globals->getAttrWrapper();
return globals;
return getTopPythonFrame()->getGlobalsDict();
}
BoxedModule* getCurrentModule() {
......@@ -890,6 +894,10 @@ CompiledFunction* PythonFrameIterator::getCF() {
return impl->getCF();
}
Box* PythonFrameIterator::getGlobalsDict() {
return impl->getGlobalsDict();
}
FrameInfo* PythonFrameIterator::getFrameInfo() {
return impl->getFrameInfo();
}
......
......@@ -53,6 +53,7 @@ public:
bool exists() { return impl.get() != NULL; }
std::unique_ptr<ExecutionPoint> getExecutionPoint();
Box* fastLocalsToBoxedLocals();
Box* getGlobalsDict();
// Gets the "current version" of this frame: if the frame has executed since
// the iterator was obtained, the methods may return old values. This returns
......
......@@ -130,9 +130,9 @@ public:
FrameInfo* fi = it.getFrameInfo();
if (fi->frame_obj == NULL) {
auto cf = it.getCF();
Box* globals = it.getGlobalsDict();
BoxedFrame* f = fi->frame_obj = new BoxedFrame(std::move(it));
assert(cf->clfunc->source->scoping->areGlobalsFromModule());
f->_globals = cf->clfunc->source->parent_module->getAttrWrapper();
f->_globals = globals;
f->_code = codeForCLFunction(cf->clfunc);
}
......
......@@ -131,3 +131,21 @@ g = {}
l = {}
exec ("a=1; print a", g, l)
print g.keys(), l.keys()
s = """
global a
a = 1
b = 2
def inner():
print sorted(globals().keys()), sorted(locals().keys())
print a
print b
print sorted(globals().keys()), sorted(locals().keys())
inner()
"""
exec s in {}
try:
exec s in {}, {}
raise Exception()
except NameError as e:
print e
......@@ -87,3 +87,15 @@ def f6(n):
else:
get_main_module()
f6(10)
def custom_globals():
s = """
def inner():
import sys
return sys._getframe(1).f_globals
print sorted(inner().keys())
""".strip()
exec s
exec s in {'a':1}
exec s in {'b':2}, {'c':3}
custom_globals()
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