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