Skip to content

Commit

Permalink
Merge pull request #214 from lokedhs/master
Browse files Browse the repository at this point in the history
Added the useful macro PRINT-UNREADABLE-SAFELY
  • Loading branch information
MiLk committed Apr 10, 2015
2 parents 53f144a + 61dbcc8 commit 43d0dec
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions cl/misc.lisp
@@ -0,0 +1,19 @@
;;;
;;; Useful function that ensures that unbound slots don't cause a
;;; recursive error when trying to print a stack trace.
;;;

(defmacro print-unreadable-safely ((&rest slots) object stream &body body)
"A version of PRINT-UNREADABLE-OBJECT and WITH-SLOTS that is safe to use with unbound slots"
(let ((object-copy (gensym "OBJECT"))
(stream-copy (gensym "STREAM")))
`(let ((,object-copy ,object)
(,stream-copy ,stream))
(symbol-macrolet ,(mapcar #'(lambda (slot-name)
`(,slot-name (if (and (slot-exists-p ,object-copy ',slot-name)
(slot-boundp ,object-copy ',slot-name))
(slot-value ,object-copy ',slot-name)
:not-bound)))
slots)
(print-unreadable-object (,object-copy ,stream-copy :type t :identity nil)
,@body)))))

0 comments on commit 43d0dec

Please sign in to comment.