Permalink
Browse files

Add an experimental WITH-PYTHON macro

Thanks to Mariano Montone (mmontone) for the idea.
  • Loading branch information...
Pixie
Pixie committed Feb 11, 2015
1 parent 40a09c6 commit 4c5792c94bee4c900cfff8cd66f731e4ba70790e
Showing with 16 additions and 0 deletions.
  1. +15 −0 api.lisp
  2. +1 −0 packages.lisp
View
@@ -27,6 +27,21 @@
:test (lambda (c) (declare (ignore c)) (not (.is-initialized)))
(startup-python)))))
(defun call-with-python (thunk)
(let ((cpython-initialized? (.is-initialized)))
(unless cpython-initialized?
(.initialize))
(unwind-protect
(funcall thunk)
(unless cpython-initialized?
(.finalize)))))
(defmacro with-python (&body body)
"EXPERIMENTAL. Ensures the CPython interpreter is running, executes BODY,
then returns the interpreter to whatever state it was in prior to the
WITH-PYTHON form being entered."
`(call-with-python (lambda () ,@body)))
(defun import (name)
"Imports a Python module into the current namespace. Should be equivalent
to (run \"import NAME\")."
View
@@ -65,6 +65,7 @@ converting to and from the type. See the WARNING above (defpytype \"PyString\"
#:apply)
(:export #:startup-python
#:shutdown-python
#:with-python
#:import
#:run
#:run*

0 comments on commit 4c5792c

Please sign in to comment.