From 4a189464add6f561e17fdac4be289373dc652dd3 Mon Sep 17 00:00:00 2001 From: "S. Mahmudul Hasan" Date: Wed, 18 Oct 2023 20:01:28 -0400 Subject: [PATCH] dropped support for __init__ for now --- python/pickle.py | 5 -- src/collections.cpp | 103 +++++++++++++++++++++++++--------------- tests/70_collections.py | 3 +- 3 files changed, 68 insertions(+), 43 deletions(-) diff --git a/python/pickle.py b/python/pickle.py index bd0d7372..bc23785f 100644 --- a/python/pickle.py +++ b/python/pickle.py @@ -152,13 +152,8 @@ class _Unpickler: if newargs is not None: newargs = [self.unwrap(i) for i in newargs] inst = new_f(cls, *newargs) - if inst.__init__ is not None: # should be called with args if exists - inst.__init__(*newargs) else: inst = new_f(cls) - if inst.__init__ is not None: - inst.__init__() # no args - self.tag(index, inst) # restore state if state is not None: diff --git a/src/collections.cpp b/src/collections.cpp index 9422f8a1..efafd1aa 100644 --- a/src/collections.cpp +++ b/src/collections.cpp @@ -29,44 +29,44 @@ namespace pkpy PyObject *maxlen = args[2]; return vm->heap.gcnew(cls_t, vm, iterable, maxlen); }); - vm->bind(type, "__init__(self, iterable=None, maxlen=None)", - [](VM *vm, ArgsView args) - { - // printf("INIT CALLED!!\n"); - PyDeque &self = _CAST(PyDeque &, args[0]); - PyObject *iterable = args[1]; - PyObject *maxlen = args[2]; + // vm->bind(type, "__init__(self, iterable=None, maxlen=None)", + // [](VM *vm, ArgsView args) + // { + // // printf("INIT CALLED!!\n"); + // PyDeque &self = _CAST(PyDeque &, args[0]); + // PyObject *iterable = args[1]; + // PyObject *maxlen = args[2]; - if (!vm->py_equals(maxlen, vm->None)) // fix the maxlen first - { - int tmp = CAST(int, maxlen); - if (tmp < 0) - vm->ValueError("maxlen must be non-negative"); - else - { - self.maxlen = tmp; - self.bounded = true; - } - } - else - { - self.bounded = false; - self.maxlen = -1; - } - if (!vm->py_equals(iterable, vm->None)) - { - self.dequeItems.clear(); // clear the deque - auto _lock = vm->heap.gc_scope_lock(); // locking the heap - PyObject *it = vm->py_iter(args[1]); // strong ref - PyObject *obj = vm->py_next(it); - while (obj != vm->StopIteration) - { - self.insertObj(false, true, -1, obj); - obj = vm->py_next(it); - } - } - return args[0]; - }); + // if (!vm->py_equals(maxlen, vm->None)) // fix the maxlen first + // { + // int tmp = CAST(int, maxlen); + // if (tmp < 0) + // vm->ValueError("maxlen must be non-negative"); + // else + // { + // self.maxlen = tmp; + // self.bounded = true; + // } + // } + // else + // { + // self.bounded = false; + // self.maxlen = -1; + // } + // if (!vm->py_equals(iterable, vm->None)) + // { + // self.dequeItems.clear(); // clear the deque + // auto _lock = vm->heap.gc_scope_lock(); // locking the heap + // PyObject *it = vm->py_iter(args[1]); // strong ref + // PyObject *obj = vm->py_next(it); + // while (obj != vm->StopIteration) + // { + // self.insertObj(false, true, -1, obj); + // obj = vm->py_next(it); + // } + // } + // return args[0]; + // }); // gets the item at the given index, if index is negative, it will be treated as index + len(deque) // if the index is out of range, IndexError will be thrown --> required for [] operator vm->bind(type, "__getitem__(self, index) -> PyObject", @@ -396,6 +396,35 @@ namespace pkpy /// @brief initializes a new PyDeque object, actual initialization is done in __init__ PyDeque::PyDeque(VM *vm, PyObject *iterable, PyObject *maxlen) { + + if (!vm->py_equals(maxlen, vm->None)) // fix the maxlen first + { + int tmp = CAST(int, maxlen); + if (tmp < 0) + vm->ValueError("maxlen must be non-negative"); + else + { + this->maxlen = tmp; + this->bounded = true; + } + } + else + { + this->bounded = false; + this->maxlen = -1; + } + if (!vm->py_equals(iterable, vm->None)) + { + this->dequeItems.clear(); // clear the deque + auto _lock = vm->heap.gc_scope_lock(); // locking the heap + PyObject *it = vm->py_iter(iterable); // strong ref + PyObject *obj = vm->py_next(it); + while (obj != vm->StopIteration) + { + this->insertObj(false, true, -1, obj); + obj = vm->py_next(it); + } + } } int PyDeque::findIndex(VM *vm, PyObject *obj, int start, int stop) diff --git a/tests/70_collections.py b/tests/70_collections.py index bf073125..f8bc9483 100644 --- a/tests/70_collections.py +++ b/tests/70_collections.py @@ -44,7 +44,8 @@ def fail(): d = deque(range(-5125, -5000)) -d.__init__(range(200)) +# d.__init__(range(200)) # not supported +d = deque(range(200)) for i in range(200, 400): d.append(i) for i in reversed(range(-200, 0)):