Skip to content

Commit

Permalink
Fix problem with tracebacks
Browse files Browse the repository at this point in the history
  • Loading branch information
pdenno committed Dec 6, 2016
1 parent d5565ce commit d88b7cc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
46 changes: 21 additions & 25 deletions src/clojupyter/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
[clj-time.core :as time]
[clj-time.format :as time-format]
[pandect.algo.sha256 :refer [sha256-hmac]]
[clojure.tools.nrepl :as repl]
[clojure.tools.nrepl.server :as nrepl-server])
[clojure.tools.nrepl :as repl :refer [connect client message combine-responses]]
[clojure.tools.nrepl.server :as nrepl-server :refer [default-handler start-server stop-server]])
(:import [org.zeromq ZMQ]
[java.net ServerSocket])
(:gen-class :main true))
Expand Down Expand Up @@ -259,22 +259,21 @@
(defn nrepl-eval
"Send message to nrepl and process response."
[code transport]
(let [result (-> (repl/client transport 3000) ; timeout=3sec.
(repl/message {:op :eval :session @nrepl-session :code code})
repl/combine-responses)]
(cond
(empty? result) "Clojure: Unbalanced parentheses or kernel timed-out while processing form.",
(seq (:ex result)) (stacktrace-string (request-trace transport))
:else result)))
; (if-let [vals (:value result)]
; (apply str (interpose " " vals)) ; could have sent multiple forms
; "Unexpected response from Clojure."))))

(defn reformat-values [result]
(binding [*out* (new java.io.StringWriter)]
(let [result (-> (repl/client transport 3000) ; timeout=3sec.
(repl/message {:op :eval :session @nrepl-session :code code})
repl/combine-responses)]
(cond
(empty? result) {:stream-string "Clojure: Unbalanced parentheses or kernel timed-out while processing form."},
(seq (:ex result)) {:nrepl-result result :stream-string (stacktrace-string (request-trace transport))}
:else {:nrepl-result result :stream-string (str *out*)}))))

(defn reformat-values
"Interpose spaces when multi-forms were sent."
[result]
(if-let [vals (:value result)]
(apply str (interpose " " vals)) ; could have sent multiple forms
))

(apply str (interpose " " vals))
"; no value returned"))

(defn execute-request-handler [shell-socket iopub-socket transport]
(let [execution-count (atom 0N)
Expand All @@ -284,14 +283,11 @@
parent-header (:header message)]
(swap! execution-count inc)
(busy-handler message signer execution-count)
(let [s# (new java.io.StringWriter)
[output result error]
(binding [*out* s#]
(let [fullresult (nrepl-eval (get-in message [:content :code]) transport)
result (reformat-values fullresult)
output (:out fullresult)
error (:err fullresult)]
[output result error]))]
(let [nrepl-map (nrepl-eval (get-in message [:content :code]) transport)
fullresult (:nrepl-result nrepl-map)
result (reformat-values fullresult)
output (:stream-string nrepl-map)
error (:err fullresult)]
(send-router-message shell-socket "execute_reply"
{:status "ok"
:execution_count @execution-count
Expand Down
12 changes: 11 additions & 1 deletion test/clojupyter/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,21 @@
[clojure.tools.nrepl.server :refer [stop-server]])
(:import (clojure.lang Compiler$CompilerException)))

;;; I typically follow these steps while developing code here:
;;; (1) In a shell: jupyter-console --existing=$HOME/Documents/clojure/clojupyter/resources/connect.json --debug"
;;; (2) In my clojure environment (which is emacs/cider): (test-start)
;;; (3) In the jupyter console: <Run some clojure, testing whatever I'm testing.>
;;; (4) In the jupyter console: (clojupyter.core-test/test-disconnect)
;;; (5) Modify code and restart one or both sides. That may entail starting over at Step 1
;;; (if console crashed) or Step 2 (otherwise).
;;;
;;; If someone knows a better way, please describe your process on the project's github repository.

(defn test-start
"Start (e.g. from a CIDER REPL) either an NREPL (:nrepl-only? true) or a full
kernel (no args). The latter uses information in resources/connect.json and, e.g.,
jupyter-console --existing=$HOME/clojupyter/resources/connect.json --debug"
[& {:keys [repl-only? nrepl-only? ]}]
[& {:keys [repl-only?]}]
(if repl-only?
(start-nrepl)
(-main (-> "connect.json" io/resource io/file))))
Expand Down

0 comments on commit d88b7cc

Please sign in to comment.