Commit 8eb4abc8 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Couple stdlib additions

2-arg iter() variant
bytearray(xrange(256))
better support for multiple inheritance from both old- and new-style classes
set.pop()
str.rindex
set.discard
parent 0235e191
......@@ -179,6 +179,20 @@ string_index(PyStringObject *self, PyObject *args)
return PyInt_FromSsize_t(result);
}
PyObject *
string_rindex(PyStringObject *self, PyObject *args)
{
Py_ssize_t result = string_find_internal(self, args, -1);
if (result == -2)
return NULL;
if (result == -1) {
PyErr_SetString(PyExc_ValueError,
"substring not found");
return NULL;
}
return PyInt_FromSsize_t(result);
}
PyObject*
string_splitlines(PyStringObject *self, PyObject *args)
{
......
......@@ -1434,7 +1434,6 @@ extern "C" PyObject* PyNumber_ToBase(PyObject* n, int base) noexcept {
}
extern "C" Py_ssize_t PyNumber_AsSsize_t(PyObject* o, PyObject* exc) noexcept {
if (isSubclass(o->cls, int_cls)) {
int64_t n = static_cast<BoxedInt*>(o)->n;
static_assert(sizeof(n) == sizeof(Py_ssize_t), "");
......@@ -1443,6 +1442,9 @@ extern "C" Py_ssize_t PyNumber_AsSsize_t(PyObject* o, PyObject* exc) noexcept {
return PyLong_AsSsize_t(o);
}
Py_FatalError("unimplemented");
PyErr_Format(PyExc_TypeError, "'%.200s' object cannot be interpreted "
"as an index",
o->cls->tp_name);
return -1;
}
}
......@@ -173,12 +173,13 @@ CompiledFunction* compileFunction(CLFunction* f, FunctionSpecialization* spec, E
assert((entry_descriptor != NULL) + (spec != NULL) == 1);
ASSERT(f->versions.size() < 20, "%ld", f->versions.size());
SourceInfo* source = f->source;
assert(source);
std::string name = source->getName();
ASSERT(f->versions.size() < 20, "%s %ld", name.c_str(), f->versions.size());
if (VERBOSITY("irgen") >= 1) {
std::string s;
llvm::raw_string_ostream ss(s);
......
......@@ -980,6 +980,16 @@ Box* pydumpAddr(Box* p) {
return None;
}
Box* builtinIter(Box* obj, Box* sentinel) {
if (sentinel == NULL)
return getiter(obj);
Box* r = PyCallIter_New(obj, sentinel);
if (!r)
throwCAPIException();
return r;
}
void setupBuiltins() {
builtins_module = createModule("__builtin__", "__builtin__");
......@@ -1121,7 +1131,8 @@ void setupBuiltins() {
boxRTFunction((void*)locals, UNKNOWN, 0, 0, false, false), "locals"));
builtins_module->giveAttr(
"iter", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)getiter, UNKNOWN, 1, 0, false, false), "iter"));
"iter", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)builtinIter, UNKNOWN, 2, 1, false, false), "iter",
{ NULL }));
builtins_module->giveAttr(
"reversed",
new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)getreversed, UNKNOWN, 1, 0, false, false), "reversed"));
......
......@@ -627,6 +627,7 @@ extern "C" Box* import(int level, Box* from_imports, const std::string* module_n
}
Box* impFindModule(Box* _name, BoxedList* path) {
_name = coerceUnicodeToStr(_name);
RELEASE_ASSERT(_name->cls == str_cls, "");
BoxedString* name = static_cast<BoxedString*>(_name);
......
......@@ -212,6 +212,16 @@ Box* setRemove(BoxedSet* self, Box* v) {
return None;
}
Box* setDiscard(BoxedSet* self, Box* v) {
assert(self->cls == set_cls);
auto it = self->s.find(v);
if (it != self->s.end())
self->s.erase(it);
return None;
}
Box* setClear(BoxedSet* self, Box* v) {
assert(self->cls == set_cls);
self->s.clear();
......@@ -283,6 +293,18 @@ Box* setCopy(BoxedSet* self) {
return rtn;
}
Box* setPop(BoxedSet* self) {
assert(self->cls == set_cls);
if (!self->s.size())
raiseExcHelper(KeyError, "pop from an empty set");
auto it = self->s.begin();
Box* rtn = *it;
self->s.erase(it);
return rtn;
}
Box* setContains(BoxedSet* self, Box* v) {
assert(self->cls == set_cls || self->cls == frozenset_cls);
return boxBool(self->s.count(v) != 0);
......@@ -377,6 +399,7 @@ void setupSet() {
set_cls->giveAttr("add", new BoxedFunction(boxRTFunction((void*)setAdd, NONE, 2)));
set_cls->giveAttr("remove", new BoxedFunction(boxRTFunction((void*)setRemove, NONE, 2)));
set_cls->giveAttr("discard", new BoxedFunction(boxRTFunction((void*)setDiscard, NONE, 2)));
set_cls->giveAttr("clear", new BoxedFunction(boxRTFunction((void*)setClear, NONE, 1)));
set_cls->giveAttr("update", new BoxedFunction(boxRTFunction((void*)setUpdate, NONE, 1, 0, true, false)));
......@@ -385,6 +408,7 @@ void setupSet() {
new BoxedFunction(boxRTFunction((void*)setIntersection, UNKNOWN, 1, 0, true, false)));
set_cls->giveAttr("copy", new BoxedFunction(boxRTFunction((void*)setCopy, UNKNOWN, 1)));
set_cls->giveAttr("pop", new BoxedFunction(boxRTFunction((void*)setPop, UNKNOWN, 1)));
set_cls->freeze();
frozenset_cls->freeze();
......
......@@ -40,6 +40,7 @@ extern "C" PyObject* string_split(PyStringObject* self, PyObject* args) noexcept
extern "C" PyObject* string_rsplit(PyStringObject* self, PyObject* args) noexcept;
extern "C" PyObject* string_find(PyStringObject* self, PyObject* args) noexcept;
extern "C" PyObject* string_index(PyStringObject* self, PyObject* args) noexcept;
extern "C" PyObject* string_rindex(PyStringObject* self, PyObject* args) noexcept;
extern "C" PyObject* string_rfind(PyStringObject* self, PyObject* args) noexcept;
extern "C" PyObject* string_splitlines(PyStringObject* self, PyObject* args) noexcept;
......@@ -2402,6 +2403,7 @@ static PyMethodDef string_methods[] = {
{ "rsplit", (PyCFunction)string_rsplit, METH_VARARGS, NULL },
{ "find", (PyCFunction)string_find, METH_VARARGS, NULL },
{ "index", (PyCFunction)string_index, METH_VARARGS, NULL },
{ "rindex", (PyCFunction)string_rindex, METH_VARARGS, NULL },
{ "rfind", (PyCFunction)string_rfind, METH_VARARGS, NULL },
{ "expandtabs", (PyCFunction)string_expandtabs, METH_VARARGS, NULL },
{ "splitlines", (PyCFunction)string_splitlines, METH_VARARGS, NULL },
......
......@@ -109,6 +109,9 @@ extern "C" PyObject* PystonType_GenericAlloc(BoxedClass* cls, Py_ssize_t nitems)
assert(cls->tp_mro && "maybe we should just skip these checks if !mro");
assert(cls->tp_mro->cls == tuple_cls);
for (auto b : static_cast<BoxedTuple*>(cls->tp_mro)->elts) {
// old-style classes are always pyston classes:
if (b->cls == classobj_cls)
continue;
assert(isSubclass(b->cls, type_cls));
ASSERT(static_cast<BoxedClass*>(b)->is_pyston_class, "%s (%s)", cls->tp_name,
static_cast<BoxedClass*>(b)->tp_name);
......
......@@ -102,3 +102,8 @@ print callable(int)
print callable(lambda: 1)
print range(5L, 7L)
print list(iter(xrange(100).__iter__().next, 20))
print bytearray(xrange(256))
......@@ -85,3 +85,16 @@ s2 = set([1, 5])
print sorted(s1.union(s2)), sorted(s1.intersection(s2))
print sorted(s1.union(range(5, 7))), sorted(s1.intersection(range(5, 7)))
print sorted(s2.union([], [], [], [])), sorted(s2.intersection())
l = []
s = set(range(5))
while s:
l.append(s.pop())
l.sort()
print l
s = set([1])
s.discard(1)
print s
s.discard(1)
print s
......@@ -136,10 +136,13 @@ for c in "hello world":
print "hello world".index("world")
print "hello world".index("world", 1, 30)
print "hello world".index("l", 3)
print "hello world".rindex("world")
print "hello world".rindex("world", 1, 30)
print "hello world".rindex("l", 3)
try:
print "hello world".index("goodbye")
except:
print "threw exception"
except Exception as e:
print e
print repr("hello\tworld\t".expandtabs())
print repr("hello\tworld\t".expandtabs(12))
......
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