This commit is contained in:
blueloveTH 2023-11-25 11:15:06 +08:00
parent c9ae759b24
commit d478f9802f
2 changed files with 8 additions and 8 deletions

View File

@ -262,3 +262,5 @@ It takes a C header file and generates a python module stub (`*.pyi`) and a C++
## Further reading ## Further reading
See [random.cpp](https://github.com/blueloveTH/pocketpy/blob/main/src/random.cpp) for an example used by `random` module. 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`.

View File

@ -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 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 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 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 // Special methods
static void _register(VM *vm, PyObject *mod, PyObject *type); // register the type 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 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) [](VM *vm, ArgsView args)
{ {
PyDeque &self = _CAST(PyDeque &, args[0]); PyDeque &self = _CAST(PyDeque &, args[0]);
std::stringstream ss = self.getRepr(vm, args[0]); return VAR(self.getRepr(vm, args[0]));
return VAR(ss.str());
}); });
// returns a string representation of the deque // returns a string representation of the deque
vm->bind(type, "__str__(self) -> str", vm->bind(type, "__str__(self) -> str",
[](VM *vm, ArgsView args) [](VM *vm, ArgsView args)
{ {
PyDeque &self = _CAST(PyDeque &, args[0]); PyDeque &self = _CAST(PyDeque &, args[0]);
std::stringstream ss = self.getRepr(vm, args[0]); return VAR(self.getRepr(vm, args[0]));
return VAR(ss.str());
}); });
// enables comparison between two deques, == and != are supported // enables comparison between two deques, == and != are supported
vm->bind(type, "__eq__(self, other) -> bool", 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; std::stringstream ss;
ss << "deque(["; ss << "deque([";
@ -441,7 +439,7 @@ namespace pkpy
ss << ", "; ss << ", ";
} }
this->bounded ? ss << "], maxlen=" << this->maxlen << ")" : ss << "])"; this->bounded ? ss << "], maxlen=" << this->maxlen << ")" : ss << "])";
return ss; return ss.str();
} }
int PyDeque::findIndex(VM *vm, PyObject *obj, int start, int stop) int PyDeque::findIndex(VM *vm, PyObject *obj, int start, int stop)
{ {