Skip to content

Commit c40ebab

Browse files
committed
Quieting failures when something passes the sequence check.
1 parent bef136e commit c40ebab

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

src/libpython_clj2/python.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ user> (py/call-attr inst \"addarg\" 10)
616616
(catch Throwable e nil))]
617617
(if reload?
618618
(let [import-lib (import-module "importlib")]
619-
(call-attr import-lib "reload" item-path))
619+
(call-attr import-lib "reload" module-retval))
620620
module-retval)
621621
(let [butlast (module-path-string item-path)]
622622
(if-let [parent-mod (path->py-obj butlast :reload? reload?)]

src/libpython_clj2/python/copy.clj

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,18 @@
3939
(when-not (= 1 (py-ffi/PySequence_Check pyobj))
4040
(errors/throwf "Object does not implement sequence protocol: %s"
4141
(py-proto/python-type pyobj)))
42-
(->> (range (py-ffi/PySequence_Length pyobj))
43-
(mapv (fn [idx]
44-
(let [pyitem (py-ffi/PySequence_GetItem pyobj idx)]
45-
(try
46-
(py-base/->jvm pyitem)
47-
(finally
48-
(py-ffi/Py_DecRef pyitem))))))))
42+
;; Unfortunately sometimes things that pass the sequence check will fail
43+
;; when you request their length. In those cases we return an empty vector
44+
;; as that is what older versions of libpython-clj would do.
45+
(try
46+
(->> (range (py-ffi/with-error-check (py-ffi/PySequence_Length pyobj)))
47+
(mapv (fn [idx]
48+
(let [pyitem (py-ffi/PySequence_GetItem pyobj idx)]
49+
(try
50+
(py-base/->jvm pyitem)
51+
(finally
52+
(py-ffi/Py_DecRef pyitem)))))))
53+
(catch Throwable e [])))
4954

5055

5156
(defmethod py-proto/pyobject->jvm :str
@@ -176,16 +181,17 @@
176181
;;Things could implement mapping and sequence logically so mapping
177182
;;takes precedence
178183
(= 1 (py-ffi/PyMapping_Check pyobj))
179-
(if-let [map-items (py-ffi/PyMapping_Items pyobj)]
180-
(try
181-
(python->jvm-copy-hashmap pyobj map-items)
182-
(finally
183-
(py-ffi/Py_DecRef map-items)))
184-
(do
185-
;;Ignore error. The mapping check isn't thorough enough to work for all
186-
;;python objects.
187-
(py-ffi/PyErr_Clear)
188-
(python->jvm-copy-persistent-vector pyobj)))
184+
(do
185+
(if-let [map-items (py-ffi/PyMapping_Items pyobj)]
186+
(try
187+
(python->jvm-copy-hashmap pyobj map-items)
188+
(finally
189+
(py-ffi/Py_DecRef map-items)))
190+
(do
191+
;;Ignore error. The mapping check isn't thorough enough to work for all
192+
;;python objects.
193+
(py-ffi/PyErr_Clear)
194+
(python->jvm-copy-persistent-vector pyobj))))
189195
;;Sequences become persistent vectors
190196
(= 1 (py-ffi/PySequence_Check pyobj))
191197
(python->jvm-copy-persistent-vector pyobj)

0 commit comments

Comments
 (0)