Skip to content

Commit f213dac

Browse files
committed
Tests for PythonInterpreter initialization now working.
1 parent 62e52d8 commit f213dac

File tree

2 files changed

+45
-26
lines changed

2 files changed

+45
-26
lines changed

src/clojure_python/core.clj

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
(ns clojure-python.core
22
(:use (clojure.core.*))
33
(:import (org.python.util PythonInterpreter)
4-
(org.python.core.*)))
5-
6-
;; instantiate a python interpreter in the python namespace
7-
;(def *interp* (new org.python.util.PythonInterpreter))
4+
(org.python.core.*)
5+
(java.lang System String)
6+
(java.util Properties )))
87

98
(defn init
109
"this may later take keywords and initialize other things
1110
for now it is just used to specify python library paths"
12-
([interp libpath]
13-
(doto interp
11+
([py libpath]
12+
(doto py
1413
(.exec "import sys")
1514
(.exec (str "sys.path.append('" libpath "')"))))
16-
([interp libpath & more]
17-
(init interp libpath)
15+
([py libpath & more]
16+
(init py libpath)
1817
(apply init more)))
1918

2019
(defmacro py-import
@@ -90,22 +89,31 @@
9089
(if (seq funcs)
9190
(dorun (map #(import-fn module %) funcs)))))
9291

93-
(defn python [name & {:keys
94-
[pre-properties
95-
post-properties
96-
argv
97-
sys-path
98-
modules]}]
99-
(do
100-
(PythonInterpreter/initialize pre-properties post-properties argv)
101-
(let [interp (PythonInterpreter.)]
92+
(defn properties [h]
93+
{:pre (map? h)}
94+
(let [p (Properties.)]
95+
(dorun (map #(.setProperty p (str (first %)) (str (second %))) h))
96+
p))
97+
98+
(defn python
99+
[name & {:keys [post-properties argv sys-path modules]
100+
:or {post-properties {} argv []}}]
101+
(let [pre (System/getProperties)
102+
post (properties post-properties)
103+
args (into-array String (map str argv))]
104+
;; Initialization has to occur before any interpreter instances are
105+
;; created.
106+
(PythonInterpreter/initialize pre post args)
107+
(let [py (PythonInterpreter.)]
102108
(if (seq sys-path)
103-
(init interp sys-path))
109+
(init py sys-path))
104110
(loop [mods modules]
105-
(let [[m args] (first mods) k (:funcs args) o (:objs args)]
106-
(python-mod interp m :funcs k :objs o)
107-
(recur (rest mods))))
108-
interp)))
111+
(println (count mods))
112+
(if (seq mods)
113+
(let [[m args] (first mods) k (:funcs args) o (:objs args)]
114+
(python-mod py m :funcs k :objs o)
115+
(recur (rest mods)))))
116+
py)))
109117

110118
(defmacro __
111119
"access attribute of class or attribute of attribute of (and so on) class"

test/clojure_python/core_test.clj

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,26 @@
22
(:use [clojure-python.core] :reload-all)
33
(:use [clojure.test]))
44

5-
(deftest replace-me ;; FIXME: write
6-
(is false "No tests have been written."))
75

6+
(comment
87
(deftest test-python
9-
(let [py (python
8+
(let [py (python 'py-interp
109
:pre-properties {:foo "bar"}
1110
:post-properties {:bar "foo"}
1211
:argv ["one" "two" "three"]
12+
1313
:sys-path []
14-
:modules ['mod1 {:function ['f1 'f2] :objs ['o1 'o2]}])]))
14+
:modules [['mod1 {:funcs ['f1 'f2] :objs ['o1 'o2]}]]
15+
)]))
1516

17+
)
1618

19+
(deftest test-python-init
20+
(let [py (python 'py-interp
21+
:post-properties {"python.home" "/usr/local/my-python"}
22+
:argv ["one" "two" "three"])]
23+
(.exec py "import sys")
24+
(is (= ["one", "two", "three"] (map str (.eval py "sys.argv"))))
25+
(is (some
26+
#(= % "/usr/local/my-python/Lib")
27+
(map str (.eval py "sys.path"))))))

0 commit comments

Comments
 (0)