Commit 11522d5e authored by Kevin Modzelewski's avatar Kevin Modzelewski

Merge pull request #1095 from undingen/minor_compat7

fix misc problems (del nonexisting attr, bool ops, super: support nonexisting attr,...)
parents f5c37a00 cfa81fb3
......@@ -14,6 +14,7 @@
#include "core/common.h"
#include "core/types.h"
#include "runtime/int.h"
#include "runtime/objmodel.h"
#include "runtime/types.h"
......@@ -54,33 +55,33 @@ extern "C" Box* boolNew(Box* cls, Box* val) {
}
extern "C" Box* boolAnd(BoxedBool* lhs, BoxedBool* rhs) {
if (lhs->cls != bool_cls)
if (!PyBool_Check(lhs))
raiseExcHelper(TypeError, "descriptor '__and__' requires a 'bool' object but received a '%s'",
getTypeName(lhs));
if (rhs->cls != bool_cls)
return NotImplemented;
if (!PyBool_Check(rhs))
return intAnd(lhs, rhs);
return boxBool(lhs->n && rhs->n);
}
extern "C" Box* boolOr(BoxedBool* lhs, BoxedBool* rhs) {
if (lhs->cls != bool_cls)
if (!PyBool_Check(lhs))
raiseExcHelper(TypeError, "descriptor '__or__' requires a 'bool' object but received a '%s'", getTypeName(lhs));
if (rhs->cls != bool_cls)
return NotImplemented;
if (!PyBool_Check(rhs))
return intOr(lhs, rhs);
return boxBool(lhs->n || rhs->n);
}
extern "C" Box* boolXor(BoxedBool* lhs, BoxedBool* rhs) {
if (lhs->cls != bool_cls)
if (!PyBool_Check(lhs))
raiseExcHelper(TypeError, "descriptor '__xor__' requires a 'bool' object but received a '%s'",
getTypeName(lhs));
if (rhs->cls != bool_cls)
return NotImplemented;
if (!PyBool_Check(rhs))
return intXor(lhs, rhs);
return boxBool(lhs->n ^ rhs->n);
}
......@@ -97,9 +98,10 @@ void setupBool() {
bool_cls->giveAttr("__new__",
new BoxedFunction(FunctionMetadata::create((void*)boolNew, UNKNOWN, 2, false, false), { None }));
bool_cls->giveAttr("__and__", new BoxedFunction(FunctionMetadata::create((void*)boolAnd, BOXED_BOOL, 2)));
bool_cls->giveAttr("__or__", new BoxedFunction(FunctionMetadata::create((void*)boolOr, BOXED_BOOL, 2)));
bool_cls->giveAttr("__xor__", new BoxedFunction(FunctionMetadata::create((void*)boolXor, BOXED_BOOL, 2)));
// TODO: type specialize
bool_cls->giveAttr("__and__", new BoxedFunction(FunctionMetadata::create((void*)boolAnd, UNKNOWN, 2)));
bool_cls->giveAttr("__or__", new BoxedFunction(FunctionMetadata::create((void*)boolOr, UNKNOWN, 2)));
bool_cls->giveAttr("__xor__", new BoxedFunction(FunctionMetadata::create((void*)boolXor, UNKNOWN, 2)));
bool_cls->freeze();
bool_cls->tp_hash = (hashfunc)bool_hash;
......
......@@ -587,7 +587,14 @@ Box* instanceDelattr(Box* _inst, Box* _attr) {
return runtimeCall(delattr, ArgPassSpec(1), _attr, NULL, NULL, NULL, NULL);
}
_inst->delattr(attr, NULL);
if (_inst->hasattr(attr))
_inst->delattr(attr, NULL);
else {
BoxedClassobj* clsobj = (BoxedClassobj*)inst->inst_cls;
RELEASE_ASSERT(PyClass_Check(clsobj), "");
raiseExcHelper(AttributeError, "%.50s instance has no attribute '%.400s'", clsobj->name->c_str(),
attr->c_str());
}
return None;
}
......
......@@ -46,8 +46,10 @@ extern "C" Box* intGe(BoxedInt* lhs, Box* rhs);
extern "C" Box* intLShift(BoxedInt* lhs, Box* rhs);
extern "C" Box* intMod(BoxedInt* lhs, Box* rhs);
extern "C" Box* intMul(BoxedInt* lhs, Box* rhs);
extern "C" Box* intOr(BoxedInt* lhs, Box* rhs);
extern "C" Box* intRShift(BoxedInt* lhs, Box* rhs);
extern "C" Box* intSub(BoxedInt* lhs, Box* rhs);
extern "C" Box* intXor(BoxedInt* lhs, Box* rhs);
extern "C" Box* intInvert(BoxedInt* v);
extern "C" Box* intPos(BoxedInt* v);
extern "C" Box* intNeg(BoxedInt* v);
......
......@@ -940,7 +940,7 @@ extern "C" Box* PyList_GetSlice(PyObject* a, Py_ssize_t ilow, Py_ssize_t ihigh)
return listGetitemSlice<CAPI>(self, new BoxedSlice(boxInt(ilow), boxInt(ihigh), boxInt(1)));
}
static inline int list_contains_shared(BoxedList* self, Box* elt) {
static inline int listContainsShared(BoxedList* self, Box* elt) {
assert(PyList_Check(self));
int size = self->size;
......@@ -962,7 +962,12 @@ static inline int list_contains_shared(BoxedList* self, Box* elt) {
}
static int list_contains(PyListObject* a, PyObject* el) noexcept {
return list_contains_shared((BoxedList*)a, el);
try {
return listContainsShared((BoxedList*)a, el);
} catch (ExcInfo e) {
setCAPIException(e);
return -1;
}
}
static PyObject* list_repeat(PyListObject* a, Py_ssize_t n) noexcept {
......@@ -1004,7 +1009,7 @@ static PyObject* list_repeat(PyListObject* a, Py_ssize_t n) noexcept {
}
Box* listContains(BoxedList* self, Box* elt) {
return boxBool(list_contains_shared(self, elt));
return boxBool(listContainsShared(self, elt));
}
Box* listCount(BoxedList* self, Box* elt) {
......
......@@ -127,10 +127,10 @@ Box* superGetattribute(Box* _s, Box* _attr) {
}
}
Box* r = typeLookup(s->cls, attr);
// TODO implement this
RELEASE_ASSERT(r, "should call the equivalent of objectGetattr here");
return processDescriptor(r, s, s->cls);
Box* rtn = PyObject_GenericGetAttr(s, attr);
if (!rtn)
throwCAPIException();
return rtn;
}
Box* super_getattro(Box* _s, Box* _attr) noexcept {
......
......@@ -31,10 +31,14 @@ print isinstance(True, int)
print isinstance(False, int)
for lhs in (True, False):
for rhs in (True, False):
print lhs | rhs
print lhs & rhs
print lhs ^ rhs
for rhs in (True, False, 1, 1.0):
try:
print lhs.__and__(rhs), lhs.__or__(rhs), lhs.__xor__(rhs)
print lhs | rhs
print lhs & rhs
print lhs ^ rhs
except Exception as e:
print e
print range(False, True, True)
......
......@@ -211,3 +211,11 @@ print l
"""
print repr(list.__hash__)
class RaisingCmp(object):
def __eq__(self, other):
1/0
try:
RaisingCmp() in [1, 2, 3]
except ZeroDivisionError as e:
print e
......@@ -387,6 +387,10 @@ try:
C.doesnt_exist
except AttributeError as e:
print e
try:
del C().doesnt_exist
except AttributeError as e:
print e
class C():
......
......@@ -19,6 +19,11 @@ class C(B):
def f(self):
print "C.f()"
super(C, self).f()
print super(C, self).__thisclass__
try:
super(C, self).does_not_exist
except AttributeError as e:
print e
c = C(1, 2)
print c.arg1
......
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