Commit 193fe95c authored by Kevin Modzelewski's avatar Kevin Modzelewski

Basic itertools support

parent 681446ac
......@@ -290,7 +290,7 @@ SRCS := $(MAIN_SRCS) $(STDLIB_SRCS)
STDLIB_OBJS := stdlib.bc.o stdlib.stripped.bc.o
STDLIB_RELEASE_OBJS := stdlib.release.bc.o
STDMODULE_SRCS := errnomodule.c shamodule.c sha256module.c sha512module.c _math.c mathmodule.c md5.c md5module.c _randommodule.c _sre.c operator.c binascii.c pwdmodule.c posixmodule.c _struct.c datetimemodule.c _functoolsmodule.c _collectionsmodule.c $(EXTRA_STDMODULE_SRCS)
STDMODULE_SRCS := errnomodule.c shamodule.c sha256module.c sha512module.c _math.c mathmodule.c md5.c md5module.c _randommodule.c _sre.c operator.c binascii.c pwdmodule.c posixmodule.c _struct.c datetimemodule.c _functoolsmodule.c _collectionsmodule.c itertoolsmodule.c $(EXTRA_STDMODULE_SRCS)
STDOBJECT_SRCS := structseq.c capsule.c stringobject.c $(EXTRA_STDOBJECT_SRCS)
STDPYTHON_SRCS := pyctype.c getargs.c formatter_string.c pystrtod.c dtoa.c $(EXTRA_STDPYTHON_SRCS)
FROM_CPYTHON_SRCS := $(addprefix from_cpython/Modules/,$(STDMODULE_SRCS)) $(addprefix from_cpython/Objects/,$(STDOBJECT_SRCS)) $(addprefix from_cpython/Python/,$(STDPYTHON_SRCS))
......
......@@ -405,7 +405,7 @@ static void
teedataobject_safe_decref(PyObject *obj)
{
while (obj && Py_TYPE(obj) == &teedataobject_type &&
Py_REFCNT(obj) == 1) {
2 /*Pyston change, was: Py_REFCNT(obj)*/ == 1) {
PyObject *nextlink = ((teedataobject *)obj)->nextlink;
((teedataobject *)obj)->nextlink = NULL;
Py_DECREF(obj);
......@@ -1942,7 +1942,7 @@ product_next(productobject *lz)
Py_ssize_t *indices = lz->indices;
/* Copy the previous result tuple or re-use it if available */
if (Py_REFCNT(result) > 1) {
if (2 /*Pyston change, was: Py_REFCNT(result)*/ > 1) {
PyObject *old_result = result;
result = PyTuple_New(npools);
if (result == NULL)
......@@ -1956,7 +1956,7 @@ product_next(productobject *lz)
Py_DECREF(old_result);
}
/* Now, we've got the only copy so we can update it in-place */
assert (npools==0 || Py_REFCNT(result) == 1);
assert (npools==0 || 2 /*Pyston change, was: Py_REFCNT(result)*/ == 1);
/* Update the pool indices right-to-left. Only advance to the
next pool when the previous one rolls-over */
......@@ -2170,7 +2170,7 @@ combinations_next(combinationsobject *co)
}
} else {
/* Copy the previous result tuple or re-use it if available */
if (Py_REFCNT(result) > 1) {
if (2 /*Pyston change, was: Py_REFCNT(result)*/ > 1) {
PyObject *old_result = result;
result = PyTuple_New(r);
if (result == NULL)
......@@ -2187,7 +2187,7 @@ combinations_next(combinationsobject *co)
* CPython's empty tuple is a singleton and cached in
* PyTuple's freelist.
*/
assert(r == 0 || Py_REFCNT(result) == 1);
assert(r == 0 || 2 /*Pyston change, was: Py_REFCNT(result)*/ == 1);
/* Scan indices right-to-left until finding one that is not
at its maximum (i + n - r). */
......@@ -2419,7 +2419,7 @@ cwr_next(cwrobject *co)
}
} else {
/* Copy the previous result tuple or re-use it if available */
if (Py_REFCNT(result) > 1) {
if (2 /*Pyston change, was: Py_REFCNT(result)*/ > 1) {
PyObject *old_result = result;
result = PyTuple_New(r);
if (result == NULL)
......@@ -2434,7 +2434,7 @@ cwr_next(cwrobject *co)
}
/* Now, we've got the only copy so we can update it in-place CPython's
empty tuple is a singleton and cached in PyTuple's freelist. */
assert(r == 0 || Py_REFCNT(result) == 1);
assert(r == 0 || 2 /*Pyston change, was: Py_REFCNT(result)*/ == 1);
/* Scan indices right-to-left until finding one that is not
* at its maximum (n-1). */
......@@ -2682,7 +2682,7 @@ permutations_next(permutationsobject *po)
goto empty;
/* Copy the previous result tuple or re-use it if available */
if (Py_REFCNT(result) > 1) {
if (2 /*Pyston change, was: Py_REFCNT(result)*/ > 1) {
PyObject *old_result = result;
result = PyTuple_New(r);
if (result == NULL)
......@@ -2696,7 +2696,7 @@ permutations_next(permutationsobject *po)
Py_DECREF(old_result);
}
/* Now, we've got the only copy so we can update it in-place */
assert(r == 0 || Py_REFCNT(result) == 1);
assert(r == 0 || 2 /*Pyston change, was: Py_REFCNT(result)*/ == 1);
/* Decrement rightmost cycle, moving leftward upon zero rollover */
for (i=r-1 ; i>=0 ; i--) {
......@@ -3583,7 +3583,7 @@ izip_next(izipobject *lz)
if (tuplesize == 0)
return NULL;
if (Py_REFCNT(result) == 1) {
if (2 /*Pyston change, was: Py_REFCNT(result)*/ == 1) {
Py_INCREF(result);
for (i=0 ; i < tuplesize ; i++) {
it = PyTuple_GET_ITEM(lz->ittuple, i);
......@@ -3928,7 +3928,7 @@ izip_longest_next(iziplongestobject *lz)
return NULL;
if (lz->numactive == 0)
return NULL;
if (Py_REFCNT(result) == 1) {
if (2 /*Pyston change, was: Py_REFCNT(result)*/ == 1) {
Py_INCREF(result);
for (i=0 ; i < tuplesize ; i++) {
it = PyTuple_GET_ITEM(lz->ittuple, i);
......
......@@ -65,7 +65,7 @@ extern "C" PyObject* PyObject_Str(PyObject* v) noexcept {
}
extern "C" PyObject* PyObject_SelfIter(PyObject* obj) noexcept {
Py_FatalError("unimplemented");
return obj;
}
extern "C" int PyObject_GenericSetAttr(PyObject* obj, PyObject* name, PyObject* value) noexcept {
......
......@@ -175,19 +175,26 @@ extern "C" Py_ssize_t PyObject_Size(PyObject* o) noexcept {
try {
return len(o)->n;
} catch (Box* b) {
Py_FatalError("unimplemented");
PyErr_SetObject(b->cls, b);
return -1;
}
}
extern "C" PyObject* PyObject_GetIter(PyObject*) noexcept {
Py_FatalError("unimplemented");
extern "C" PyObject* PyObject_GetIter(PyObject* o) noexcept {
try {
return getiter(o);
} catch (Box* b) {
PyErr_SetObject(b->cls, b);
return NULL;
}
}
extern "C" PyObject* PyObject_Repr(PyObject* obj) noexcept {
try {
return repr(obj);
} catch (Box* b) {
Py_FatalError("unimplemented");
PyErr_SetObject(b->cls, b);
return NULL;
}
}
......@@ -539,8 +546,15 @@ extern "C" PyObject* PySequence_Fast(PyObject* o, const char* m) noexcept {
Py_FatalError("unimplemented");
}
extern "C" PyObject* PyIter_Next(PyObject*) noexcept {
Py_FatalError("unimplemented");
extern "C" PyObject* PyIter_Next(PyObject* iter) noexcept {
static const std::string next_str("next");
try {
return callattr(iter, &next_str, CallattrFlags({.cls_only = true, .null_on_nonexistent = false }),
ArgPassSpec(0), NULL, NULL, NULL, NULL, NULL);
} catch (Box* b) {
PyErr_SetObject(b->cls, b);
return NULL;
}
}
extern "C" int PyCallable_Check(PyObject* x) noexcept {
......
......@@ -51,6 +51,7 @@ extern "C" void init_struct();
extern "C" void initdatetime();
extern "C" void init_functools();
extern "C" void init_collections();
extern "C" void inititertools();
namespace pyston {
......@@ -1147,6 +1148,7 @@ void setupRuntime() {
initdatetime();
init_functools();
init_collections();
inititertools();
setupSysEnd();
......
import itertools
its = []
its.append(itertools.count(5))
its.append(itertools.cycle([1, 2, 3]))
its.append(itertools.repeat(1337))
its.append(itertools.chain([1, 2, 3], itertools.repeat(5)))
its.append(itertools.compress(itertools.count(), itertools.count(-3)))
for i in xrange(10):
for it in its:
print it.next(),
print
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