Commit 0227fde8 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Merge pull request #893 from Daetalus/test_index

Some improvments that let test_index could pass
parents cf9a690c 541b7543
......@@ -75,6 +75,9 @@ PyAPI_FUNC(PyObject *) PyLong_FromUnicode(Py_UNICODE*, Py_ssize_t, int) PYSTON_N
*/
PyAPI_FUNC(int) _PyLong_Sign(PyObject *v) PYSTON_NOEXCEPT;
// Pyston change: copied from CPython/Include/longintrepr.h
/* Return a copy of src. */
PyAPI_FUNC(PyObject *) _PyLong_Copy(PyLongObject *src) PYSTON_NOEXCEPT;
/* _PyLong_NumBits. Return the number of bits needed to represent the
absolute value of a long. For example, this returns 1 for 1 and -1, 2
......
# expected: fail
import unittest
from test import test_support
import operator
......
......@@ -2071,8 +2071,7 @@ extern "C" PyObject* PyNumber_Long(PyObject* o) noexcept {
return res;
}
if (PyLong_Check(o)) { /* A long subclass without nb_long */
BoxedInt* lo = (BoxedInt*)o;
return PyLong_FromLong(lo->n);
return _PyLong_Copy((PyLongObject*)o);
}
trunc_func = PyObject_GetAttr(o, trunc_name);
if (trunc_func) {
......
......@@ -657,21 +657,22 @@ extern "C" int PyList_Insert(PyObject* op, Py_ssize_t where, PyObject* newitem)
}
Box* listMul(BoxedList* self, Box* rhs) {
if (rhs->cls != int_cls) {
raiseExcHelper(TypeError, "can't multiply sequence by non-int of type '%s'", getTypeName(rhs));
}
static BoxedString* index_str = internStringImmortal("__index__");
Py_ssize_t n = PyNumber_AsSsize_t(rhs, PyExc_IndexError);
if (n == -1 && PyErr_Occurred())
throwCAPIException();
int n = static_cast<BoxedInt*>(rhs)->n;
int s = self->size;
BoxedList* rtn = new BoxedList();
rtn->ensure(n * s);
if (s == 1) {
for (int i = 0; i < n; i++) {
for (long i = 0; i < n; i++) {
listAppendInternal(rtn, self->elts->elts[0]);
}
} else {
for (int i = 0; i < n; i++) {
for (long i = 0; i < n; i++) {
listAppendArrayInternal(rtn, &self->elts->elts[0], s);
}
}
......@@ -679,6 +680,33 @@ Box* listMul(BoxedList* self, Box* rhs) {
return rtn;
}
Box* listImul(BoxedList* self, Box* rhs) {
static BoxedString* index_str = internStringImmortal("__index__");
Py_ssize_t n = PyNumber_AsSsize_t(rhs, PyExc_IndexError);
if (n == -1 && PyErr_Occurred())
throwCAPIException();
int s = self->size;
self->ensure(n * s);
if (n == 0) {
listSetitemSliceInt64(self, 0, s, 1, NULL);
} else if (n == 1) {
return self;
} else if (s == 1) {
for (long i = 1; i < n; i++) {
listAppendInternal(self, self->elts->elts[0]);
}
} else {
for (long i = 1; i < n; i++) {
listAppendArrayInternal(self, &self->elts->elts[0], s);
}
}
return self;
}
Box* listIAdd(BoxedList* self, Box* _rhs) {
if (_rhs->cls == list_cls) {
// This branch is safe if self==rhs:
......@@ -1230,6 +1258,7 @@ void setupList() {
list_cls->giveAttr("insert", new BoxedFunction(boxRTFunction((void*)listInsert, NONE, 3)));
list_cls->giveAttr("__mul__", new BoxedFunction(boxRTFunction((void*)listMul, LIST, 2)));
list_cls->giveAttr("__rmul__", new BoxedFunction(boxRTFunction((void*)listMul, LIST, 2)));
list_cls->giveAttr("__imul__", new BoxedFunction(boxRTFunction((void*)listImul, LIST, 2)));
list_cls->giveAttr("__iadd__", new BoxedFunction(boxRTFunction((void*)listIAdd, UNKNOWN, 2)));
list_cls->giveAttr("__add__", new BoxedFunction(boxRTFunction((void*)listAdd, UNKNOWN, 2)));
......
......@@ -72,6 +72,12 @@ extern "C" int _PyLong_Sign(PyObject* l) noexcept {
return mpz_sgn(static_cast<BoxedLong*>(l)->n);
}
extern "C" PyObject* _PyLong_Copy(PyLongObject* src) noexcept {
BoxedLong* rtn = new BoxedLong();
mpz_init_set(((BoxedLong*)src)->n, rtn->n);
return rtn;
}
extern "C" unsigned PY_LONG_LONG PyLong_AsUnsignedLongLong(PyObject* vv) noexcept {
unsigned PY_LONG_LONG bytes;
int one = 1;
......
......@@ -1146,9 +1146,24 @@ extern "C" Box* strMul(BoxedString* lhs, Box* rhs) {
assert(PyString_Check(lhs));
int n;
if (PyIndex_Check(rhs)) {
Box* index = PyNumber_Index(rhs);
if (!index) {
throwCAPIException();
}
rhs = index;
}
if (PyInt_Check(rhs))
n = static_cast<BoxedInt*>(rhs)->n;
else
else if (isSubclass(rhs->cls, long_cls)) {
n = _PyLong_AsInt(rhs);
if (PyErr_Occurred()) {
PyErr_Clear();
raiseExcHelper(OverflowError, "cannot fit 'long' into index-sized integer");
}
} else
return NotImplemented;
if (n <= 0)
return EmptyString;
......
......@@ -23,7 +23,6 @@ test_builtin execfile scoping issue
test_class needs ellipsis
test_coercion 1**1L, divmod(1, 1L); some unknown bug
test_collections assertion failed when doing vars(collections.namedtuple('Point', 'x y')(11, 12))
test_complex need complex.__nonzero__
test_contextlib lock.locked attributes
test_datetime needs _PyObject_GetDictPtr
test_decimal I think we need to copy decimaltestdata from cpython
......@@ -47,7 +46,6 @@ test_generators crash when sending non-None to a just-started generator
test_genexps parser not raising a SyntaxError when assigning to a genexp
test_global SyntaxWarnings for global statements after uses
test_grammar bug in our tokenizer
test_index slice.indices, old-styl-class __mul__
test_io memory/gc issue?
test_iterlen [unknown]
test_itertools [unknown]
......
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