Skip to content

Commit

Permalink
Updates trying to enable pregeneration of all required classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
cnuernber committed Mar 4, 2022
1 parent cc745de commit ceb4e5e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 31 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -19,4 +19,5 @@ a.out
/.idea
*.iml
.lsp
.clj-kondo
.clj-kondo
pregen-ffi-test
4 changes: 2 additions & 2 deletions deps.edn
@@ -1,6 +1,6 @@
{:paths ["src"]
:deps {org.clojure/clojure {:mvn/version "1.10.2" :scope "provided"}
cnuernber/dtype-next {:mvn/version "9.009"}
cnuernber/dtype-next {:mvn/version "9.015"}
net.java.dev.jna/jna {:mvn/version "5.10.0"}
org.clojure/data.json {:mvn/version "1.0.0"}}

Expand Down Expand Up @@ -55,7 +55,7 @@
:exec-fn hf.depstar/jar
:exec-args {:group-id "clj-python"
:artifact-id "libpython-clj"
:version "2.018"
:version "2.019-SNAPSHOT"
:sync-pom true
:aot true
:compile-ns [libpython-clj2.java-api]
Expand Down
20 changes: 9 additions & 11 deletions docs/Usage.html
Expand Up @@ -28,17 +28,15 @@ <h4>MacOSX</h4>
<h3>Initialize python</h3>
<pre><code class="language-clojure">user&gt;

user&gt; (require '[libpython-clj.python
:refer [as-python as-jvm
-&gt;python -&gt;jvm
get-attr call-attr call-attr-kw
get-item att-type-map
call call-kw initialize!
as-numpy as-tensor -&gt;numpy
run-simple-string
add-module module-dict
import-module
python-type]])
user&gt; (require [libpython-clj2.python
:refer [as-python as-jvm
-&gt;python -&gt;jvm
get-attr call-attr call-attr-kw
get-item initialize!
run-simple-string
add-module module-dict
import-module
python-type]])
nil

; Mac and Linux
Expand Down
39 changes: 22 additions & 17 deletions src/libpython_clj2/python/ffi.clj
Expand Up @@ -303,18 +303,22 @@ Each call must be matched with PyGILState_Release"}
:doc "Mark a python function as being an instance method."}
})

(defn define-library!
[python-lib-classname]
(dt-ffi/define-library
python-library-fns
["_Py_NoneStruct"
"_Py_FalseStruct"
"_Py_TrueStruct"
"PyType_Type"
"PyExc_StopIteration"
"PyRange_Type"
"PyExc_Exception"]
{:classname python-lib-classname}))


(def python-lib-def* (delay (define-library! nil)))

(def python-lib-def* (delay (dt-ffi/define-library
python-library-fns
["_Py_NoneStruct"
"_Py_FalseStruct"
"_Py_TrueStruct"
"PyType_Type"
"PyExc_StopIteration"
"PyRange_Type"
"PyExc_Exception"]
nil
)))
(defonce pyobject-struct-type*
(delay (dt-struct/define-datatype!
:pyobject [{:name :ob_refcnt :datatype (ffi-size-t/size-t-type)}
Expand Down Expand Up @@ -345,19 +349,19 @@ Each call must be matched with PyGILState_Release"}


(defn reset-library!
[]
[& [library-definition]]
(when @library-path*
(reset! library* (dt-ffi/instantiate-library @python-lib-def*
(reset! library* (dt-ffi/instantiate-library (or library-definition @python-lib-def*)
(:libpath @library-path*)))))


(defn set-library!
[libpath]
[libpath & [library-definition]]
(when @library*
(log/warnf "Python library is being reinitialized to (%s). Is this what you want?"
libpath))
(reset! library-path* {:libpath libpath})
(reset-library!))
(reset-library! library-definition))

;;Useful for repling around - this regenerates the library function bindings
(reset-library!)
Expand Down Expand Up @@ -572,11 +576,12 @@ Each call must be matched with PyGILState_Release"}


(defn initialize!
[libpath python-home & [{:keys [signals? program-name python-home]
[libpath python-home & [{:keys [signals? program-name python-home
library-definition]
:or {signals? true
program-name ""}
:as opts}]]
(set-library! libpath)
(set-library! libpath library-definition)
(when-not (= 1 (Py_IsInitialized))
(log/debug "Initializing Python C Layer")
;;platform specific encoding
Expand Down

0 comments on commit ceb4e5e

Please sign in to comment.