Commit 3d94e842 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Merge pull request #320 from tjhance/builtin-function-name

__name__ for builtin functions
parents 0084e712 f6160517
This diff is collapsed.
......@@ -40,9 +40,11 @@ static Box* enable() {
void setupGC() {
BoxedModule* gc_module = createModule("gc", "__builtin__");
gc_module->giveAttr("collect", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)gcCollect, NONE, 0)));
gc_module->giveAttr("isenabled", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)isEnabled, BOXED_BOOL, 0)));
gc_module->giveAttr("disable", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)disable, NONE, 0)));
gc_module->giveAttr("enable", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)enable, NONE, 0)));
gc_module->giveAttr("collect",
new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)gcCollect, NONE, 0), "collect"));
gc_module->giveAttr("isenabled",
new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)isEnabled, BOXED_BOOL, 0), "isenabled"));
gc_module->giveAttr("disable", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)disable, NONE, 0), "disable"));
gc_module->giveAttr("enable", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)enable, NONE, 0), "enable"));
}
}
......@@ -51,6 +51,7 @@ static Box* setOption(Box* option, Box* value) {
void setupPyston() {
pyston_module = createModule("__pyston__", "__builtin__");
pyston_module->giveAttr("setOption", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)setOption, UNKNOWN, 2)));
pyston_module->giveAttr("setOption",
new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)setOption, UNKNOWN, 2), "setOption"));
}
}
......@@ -233,11 +233,12 @@ void setupSys() {
sys_module->giveAttr("stdin", new BoxedFile(stdin, "<stdin>", "r"));
sys_module->giveAttr("stderr", new BoxedFile(stderr, "<stderr>", "w"));
sys_module->giveAttr("exc_info",
new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)sysExcInfo, BOXED_TUPLE, 0)));
sys_module->giveAttr("exc_clear", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)sysExcClear, NONE, 0)));
sys_module->giveAttr(
"exit", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)sysExit, NONE, 1, 1, false, false), { None }));
"exc_info", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)sysExcInfo, BOXED_TUPLE, 0), "exc_info"));
sys_module->giveAttr("exc_clear",
new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)sysExcClear, NONE, 0), "exc_clear"));
sys_module->giveAttr("exit", new BoxedBuiltinFunctionOrMethod(
boxRTFunction((void*)sysExit, NONE, 1, 1, false, false), "exit", { None }));
sys_module->giveAttr("warnoptions", new BoxedList());
sys_module->giveAttr("py3kwarning", False);
......
......@@ -169,14 +169,15 @@ Box* stackSize() {
void setupThread() {
thread_module = createModule("thread", "__builtin__");
thread_module->giveAttr("start_new_thread",
new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)startNewThread, BOXED_INT, 2)));
thread_module->giveAttr("allocate_lock",
new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)allocateLock, UNKNOWN, 0)));
thread_module->giveAttr("get_ident",
new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)getIdent, BOXED_INT, 0)));
thread_module->giveAttr("stack_size",
new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)stackSize, BOXED_INT, 0)));
thread_module->giveAttr(
"start_new_thread",
new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)startNewThread, BOXED_INT, 2), "start_new_thread"));
thread_module->giveAttr("allocate_lock", new BoxedBuiltinFunctionOrMethod(
boxRTFunction((void*)allocateLock, UNKNOWN, 0), "allocate_lock"));
thread_module->giveAttr(
"get_ident", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)getIdent, BOXED_INT, 0), "get_ident"));
thread_module->giveAttr(
"stack_size", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)stackSize, BOXED_INT, 0), "stack_size"));
thread_lock_cls = new BoxedHeapClass(object_cls, NULL, 0, sizeof(BoxedThreadLock), false, "lock");
thread_lock_cls->giveAttr("__module__", boxStrConstant("thread"));
......
......@@ -302,7 +302,7 @@ Box* impFindModule(Box* _name) {
void setupImport() {
BoxedModule* imp_module = createModule("imp", "__builtin__");
imp_module->giveAttr("find_module",
new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)impFindModule, UNKNOWN, 1)));
imp_module->giveAttr("find_module", new BoxedBuiltinFunctionOrMethod(
boxRTFunction((void*)impFindModule, UNKNOWN, 1), "find_module"));
}
}
......@@ -1533,8 +1533,8 @@ bool dataDescriptorSetSpecialCases(Box* obj, Box* val, Box* descr, SetattrRewrit
// TODO type checking goes here
if (getset_descr->set == NULL) {
raiseExcHelper(AttributeError, "attribute '%s' of '%s' object is not writable", attr_name.c_str(),
getTypeName(getset_descr));
raiseExcHelper(AttributeError, "attribute '%s' of '%s' objects is not writable", attr_name.c_str(),
getTypeName(obj));
}
if (rewrite_args) {
......
......@@ -321,6 +321,19 @@ BoxedFunction::BoxedFunction(CLFunction* f, std::initializer_list<Box*> defaults
}
}
BoxedBuiltinFunctionOrMethod::BoxedBuiltinFunctionOrMethod(CLFunction* f, const char* name)
: BoxedBuiltinFunctionOrMethod(f, name, {}) {
}
BoxedBuiltinFunctionOrMethod::BoxedBuiltinFunctionOrMethod(CLFunction* f, const char* name,
std::initializer_list<Box*> defaults, BoxedClosure* closure,
bool isGenerator)
: BoxedFunctionBase(f, defaults, closure, isGenerator) {
assert(name);
this->name = static_cast<BoxedString*>(boxString(name));
}
extern "C" void functionGCHandler(GCVisitor* v, Box* b) {
boxGCHandler(v, b);
......@@ -685,6 +698,18 @@ static void func_set_name(Box* b, Box* v, void*) {
func->name = static_cast<BoxedString*>(v);
}
static Box* builtin_function_or_method_name(Box* b, void*) {
// In CPython, these guys just store char*, and it gets wrapped here
// But we already share the BoxedString* field with BoxedFunctions...
// so it's more convenient to just use that, which is what we do here.
// Is there any advantage to using the char* way, here?
assert(b->cls == builtin_function_or_method_cls);
BoxedBuiltinFunctionOrMethod* func = static_cast<BoxedBuiltinFunctionOrMethod*>(b);
assert(func->name);
return func->name;
}
static Box* functionNonzero(BoxedFunction* self) {
return True;
}
......@@ -1302,6 +1327,8 @@ void setupRuntime() {
new BoxedMemberDescriptor(BoxedMemberDescriptor::OBJECT, offsetof(BoxedBuiltinFunctionOrMethod, modname)));
builtin_function_or_method_cls->giveAttr(
"__repr__", new BoxedFunction(boxRTFunction((void*)builtinFunctionOrMethodRepr, STR, 1)));
builtin_function_or_method_cls->giveAttr("__name__",
new BoxedGetsetDescriptor(builtin_function_or_method_name, NULL, NULL));
builtin_function_or_method_cls->freeze();
instancemethod_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)instancemethodRepr, STR, 1)));
......
......@@ -476,10 +476,9 @@ public:
class BoxedBuiltinFunctionOrMethod : public BoxedFunctionBase {
public:
BoxedBuiltinFunctionOrMethod(CLFunction* f) : BoxedFunctionBase(f) {}
BoxedBuiltinFunctionOrMethod(CLFunction* f, std::initializer_list<Box*> defaults, BoxedClosure* closure = NULL,
bool isGenerator = false)
: BoxedFunctionBase(f, defaults, closure, isGenerator) {}
BoxedBuiltinFunctionOrMethod(CLFunction* f, const char* name);
BoxedBuiltinFunctionOrMethod(CLFunction* f, const char* name, std::initializer_list<Box*> defaults,
BoxedClosure* closure = NULL, bool isGenerator = false);
DEFAULT_CLASS(builtin_function_or_method_cls);
};
......
......@@ -32,6 +32,7 @@ set_name(int, "bob")
set_name(C, 5)
set_name(C, "b\0b")
set_name(C, "car")
set_name(C, "")
def g():
pass
......@@ -39,6 +40,7 @@ print g.__name__
set_name(g, "bob")
set_name(g, 5)
set_name(g, "b\0b")
set_name(g, "")
f = lambda x : 5
print f.__name__
......@@ -46,3 +48,9 @@ set_name(f, "bob")
set_name(f, 5)
set_name(f, "b\0b")
#del_name(f)
set_name(f, "")
print sorted.__name__
# should all fail:
set_name(sorted, "blah")
set_name(sorted, 5)
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