Skip to content

Commit

Permalink
Adress issue with intern not working (#725)
Browse files Browse the repository at this point in the history
Hi,

could you please review patch to fix `intern`. It addresses #724.

It was using var methods such as `.set-root` that don't exist.

Tests included.

Thanks

---------

Co-authored-by: ikappaki <[email protected]>
  • Loading branch information
ikappaki and ikappaki authored Dec 5, 2023
1 parent 44760b6 commit 63ba73f
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Fix `(is (= exp act))` should only evaluate its args once on failure (#712).
* Fix issue with `with` failing with a traceback error when an exception is thrown (#714).
* Fix issue with `sort-*` family of funtions returning an error on an empty seq (#716).
* Fix issue with `intern` failing when used (#725).

## [v0.1.0a2]
### Added
Expand Down
3 changes: 2 additions & 1 deletion docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
sphinx~=4.4.0
sphinx~=4.4.0
sphinx-rtd-theme==1.3.0
10 changes: 5 additions & 5 deletions src/basilisp/core.lpy
Original file line number Diff line number Diff line change
Expand Up @@ -4533,13 +4533,13 @@
the root binding to ``val``\\, if provided. The namespace must exist. Return the Var."
([ns name]
(let [ns (the-ns ns)
v (basilisp.lang.runtime/Var ns name)]
(.intern ns v)))
v (basilisp.lang.runtime/Var ns name ** :meta {:ns ns :name name})]
(.intern ns name v)))
([ns name val]
(let [ns (the-ns ns)
v (basilisp.lang.runtime/Var ns name)]
(.set-root v val)
(.intern ns v))))
v (basilisp.lang.runtime/Var ns name ** :meta {:ns ns :name name})]
(.bind-root v val)
(.intern ns name v))))

(defn ^:inline create-ns
"Create a Namespace with the name ``ns-sym`` or return the existing one if it already
Expand Down
2 changes: 1 addition & 1 deletion src/basilisp/lang/compiler/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ def __init__(
self._func_ctx: Deque[FunctionContext] = collections.deque([])
self._is_quoted: Deque[bool] = collections.deque([])
self._opts = (
Maybe(opts).map(lmap.map).or_else_get(lmap.PersistentMap.empty()) # type: ignore
Maybe(opts).map(lmap.map).or_else_get(lmap.PersistentMap.empty()) # type: ignore[arg-type, unused-ignore]
)
self._recur_points: Deque[RecurPoint] = collections.deque([])
self._should_macroexpand = should_macroexpand
Expand Down
2 changes: 1 addition & 1 deletion src/basilisp/lang/compiler/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3527,7 +3527,7 @@ def _const_type_to_py_ast(form: IType, ctx: GeneratorContext) -> GeneratedPyAST:

ctor_args = []
ctor_arg_deps: List[ast.AST] = []
for field in attr.fields(tp): # type: ignore[arg-type]
for field in attr.fields(tp): # type: ignore[arg-type, misc, unused-ignore]
field_nodes = _const_val_to_py_ast(getattr(form, field.name, None), ctx)
ctor_args.append(field_nodes.node)
ctor_args.extend(field_nodes.dependencies)
Expand Down
2 changes: 1 addition & 1 deletion src/basilisp/lang/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -1963,7 +1963,7 @@ def add_generated_python(
which_ns = get_current_ns()
v = Maybe(which_ns.find(sym.symbol(GENERATED_PYTHON_VAR_NAME))).or_else(
lambda: Var.intern(
which_ns, # type: ignore
which_ns, # type: ignore[arg-type, unused-ignore]
sym.symbol(GENERATED_PYTHON_VAR_NAME),
"",
dynamic=True,
Expand Down
33 changes: 33 additions & 0 deletions tests/basilisp/test_core_fns.lpy
Original file line number Diff line number Diff line change
Expand Up @@ -1363,6 +1363,39 @@
;; a valid symbol in that namespace
(is (= (resolve 'basilisp.shell/sh) (requiring-resolve 'basilisp.shell/sh))))

(deftest intern-test
(let [ns-sym (gensym "intern-test-ns")
ns0 (create-ns ns-sym)]
(testing "unbound"
(let [sym (gensym "intern-test")
v (intern ns0 sym)
{:keys [name ns] :as m} (meta v)]
(is (= ns0 ns))
(is (= sym name))
(is (= false (.-is-bound v)))))

(testing "unbound ns-sym"
(let [sym (gensym "intern-test")
v (intern ns-sym sym)
{:keys [name ns] :as m} (meta v)]
(is (= ns0 ns))
(is (= sym name))
(is (= false (.-is-bound v)))))

(testing "bound"
(let [sym (gensym "intern-test")
v (intern ns0 sym 5)
{:keys [name ns] :as m} (meta v)]
(is (= ns0 ns))
(is (= sym name))
(is (= true (.-is-bound v)))
(is (= 5 @v))
(is (= v (find-var (symbol (str ns0) (str sym))))))))


(testing "ns-not-exists"
(is (thrown? python/AttributeError (intern 'ns-non-existant 'xyz)))))

;;;;;;;;;;;;;;;;;
;; Hierarchies ;;
;;;;;;;;;;;;;;;;;
Expand Down

0 comments on commit 63ba73f

Please sign in to comment.