Skip to content

Commit 220065d

Browse files
authored
Added rudimentary support for basilisp.stacktrace/print-cause-trace (#730)
Hi, could you please review patch to introduce a new namespace `basilisp.stacktrace` and a `print-cause-trace`. It partly addresses #721. This is a requirement of the upcoming nbb nrepl-server port implemented in #723. CIDER specifically requests this function when an exception is thrown to display to the user, making it essential. thanks Co-authored-by: ikappaki <[email protected]>
1 parent 14689d6 commit 220065d

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
### Added
9+
* Added rudimentary support for `clojure.stacktrace` with `print-cause-trace` (part of #721).
10+
11+
### Fixed
812
* Fix issue with `case` evaluating all of its clauses expressions (#699).
913
* Fix issue with relative paths dropping their first character on MS-Windows (#703).
1014
* Fix incompatibility with `(str nil)` returning "nil" (#706).

src/basilisp/stacktrace.lpy

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
(ns basilisp.stacktrace
2+
"Prints stacktraces."
3+
(:require [basilisp.string :as str])
4+
(:import [traceback :as tb]))
5+
6+
(defn print-cause-trace
7+
"Prints the stacktrace of chained ``exc`` (cause), using ``n`` stack
8+
frames (defaults to all)."
9+
([exc]
10+
(print-cause-trace exc nil))
11+
([exc n]
12+
(print (str/join " " (tb/format_exception (python/type exc) exc (.-__traceback__ exc)
13+
** :limit n :chain true)))))

tests/basilisp/stacktrace_test.lpy

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
(ns tests.basilisp.stacktrace-test
2+
(:require [basilisp.stacktrace :as s]
3+
[basilisp.string :as str]
4+
[basilisp.test :refer [deftest are is testing]]))
5+
6+
7+
(defn- exception-test []
8+
(/ 5 0))
9+
10+
(deftest stacktrace-basic
11+
(try
12+
(exception-test)
13+
(catch python/Exception e
14+
;; one stack frame
15+
(let [trace1 (-> (with-out-str (s/print-cause-trace e 1))
16+
(str/split-lines))]
17+
(is (= 4 (count trace1)) trace1)
18+
(is (= "Traceback (most recent call last):" (first trace1)))
19+
(is (= [" (try" " ZeroDivisionError: Fraction(5, 0)" ] (take-last 2 trace1))))
20+
21+
;; full stack
22+
(let [trace (-> (with-out-str (s/print-cause-trace e))
23+
(str/split-lines))]
24+
(is (< 4 (count trace)) trace)
25+
(is (= "Traceback (most recent call last):" (first trace)))
26+
(is (= [" raise ZeroDivisionError('Fraction(%s, 0)' % numerator)"
27+
" ZeroDivisionError: Fraction(5, 0)" ] (take-last 2 trace)))))))

0 commit comments

Comments
 (0)