Skip to content

Commit

Permalink
Added rudimentary support for basilisp.stacktrace/print-cause-trace (#…
Browse files Browse the repository at this point in the history
…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]>
  • Loading branch information
ikappaki and ikappaki authored Dec 15, 2023
1 parent 14689d6 commit 220065d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
* Added rudimentary support for `clojure.stacktrace` with `print-cause-trace` (part of #721).

### Fixed
* Fix issue with `case` evaluating all of its clauses expressions (#699).
* Fix issue with relative paths dropping their first character on MS-Windows (#703).
* Fix incompatibility with `(str nil)` returning "nil" (#706).
Expand Down
13 changes: 13 additions & 0 deletions src/basilisp/stacktrace.lpy
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
(ns basilisp.stacktrace
"Prints stacktraces."
(:require [basilisp.string :as str])
(:import [traceback :as tb]))

(defn print-cause-trace
"Prints the stacktrace of chained ``exc`` (cause), using ``n`` stack
frames (defaults to all)."
([exc]
(print-cause-trace exc nil))
([exc n]
(print (str/join " " (tb/format_exception (python/type exc) exc (.-__traceback__ exc)
** :limit n :chain true)))))
27 changes: 27 additions & 0 deletions tests/basilisp/stacktrace_test.lpy
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
(ns tests.basilisp.stacktrace-test
(:require [basilisp.stacktrace :as s]
[basilisp.string :as str]
[basilisp.test :refer [deftest are is testing]]))


(defn- exception-test []
(/ 5 0))

(deftest stacktrace-basic
(try
(exception-test)
(catch python/Exception e
;; one stack frame
(let [trace1 (-> (with-out-str (s/print-cause-trace e 1))
(str/split-lines))]
(is (= 4 (count trace1)) trace1)
(is (= "Traceback (most recent call last):" (first trace1)))
(is (= [" (try" " ZeroDivisionError: Fraction(5, 0)" ] (take-last 2 trace1))))

;; full stack
(let [trace (-> (with-out-str (s/print-cause-trace e))
(str/split-lines))]
(is (< 4 (count trace)) trace)
(is (= "Traceback (most recent call last):" (first trace)))
(is (= [" raise ZeroDivisionError('Fraction(%s, 0)' % numerator)"
" ZeroDivisionError: Fraction(5, 0)" ] (take-last 2 trace)))))))

0 comments on commit 220065d

Please sign in to comment.