Commit d84100af authored by Kevin Modzelewski's avatar Kevin Modzelewski

Merge pull request #140 from tjhance/basestring

basestring
parents d021017c 69c1a6d4
...@@ -684,6 +684,7 @@ void setupBuiltins() { ...@@ -684,6 +684,7 @@ void setupBuiltins() {
builtins_module->giveAttr("dir", new BoxedFunction(boxRTFunction((void*)dir, LIST, 1, 1, false, false), { NULL })); builtins_module->giveAttr("dir", new BoxedFunction(boxRTFunction((void*)dir, LIST, 1, 1, false, false), { NULL }));
builtins_module->giveAttr("object", object_cls); builtins_module->giveAttr("object", object_cls);
builtins_module->giveAttr("str", str_cls); builtins_module->giveAttr("str", str_cls);
builtins_module->giveAttr("basestring", basestring_cls);
builtins_module->giveAttr("int", int_cls); builtins_module->giveAttr("int", int_cls);
builtins_module->giveAttr("long", long_cls); builtins_module->giveAttr("long", long_cls);
builtins_module->giveAttr("float", float_cls); builtins_module->giveAttr("float", float_cls);
......
...@@ -353,6 +353,10 @@ extern "C" Box* strNew(BoxedClass* cls, Box* obj) { ...@@ -353,6 +353,10 @@ extern "C" Box* strNew(BoxedClass* cls, Box* obj) {
return str(obj); return str(obj);
} }
extern "C" Box* basestringNew(BoxedClass* cls, Box* args, Box* kwargs) {
raiseExcHelper(TypeError, "The basestring type cannot be instantiated");
}
Box* _strSlice(BoxedString* self, i64 start, i64 stop, i64 step) { Box* _strSlice(BoxedString* self, i64 start, i64 stop, i64 step) {
assert(self->cls == str_cls); assert(self->cls == str_cls);
...@@ -954,6 +958,13 @@ void setupStr() { ...@@ -954,6 +958,13 @@ void setupStr() {
{ boxStrConstant("") })); { boxStrConstant("") }));
str_cls->freeze(); str_cls->freeze();
basestring_cls->giveAttr(
"__doc__", boxStrConstant("Type basestring cannot be instantiated; it is the base for str and unicode."));
basestring_cls->giveAttr("__new__",
new BoxedFunction(boxRTFunction((void*)basestringNew, UNKNOWN, 1, 0, true, true)));
basestring_cls->giveAttr("__name__", boxStrConstant("basestring"));
basestring_cls->freeze();
} }
void teardownStr() { void teardownStr() {
......
...@@ -300,7 +300,7 @@ extern "C" void closureGCHandler(GCVisitor* v, Box* b) { ...@@ -300,7 +300,7 @@ extern "C" void closureGCHandler(GCVisitor* v, Box* b) {
extern "C" { extern "C" {
BoxedClass* object_cls, *type_cls, *none_cls, *bool_cls, *int_cls, *float_cls, *str_cls, *function_cls, BoxedClass* object_cls, *type_cls, *none_cls, *bool_cls, *int_cls, *float_cls, *str_cls, *function_cls,
*instancemethod_cls, *list_cls, *slice_cls, *module_cls, *dict_cls, *tuple_cls, *file_cls, *member_cls, *instancemethod_cls, *list_cls, *slice_cls, *module_cls, *dict_cls, *tuple_cls, *file_cls, *member_cls,
*closure_cls, *generator_cls, *complex_cls; *closure_cls, *generator_cls, *complex_cls, *basestring_cls;
BoxedTuple* EmptyTuple; BoxedTuple* EmptyTuple;
...@@ -646,12 +646,16 @@ void setupRuntime() { ...@@ -646,12 +646,16 @@ void setupRuntime() {
None = new Box(none_cls); None = new Box(none_cls);
gc::registerPermanentRoot(None); gc::registerPermanentRoot(None);
// You can't actually have an instance of basestring
basestring_cls = new BoxedClass(type_cls, object_cls, NULL, 0, sizeof(Box), false);
// TODO we leak all the string data! // TODO we leak all the string data!
str_cls = new BoxedClass(type_cls, object_cls, NULL, 0, sizeof(BoxedString), false); str_cls = new BoxedClass(type_cls, basestring_cls, NULL, 0, sizeof(BoxedString), false);
// It wasn't safe to add __base__ attributes until object+type+str are set up, so do that now: // It wasn't safe to add __base__ attributes until object+type+str are set up, so do that now:
type_cls->giveAttr("__base__", object_cls); type_cls->giveAttr("__base__", object_cls);
str_cls->giveAttr("__base__", object_cls); basestring_cls->giveAttr("__base__", object_cls);
str_cls->giveAttr("__base__", basestring_cls);
none_cls->giveAttr("__base__", object_cls); none_cls->giveAttr("__base__", object_cls);
object_cls->giveAttr("__base__", None); object_cls->giveAttr("__base__", None);
......
...@@ -77,7 +77,7 @@ Box* getSysStdout(); ...@@ -77,7 +77,7 @@ Box* getSysStdout();
extern "C" { extern "C" {
extern BoxedClass* object_cls, *type_cls, *bool_cls, *int_cls, *long_cls, *float_cls, *str_cls, *function_cls, extern BoxedClass* object_cls, *type_cls, *bool_cls, *int_cls, *long_cls, *float_cls, *str_cls, *function_cls,
*none_cls, *instancemethod_cls, *list_cls, *slice_cls, *module_cls, *dict_cls, *tuple_cls, *file_cls, *xrange_cls, *none_cls, *instancemethod_cls, *list_cls, *slice_cls, *module_cls, *dict_cls, *tuple_cls, *file_cls, *xrange_cls,
*member_cls, *method_cls, *closure_cls, *generator_cls, *complex_cls; *member_cls, *method_cls, *closure_cls, *generator_cls, *complex_cls, *basestring_cls;
} }
extern "C" { extern Box* None, *NotImplemented, *True, *False; } extern "C" { extern Box* None, *NotImplemented, *True, *False; }
extern "C" { extern "C" {
......
print isinstance("", basestring)
print isinstance(3, basestring)
print basestring.__doc__
# should raise an exception
t = basestring.__new__(basestring)
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