Commit d6db620d authored by Kevin Modzelewski's avatar Kevin Modzelewski

Merge pull request #366 from undingen/virtualenv_fixes3

Add a few 'long' methods, support tuple sublasses, add uuid test and fix a _struct GC bug
parents 7b3dc080 b4142f71
......@@ -1819,6 +1819,7 @@ cache_struct(PyObject *fmt)
cache = PyDict_New();
if (cache == NULL)
return NULL;
PyGC_AddRoot(cache);
}
s_object = PyDict_GetItem(cache, fmt);
......
......@@ -27,7 +27,7 @@ Box* tupleIterIter(Box* s) {
}
Box* tupleIter(Box* s) {
assert(s->cls == tuple_cls);
assert(isSubclass(s->cls, tuple_cls));
BoxedTuple* self = static_cast<BoxedTuple*>(s);
return new BoxedTupleIterator(self);
}
......
......@@ -972,19 +972,27 @@ Box* longPow(BoxedLong* v1, Box* _v2) {
if (!isSubclass(v1->cls, long_cls))
raiseExcHelper(TypeError, "descriptor '__pow__' requires a 'long' object but received a '%s'", getTypeName(v1));
if (!isSubclass(_v2->cls, long_cls))
return NotImplemented;
BoxedLong* v2 = static_cast<BoxedLong*>(_v2);
if (isSubclass(_v2->cls, long_cls)) {
BoxedLong* v2 = static_cast<BoxedLong*>(_v2);
RELEASE_ASSERT(mpz_sgn(v2->n) >= 0, "");
RELEASE_ASSERT(mpz_fits_ulong_p(v2->n), "");
uint64_t n2 = mpz_get_ui(v2->n);
RELEASE_ASSERT(mpz_sgn(v2->n) >= 0, "");
RELEASE_ASSERT(mpz_fits_ulong_p(v2->n), "");
uint64_t n2 = mpz_get_ui(v2->n);
BoxedLong* r = new BoxedLong();
mpz_init(r->n);
mpz_pow_ui(r->n, v1->n, n2);
return r;
BoxedLong* r = new BoxedLong();
mpz_init(r->n);
mpz_pow_ui(r->n, v1->n, n2);
return r;
} else if (isSubclass(_v2->cls, int_cls)) {
BoxedInt* v2 = static_cast<BoxedInt*>(_v2);
RELEASE_ASSERT(v2->n >= 0, "");
BoxedLong* r = new BoxedLong();
mpz_init(r->n);
mpz_pow_ui(r->n, v1->n, v2->n);
return r;
} else {
return NotImplemented;
}
}
extern "C" Box* longInvert(BoxedLong* v) {
......@@ -1055,6 +1063,8 @@ void setupLong() {
long_cls->giveAttr("__add__", new BoxedFunction(boxRTFunction((void*)longAdd, UNKNOWN, 2)));
long_cls->giveAttr("__radd__", long_cls->getattr("__add__"));
long_cls->giveAttr("__pow__", new BoxedFunction(boxRTFunction((void*)longPow, UNKNOWN, 2)));
long_cls->giveAttr("__and__", new BoxedFunction(boxRTFunction((void*)longAnd, UNKNOWN, 2)));
long_cls->giveAttr("__rand__", long_cls->getattr("__and__"));
long_cls->giveAttr("__or__", new BoxedFunction(boxRTFunction((void*)longOr, UNKNOWN, 2)));
......@@ -1079,6 +1089,7 @@ void setupLong() {
long_cls->giveAttr("__oct__", new BoxedFunction(boxRTFunction((void*)longOct, STR, 1)));
long_cls->giveAttr("__invert__", new BoxedFunction(boxRTFunction((void*)longInvert, UNKNOWN, 1)));
long_cls->giveAttr("__neg__", new BoxedFunction(boxRTFunction((void*)longNeg, UNKNOWN, 1)));
long_cls->giveAttr("__nonzero__", new BoxedFunction(boxRTFunction((void*)longNonzero, BOXED_BOOL, 1)));
long_cls->giveAttr("__hash__", new BoxedFunction(boxRTFunction((void*)longHash, BOXED_INT, 1)));
......
......@@ -85,7 +85,7 @@ extern "C" PyObject* PyTuple_GetItem(PyObject* op, Py_ssize_t i) noexcept {
}
Box* tupleGetitemSlice(BoxedTuple* self, BoxedSlice* slice) {
assert(self->cls == tuple_cls);
assert(isSubclass(self->cls, tuple_cls));
assert(slice->cls == slice_cls);
i64 start, stop, step, length;
......@@ -94,7 +94,7 @@ Box* tupleGetitemSlice(BoxedTuple* self, BoxedSlice* slice) {
}
extern "C" PyObject* PyTuple_GetSlice(PyObject* p, Py_ssize_t low, Py_ssize_t high) noexcept {
RELEASE_ASSERT(p->cls == tuple_cls, ""); // could it be a subclass or something else?
RELEASE_ASSERT(isSubclass(p->cls, tuple_cls), "");
BoxedTuple* t = static_cast<BoxedTuple*>(p);
Py_ssize_t n = t->elts.size();
......@@ -123,7 +123,7 @@ Box* tupleGetitem(BoxedTuple* self, Box* slice) {
}
Box* tupleAdd(BoxedTuple* self, Box* rhs) {
if (rhs->cls != tuple_cls) {
if (!isSubclass(rhs->cls, tuple_cls)) {
return NotImplemented;
}
......@@ -159,7 +159,7 @@ Box* tupleMul(BoxedTuple* self, Box* rhs) {
}
Box* tupleLen(BoxedTuple* t) {
assert(t->cls == tuple_cls);
assert(isSubclass(t->cls, tuple_cls));
return boxInt(t->elts.size());
}
......@@ -169,7 +169,7 @@ extern "C" Py_ssize_t PyTuple_Size(PyObject* op) noexcept {
}
Box* tupleRepr(BoxedTuple* t) {
assert(t->cls == tuple_cls);
assert(isSubclass(t->cls, tuple_cls));
std::ostringstream os("");
os << "(";
......@@ -231,49 +231,49 @@ Box* _tupleCmp(BoxedTuple* lhs, BoxedTuple* rhs, AST_TYPE::AST_TYPE op_type) {
}
Box* tupleLt(BoxedTuple* self, Box* rhs) {
if (rhs->cls != tuple_cls) {
if (!isSubclass(rhs->cls, tuple_cls)) {
return NotImplemented;
}
return _tupleCmp(self, static_cast<BoxedTuple*>(rhs), AST_TYPE::Lt);
}
Box* tupleLe(BoxedTuple* self, Box* rhs) {
if (rhs->cls != tuple_cls) {
if (!isSubclass(rhs->cls, tuple_cls)) {
return NotImplemented;
}
return _tupleCmp(self, static_cast<BoxedTuple*>(rhs), AST_TYPE::LtE);
}
Box* tupleGt(BoxedTuple* self, Box* rhs) {
if (rhs->cls != tuple_cls) {
if (!isSubclass(rhs->cls, tuple_cls)) {
return NotImplemented;
}
return _tupleCmp(self, static_cast<BoxedTuple*>(rhs), AST_TYPE::Gt);
}
Box* tupleGe(BoxedTuple* self, Box* rhs) {
if (rhs->cls != tuple_cls) {
if (!isSubclass(rhs->cls, tuple_cls)) {
return NotImplemented;
}
return _tupleCmp(self, static_cast<BoxedTuple*>(rhs), AST_TYPE::GtE);
}
Box* tupleEq(BoxedTuple* self, Box* rhs) {
if (rhs->cls != tuple_cls) {
if (!isSubclass(rhs->cls, tuple_cls)) {
return NotImplemented;
}
return _tupleCmp(self, static_cast<BoxedTuple*>(rhs), AST_TYPE::Eq);
}
Box* tupleNe(BoxedTuple* self, Box* rhs) {
if (rhs->cls != tuple_cls) {
if (!isSubclass(rhs->cls, tuple_cls)) {
return NotImplemented;
}
return _tupleCmp(self, static_cast<BoxedTuple*>(rhs), AST_TYPE::NotEq);
}
Box* tupleNonzero(BoxedTuple* self) {
RELEASE_ASSERT(self->cls == tuple_cls, "");
RELEASE_ASSERT(isSubclass(self->cls, tuple_cls), "");
return boxBool(self->elts.size() != 0);
}
......@@ -290,7 +290,7 @@ Box* tupleContains(BoxedTuple* self, Box* elt) {
}
Box* tupleHash(BoxedTuple* self) {
assert(self->cls == tuple_cls);
assert(isSubclass(self->cls, tuple_cls));
int64_t rtn = 3527539;
for (Box* e : self->elts) {
......@@ -391,10 +391,9 @@ void setupTuple() {
tuple_cls->giveAttr("__new__", new BoxedFunction(boxRTFunction((void*)tupleNew, UNKNOWN, 1, 0, true, true)));
CLFunction* getitem = createRTFunction(2, 0, 0, 0);
addRTFunction(getitem, (void*)tupleGetitemInt, UNKNOWN,
std::vector<ConcreteCompilerType*>{ BOXED_TUPLE, BOXED_INT });
addRTFunction(getitem, (void*)tupleGetitemSlice, UNKNOWN, std::vector<ConcreteCompilerType*>{ BOXED_TUPLE, SLICE });
addRTFunction(getitem, (void*)tupleGetitem, UNKNOWN, std::vector<ConcreteCompilerType*>{ BOXED_TUPLE, UNKNOWN });
addRTFunction(getitem, (void*)tupleGetitemInt, UNKNOWN, std::vector<ConcreteCompilerType*>{ UNKNOWN, BOXED_INT });
addRTFunction(getitem, (void*)tupleGetitemSlice, UNKNOWN, std::vector<ConcreteCompilerType*>{ UNKNOWN, SLICE });
addRTFunction(getitem, (void*)tupleGetitem, UNKNOWN, std::vector<ConcreteCompilerType*>{ UNKNOWN, UNKNOWN });
tuple_cls->giveAttr("__getitem__", new BoxedFunction(getitem));
tuple_cls->giveAttr("__contains__", new BoxedFunction(boxRTFunction((void*)tupleContains, BOXED_BOOL, 2)));
......
......@@ -53,6 +53,10 @@ print ~(1L)
print ~(10L)
print ~(-10L)
print -(1L)
print 1L**2L
print 1L**2
print long("100", 16)
print long("100", 10)
print long("100", 26)
......
......@@ -71,6 +71,16 @@ t((T(1),), (T(2),1))
print ("hello", "world", ["test"])
class MyTuple(tuple):
pass
mt = MyTuple((1, 2))
print mt < (1, 2)
print (1, 2) < mt
print mt[1]
print mt + (1,)
print list(mt)
print len(mt)
# __add__
print () + ()
print (1, 2, 3) + ()
......
import uuid
print len(str(uuid.uuid1()))
print len(str(uuid.uuid4()))
print uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org')
print uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org')
x = uuid.UUID('{00010203-0405-0607-0809-0a0b0c0d0e0f}')
print str(x)
print uuid.UUID(bytes=x.bytes)
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