From 0d10c13bcd9af1df4240703c78f9a875e2b064d6 Mon Sep 17 00:00:00 2001 From: Hugo Duncan Date: Thu, 1 Mar 2012 20:25:01 -0500 Subject: [PATCH] Remove namespace if load-lib fails and namespace didn't previously exist --- src/clj/clojure/core.clj | 10 ++++++++-- test/clojure/test_clojure/load.clj | 6 ++++++ test/clojure/test_clojure/load/invalid.clj | 11 +++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 test/clojure/test_clojure/load/invalid.clj diff --git a/src/clj/clojure/core.clj b/src/clj/clojure/core.clj index e1aa2df..e3fde86 100644 --- a/src/clj/clojure/core.clj +++ b/src/clj/clojure/core.clj @@ -5258,10 +5258,16 @@ (or reload (not require) (not loaded)) load-one) need-ns (or as use) - filter-opts (select-keys opts '(:exclude :only :rename :refer))] + filter-opts (select-keys opts '(:exclude :only :rename :refer)) + undefined-on-entry (not (find-ns lib))] (binding [*loading-verbosely* (or *loading-verbosely* verbose)] (if load - (load lib need-ns require) + (try + (load lib need-ns require) + (catch Exception e + (when undefined-on-entry + (remove-ns lib)) + (throw e))) (throw-if (and need-ns (not (find-ns lib))) "namespace '%s' not found" lib)) (when (and need-ns *loading-verbosely*) diff --git a/test/clojure/test_clojure/load.clj b/test/clojure/test_clojure/load.clj index e2125a2..bb9d514 100644 --- a/test/clojure/test_clojure/load.clj +++ b/test/clojure/test_clojure/load.clj @@ -20,6 +20,12 @@ (is (thrown-with-msg? Exception #".*Cyclic load dependency.*" (require 'clojure.test-clojure.load.cyclic3)))))) +(deftest test-load-lib + (testing "Shouldn't leak failed namespace" + (try (require 'clojure.test-clojure.load.invalid) + (catch Exception _)) + (is (nil? (find-ns 'clojure.test-clojure.load.invalid))))) + (deftest test-require-refer (try (binding [*ns* *ns*] diff --git a/test/clojure/test_clojure/load/invalid.clj b/test/clojure/test_clojure/load/invalid.clj new file mode 100644 index 0000000..fff9901 --- /dev/null +++ b/test/clojure/test_clojure/load/invalid.clj @@ -0,0 +1,11 @@ +; Copyright (c) Rich Hickey. All rights reserved. +; The use and distribution terms for this software are covered by the +; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) +; which can be found in the file epl-v10.html at the root of this distribution. +; By using this software in any fashion, you are agreeing to be bound by +; the terms of this license. +; You must not remove this notice, or any other, from this software. + +(ns clojure.test-clojure.load.invalid) + +(this isn't valid code) -- 1.7.7.3