From a09619f2598a1658feac6794e85bc61a07c4855f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Mattias=20Engdeg=C3=A5rd?= Date: Thu, 2 Nov 2023 11:10:24 +0100 Subject: [PATCH] * src/print.c (print_object): Don't print empty hash-table data Since no data is the default, this preserves bidirectional compatibility. --- src/print.c | 37 +++++++++++++++++++++++-------------- test/src/print-tests.el | 7 ------- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/print.c b/src/print.c index 0a5f2ee48d4..d011962d85b 100644 --- a/src/print.c +++ b/src/print.c @@ -2603,21 +2603,30 @@ print_object (Lisp_Object obj, Lisp_Object printcharfun, bool escapeflag) if (h->purecopy) print_c_string (" purecopy t", printcharfun); - print_c_string (" data (", printcharfun); - ptrdiff_t size = h->count; - /* Don't print more elements than the specified maximum. */ - if (FIXNATP (Vprint_length) && XFIXNAT (Vprint_length) < size) - size = XFIXNAT (Vprint_length); - - print_stack_push ((struct print_stack_entry){ - .type = PE_hash, - .u.hash.obj = obj, - .u.hash.nobjs = size * 2, - .u.hash.idx = 0, - .u.hash.printed = 0, - .u.hash.truncated = (size < h->count), - }); + if (size > 0) + { + print_c_string (" data (", printcharfun); + + /* Don't print more elements than the specified maximum. */ + if (FIXNATP (Vprint_length) && XFIXNAT (Vprint_length) < size) + size = XFIXNAT (Vprint_length); + + print_stack_push ((struct print_stack_entry){ + .type = PE_hash, + .u.hash.obj = obj, + .u.hash.nobjs = size * 2, + .u.hash.idx = 0, + .u.hash.printed = 0, + .u.hash.truncated = (size < h->count), + }); + } + else + { + /* Empty table: we can omit the data entirely. */ + printchar (')', printcharfun); + --print_depth; /* Done with this. */ + } goto next_obj; } diff --git a/test/src/print-tests.el b/test/src/print-tests.el index aedaa9a4e06..ff3a6fe7483 100644 --- a/test/src/print-tests.el +++ b/test/src/print-tests.el @@ -367,13 +367,6 @@ otherwise, use a different charset." (remhash 1 h) (format "%S" h)))) - (should - (string-match - "data ()" - (let ((h (make-hash-table))) - (let ((print-length 0)) - (format "%S" h))))) - (should (string-match "data (99 99)" -- 2.39.2