clj-python / libpython-clj

Python bindings for Clojure
Eclipse Public License 2.0
1.05k stars 68 forks source link

builtins/eval throws 'frame does not exist' #244

Open behrica opened 1 year ago

behrica commented 1 year ago
(require-python '[builtins :as bt])
(bt/eval "1") 
;Execution error at libpython-clj2.python.ffi/check-error-throw (ffi.clj:708) .
; SystemError: frame does not exist

while this works

(require-python '[builtins :as bt])
(bt/eval "1" {})

while in python both work:

>>> eval('1+1')
2
>>> eval('1+1',{})
2
jjtolton commented 1 year ago

If you try from embedded mode it should work. I can't remember the trick but you need to create a base frame by running py/run-simple-string otherwise.

Footnotes:

  1. if you do (import-python) you get access to the Python namespace which will Let you do python/eval.
jjtolton commented 8 months ago

@behrica

(import-python)
(python/eval "1" (:globals (py/run-simple-string ""))) ;;=> 1

Black magic.

Actually...

(python/eval "1" (python/dict)) ;;=> 1

also works :thinking:

You know, I gotta say, sometimes libpython-clj confuses me.

(python/eval "eval('eval(\"1\")', {\"eval\": eval})" {"eval" python/eval}) ;;=> 1