Commit f6160517 authored by Travis Hance's avatar Travis Hance

__name__ for builtin functions

parent befacc74
This diff is collapsed.
...@@ -40,9 +40,11 @@ static Box* enable() { ...@@ -40,9 +40,11 @@ static Box* enable() {
void setupGC() { void setupGC() {
BoxedModule* gc_module = createModule("gc", "__builtin__"); BoxedModule* gc_module = createModule("gc", "__builtin__");
gc_module->giveAttr("collect", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)gcCollect, NONE, 0))); gc_module->giveAttr("collect",
gc_module->giveAttr("isenabled", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)isEnabled, BOXED_BOOL, 0))); new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)gcCollect, NONE, 0), "collect"));
gc_module->giveAttr("disable", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)disable, NONE, 0))); gc_module->giveAttr("isenabled",
gc_module->giveAttr("enable", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)enable, NONE, 0))); 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) { ...@@ -51,6 +51,7 @@ static Box* setOption(Box* option, Box* value) {
void setupPyston() { void setupPyston() {
pyston_module = createModule("__pyston__", "__builtin__"); 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() { ...@@ -233,11 +233,12 @@ void setupSys() {
sys_module->giveAttr("stdin", new BoxedFile(stdin, "<stdin>", "r")); sys_module->giveAttr("stdin", new BoxedFile(stdin, "<stdin>", "r"));
sys_module->giveAttr("stderr", new BoxedFile(stderr, "<stderr>", "w")); 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( 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("warnoptions", new BoxedList());
sys_module->giveAttr("py3kwarning", False); sys_module->giveAttr("py3kwarning", False);
......
...@@ -169,14 +169,15 @@ Box* stackSize() { ...@@ -169,14 +169,15 @@ Box* stackSize() {
void setupThread() { void setupThread() {
thread_module = createModule("thread", "__builtin__"); thread_module = createModule("thread", "__builtin__");
thread_module->giveAttr("start_new_thread", thread_module->giveAttr(
new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)startNewThread, BOXED_INT, 2))); "start_new_thread",
thread_module->giveAttr("allocate_lock", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)startNewThread, BOXED_INT, 2), "start_new_thread"));
new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)allocateLock, UNKNOWN, 0))); thread_module->giveAttr("allocate_lock", new BoxedBuiltinFunctionOrMethod(
thread_module->giveAttr("get_ident", boxRTFunction((void*)allocateLock, UNKNOWN, 0), "allocate_lock"));
new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)getIdent, BOXED_INT, 0))); thread_module->giveAttr(
thread_module->giveAttr("stack_size", "get_ident", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)getIdent, BOXED_INT, 0), "get_ident"));
new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)stackSize, BOXED_INT, 0))); 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 = new BoxedHeapClass(object_cls, NULL, 0, sizeof(BoxedThreadLock), false, "lock");
thread_lock_cls->giveAttr("__module__", boxStrConstant("thread")); thread_lock_cls->giveAttr("__module__", boxStrConstant("thread"));
......
...@@ -302,7 +302,7 @@ Box* impFindModule(Box* _name) { ...@@ -302,7 +302,7 @@ Box* impFindModule(Box* _name) {
void setupImport() { void setupImport() {
BoxedModule* imp_module = createModule("imp", "__builtin__"); BoxedModule* imp_module = createModule("imp", "__builtin__");
imp_module->giveAttr("find_module", imp_module->giveAttr("find_module", new BoxedBuiltinFunctionOrMethod(
new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)impFindModule, UNKNOWN, 1))); boxRTFunction((void*)impFindModule, UNKNOWN, 1), "find_module"));
} }
} }
...@@ -1533,8 +1533,8 @@ bool dataDescriptorSetSpecialCases(Box* obj, Box* val, Box* descr, SetattrRewrit ...@@ -1533,8 +1533,8 @@ bool dataDescriptorSetSpecialCases(Box* obj, Box* val, Box* descr, SetattrRewrit
// TODO type checking goes here // TODO type checking goes here
if (getset_descr->set == NULL) { if (getset_descr->set == NULL) {
raiseExcHelper(AttributeError, "attribute '%s' of '%s' object is not writable", attr_name.c_str(), raiseExcHelper(AttributeError, "attribute '%s' of '%s' objects is not writable", attr_name.c_str(),
getTypeName(getset_descr)); getTypeName(obj));
} }
if (rewrite_args) { if (rewrite_args) {
......
...@@ -321,6 +321,19 @@ BoxedFunction::BoxedFunction(CLFunction* f, std::initializer_list<Box*> defaults ...@@ -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) { extern "C" void functionGCHandler(GCVisitor* v, Box* b) {
boxGCHandler(v, b); boxGCHandler(v, b);
...@@ -685,6 +698,18 @@ static void func_set_name(Box* b, Box* v, void*) { ...@@ -685,6 +698,18 @@ static void func_set_name(Box* b, Box* v, void*) {
func->name = static_cast<BoxedString*>(v); 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) { static Box* functionNonzero(BoxedFunction* self) {
return True; return True;
} }
...@@ -1302,6 +1327,8 @@ void setupRuntime() { ...@@ -1302,6 +1327,8 @@ void setupRuntime() {
new BoxedMemberDescriptor(BoxedMemberDescriptor::OBJECT, offsetof(BoxedBuiltinFunctionOrMethod, modname))); new BoxedMemberDescriptor(BoxedMemberDescriptor::OBJECT, offsetof(BoxedBuiltinFunctionOrMethod, modname)));
builtin_function_or_method_cls->giveAttr( builtin_function_or_method_cls->giveAttr(
"__repr__", new BoxedFunction(boxRTFunction((void*)builtinFunctionOrMethodRepr, STR, 1))); "__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(); builtin_function_or_method_cls->freeze();
instancemethod_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)instancemethodRepr, STR, 1))); instancemethod_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)instancemethodRepr, STR, 1)));
......
...@@ -476,10 +476,9 @@ public: ...@@ -476,10 +476,9 @@ public:
class BoxedBuiltinFunctionOrMethod : public BoxedFunctionBase { class BoxedBuiltinFunctionOrMethod : public BoxedFunctionBase {
public: public:
BoxedBuiltinFunctionOrMethod(CLFunction* f) : BoxedFunctionBase(f) {} BoxedBuiltinFunctionOrMethod(CLFunction* f, const char* name);
BoxedBuiltinFunctionOrMethod(CLFunction* f, std::initializer_list<Box*> defaults, BoxedClosure* closure = NULL, BoxedBuiltinFunctionOrMethod(CLFunction* f, const char* name, std::initializer_list<Box*> defaults,
bool isGenerator = false) BoxedClosure* closure = NULL, bool isGenerator = false);
: BoxedFunctionBase(f, defaults, closure, isGenerator) {}
DEFAULT_CLASS(builtin_function_or_method_cls); DEFAULT_CLASS(builtin_function_or_method_cls);
}; };
......
...@@ -32,6 +32,7 @@ set_name(int, "bob") ...@@ -32,6 +32,7 @@ set_name(int, "bob")
set_name(C, 5) set_name(C, 5)
set_name(C, "b\0b") set_name(C, "b\0b")
set_name(C, "car") set_name(C, "car")
set_name(C, "")
def g(): def g():
pass pass
...@@ -39,6 +40,7 @@ print g.__name__ ...@@ -39,6 +40,7 @@ print g.__name__
set_name(g, "bob") set_name(g, "bob")
set_name(g, 5) set_name(g, 5)
set_name(g, "b\0b") set_name(g, "b\0b")
set_name(g, "")
f = lambda x : 5 f = lambda x : 5
print f.__name__ print f.__name__
...@@ -46,3 +48,9 @@ set_name(f, "bob") ...@@ -46,3 +48,9 @@ set_name(f, "bob")
set_name(f, 5) set_name(f, 5)
set_name(f, "b\0b") set_name(f, "b\0b")
#del_name(f) #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