DEFSTRUCT's :PRINT-OBJECT and (nested) printer errors

Bug #310101 reported by Tobias C. Rittweiler
2
Affects Status Importance Assigned to Milestone
SBCL
Confirmed
Low
Unassigned

Bug Description

Specifying an explicit print-function for a structure results in cryptic
errors if that print function signals errors.

Minimal test case:

  (defun print-bar (obj stream)
    (error "PRINT-BAR: ~S~%" obj))

  (defstruct (bar (:print-object print-bar))
    a b c)

then type (MAKE-BAR) at the REPL. The resulting error message is

  Unable to display error condition

In the actual case I was bitten by this the backtrace didn't look as
good as in this simple case, and pressing `C' in SLDB wasn't useful at
all.

Perhaps the PRINT-OBJECT method that is generated by DEFSTRUCT should
look more like the following

  (defmethod print-object ((obj bar) stream)
    (if (boundp '%error-occured-during-printing%)
        (print-unreadable-object (obj stream :type t :identity t)
          (format stream "[PRINT ERROR]"))
        (handler-case (funcall #'PRINT-BAR obj stream)
          (error (e)
            (let ((%error-occured-during-printing% t))
              (declare (special %error-occured-during-printing%))
              (error e))))))

Tags: printer
Revision history for this message
Nikodemus Siivola (nikodemus) wrote :

We should generally speaking review treatment of errors during PRINT-OBJECT and document some guidelines on how to write robust PRINT-OBJECT methods, and how to debug problems with hairy ones.

Changed in sbcl:
importance: Undecided → Low
status: New → Confirmed
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.