From a785a9131959946fe7df47a274b25d766897fc0f Mon Sep 17 00:00:00 2001 From: James Tolton Date: Fri, 21 Oct 2022 14:35:34 -0400 Subject: [PATCH 1/4] Prevent NPE by substituting vector if `keys` evaluates to `nil` --- .../python/bridge_as_python.clj | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/src/libpython_clj2/python/bridge_as_python.clj b/src/libpython_clj2/python/bridge_as_python.clj index 676e7921..5c4f16cf 100644 --- a/src/libpython_clj2/python/bridge_as_python.clj +++ b/src/libpython_clj2/python/bridge_as_python.clj @@ -104,31 +104,31 @@ (defonce mapping-type* (jvm-handle/py-global-delay - (with-gil - (py-ffi/with-decref - [mod (py-ffi/PyImport_ImportModule "collections.abc") - map-type (py-ffi/PyObject_GetAttrString mod "MutableMapping")] + (with-gil + (py-ffi/with-decref + [mod (py-ffi/PyImport_ImportModule "collections.abc") + map-type (py-ffi/PyObject_GetAttrString mod "MutableMapping")] ;;In order to make things work ingeneral - (py-class/create-class - "jvm-map-as-python" - [map-type] - {"__init__" (py-class/wrapped-jvm-constructor) - "__del__" (py-class/wrapped-jvm-destructor) - "__contains__" (as-tuple-instance-fn #(.containsKey (self->map %1) - (py-base/as-jvm %2))) - "__eq__" (as-tuple-instance-fn #(.equals (self->map %1) (py-base/as-jvm %2))) - "__getitem__" (as-tuple-instance-fn - #(.get (self->map %1) (py-base/as-jvm %2))) - "__setitem__" (as-tuple-instance-fn #(.put (self->map %1) (py-base/as-jvm %2) %3)) - "__delitem__" (as-tuple-instance-fn #(.remove (self->map %1) (py-base/as-jvm %2))) - "__hash__" (as-tuple-instance-fn #(.hashCode (self->map %1))) - "__iter__" (as-tuple-instance-fn #(.iterator ^Iterable (keys (self->map %1)))) - "__len__" (as-tuple-instance-fn #(.size (self->map %1))) - "__str__" (as-tuple-instance-fn #(.toString (self->map %1))) - "clear" (as-tuple-instance-fn #(.clear (self->map %1))) - "keys" (as-tuple-instance-fn #(seq (.keySet (self->map %1)))) - "values" (as-tuple-instance-fn #(seq (.values (self->map %1)))) - "pop" (as-tuple-instance-fn #(.remove (self->map %1) (py-base/as-jvm %2)))}))))) + (py-class/create-class + "jvm-map-as-python" + [map-type] + {"__init__" (py-class/wrapped-jvm-constructor) + "__del__" (py-class/wrapped-jvm-destructor) + "__contains__" (as-tuple-instance-fn #(.containsKey (self->map %1) + (py-base/as-jvm %2))) + "__eq__" (as-tuple-instance-fn #(.equals (self->map %1) (py-base/as-jvm %2))) + "__getitem__" (as-tuple-instance-fn + #(.get (self->map %1) (py-base/as-jvm %2))) + "__setitem__" (as-tuple-instance-fn #(.put (self->map %1) (py-base/as-jvm %2) %3)) + "__delitem__" (as-tuple-instance-fn #(.remove (self->map %1) (py-base/as-jvm %2))) + "__hash__" (as-tuple-instance-fn #(.hashCode (self->map %1))) + "__iter__" (as-tuple-instance-fn #(.iterator ^Iterable (or (keys (self->map %1)) []))) + "__len__" (as-tuple-instance-fn #(.size (self->map %1))) + "__str__" (as-tuple-instance-fn #(.toString (self->map %1))) + "clear" (as-tuple-instance-fn #(.clear (self->map %1))) + "keys" (as-tuple-instance-fn #(seq (.keySet (self->map %1)))) + "values" (as-tuple-instance-fn #(seq (.values (self->map %1)))) + "pop" (as-tuple-instance-fn #(.remove (self->map %1) (py-base/as-jvm %2)))}))))) (defn map-as-python From b4db13f5a20b20ea570afe24c49ac1e4e78f0b28 Mon Sep 17 00:00:00 2001 From: James Tolton Date: Fri, 21 Oct 2022 14:38:28 -0400 Subject: [PATCH 2/4] fix whitespace issue on previous commit --- .../python/bridge_as_python.clj | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/src/libpython_clj2/python/bridge_as_python.clj b/src/libpython_clj2/python/bridge_as_python.clj index 5c4f16cf..9754c08b 100644 --- a/src/libpython_clj2/python/bridge_as_python.clj +++ b/src/libpython_clj2/python/bridge_as_python.clj @@ -104,31 +104,31 @@ (defonce mapping-type* (jvm-handle/py-global-delay - (with-gil - (py-ffi/with-decref - [mod (py-ffi/PyImport_ImportModule "collections.abc") - map-type (py-ffi/PyObject_GetAttrString mod "MutableMapping")] + (with-gil + (py-ffi/with-decref + [mod (py-ffi/PyImport_ImportModule "collections.abc") + map-type (py-ffi/PyObject_GetAttrString mod "MutableMapping")] ;;In order to make things work ingeneral - (py-class/create-class - "jvm-map-as-python" - [map-type] - {"__init__" (py-class/wrapped-jvm-constructor) - "__del__" (py-class/wrapped-jvm-destructor) - "__contains__" (as-tuple-instance-fn #(.containsKey (self->map %1) - (py-base/as-jvm %2))) - "__eq__" (as-tuple-instance-fn #(.equals (self->map %1) (py-base/as-jvm %2))) - "__getitem__" (as-tuple-instance-fn - #(.get (self->map %1) (py-base/as-jvm %2))) - "__setitem__" (as-tuple-instance-fn #(.put (self->map %1) (py-base/as-jvm %2) %3)) - "__delitem__" (as-tuple-instance-fn #(.remove (self->map %1) (py-base/as-jvm %2))) - "__hash__" (as-tuple-instance-fn #(.hashCode (self->map %1))) - "__iter__" (as-tuple-instance-fn #(.iterator ^Iterable (or (keys (self->map %1)) []))) - "__len__" (as-tuple-instance-fn #(.size (self->map %1))) - "__str__" (as-tuple-instance-fn #(.toString (self->map %1))) - "clear" (as-tuple-instance-fn #(.clear (self->map %1))) - "keys" (as-tuple-instance-fn #(seq (.keySet (self->map %1)))) - "values" (as-tuple-instance-fn #(seq (.values (self->map %1)))) - "pop" (as-tuple-instance-fn #(.remove (self->map %1) (py-base/as-jvm %2)))}))))) + (py-class/create-class + "jvm-map-as-python" + [map-type] + {"__init__" (py-class/wrapped-jvm-constructor) + "__del__" (py-class/wrapped-jvm-destructor) + "__contains__" (as-tuple-instance-fn #(.containsKey (self->map %1) + (py-base/as-jvm %2))) + "__eq__" (as-tuple-instance-fn #(.equals (self->map %1) (py-base/as-jvm %2))) + "__getitem__" (as-tuple-instance-fn + #(.get (self->map %1) (py-base/as-jvm %2))) + "__setitem__" (as-tuple-instance-fn #(.put (self->map %1) (py-base/as-jvm %2) %3)) + "__delitem__" (as-tuple-instance-fn #(.remove (self->map %1) (py-base/as-jvm %2))) + "__hash__" (as-tuple-instance-fn #(.hashCode (self->map %1))) + "__iter__" (as-tuple-instance-fn #(.iterator ^Iterable (or (keys (self->map %1)) []))) + "__len__" (as-tuple-instance-fn #(.size (self->map %1))) + "__str__" (as-tuple-instance-fn #(.toString (self->map %1))) + "clear" (as-tuple-instance-fn #(.clear (self->map %1))) + "keys" (as-tuple-instance-fn #(seq (.keySet (self->map %1)))) + "values" (as-tuple-instance-fn #(seq (.values (self->map %1)))) + "pop" (as-tuple-instance-fn #(.remove (self->map %1) (py-base/as-jvm %2)))}))))) (defn map-as-python From 91d3c03c48fea2fac693da3b1c5bc96e80f3d369 Mon Sep 17 00:00:00 2001 From: James Tolton Date: Fri, 21 Oct 2022 14:46:18 -0400 Subject: [PATCH 3/4] Add __iter__ test --- test/libpython_clj2/python_test.clj | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/libpython_clj2/python_test.clj b/test/libpython_clj2/python_test.clj index bc50c6dd..5eb53e59 100644 --- a/test/libpython_clj2/python_test.clj +++ b/test/libpython_clj2/python_test.clj @@ -7,7 +7,8 @@ [tech.v3.datatype.functional :as dfn] [tech.v3.datatype.ffi :as dt-ffi] [tech.v3.tensor :as dtt] - [clojure.test :refer :all]) + [clojure.test :refer :all] + libpython-clj2.python.bridge-as-python) (:import [java.io StringWriter] [java.util Map List] [tech.v3.datatype.ffi Pointer])) @@ -422,6 +423,9 @@ class Foo: (is (= [5 2] (py/->jvm (py/get-attr pp "shape")))))) +(deftest iter-should-not-npe + (is (true? (boolean (py. (libpython-clj2.python.bridge-as-python/map-as-python {}) __iter__))))) + (comment (require '[libpython-clj.require :refer [require-python]]) From b5e3b4cda875dbd80c77880bb4b80a46a3950df5 Mon Sep 17 00:00:00 2001 From: James Tolton Date: Fri, 21 Oct 2022 14:47:25 -0400 Subject: [PATCH 4/4] give credit where credit is due --- test/libpython_clj2/python_test.clj | 1 + 1 file changed, 1 insertion(+) diff --git a/test/libpython_clj2/python_test.clj b/test/libpython_clj2/python_test.clj index 5eb53e59..7f1b2708 100644 --- a/test/libpython_clj2/python_test.clj +++ b/test/libpython_clj2/python_test.clj @@ -424,6 +424,7 @@ class Foo: (py/->jvm (py/get-attr pp "shape")))))) (deftest iter-should-not-npe + ;; credit Carsten Behring (is (true? (boolean (py. (libpython-clj2.python.bridge-as-python/map-as-python {}) __iter__))))) pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy