Skip to content

Commit c58e9af

Browse files
committed
Fixes #157
1 parent 067c193 commit c58e9af

File tree

4 files changed

+28
-9
lines changed

4 files changed

+28
-9
lines changed

deps.edn

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{:paths ["src"]
22
:deps {org.clojure/clojure {:mvn/version "1.10.2" :scope "provided"}
33
camel-snake-kebab/camel-snake-kebab {:mvn/version "0.4.0"}
4-
cnuernber/dtype-next {:mvn/version "7.01"}
4+
cnuernber/dtype-next {:mvn/version "7.07"}
55
net.java.dev.jna/jna {:mvn/version "5.7.0"}
66
org.clojure/data.json {:mvn/version "1.0.0"}}
77

src/libpython_clj2/python/bridge_as_jvm.clj

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,16 @@
196196
(py-proto/set-item! @pyobj*# item-name# item-value#))))
197197
py-proto/PyCall
198198
(call [callable# arglist# kw-arg-map#]
199-
(-> (py-fn/call-py-fn @pyobj*# arglist# kw-arg-map# py-base/->python)
200-
(py-base/as-jvm)))
199+
(with-gil
200+
(-> (py-fn/call-py-fn @pyobj*# arglist# kw-arg-map# py-base/->python)
201+
(py-base/as-jvm))))
201202
(marshal-return [callable# retval#]
202-
(py-base/as-jvm retval#))
203+
(with-gil
204+
(py-base/as-jvm retval#)))
203205
clj-proto/Datafiable
204-
(datafy [callable#] (py-proto/pydatafy callable#))
206+
(datafy [callable#]
207+
(with-gil
208+
(py-proto/pydatafy callable#)))
205209
Object
206210
(toString [this#]
207211
(with-gil

src/libpython_clj2/python/ffi.clj

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,8 +370,11 @@ Each call must be matched with PyGILState_Release"}
370370
;;When this is true, generated functions will throw an exception if called when the
371371
;;GIL is not captured. It makes sense to periodically enable this flag in order
372372
;;to ensure we aren't getting away with sneaky non-GIL access to Python.
373-
(def enable-api-gilcheck false)
373+
(def enable-api-gilcheck* (atom false))
374374

375+
(defn enable-gil-check!
376+
([] (reset! enable-api-gilcheck* true))
377+
([value] (reset! enable-api-gilcheck* (boolean value))))
375378

376379
(defn- find-pylib-fn
377380
[fn-kwd]
@@ -409,7 +412,7 @@ Each call must be matched with PyGILState_Release"}
409412
(let [fn-symbol (symbol (name fn-name))
410413
requires-resctx? (first (filter #(= :string %)
411414
(map second argtypes)))
412-
gilcheck? (when enable-api-gilcheck
415+
gilcheck? (when @enable-api-gilcheck*
413416
(if (contains? fn-data :requires-gil?)
414417
(fn-data :requires-gil?)
415418
true))]

test/libpython_clj2/python_test.clj

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515

1616
(py/initialize!)
17+
(py-ffi/enable-gil-check!)
1718
;; Useful to see how many times we convert a string to a python object.
1819
;; (dt-ffi/enable-string->c-stats!)
1920
;; (.addShutdownHook (Runtime/getRuntime) (Thread.
@@ -386,8 +387,19 @@ class Foo:
386387

387388
(deftest numpy-all
388389
(let [np (py/import-module "numpy")]
389-
(is (= true (py/call-attr np "all" (py/call-attr np "array" [true true true]))))
390-
(is (= false (py/call-attr np "all" (py/call-attr np "array" [true false true]))))))
390+
(is (= true (py/call-attr np "all"
391+
(py/call-attr np "array" [true true true]))))
392+
(is (= false (py/call-attr np "all"
393+
(py/call-attr np "array" [true false true]))))))
394+
395+
396+
(deftest np-dot
397+
(let [np (py/import-module "numpy")
398+
np-dot (py/get-attr np "dot")
399+
np-ary (py/call-attr np "array" [1 2 3])]
400+
(is (== 14 (np-dot np-ary np-ary)))))
401+
402+
391403

392404

393405
(comment

0 commit comments

Comments
 (0)