-
Notifications
You must be signed in to change notification settings - Fork 598
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #214 from lokedhs/master
Added the useful macro PRINT-UNREADABLE-SAFELY
- Loading branch information
Showing
1 changed file
with
19 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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))))) |