Skip to content

Commit bfd5646

Browse files
authored
ISSUE-224: Prevent NPE by substituting vector if keys evaluates to nil (#227)
* Prevent NPE by substituting vector if `keys` evaluates to `nil` * fix whitespace issue on previous commit * Add __iter__ test * give credit where credit is due
1 parent 3c8be2f commit bfd5646

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

src/libpython_clj2/python/bridge_as_python.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@
122122
"__setitem__" (as-tuple-instance-fn #(.put (self->map %1) (py-base/as-jvm %2) %3))
123123
"__delitem__" (as-tuple-instance-fn #(.remove (self->map %1) (py-base/as-jvm %2)))
124124
"__hash__" (as-tuple-instance-fn #(.hashCode (self->map %1)))
125-
"__iter__" (as-tuple-instance-fn #(.iterator ^Iterable (keys (self->map %1))))
125+
"__iter__" (as-tuple-instance-fn #(.iterator ^Iterable (or (keys (self->map %1)) [])))
126126
"__len__" (as-tuple-instance-fn #(.size (self->map %1)))
127127
"__str__" (as-tuple-instance-fn #(.toString (self->map %1)))
128128
"clear" (as-tuple-instance-fn #(.clear (self->map %1)))

test/libpython_clj2/python_test.clj

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
[tech.v3.datatype.functional :as dfn]
88
[tech.v3.datatype.ffi :as dt-ffi]
99
[tech.v3.tensor :as dtt]
10-
[clojure.test :refer :all])
10+
[clojure.test :refer :all]
11+
libpython-clj2.python.bridge-as-python)
1112
(:import [java.io StringWriter]
1213
[java.util Map List]
1314
[tech.v3.datatype.ffi Pointer]))
@@ -422,6 +423,10 @@ class Foo:
422423
(is (= [5 2]
423424
(py/->jvm (py/get-attr pp "shape"))))))
424425

426+
(deftest iter-should-not-npe
427+
;; credit Carsten Behring
428+
(is (true? (boolean (py. (libpython-clj2.python.bridge-as-python/map-as-python {}) __iter__)))))
429+
425430

426431
(comment
427432
(require '[libpython-clj.require :refer [require-python]])

0 commit comments

Comments
 (0)