Skip to content

Commit 4e355c1

Browse files
authored
allow nil ns-prefix in codegen/write-namespace! (#279)
* allow blank ns-prefix * add codegen_test
1 parent 27078c9 commit 4e355c1

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/libpython_clj2/codegen.clj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,9 @@ user> (doto (python/list)
151151
exclude default-exclude}}]
152152
(let [metadata-fn (requiring-resolve
153153
'libpython-clj2.metadata/datafy-module-or-class)
154-
ns-symbol (or ns-symbol (symbol (str ns-prefix "." py-mod-or-cls)))]
154+
ns-symbol (or ns-symbol (symbol (str (when-not (s/blank? ns-prefix)
155+
(str ns-prefix "."))
156+
py-mod-or-cls)))]
155157
(py/with-gil-stack-rc-context
156158
(let [target (py/path->py-obj py-mod-or-cls)
157159
target-metadata (metadata-fn target)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
(ns libpython-clj2.codegen-test
2+
(:require [clojure.test :refer [deftest is testing]]
3+
[clojure.string :as s]
4+
[clojure.java.io :as io]
5+
[libpython-clj2.codegen :as codegen]))
6+
7+
(defn- ns-symbol-for
8+
[py-mod-or-cls ns-prefix]
9+
(symbol (str (when-not (s/blank? ns-prefix)
10+
(str ns-prefix "."))
11+
py-mod-or-cls)))
12+
13+
(deftest ns-prefix-nil-test
14+
(testing "ns-prefix nil should not produce leading dot"
15+
(is (= 'numpy (ns-symbol-for "numpy" nil)))
16+
(is (= 'builtins (ns-symbol-for "builtins" nil))))
17+
18+
(testing "ns-prefix empty string should not produce leading dot"
19+
(is (= 'numpy (ns-symbol-for "numpy" "")))
20+
(is (= 'builtins (ns-symbol-for "builtins" ""))))
21+
22+
(testing "ns-prefix with value should produce prefixed namespace"
23+
(is (= 'python.numpy (ns-symbol-for "numpy" "python")))
24+
(is (= 'my.prefix.builtins (ns-symbol-for "builtins" "my.prefix")))))
25+
26+
(deftest write-namespace-nil-prefix-test
27+
(testing "write-namespace! with nil ns-prefix"
28+
(let [tmp-dir (str (System/getProperty "java.io.tmpdir") "/libpython-clj-test-" (System/currentTimeMillis))]
29+
(try
30+
(codegen/write-namespace! "builtins" {:output-dir tmp-dir
31+
:ns-prefix nil})
32+
(is (.exists (io/file tmp-dir "builtins.clj")))
33+
(let [content (slurp (io/file tmp-dir "builtins.clj"))]
34+
(is (re-find #"\(ns builtins" content)))
35+
(finally
36+
(doseq [f (reverse (file-seq (io/file tmp-dir)))]
37+
(.delete f)))))))

0 commit comments

Comments
 (0)