Commit 9c86cf20 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Some misc stuff to get a benchmark working

1-arg version of exception constructors
str*bool -- who would have thought
parent 6249512d
...@@ -433,6 +433,7 @@ public: ...@@ -433,6 +433,7 @@ public:
Box* exceptionNew2(BoxedClass* cls, Box* message) { Box* exceptionNew2(BoxedClass* cls, Box* message) {
assert(cls->tp_basicsize == sizeof(BoxedException)); assert(cls->tp_basicsize == sizeof(BoxedException));
Box* r = new BoxedException(cls); Box* r = new BoxedException(cls);
// TODO: maybe this should be a MemberDescriptor?
r->giveAttr("message", message); r->giveAttr("message", message);
return r; return r;
} }
...@@ -463,10 +464,13 @@ static BoxedClass* makeBuiltinException(BoxedClass* base, const char* name) { ...@@ -463,10 +464,13 @@ static BoxedClass* makeBuiltinException(BoxedClass* base, const char* name) {
= new BoxedClass(type_cls, base, NULL, offsetof(BoxedException, attrs), sizeof(BoxedException), false); = new BoxedClass(type_cls, base, NULL, offsetof(BoxedException, attrs), sizeof(BoxedException), false);
cls->giveAttr("__name__", boxStrConstant(name)); cls->giveAttr("__name__", boxStrConstant(name));
// TODO these should be on the base Exception class: if (base == object_cls) {
cls->giveAttr("__new__", new BoxedFunction(boxRTFunction((void*)exceptionNew1, UNKNOWN, 1))); cls->giveAttr("__new__", new BoxedFunction(boxRTFunction((void*)exceptionNew2, UNKNOWN, 2, 1, false, false),
{ boxStrConstant("") }));
cls->giveAttr("__str__", new BoxedFunction(boxRTFunction((void*)exceptionStr, STR, 1))); cls->giveAttr("__str__", new BoxedFunction(boxRTFunction((void*)exceptionStr, STR, 1)));
cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)exceptionRepr, STR, 1))); cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)exceptionRepr, STR, 1)));
}
cls->freeze(); cls->freeze();
builtins_module->giveAttr(name, cls); builtins_module->giveAttr(name, cls);
......
...@@ -211,14 +211,18 @@ extern "C" Box* strMod(BoxedString* lhs, Box* rhs) { ...@@ -211,14 +211,18 @@ extern "C" Box* strMod(BoxedString* lhs, Box* rhs) {
return boxString(os.str()); return boxString(os.str());
} }
extern "C" BoxedString* strMul(BoxedString* lhs, BoxedInt* rhs) { extern "C" Box* strMul(BoxedString* lhs, Box* rhs) {
assert(lhs->cls == str_cls); assert(lhs->cls == str_cls);
assert(rhs->cls == int_cls);
RELEASE_ASSERT(rhs->n >= 0, ""); int n;
if (isSubclass(rhs->cls, int_cls))
n = static_cast<BoxedInt*>(rhs)->n;
else if (isSubclass(rhs->cls, bool_cls))
n = static_cast<BoxedBool*>(rhs)->b;
else
return NotImplemented;
int sz = lhs->s.size(); int sz = lhs->s.size();
int n = rhs->n;
char* buf = new char[sz * n + 1]; char* buf = new char[sz * n + 1];
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
memcpy(buf + (sz * i), lhs->s.c_str(), sz); memcpy(buf + (sz * i), lhs->s.c_str(), sz);
......
...@@ -74,3 +74,20 @@ def f12(): ...@@ -74,3 +74,20 @@ def f12():
except (KeyError, IndexError), e: except (KeyError, IndexError), e:
print e print e
f12() f12()
def f13():
try:
raise IndexError
except Exception, e:
print repr(e.message)
try:
raise IndexError()
except Exception, e:
print repr(e.message)
try:
raise IndexError(1)
except Exception, e:
print repr(e.message)
f13()
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