|
197 | 197 | (py/call-attr item "doit_noerr")) |
198 | 198 | (is (= ["enter" "exit: None"] |
199 | 199 | (py/->jvm fn-list)))))) |
| 200 | + |
| 201 | + |
| 202 | +(deftest arrow-as-fns-with-nil |
| 203 | + (py/initialize!) |
| 204 | + (is (= nil (py/->jvm nil))) |
| 205 | + (is (= nil (py/as-jvm nil)))) |
| 206 | + |
| 207 | + |
| 208 | +(deftest pydict-nil-get |
| 209 | + (py/initialize!) |
| 210 | + (let [dict (py/->python {:a 1 :b {:a 1 :b 2}}) |
| 211 | + bridged (py/as-jvm dict)] |
| 212 | + (is (= nil (bridged nil))))) |
| 213 | + |
| 214 | + |
| 215 | +(deftest custom-clojure-item |
| 216 | + (let [att-map {"clojure_fn" (py/->python #(vector 1 2 3))} |
| 217 | + my-python-item (py/create-bridge-from-att-map |
| 218 | + ;;First is the jvm object that this bridge stands for. If this |
| 219 | + ;;gets garbage collected then the python side will be removed |
| 220 | + ;;also. |
| 221 | + att-map |
| 222 | + ;;second is the list of attributes. In this case, since this |
| 223 | + ;;object isn't iterable or anything, this function will do. |
| 224 | + att-map |
| 225 | + ) |
| 226 | + py-mod (py/import-module "testcode")] |
| 227 | + (is (= [1 2 3] |
| 228 | + (py/call-attr py-mod "calling_custom_clojure_fn" my-python-item))) |
| 229 | + |
| 230 | + ;;Now this case is harder. Let's say we have something that is iterable and we |
| 231 | + ;;want this to be reflected in python. In that case we have to call 'as-python' |
| 232 | + ;;on the iterator and that 'should' work. |
| 233 | + |
| 234 | + (let [my-obj (reify |
| 235 | + Iterable |
| 236 | + (iterator [this] (.iterator [4 5 6]))) |
| 237 | + ;;Note that attributes themselves have to be python objects and wrapping |
| 238 | + ;;this with as-python makes that function wrap whatever it returns in a |
| 239 | + ;;bridging python object also. |
| 240 | + att-map {"__iter__" (py/as-python #(.iterator my-obj))} |
| 241 | + my-python-item (py/create-bridge-from-att-map |
| 242 | + my-obj |
| 243 | + ;;second is the list of attributes. In this case, since this |
| 244 | + ;;object isn't iterable or anything, this function will do. |
| 245 | + att-map |
| 246 | + )] |
| 247 | + (is (= [4 5 6] |
| 248 | + (vec my-obj))) |
| 249 | + (is (= [4 5 6] |
| 250 | + (vec (py/call-attr py-mod "for_iter" my-python-item))))))) |
0 commit comments