Skip to content

Commit

Permalink
Fixed issue with import modules aliasing using ns eval (#719) (#728)
Browse files Browse the repository at this point in the history
Hi,

could you please review patch to fix issue with import name aliasing in
the context of an `eval` operation. It addresses #719.

(part of breaking the individual bugfixes from
#723)

Thanks.

Co-authored-by: ikappaki <[email protected]>
  • Loading branch information
ikappaki and ikappaki authored Dec 14, 2023
1 parent 78d841a commit 77e2f29
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Fix issue with `sort-*` family of funtions returning an error on an empty seq (#716).
* Fix issue with `intern` failing when used (#725).
* Fix issue with `ns` not being available after `in-ns` on the REPL (#718).
* Fixed issue with import modules aliasing using ns eval (#719).

## [v0.1.0a2]
### Added
Expand Down
8 changes: 7 additions & 1 deletion src/basilisp/core.lpy
Original file line number Diff line number Diff line change
Expand Up @@ -4642,7 +4642,13 @@
Use of ``import`` directly is discouraged in favor of the ``:import`` directive in
the :lpy:fn:`ns` macro."
[& modules]
`(import* ~@modules))
;; We need to use eval here, because there could have been an ns
;; switch inside an eval earlier, and although *ns* is correctly set
;; to the new ns, the global python namespace can still refer to the
;; old python global ns where the import would have been evaluated
;; otherwise.
(let [form `(import* ~@modules)]
`(eval '~form *ns*)))

(defn ^:private rewrite-clojure-libspec
"Rewrite libspecs which point to namespaces starting with 'clojure.' to use a
Expand Down
8 changes: 7 additions & 1 deletion tests/basilisp/test_core_fns.lpy
Original file line number Diff line number Diff line change
Expand Up @@ -2021,4 +2021,10 @@
(binding [*ns* *ns*]
(eval '(in-ns 'test-core-fns.ns-after-in-ns))
(eval '(ns test-core-fns.ns-after-in-ns))
(is (= *ns* (the-ns 'test-core-fns.ns-after-in-ns))))))
(is (= *ns* (the-ns 'test-core-fns.ns-after-in-ns)))))

(testing "eval ns with import aliases"
(binding [*ns* *ns*]
(eval '(ns test-core-fns.ns-with-import (:import [time :as time-alias])))
(is (= *ns* (the-ns 'test-core-fns.ns-with-import)))
(is (= "Thu Jan 1 00:00:01 1970" (eval '(time-alias/asctime (time-alias/gmtime 1))))))))

0 comments on commit 77e2f29

Please sign in to comment.