Skip to content

Commit

Permalink
[add] pyfor iterate clause
Browse files Browse the repository at this point in the history
  • Loading branch information
digikar99 committed Jul 26, 2024
1 parent f741771 commit 47bf678
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions py4cl2-cffi.asd
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
19 changes: 19 additions & 0 deletions src/iterate.lisp
Original file line number Diff line number Diff line change
@@ -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)))))))
4 changes: 3 additions & 1 deletion src/package.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@
#:+py-none-pointer+

#:python-getattr
#:python-setattr))
#:python-setattr

#:pyfor))

(in-package :py4cl2-cffi)

0 comments on commit 47bf678

Please sign in to comment.