Commit 31aa93b1 authored by Marius Wachtler's avatar Marius Wachtler

add func.__globals__/func.func_globals

parent d217f1bc
......@@ -848,6 +848,17 @@ static Box* functionDefaults(Box* self, void*) {
return BoxedTuple::create(func->ndefaults, &func->defaults->elts[0]);
}
static Box* functionGlobals(Box* self, void*) {
assert(self->cls == function_cls);
BoxedFunction* func = static_cast<BoxedFunction*>(self);
if (func->globals) {
assert(!func->f->source || !func->f->source->scoping->areGlobalsFromModule());
return func->globals;
}
assert(func->f->source);
return getattr(func->f->source->parent_module, "__dict__");
}
static void functionSetDefaults(Box* b, Box* v, void*) {
RELEASE_ASSERT(v, "can't delete __defaults__");
......@@ -2430,6 +2441,7 @@ void setupRuntime() {
offsetof(BoxedFunction, modname), false));
function_cls->giveAttr(
"__doc__", new BoxedMemberDescriptor(BoxedMemberDescriptor::OBJECT, offsetof(BoxedFunction, doc), false));
function_cls->giveAttr("__globals__", new (pyston_getset_cls) BoxedGetsetDescriptor(functionGlobals, NULL, NULL));
function_cls->giveAttr("__get__", new BoxedFunction(boxRTFunction((void*)functionGet, UNKNOWN, 3)));
function_cls->giveAttr("__call__",
new BoxedFunction(boxRTFunction((void*)functionCall, UNKNOWN, 1, 0, true, true)));
......@@ -2440,6 +2452,7 @@ void setupRuntime() {
function_cls->giveAttr("func_defaults",
new (pyston_getset_cls) BoxedGetsetDescriptor(functionDefaults, functionSetDefaults, NULL));
function_cls->giveAttr("__defaults__", function_cls->getattr("func_defaults"));
function_cls->giveAttr("func_globals", function_cls->getattr("__globals__"));
function_cls->freeze();
builtin_function_or_method_cls->giveAttr(
......
......@@ -40,3 +40,11 @@ except TypeError as e:
def func_without_defaults():
pass
print repr(func_without_defaults.__defaults__)
print func_without_defaults.func_globals == globals()
import os
print os.renames.__globals__ == os.__dict__
print os.renames.__globals__ == globals()
d = {}
exec "def foo(): pass" in d
print d["foo"].func_globals == d
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