[clpython-devel] Calling Python functions from Lisp [Re Franz thread: spr35431]

Willem Broekema metawilm at gmail.com
Fri Jan 9 10:12:24 UTC 2009


Hello Stuart, welcome to the list.

On Thu, Jan 8, 2009 at 4:56 PM, Stuart C. Shapiro
<shapiro at cse.buffalo.edu> wrote:
> cl-user(3): (clpython:run "import test; test.test()")
> Warning: *import-recompiled-files* = #<equal hash-table with 0 entries>
> Warning: /net/projects/shapiro/clpython/test.fasl not in
>         #<equal hash-table with 0 entries>
> ; Fast loading /net/projects/shapiro/clpython/test.fasl
> [...]
> First question:  Why the Warning messages?  How can I get rid of them?

Those were debug messages accidentally left in. They have been removed
now. Sorry about that.

> Notice that I can make use of the values returned by the Python function:
> -----------------------------
> cl-user(4): (setf x (clpython:run "import test; test.test()"))
> Warning: *import-recompiled-files* = #<equal hash-table with 0 entries>
> Warning: /net/projects/shapiro/clpython/test.fasl not in
>         #<equal hash-table with 0 entries>
> ; Fast loading /net/projects/shapiro/clpython/test.fasl
> The Python test file has been run.
> Warning: *import-recompiled-files* = #<equal hash-table with 0 entries>
> Warning: /net/projects/shapiro/clpython/test.fasl not in
>         #<equal hash-table with 0 entries>
> I am a Python function.
> "I am done."
>
> cl-user(5): x
> "I am done."

Yes, the last value of the Python expression is returned.

> What I'd really like to do now is to call test.test() as much as
> possible as though it were a Common Lisp function.  One possible way is:
> -----------------------------
> cl-user(6): (clpython:run "test.test()")
> Error: NameError: Variable `test' is unbound.
>  [condition type: NameError]
>
> Restart actions (select using :continue):
>  0: Enter a Lisp value to use for `test'.
>  1: Return to Top Level (an "abort" restart).
>  2: Abort entirely from this (lisp) process.
> -----------------------------
> But, the name of the function does not seem to survive from one call of
> clpython:run to another.
>
> Final question: How can I do this?

There is not an elegent way for that yet (I'm working on it), but the
following works:

cl-user(6): :pa clpython
clpython(7): (setf *habitat* (make-habitat)) ;; required for py-import
#<habitat @ #x10fd5472>
clpython(8): (setq m (py-import '(foo)))
#<module `foo'
   Src: /Users/willem/dev/lisp/tmp/git-clpython/foo.py
   Binary: /Users/willem/.fasl/allegro-8.1m-macosx-x86/Users/willem/dev/lisp/tmp/git-clpython/foo.fasl
@ #x10a5a5a2>
clpython(9): (attr m 'f) ;; attribute lookup
#<python-function f @ #x10a5a872>
clpython(10): (funcall *)
24
None

Where foo.y is:
 def f(): print 24

- Willem




More information about the Clpython-devel mailing list