Skip to content

Commit e03d787

Browse files
committed
include hash in default object print-method. read-able print-method for IDerefs
1 parent 692645c commit e03d787

File tree

1 file changed

+38
-19
lines changed

1 file changed

+38
-19
lines changed

src/clj/clojure/core_print.clj

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494
(print-args o w)
9595
(.write w ")"))
9696

97-
(defn- print-object [o, ^Writer w]
97+
(defn- print-tagged-object [o rep ^Writer w]
9898
(when (instance? clojure.lang.IMeta o)
9999
(print-meta o w))
100100
(.write w "#object[")
@@ -103,9 +103,13 @@
103103
(print-method (.getName c) w)
104104
(.write w (.getName c))))
105105
(.write w " ")
106-
(print-method (str o) w)
106+
(.write w (format "0x%x " (System/identityHashCode o)))
107+
(print-method rep w)
107108
(.write w "]"))
108109

110+
(defn- print-object [o, ^Writer w]
111+
(print-tagged-object o (str o) w))
112+
109113
(defmethod print-method Object [o, ^Writer w]
110114
(print-object o w))
111115

@@ -380,36 +384,51 @@
380384
(print-dup (.name n) w)
381385
(.write w ")"))
382386

387+
(defn- deref-as-map [^clojure.lang.IDeref o]
388+
(let [pending (and (instance? clojure.lang.IPending o)
389+
(not (.isRealized ^clojure.lang.IPending o)))
390+
[ex val]
391+
(when-not pending
392+
(try [false (deref o)]
393+
(catch Throwable e
394+
[true e])))]
395+
{:status
396+
(cond
397+
(or ex
398+
(and (instance? clojure.lang.Agent o)
399+
(agent-error o)))
400+
:failed
401+
402+
pending
403+
:pending
404+
405+
:else
406+
:ready)
407+
408+
:val val}))
409+
383410
(defmethod print-method clojure.lang.IDeref [o ^Writer w]
384-
(print-sequential (format "#<%s@%x%s: "
385-
(.getSimpleName (class o))
386-
(System/identityHashCode o)
387-
(if (and (instance? clojure.lang.Agent o)
388-
(agent-error o))
389-
" FAILED"
390-
""))
391-
pr-on, "", ">", (list (if (and (instance? clojure.lang.IPending o)
392-
(not (.isRealized ^clojure.lang.IPending o)))
393-
:pending
394-
@o)), w))
411+
(print-tagged-object o (deref-as-map o) w))
395412

396413
(defmethod print-method StackTraceElement [^StackTraceElement o ^Writer w]
397414
(print-method [(symbol (.getClassName o)) (symbol (.getMethodName o)) (.getFileName o) (.getLineNumber o)] w))
398415

399-
(defn print-throwable [^Throwable o ^Writer w]
400-
(.write w "#error")
416+
(defn- throwable-as-map [^Throwable o]
401417
(let [base (fn [^Throwable t]
402418
{:type (class t)
403419
:message (.getLocalizedMessage t)
404420
:at (get (.getStackTrace t) 0)})
405421
via (loop [via [], ^Throwable t o]
406422
(if t
407423
(recur (conj via t) (.getCause t))
408-
via))
409-
x {:cause (.getLocalizedMessage ^Throwable (last via))
424+
via))]
425+
{:cause (.getLocalizedMessage ^Throwable (last via))
410426
:via (vec (map base via))
411-
:trace (vec (.getStackTrace (or ^Throwable (last via) o)))}]
412-
(print-method x w)))
427+
:trace (vec (.getStackTrace (or ^Throwable (last via) o)))}))
428+
429+
(defn- print-throwable [^Throwable o ^Writer w]
430+
(.write w "#error")
431+
(print-method (throwable-as-map o) w))
413432

414433
(defmethod print-method Throwable [^Throwable o ^Writer w]
415434
(print-throwable o w))

0 commit comments

Comments
 (0)