Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable test slice and add missing function. #791

Merged
merged 2 commits into from
Aug 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion from_cpython/Lib/test/test_slice.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# expected: fail
# tests for slice objects; in particular the indices method.

import unittest
Expand Down
46 changes: 46 additions & 0 deletions src/runtime/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1719,6 +1719,48 @@ Box* sliceRepr(BoxedSlice* self) {
return boxStringTwine(llvm::Twine("slice(") + start->s() + ", " + stop->s() + ", " + step->s() + ")");
}

Box* sliceHash(BoxedSlice* self) {
raiseExcHelper(TypeError, "unhashable type");
return boxLong(-1l);
}

Box* sliceReduce(BoxedSlice* self) {
return Py_BuildValue("O(OOO)", self->cls, self->start, self->stop, self->step);
}

Box* sliceIndices(BoxedSlice* self, Box* len) {
Py_ssize_t ilen, start, stop, step, slicelength;

ilen = PyNumber_AsSsize_t(len, PyExc_OverflowError);

if (ilen == -1 && PyErr_Occurred()) {
throwCAPIException();
}

if (PySlice_GetIndicesEx((PySliceObject*)self, ilen, &start, &stop, &step, &slicelength) < 0) {
throwCAPIException();
}
return BoxedTuple::create({ boxInt(start), boxInt(stop), boxInt(step) });
}

static int slice_compare(PySliceObject* v, PySliceObject* w) noexcept {
int result = 0;

if (v == w)
return 0;

if (PyObject_Cmp(v->start, w->start, &result) < 0)
return -2;
if (result != 0)
return result;
if (PyObject_Cmp(v->stop, w->stop, &result) < 0)
return -2;
if (result != 0)
return result;
if (PyObject_Cmp(v->step, w->step, &result) < 0)
return -2;
return result;
}
extern "C" int PySlice_GetIndices(PySliceObject* r, Py_ssize_t length, Py_ssize_t* start, Py_ssize_t* stop,
Py_ssize_t* step) noexcept {
/* XXX support long ints */
Expand Down Expand Up @@ -3541,10 +3583,14 @@ void setupRuntime() {
slice_cls->giveAttr("__new__",
new BoxedFunction(boxRTFunction((void*)sliceNew, UNKNOWN, 4, 2, false, false), { NULL, None }));
slice_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)sliceRepr, STR, 1)));
slice_cls->giveAttr("__hash__", new BoxedFunction(boxRTFunction((void*)sliceHash, UNKNOWN, 1)));
slice_cls->giveAttr("indices", new BoxedFunction(boxRTFunction((void*)sliceIndices, BOXED_TUPLE, 2)));
slice_cls->giveAttr("__reduce__", new BoxedFunction(boxRTFunction((void*)sliceReduce, BOXED_TUPLE, 1)));
slice_cls->giveAttr("start", new BoxedMemberDescriptor(BoxedMemberDescriptor::OBJECT, offsetof(BoxedSlice, start)));
slice_cls->giveAttr("stop", new BoxedMemberDescriptor(BoxedMemberDescriptor::OBJECT, offsetof(BoxedSlice, stop)));
slice_cls->giveAttr("step", new BoxedMemberDescriptor(BoxedMemberDescriptor::OBJECT, offsetof(BoxedSlice, step)));
slice_cls->freeze();
slice_cls->tp_compare = (cmpfunc)slice_compare;

attrwrapper_cls->giveAttr("__setitem__", new BoxedFunction(boxRTFunction((void*)AttrWrapper::setitem, UNKNOWN, 3)));
attrwrapper_cls->giveAttr(
Expand Down