From: Stefan Monnier <monnier@IRO.UMontreal.CA>
To: npostavs@users.sourceforge.net
Cc: Eric Abrahamsen <eric@ericabrahamsen.net>, 25295@debbugs.gnu.org
Subject: bug#25295: Represent eieio objects using object-print in backtraces and edebug
Date: Tue, 21 Feb 2017 12:23:12 -0500 [thread overview]
Message-ID: <jwvk28jmpl5.fsf-monnier+emacsbugs@gnu.org> (raw)
In-Reply-To: <878tp0i74g.fsf@users.sourceforge.net> (npostavs's message of "Mon, 20 Feb 2017 21:56:47 -0500")
> Can we allow overriding printing of primitive types too?
> I'm wanting that for e.g., printing byte code functions in nicer ways.
Maybe we should just switch to an Elisp version of printing, in that case.
We could keep the C code for the "print-readably" case only.
The main question is whether it's fast enough.
Stefan
;;; cl-print.el --- Generic printer facilies -*- lexical-binding: t; -*-
;; Copyright (C) 2017 Stefan Monnier
;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
;; Keywords:
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;; Code:
;;;###autoload
(cl-defgeneric cl-print-object (object stream)
"Dispatcher to print OBJECT on STREAM according to its type."
(prin1 object stream))
(cl-defmethod cl-print-object ((object cons) stream)
(let ((car (pop object)))
(if (and (memq car '(\, quote \` \,@ \,.))
(consp object)
(null (cdr object)))
(progn
(princ (if (eq car 'quote) '\' car) stream)
(cl-print-object (car object) stream))
(princ "(" stream)
(cl-print-object car stream)
(while (consp object)
(princ " " stream)
(cl-print-object (pop object) stream))
(when object
(princ " . ") (cl-print-object object stream))
(princ ")"))))
(cl-defmethod cl-print-object ((object vector) stream)
(princ "[" stream)
(dotimes (i (length object))
(unless (zerop i) (princ " " stream))
(cl-print-object (aref object i) stream))
(princ "]" stream))
(cl-defmethod cl-print-object ((object compiled-function) stream)
;; FIXME: Give a prettier representation.
(princ "#<compiled-function>" stream))
(cl-defmethod cl-print-object ((object cl-structure-object) stream)
(princ "#s(")
(let* ((class (symbol-value (aref object 0)))
(slots (cl--struct-class-slots class)))
(princ (cl--struct-class-name class) stream)
(dotimes (i (length slots))
(let ((slot (aref slots i)))
(princ " :" stream)
(princ (cl--slot-descriptor-name slot) stream)
(princ " " stream)
(cl-print-object (aref object (1+ i)) stream))))
(princ ")"))
;;; Circularity and sharing.
;; I don't try to support the `print-continuous-numbering', because
;; I think it's ill defined anyway: if an object appears only once in each call
;; its sharing can't be properly preserved!
(defvar cl-print--number-index nil)
(defvar cl-print--number-table nil)
(cl-defmethod cl-print-object :around (object stream)
;; FIXME: Only put such an :around method on types where it's relevant.
(let ((n (if cl-print--number-table (gethash object cl-print--number-table))))
(if (not (numberp n))
(cl-call-next-method)
(if (> n 0)
;; Already printed. Just print a reference.
(progn (princ "#" stream) (princ n stream) (princ "#" stream))
(puthash object (- n) cl-print--number-table)
(princ "#" stream) (princ (- n) stream) (princ "=" stream)
(cl-call-next-method)))))
(defun cl-print--find-sharing (object table)
(unless
;; Skip objects which don't have identity!
(or (floatp object) (numberp object))
(let ((n (gethash object table)))
(cond
((numberp n)) ;All done.
(n ;Already seen, but only once.
(let ((n (1+ cl-print--number-index)))
(setq cl-print--number-index n)
(puthash object (- n) table)))
(t
(puthash object t table)
(pcase object
(`(,car . ,cdr)
(cl-print--find-sharing car table)
(cl-print--find-sharing cdr table))
((pred stringp)
;; We presumably won't print its text-properties.
nil)
((pred arrayp) ;FIXME: Inefficient for char-tables!
(dotimes (i (length object))
(cl-print--find-sharing (aref object i) table)))))))))
;;;###autoload
(defun cl-prin1 (object &optional stream)
(if (not print-circle)
(cl-print-object object stream)
(let ((cl-print--number-table (make-hash-table :test 'eq))
(cl-print--number-index 0))
(cl-print--find-sharing object cl-print--number-table)
(cl-print-object object stream))))
(provide 'cl-print)
;;; cl-print.el ends here
next prev parent reply other threads:[~2017-02-21 17:23 UTC|newest]
Thread overview: 74+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-12-29 20:52 bug#25295: 26.0.50; Represent eieio objects using object-print in backtraces and edebug Eric Abrahamsen
[not found] ` <handler.25295.B.148304476023950.ack@debbugs.gnu.org>
2016-12-29 21:50 ` bug#25295: Acknowledgement (26.0.50; Represent eieio objects using object-print in backtraces and edebug) Eric Abrahamsen
2016-12-30 3:42 ` npostavs
2016-12-30 19:14 ` Eric Abrahamsen
2016-12-31 5:48 ` npostavs
2016-12-31 18:56 ` Eric Abrahamsen
2016-12-31 19:23 ` npostavs
2016-12-31 20:52 ` Eric Abrahamsen
2016-12-30 7:51 ` Eli Zaretskii
2017-01-03 18:21 ` bug#25295: Represent eieio objects using object-print in backtraces and edebug Stefan Monnier
2017-01-04 23:40 ` Eric Abrahamsen
2017-01-05 1:51 ` Stefan Monnier
2017-01-05 2:11 ` Stefan Monnier
2017-01-05 4:37 ` Stefan Monnier
2017-01-05 15:39 ` Eli Zaretskii
2017-01-06 0:42 ` Stefan Monnier
2017-01-06 7:50 ` Eli Zaretskii
2017-02-21 2:56 ` npostavs
2017-02-21 17:23 ` Stefan Monnier [this message]
2017-02-22 16:15 ` Richard Stallman
2017-02-22 19:08 ` Stefan Monnier
2017-03-02 5:36 ` Elisp printer (was: bug#25295: Represent eieio objects using object-print in backtraces and edebug) Michael Heerdegen
2017-03-02 6:38 ` Elisp printer Stefan Monnier
2017-03-03 2:14 ` Michael Heerdegen
2017-03-03 2:38 ` Stefan Monnier
2017-03-03 4:23 ` Michael Heerdegen
2017-03-06 2:50 ` Stefan Monnier
2017-03-08 4:09 ` Tom Tromey
2017-03-08 4:39 ` Stefan Monnier
2017-03-08 6:35 ` Tom Tromey
2017-03-08 9:43 ` Stefan Monnier
2017-03-08 18:17 ` Lars Brinkhoff
2017-03-08 23:02 ` Stefan Monnier
2017-03-09 15:12 ` Lars Brinkhoff
2017-03-14 9:52 ` User-defined record types Lars Brinkhoff
2017-03-14 12:28 ` Lars Brinkhoff
[not found] ` <86bmt42nk2.fsf_-_@molnjunk.nocrew.org>
[not found] ` <jwvzigoow0k.fsf-monnier+emacs@gnu.org>
2017-03-14 13:25 ` Lars Brinkhoff
2017-03-14 14:28 ` Lars Brinkhoff
2017-03-14 15:20 ` Stefan Monnier
2017-03-14 17:23 ` Lars Brinkhoff
2017-03-15 14:38 ` Stefan Monnier
2017-03-15 18:14 ` Lars Brinkhoff
2017-03-15 19:12 ` Stefan Monnier
2017-03-15 19:21 ` Lars Brinkhoff
2017-03-15 20:05 ` Stefan Monnier
2017-03-15 21:49 ` Lars Brinkhoff
2017-03-15 23:42 ` Stefan Monnier
2017-03-16 3:05 ` Stefan Monnier
2017-03-16 3:08 ` Stefan Monnier
2017-03-16 20:03 ` Lars Brinkhoff
2017-03-16 21:32 ` Stefan Monnier
2017-03-17 11:22 ` Lars Brinkhoff
2017-03-17 20:45 ` Lars Brinkhoff
2017-03-18 23:24 ` Stefan Monnier
2017-03-18 23:36 ` Stefan Monnier
2017-03-19 9:34 ` Lars Brinkhoff
2017-03-19 12:42 ` Stefan Monnier
2017-03-02 7:00 ` bug#25295: 26.0.50; Represent eieio objects using object-print in backtraces and edebug Stefan Monnier
2017-03-02 12:52 ` npostavs
2017-03-02 13:38 ` Stefan Monnier
2017-03-11 5:43 ` npostavs
2017-03-11 15:38 ` New pp (was: bug#25295: 26.0.50; Represent eieio objects using object-print in backtraces and edebug) Stefan Monnier
2017-03-11 16:05 ` Noam Postavsky
2017-03-11 16:40 ` New pp Stefan Monnier
2017-03-11 16:52 ` Noam Postavsky
2017-03-11 16:57 ` Stefan Monnier
2017-03-12 13:31 ` Noam Postavsky
2017-03-12 14:06 ` Stefan Monnier
2017-03-12 16:13 ` Noam Postavsky
2017-03-02 17:35 ` bug#25295: 26.0.50; Represent eieio objects using object-print in backtraces and edebug Eric Abrahamsen
2017-03-02 18:31 ` Stefan Monnier
2017-10-21 21:36 ` Eric Abrahamsen
2017-10-22 3:21 ` Stefan Monnier
2018-10-18 19:01 ` Eric Abrahamsen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=jwvk28jmpl5.fsf-monnier+emacsbugs@gnu.org \
--to=monnier@iro.umontreal.ca \
--cc=25295@debbugs.gnu.org \
--cc=eric@ericabrahamsen.net \
--cc=npostavs@users.sourceforge.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.