Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/libpython_clj2/codegen.clj
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ user> (doto (python/list)
exclude default-exclude}}]
(let [metadata-fn (requiring-resolve
'libpython-clj2.metadata/datafy-module-or-class)
ns-symbol (or ns-symbol (symbol (str ns-prefix "." py-mod-or-cls)))]
ns-symbol (or ns-symbol (symbol (str (when-not (s/blank? ns-prefix)
(str ns-prefix "."))
py-mod-or-cls)))]
(py/with-gil-stack-rc-context
(let [target (py/path->py-obj py-mod-or-cls)
target-metadata (metadata-fn target)
Expand Down
37 changes: 37 additions & 0 deletions test/libpython_clj2/codegen_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
(ns libpython-clj2.codegen-test
(:require [clojure.test :refer [deftest is testing]]
[clojure.string :as s]
[clojure.java.io :as io]
[libpython-clj2.codegen :as codegen]))

(defn- ns-symbol-for
[py-mod-or-cls ns-prefix]
(symbol (str (when-not (s/blank? ns-prefix)
(str ns-prefix "."))
py-mod-or-cls)))

(deftest ns-prefix-nil-test
(testing "ns-prefix nil should not produce leading dot"
(is (= 'numpy (ns-symbol-for "numpy" nil)))
(is (= 'builtins (ns-symbol-for "builtins" nil))))

(testing "ns-prefix empty string should not produce leading dot"
(is (= 'numpy (ns-symbol-for "numpy" "")))
(is (= 'builtins (ns-symbol-for "builtins" ""))))

(testing "ns-prefix with value should produce prefixed namespace"
(is (= 'python.numpy (ns-symbol-for "numpy" "python")))
(is (= 'my.prefix.builtins (ns-symbol-for "builtins" "my.prefix")))))

(deftest write-namespace-nil-prefix-test
(testing "write-namespace! with nil ns-prefix"
(let [tmp-dir (str (System/getProperty "java.io.tmpdir") "/libpython-clj-test-" (System/currentTimeMillis))]
(try
(codegen/write-namespace! "builtins" {:output-dir tmp-dir
:ns-prefix nil})
(is (.exists (io/file tmp-dir "builtins.clj")))
(let [content (slurp (io/file tmp-dir "builtins.clj"))]
(is (re-find #"\(ns builtins" content)))
(finally
(doseq [f (reverse (file-seq (io/file tmp-dir)))]
(.delete f)))))))