-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
base.clj
60 lines (39 loc) · 1.33 KB
/
base.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
(ns libpython-clj.jna.base
(:require [tech.jna :as jna]
[tech.jna.base :as jna-base]
[camel-snake-kebab.core :refer [->kebab-case]])
(:import [com.sun.jna Pointer NativeLibrary]
[libpython_clj.jna PyObject]))
(def ^:dynamic *python-library* "python3.6m")
(defprotocol PToPyObjectPtr
(convertible-to-pyobject-ptr? [item])
(->py-object-ptr [item]))
(extend-type PyObject
PToPyObjectPtr
(convertible-to-pyobject-ptr? [item] true)
(->py-object-ptr [item] (.getPointer item)))
(extend-type Pointer
PToPyObjectPtr
(convertible-to-pyobject-ptr? [item] true)
(->py-object-ptr [item] item))
(defn ensure-pyobj
[item]
(if-let [retval (->py-object-ptr item)]
retval
(throw (ex-info "Failed to get a pyobject pointer from object." {}))))
(defn ensure-pydict
"The return value of this has to be a python dictionary object."
[item]
(ensure-pyobj item))
(defn ensure-pytuple
"The return value of this has to be a python tuple object."
[item]
(ensure-pyobj item))
(defmacro def-pylib-fn
[fn-name docstring & args]
`(jna/def-jna-fn *python-library* ~fn-name ~docstring ~@args))
(def size-t-type (type (jna/size-t 0)))
(defn find-pylib-symbol
[sym-name]
(.getGlobalVariableAddress ^NativeLibrary (jna-base/load-library *python-library*)
sym-name))