clj-python / libpython-clj

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

ISSUE-224: Null Pointer Exception on python-as-map iterator without explicit casting #224

Closed jjtolton closed 1 year ago

jjtolton commented 1 year ago
(require '[libpython-clj2.python :as py :refer [py.. py**] :reload true])
(py/initialize! :python-executable "venv/bin/python")
(require '[libpython-clj2.require :refer [require-python import-python]])
(import-python)
(py/run-simple-string "
class Thing:
    def __call__(this, data):
        return {a: b for a, b in data.items()}

" )
(py.. (__main__/Thing) (__call__  {}  ))

Expected: {} Actual:

   Traceback (most recent call last): File "<string>", line 4, in
   __call__ File "<string>", line 4, in <dictcomp> File
   "/home/jay/.pyenv/versions/3.7.13/lib/python3.7/_collections_abc.py",
   line 743, in __iter__ for key in self._mapping: Exception:
   java.lang.NullPointerException:java.lang.NullPointerException: null

Note: This works

(py.. (__main__/Thing) (__call__  (python/dict {})  )) ;;=> {}  

See conversation for more details

behrica commented 1 year ago

The simplest test case for the problem is this:

(py/py. (libpython-clj2.python.bridge-as-python/map-as-python {}) __iter__)

Not sure, if you consider this public API, but it fails with NPE

This is not fixed by PR #225

jjtolton commented 1 year ago

Thanks for the test case! https://github.com/clj-python/libpython-clj/pull/227