File tree Expand file tree Collapse file tree 4 files changed +60
-1
lines changed Expand file tree Collapse file tree 4 files changed +60
-1
lines changed Original file line number Diff line number Diff line change 1
1
# Time for a ChangeLog!
2
+ ## 2.020
3
+ * Better support for running with ` -Dlibpython_clj.manual_gil=true ` - ` with-manual-gil ` .
4
+ Addresses [ issue 221] ( https://github.com/clj-python/libpython-clj/issues/221 ) .
5
+
2
6
## 2.019
3
7
* Upgrade to clojure 1.11 as development version.
4
8
* Upgrade dtype-next to get rid of 1.11 warnings (and for unary min,max).
Original file line number Diff line number Diff line change @@ -126,7 +126,7 @@ user> (py/py. np linspace 2 3 :num 10)
126
126
127
127
(defmacro with-gil
128
128
" Capture the gil for an extended amount of time. This can greatly speed up
129
- operations as the mutex is captured and held once as opposed to find grained
129
+ operations as the mutex is captured and held once as opposed to fine grained
130
130
grabbing/releasing of the mutex."
131
131
[& body]
132
132
`(py-ffi/with-gil
@@ -146,6 +146,33 @@ user> (py/py. np linspace 2 3 :num 10)
146
146
(pygc/with-stack-context
147
147
~@body)))
148
148
149
+
150
+ (defmacro with-manual-gil
151
+ " When running with -Dlibpython_clj.manual_gil=true, you need to wrap all accesses to
152
+ the python runtime with this locker. This includes calls to require-python or any other
153
+ pathways.
154
+
155
+ ```clojure
156
+ (with-manual-gil
157
+ ...)
158
+ ```
159
+ "
160
+ [& body]
161
+ `(with-open [locker# (py-ffi/manual-gil-locker )]
162
+ ~@body))
163
+
164
+
165
+ (defmacro with-manual-gil-stack-rc-context
166
+ " When running with -Dlibpython_clj.manual_gil=true, you need to wrap all accesses to
167
+ the python runtime with this locker. This includes calls to require-python or any other
168
+ pathways. This macro furthermore defines a stack-based gc context to immediately release
169
+ objects when the stack frame exits."
170
+ [& body]
171
+ `(with-manual-gil
172
+ (pygc/with-stack-context
173
+ ~@body)))
174
+
175
+
149
176
(declare ->jvm )
150
177
151
178
(defn ^:no-doc in-py-ctx
Original file line number Diff line number Diff line change @@ -737,6 +737,14 @@ Each call must be matched with PyGILState_Release"}
737
737
(PyGILState_Release gilstate)))
738
738
739
739
740
+ (defn ^:no-doc manual-gil-locker
741
+ ^java.lang.AutoCloseable []
742
+ (let [gil-state (lock-gil )]
743
+ (reify java.lang.AutoCloseable
744
+ (close [this]
745
+ (unlock-gil gil-state)))))
746
+
747
+
740
748
(defmacro with-gil
741
749
" Grab the gil and use the main interpreter using reentrant acquire-gil pathway."
742
750
[& body]
Original file line number Diff line number Diff line change 48
48
(is (= {" name" " ACME" , " shares" 50 , " price" 90 }
49
49
(edn/read-string (.toString new-instance))))
50
50
(is (= 116.0 (py/call-attr-kw new-instance " kw_clj_fn" [1 2 3 ] {:a 20 })))))
51
+
52
+
53
+ (deftest new-kw-init-cls-test
54
+ ; ;The crux of this is making instance functions to get the 'self' parameter
55
+ ; ;passed in.
56
+ (let [cls-obj (py/create-class
57
+ " Stock" nil
58
+ {" __init__" (py/make-kw-instance-fn
59
+ (fn [[self :as args] {:as kwargs}]
60
+ (py/set-attr! self " kwargs" kwargs)
61
+ ; ;Because we did not use an arg-converter, all the
62
+ ; ;arguments above are raw jna Pointers - borrowed
63
+ ; ;references.
64
+ ; ;If you don't return nil from __init__ that is an
65
+ ; ;error.
66
+ nil ))})
67
+ new-instance (py/cfn cls-obj " ACME" 50 :a 1 :b 2 )
68
+ dict (py/get-attr new-instance " kwargs" )]
69
+ (is (= {" a" 1 " b" 2 }
70
+ (py/->jvm dict)))))
You can’t perform that action at this time.
0 commit comments