From d478f9802f98821d967bb2401502e43faf50e67e Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sat, 25 Nov 2023 11:15:06 +0800 Subject: [PATCH] ... --- docs/bindings.md | 4 +++- src/collections.cpp | 12 +++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/bindings.md b/docs/bindings.md index 5c8c8176..6eeac613 100644 --- a/docs/bindings.md +++ b/docs/bindings.md @@ -261,4 +261,6 @@ It takes a C header file and generates a python module stub (`*.pyi`) and a C++ ## Further reading -See [random.cpp](https://github.com/blueloveTH/pocketpy/blob/main/src/random.cpp) for an example used by `random` module. \ No newline at end of file +See [random.cpp](https://github.com/blueloveTH/pocketpy/blob/main/src/random.cpp) for an example used by `random` module. + +See [collections.cpp](https://github.com/blueloveTH/pocketpy/blob/main/src/collections.cpp) for a modern implementation of `collections.deque`. \ No newline at end of file diff --git a/src/collections.cpp b/src/collections.cpp index 3cec83f5..f77a7f3a 100644 --- a/src/collections.cpp +++ b/src/collections.cpp @@ -56,7 +56,7 @@ namespace pkpy void insertObj(bool front, bool back, int index, PyObject *item); // insert at index, used purely for internal purposes: append, appendleft, insert methods PyObject *popObj(bool front, bool back, PyObject *item, VM *vm); // pop at index, used purely for internal purposes: pop, popleft, remove methods int findIndex(VM *vm, PyObject *obj, int start, int stop); // find the index of the given object in the deque - std::stringstream getRepr(VM *vm, PyObject* thisObj); // get the string representation of the deque + std::string getRepr(VM *vm, PyObject* thisObj); // get the string representation of the deque // Special methods static void _register(VM *vm, PyObject *mod, PyObject *type); // register the type void _gc_mark() const; // needed for container types, mark all objects in the deque for gc @@ -125,16 +125,14 @@ namespace pkpy [](VM *vm, ArgsView args) { PyDeque &self = _CAST(PyDeque &, args[0]); - std::stringstream ss = self.getRepr(vm, args[0]); - return VAR(ss.str()); + return VAR(self.getRepr(vm, args[0])); }); // returns a string representation of the deque vm->bind(type, "__str__(self) -> str", [](VM *vm, ArgsView args) { PyDeque &self = _CAST(PyDeque &, args[0]); - std::stringstream ss = self.getRepr(vm, args[0]); - return VAR(ss.str()); + return VAR(self.getRepr(vm, args[0])); }); // enables comparison between two deques, == and != are supported vm->bind(type, "__eq__(self, other) -> bool", @@ -427,7 +425,7 @@ namespace pkpy } } } - std::stringstream PyDeque::getRepr(VM *vm, PyObject *thisObj) + std::string PyDeque::getRepr(VM *vm, PyObject *thisObj) { std::stringstream ss; ss << "deque(["; @@ -441,7 +439,7 @@ namespace pkpy ss << ", "; } this->bounded ? ss << "], maxlen=" << this->maxlen << ")" : ss << "])"; - return ss; + return ss.str(); } int PyDeque::findIndex(VM *vm, PyObject *obj, int start, int stop) {