|
39 | 39 | (when-not (= 1 (py-ffi/PySequence_Check pyobj)) |
40 | 40 | (errors/throwf "Object does not implement sequence protocol: %s" |
41 | 41 | (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 []))) |
49 | 54 |
|
50 | 55 |
|
51 | 56 | (defmethod py-proto/pyobject->jvm :str |
|
176 | 181 | ;;Things could implement mapping and sequence logically so mapping |
177 | 182 | ;;takes precedence |
178 | 183 | (= 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)))) |
189 | 195 | ;;Sequences become persistent vectors |
190 | 196 | (= 1 (py-ffi/PySequence_Check pyobj)) |
191 | 197 | (python->jvm-copy-persistent-vector pyobj) |
|
0 commit comments