unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#70988: (read FUNCTION) uses Latin-1 [PATCH]
@ 2024-05-16 18:13 Mattias Engdegård
  2024-05-16 18:47 ` Eli Zaretskii
  0 siblings, 1 reply; 8+ messages in thread
From: Mattias Engdegård @ 2024-05-16 18:13 UTC (permalink / raw)
  To: 70988; +Cc: Stefan Monnier

[-- Attachment #1: Type: text/plain, Size: 606 bytes --]

When `read` is called with a function as stream argument, the return values of that function are often interpreted as Latin-1 characters with only the 8 low bits used. Example:

(let* ((next '(?A #x12a nil))
       (f (lambda (&rest args)
            (if args
                (push (car args) next)
              (pop next)))))
  (read f))
=> A*   ; expected: AĪ

This is a result of `readchar` setting *multibyte to 0 on this code path.

The reader is not very consistent: inside string and character literals, the code seems to work as expected.

The fix is straightforward (attached).


[-- Attachment #2: read-from-function.diff --]
[-- Type: application/octet-stream, Size: 1404 bytes --]

diff --git a/src/lread.c b/src/lread.c
index c92b2ede932..2626272c4e2 100644
--- a/src/lread.c
+++ b/src/lread.c
@@ -422,6 +422,8 @@ readchar (Lisp_Object readcharfun, bool *multibyte)
       goto read_multibyte;
     }
 
+  if (multibyte)
+    *multibyte = 1;
   tem = call0 (readcharfun);
 
   if (NILP (tem))
diff --git a/test/src/lread-tests.el b/test/src/lread-tests.el
index cc17f7eb3fa..41c9256a9bf 100644
--- a/test/src/lread-tests.el
+++ b/test/src/lread-tests.el
@@ -387,4 +387,19 @@ lread-skip-to-eof
     (goto-char (point-min))
     (should-error (read (current-buffer)) :type 'end-of-file)))
 
+(ert-deftest lread-from-function ()
+  ;; Test reading from a stream defined by a function.
+  (let ((make-reader (lambda (chars)
+                       (lambda (&rest args)
+                         (if args
+                             (push (car args) chars)
+                           (pop chars))))))
+    (dolist (seq '((?A ?B) (?E ?ä ?ÿ) (?A ?Ω) (?* ?☃) (?a #o303 #o245 ?b)))
+      (let ((str (apply #'string seq)))
+        (should (eq (read (funcall make-reader seq)) (intern str)))
+        (let ((quoted-seq `(?\" ,@seq ?\")))
+          (should (equal (read (funcall make-reader quoted-seq)) str)))))
+    (dolist (c '(?A ?ä ?ÿ ?Ω ?☃))
+      (should (eq (read (funcall make-reader `(?? ,c))) c)))))
+
 ;;; lread-tests.el ends here

^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2024-05-30 15:43 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-05-16 18:13 bug#70988: (read FUNCTION) uses Latin-1 [PATCH] Mattias Engdegård
2024-05-16 18:47 ` Eli Zaretskii
2024-05-16 19:45   ` Mattias Engdegård
2024-05-16 19:54     ` Eli Zaretskii
2024-05-17  8:08       ` Mattias Engdegård
2024-05-17 10:45         ` Eli Zaretskii
2024-05-17 17:08           ` Mattias Engdegård
2024-05-30 15:43             ` Mattias Engdegård

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).