Commit f6160517 authored by Travis Hance's avatar Travis Hance

__name__ for builtin functions

parent befacc74
......@@ -980,8 +980,8 @@ void setupBuiltins() {
builtins_module->giveAttr("__debug__", False);
builtins_module->giveAttr("print",
new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)print, NONE, 0, 0, true, true)));
builtins_module->giveAttr(
"print", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)print, NONE, 0, 0, true, true), "print"));
notimplemented_cls = new BoxedHeapClass(object_cls, NULL, 0, sizeof(Box), false, "NotImplementedType");
notimplemented_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)notimplementedRepr, STR, 1)));
......@@ -992,8 +992,8 @@ void setupBuiltins() {
builtins_module->giveAttr("NotImplemented", NotImplemented);
builtins_module->giveAttr("NotImplementedType", notimplemented_cls);
builtins_module->giveAttr("all", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)all, BOXED_BOOL, 1)));
builtins_module->giveAttr("any", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)any, BOXED_BOOL, 1)));
builtins_module->giveAttr("all", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)all, BOXED_BOOL, 1), "all"));
builtins_module->giveAttr("any", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)any, BOXED_BOOL, 1), "any"));
BaseException = makeBuiltinException(object_cls, "BaseException", sizeof(BoxedException));
Exception = makeBuiltinException(BaseException, "Exception");
......@@ -1056,60 +1056,67 @@ void setupBuiltins() {
EnvironmentError->giveAttr("__str__",
new BoxedFunction(boxRTFunction((void*)BoxedEnvironmentError::__str__, UNKNOWN, 1)));
repr_obj = new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)repr, UNKNOWN, 1));
repr_obj = new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)repr, UNKNOWN, 1), "repr");
builtins_module->giveAttr("repr", repr_obj);
auto len_func = boxRTFunction((void*)len, UNKNOWN, 1);
len_func->internal_callable = lenCallInternal;
len_obj = new BoxedBuiltinFunctionOrMethod(len_func);
len_obj = new BoxedBuiltinFunctionOrMethod(len_func, "len");
builtins_module->giveAttr("len", len_obj);
hash_obj = new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)hash, UNKNOWN, 1));
hash_obj = new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)hash, UNKNOWN, 1), "hash");
builtins_module->giveAttr("hash", hash_obj);
abs_obj = new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)abs_, UNKNOWN, 1));
abs_obj = new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)abs_, UNKNOWN, 1), "abs");
builtins_module->giveAttr("abs", abs_obj);
builtins_module->giveAttr("hex", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)hexFunc, UNKNOWN, 1)));
builtins_module->giveAttr("oct", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)octFunc, UNKNOWN, 1)));
builtins_module->giveAttr("hex",
new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)hexFunc, UNKNOWN, 1), "hex"));
builtins_module->giveAttr("oct",
new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)octFunc, UNKNOWN, 1), "oct"));
min_obj = new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)min, UNKNOWN, 1, 0, true, false));
min_obj = new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)min, UNKNOWN, 1, 0, true, false), "min");
builtins_module->giveAttr("min", min_obj);
max_obj = new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)max, UNKNOWN, 1, 0, true, false));
max_obj = new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)max, UNKNOWN, 1, 0, true, false), "max");
builtins_module->giveAttr("max", max_obj);
builtins_module->giveAttr(
"sum", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)sum, UNKNOWN, 2, 1, false, false), { boxInt(0) }));
builtins_module->giveAttr("sum", new BoxedBuiltinFunctionOrMethod(
boxRTFunction((void*)sum, UNKNOWN, 2, 1, false, false), "sum", { boxInt(0) }));
id_obj = new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)id, BOXED_INT, 1));
id_obj = new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)id, BOXED_INT, 1), "id");
builtins_module->giveAttr("id", id_obj);
chr_obj = new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)chr, STR, 1));
chr_obj = new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)chr, STR, 1), "chr");
builtins_module->giveAttr("chr", chr_obj);
ord_obj = new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)ord, BOXED_INT, 1));
ord_obj = new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)ord, BOXED_INT, 1), "ord");
builtins_module->giveAttr("ord", ord_obj);
trap_obj = new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)trap, UNKNOWN, 0));
trap_obj = new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)trap, UNKNOWN, 0), "trap");
builtins_module->giveAttr("trap", trap_obj);
builtins_module->giveAttr("dump", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)pydump, UNKNOWN, 1)));
builtins_module->giveAttr("dump",
new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)pydump, UNKNOWN, 1), "dump"));
builtins_module->giveAttr("getattr", new BoxedBuiltinFunctionOrMethod(
boxRTFunction((void*)getattrFunc, UNKNOWN, 3, 1, false, false), { NULL }));
builtins_module->giveAttr(
"getattr", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)getattrFunc, UNKNOWN, 3, 1, false, false),
"getattr", { NULL }));
builtins_module->giveAttr(
"setattr", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)setattrFunc, UNKNOWN, 3, 0, false, false)));
"setattr",
new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)setattrFunc, UNKNOWN, 3, 0, false, false), "setattr"));
Box* hasattr_obj = new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)hasattr, BOXED_BOOL, 2));
Box* hasattr_obj = new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)hasattr, BOXED_BOOL, 2), "hasattr");
builtins_module->giveAttr("hasattr", hasattr_obj);
Box* isinstance_obj = new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)isinstance_func, BOXED_BOOL, 2));
Box* isinstance_obj
= new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)isinstance_func, BOXED_BOOL, 2), "isinstance");
builtins_module->giveAttr("isinstance", isinstance_obj);
Box* issubclass_obj = new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)issubclass_func, BOXED_BOOL, 2));
Box* issubclass_obj
= new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)issubclass_func, BOXED_BOOL, 2), "issubclass");
builtins_module->giveAttr("issubclass", issubclass_obj);
CLFunction* import_func = boxRTFunction((void*)bltinImport, UNKNOWN, 5, 4, false, false,
ParamNames({ "name", "globals", "locals", "fromlist", "level" }, "", ""));
builtins_module->giveAttr("__import__",
new BoxedBuiltinFunctionOrMethod(import_func, { None, None, None, new BoxedInt(-1) }));
builtins_module->giveAttr("__import__", new BoxedBuiltinFunctionOrMethod(import_func, "__import__",
{ None, None, None, new BoxedInt(-1) }));
enumerate_cls
= new BoxedHeapClass(object_cls, &BoxedEnumerate::gcHandler, 0, sizeof(BoxedEnumerate), false, "enumerate");
......@@ -1127,12 +1134,13 @@ void setupBuiltins() {
CLFunction* sorted_func = createRTFunction(4, 3, false, false, ParamNames({ "", "cmp", "key", "reverse" }, "", ""));
addRTFunction(sorted_func, (void*)sorted, LIST, { UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN });
builtins_module->giveAttr("sorted", new BoxedBuiltinFunctionOrMethod(sorted_func, { None, None, False }));
builtins_module->giveAttr("sorted", new BoxedBuiltinFunctionOrMethod(sorted_func, "sorted", { None, None, False }));
builtins_module->giveAttr("True", True);
builtins_module->giveAttr("False", False);
range_obj = new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)range, LIST, 3, 2, false, false), { NULL, NULL });
range_obj = new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)range, LIST, 3, 2, false, false), "range",
{ NULL, NULL });
builtins_module->giveAttr("range", range_obj);
setupXrange();
......@@ -1140,32 +1148,37 @@ void setupBuiltins() {
open_obj = new BoxedBuiltinFunctionOrMethod(
boxRTFunction((void*)open, typeFromClass(file_cls), 2, 1, false, false, ParamNames({ "name", "mode" }, "", "")),
{ boxStrConstant("r") });
"open", { boxStrConstant("r") });
builtins_module->giveAttr("open", open_obj);
builtins_module->giveAttr(
"globals", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)globals, UNKNOWN, 0, 0, false, false)));
builtins_module->giveAttr(
"locals", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)locals, UNKNOWN, 0, 0, false, false)));
builtins_module->giveAttr("globals", new BoxedBuiltinFunctionOrMethod(
boxRTFunction((void*)globals, UNKNOWN, 0, 0, false, false), "globals"));
builtins_module->giveAttr("locals", new BoxedBuiltinFunctionOrMethod(
boxRTFunction((void*)locals, UNKNOWN, 0, 0, false, false), "locals"));
builtins_module->giveAttr(
"iter", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)getiter, UNKNOWN, 1, 0, false, false)));
"iter", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)getiter, UNKNOWN, 1, 0, false, false), "iter"));
builtins_module->giveAttr(
"reversed", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)getreversed, UNKNOWN, 1, 0, false, false)));
"reversed",
new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)getreversed, UNKNOWN, 1, 0, false, false), "reversed"));
builtins_module->giveAttr("divmod", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)divmod, UNKNOWN, 2)));
builtins_module->giveAttr("divmod",
new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)divmod, UNKNOWN, 2), "divmod"));
builtins_module->giveAttr("execfile", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)execfile, UNKNOWN, 1)));
builtins_module->giveAttr("execfile",
new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)execfile, UNKNOWN, 1), "execfile"));
builtins_module->giveAttr("map", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)map2, LIST, 2)));
builtins_module->giveAttr("reduce", new BoxedBuiltinFunctionOrMethod(
boxRTFunction((void*)reduce, UNKNOWN, 3, 1, false, false), { NULL }));
builtins_module->giveAttr("filter", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)filter2, LIST, 2)));
builtins_module->giveAttr("zip", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)zip2, LIST, 2)));
builtins_module->giveAttr("map", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)map2, LIST, 2), "map"));
builtins_module->giveAttr(
"dir", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)dir, LIST, 1, 1, false, false), { NULL }));
"reduce", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)reduce, UNKNOWN, 3, 1, false, false), "reduce",
{ NULL }));
builtins_module->giveAttr("filter",
new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)filter2, LIST, 2), "filter"));
builtins_module->giveAttr("zip", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)zip2, LIST, 2), "zip"));
builtins_module->giveAttr(
"vars", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)vars, UNKNOWN, 1, 1, false, false), { NULL }));
"dir", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)dir, LIST, 1, 1, false, false), "dir", { NULL }));
builtins_module->giveAttr("vars", new BoxedBuiltinFunctionOrMethod(
boxRTFunction((void*)vars, UNKNOWN, 1, 1, false, false), "vars", { NULL }));
builtins_module->giveAttr("object", object_cls);
builtins_module->giveAttr("str", str_cls);
builtins_module->giveAttr("basestring", basestring_cls);
......
......@@ -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