From 47bf6786cc7ee82dfcdf29a31a6dde3a8e83f93b Mon Sep 17 00:00:00 2001 From: digikar Date: Sat, 27 Jul 2024 00:01:23 +0530 Subject: [PATCH] [add] pyfor iterate clause --- py4cl2-cffi.asd | 1 + src/iterate.lisp | 19 +++++++++++++++++++ src/package.lisp | 4 +++- 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 src/iterate.lisp diff --git a/py4cl2-cffi.asd b/py4cl2-cffi.asd index a2c779b..0b2a6dd 100644 --- a/py4cl2-cffi.asd +++ b/py4cl2-cffi.asd @@ -55,6 +55,7 @@ (:file "arg-list") (:file "import-export") (:file "lisp-classes") + (:file "iterate") (:file "do-after-load")) :perform (test-op (o c) (declare (ignore o c)) diff --git a/src/iterate.lisp b/src/iterate.lisp new file mode 100644 index 0000000..a41eb3b --- /dev/null +++ b/src/iterate.lisp @@ -0,0 +1,19 @@ +(in-package :py4cl2-cffi) + +(declaim (inline pyerror-stop-iteration-p)) +(defun pyerror-stop-iteration-p (simple-pyerror) + (declare (optimize speed) + (type simple-pyerror simple-pyerror)) + (string= "StopIteration" (slot-value simple-pyerror 'type))) + +(deftype pyerror-stop-iteration () + `(and simple-pyerror + (satisfies pyerror-stop-iteration-p))) + +(defmacro-clause (PYFOR var IN pyobject-wrapper) + ;; TODO: Tests and docstrings + (let ((pygen (gensym "PYGENERATOR"))) + `(progn + (with ,pygen = (pycall "iter" ,pyobject-wrapper)) + (for ,var = (handler-case (pycall "next" ,pygen) + (pyerror-stop-iteration () (finish))))))) diff --git a/src/package.lisp b/src/package.lisp index e9a1309..347c0e2 100644 --- a/src/package.lisp +++ b/src/package.lisp @@ -66,6 +66,8 @@ #:+py-none-pointer+ #:python-getattr - #:python-setattr)) + #:python-setattr + + #:pyfor)) (in-package :py4cl2-cffi)