From 95392658bf93a420e8b7413cd997f2f32901cda0 Mon Sep 17 00:00:00 2001 From: Jon Ruttan Date: Fri, 27 Nov 2026 10:48:30 -0500 Subject: [PATCH 1/2] feat: open() reads and writes utf-16, utf-32 and utf-8-sig files A text file is decoded a piece at a time, and the new codecs need the stream to carry what a piece ends inside: utf-16's odd byte or the high half of a surrogate pair, utf-32's part of a unit, utf-8's sequence. Each codec now holds those bytes for the next read. The BOM codecs settle what the file is in at its start, as CPython's file decoders do: - utf-16 and utf-32 take the byte order their BOM names and drop it, and refuse a stream with none ("Stream does not start with BOM"). - utf-8-sig drops a BOM when there is one; a file that is only the start of one reads as nothing. - A seek back to the first byte looks for the BOM again. A write puts the BOM out only at the file's first byte, so a file written in several writes, or appended to, has one; seek(0) and a write puts it out again, over the old one. Error positions in a file count from where reading began, which is where CPython's read() reports them. bytes.decode is unchanged: ps-decode-from is ps-decode-as with that offset. D14-file-codecs covers writing, reading and a character split between two reads, with output recorded from CPython 3.14. Its long files are data files under tests/modules/data, since a long str built in the program is costly here on main as well. Co-Authored-By: Claude Opus 5.5 --- lib/python/core.x | 3 +- lib/python/io.x | 149 ++++++++++++++++++++++++---- lib/python/str.x | 118 +++++++++++++++------- tests/modules/data/long-utf16.txt | Bin 0 -> 8226 bytes tests/modules/data/long-utf16be.txt | Bin 0 -> 8226 bytes tests/modules/data/long-utf8sig.txt | 6 ++ tests/specs/D14-file-codecs.spec.md | 108 ++++++++++++++++++++ 7 files changed, 330 insertions(+), 54 deletions(-) create mode 100644 tests/modules/data/long-utf16.txt create mode 100644 tests/modules/data/long-utf16be.txt create mode 100644 tests/modules/data/long-utf8sig.txt create mode 100644 tests/specs/D14-file-codecs.spec.md diff --git a/lib/python/core.x b/lib/python/core.x index 294d0c9..fd80eb3 100644 --- a/lib/python/core.x +++ b/lib/python/core.x @@ -74,7 +74,8 @@ python-exc-info python-exception-now python-f-2p52 make-instance make-type pb->str pb-len pb-of-str ps->x ps-codec - ps-decode-as ps-enc1 ps-encode ps-encode-as ps-stream-codec ps-write-bytes-to python-add + ps-decode-as ps-decode-from ps-enc1 ps-encode ps-encode-as ps-encode-rest ps-bom-codec? ps-bom-len ps-stream-plain + ps-stream-bom ps-bom8-start? ps-write-bytes-to python-add python-alist-find python-barr-is python-barr-set! python-boolnorm python-builtins python-byte-len python-bytes-hex python-bytes-is python-bytes-list python-bytes-new python-bytes-of-codes python-bytes-only? python-bytes-str python-char-code python-cim python-class-methods-set! diff --git a/lib/python/io.x b/lib/python/io.x index e94f9a0..6582fcc 100644 --- a/lib/python/io.x +++ b/lib/python/io.x @@ -24,7 +24,8 @@ (module python/io) (import python/modules PyModule) (import python/core - make-instance make-type ps-codec ps-decode-as ps-encode ps-encode-as ps-stream-codec + make-instance make-type ps-bom-codec? ps-bom-len ps-bom8-start? ps-codec ps-decode-as ps-decode-from ps-encode + ps-encode-as ps-encode-rest ps-stream-bom ps-stream-plain ps-write-bytes-to python-alist-find python-barr-is python-barr-set! python-boolnorm python-bytes-is python-bytes-list python-bytes-new python-class-name python-class-new python-class-qualname python-cls-object python-drop python-exc-FileExistsError @@ -51,7 +52,7 @@ ; field holds what it was opened as, (NAME MODE ENCODING), and a sixth what ; reading it keeps: ; -; 0 the descriptor 5 (ENCODING ERRORS NEWLINE), for text +; 0 the descriptor 5 (ENCODING ERRORS NEWLINE CODEC), for text ; 1 bytes not yet decoded 6 the string each read fills ; 2 whether the end came 7 whether it reads ; 3 the position the 8 whether it writes @@ -59,6 +60,12 @@ ; 4 whether a \r waits ; for what follows it ; +; CODEC is a cell, (READS-AS BOM? DECODED): the codec the bytes are decoded +; in, nil while a BOM codec has not yet seen whether its start has a BOM; +; whether a write to a descriptor that cannot say where it is still owes the +; BOM; and how many bytes have been decoded since reading began, which is +; where an error's position counts from. +; ; The buffer holds what was read and not yet taken, so a file costs what is ; asked of it. An in-memory stream has neither field, and its buffer starts ; at 0. @@ -207,20 +214,48 @@ (%seq (if (null? bs) (%py-io-st! v 2 #t) ()) (if (python-io-text? v) (%py-io-decode! v bs (null? bs)) bs)))))) -; A utf-8 sequence a read ends inside waits for the rest of it; at the end of -; the file what waits is decoded as it stands. +; A FILE IS DECODED A PIECE AT A TIME, and a piece can end inside a +; character: the bytes of it that have arrived wait for the rest -- a utf-8 +; sequence's start, an odd byte of utf-16 or the high half of a surrogate +; pair, a part of utf-32's four. At the end of the file what waits is +; decoded as it stands. +; +; A BOM codec settles at the start what the rest is in. utf-16 and utf-32 +; take the byte order their BOM names and drop it, and refuse a stream with +; none, as CPython's file decoders do; utf-8-sig drops a BOM when there is one. +(def %py-io-cell (fn (_ v) (first (rest (rest (rest (%py-io-st v 5))))))) +(def %py-io-cell! (fn (_ v w) (%seq (%set-first! (%py-io-cell v) w) ()))) + (def %py-io-decode! (fn (_ v bs last?) - (let ((d (%py-io-st v 5)) (all (python-list-cat (%py-io-st v 1) bs))) - (let ((n (- (python-length all) - (match - (last? 0) - ((eq? (ps-codec (first d)) (lit ascii)) 0) - (#t (%py-io-tail (python-reverse all) 1)))))) + (let ((all (python-list-cat (%py-io-st v 1) bs)) (w (first (%py-io-cell v)))) + (if (null? w) + (%py-io-decode-start! v all last?) + (%py-io-decode-as! v all (%py-io-held w all last?) w last?))))) + +; All but the last K bytes, decoded in ENC; the K wait. +(def %py-io-decode-as! + (fn (_ v all k enc last?) + (let ((n (- (python-length all) k)) (at (rest (rest (%py-io-cell v))))) + (let ((cps (ps-decode-from (%py-io-take all n ()) enc + (first (rest (%py-io-st v 5))) (first at)))) (%seq (%py-io-st! v 1 (python-drop all n)) - (%py-io-newlines! v - (ps-decode-as (%py-io-take all n ()) (first d) (first (rest d))) - last?)))))) + (%seq (%set-first! at (+ (first at) n)) + (%py-io-newlines! v cps last?))))))) + +; How many bytes at the end wait, in the codec W. +(def %py-io-held + (fn (_ w all last?) + (if last? + 0 + (let ((c (ps-codec w))) + (match + ((eq? c (lit ascii)) 0) + ((eq? c (lit utf-16-le)) (%py-io-tail16 (python-reverse all) #t)) + ((eq? c (lit utf-16-be)) (%py-io-tail16 (python-reverse all) #f)) + ((eq? c (lit utf-32-le)) (% (python-length all) 4)) + ((eq? c (lit utf-32-be)) (% (python-length all) 4)) + (#t (%py-io-tail (python-reverse all) 1))))))) ; How many bytes at the end begin a sequence the rest of has not arrived: R ; is the bytes backwards and K the count so far. @@ -234,6 +269,58 @@ ((< k (match ((< (first r) 224) 2) ((< (first r) 240) 3) (#t 4))) k) (#t 0)))) +; utf-16's: an odd byte, and before it a unit that is a high surrogate, whose +; high byte is D8 to DB. R is the bytes backwards. +(def %py-io-tail16 + (fn (_ r le?) + (let ((odd (% (python-length r) 2))) + (let ((u (python-drop r odd))) + (if (if (null? u) #t (null? (rest u))) + odd + (let ((hi (if le? (first u) (first (rest u))))) + (if (if (< hi 216) #f (< hi 220)) (+ odd 2) odd))))))) + +; The start of a BOM codec's file: its BOM, once enough has arrived to hold +; one, settles the codec. +(def %py-io-decode-start! + (fn (_ v all last?) + (let ((enc (first (%py-io-st v 5)))) + (let ((k (ps-bom-len enc))) + (if (if (< (python-length all) k) (not last?) #f) + (%seq (%py-io-st! v 1 all) ()) + (let ((w (ps-stream-bom enc all))) + (match + ((not (null? w)) + (%seq (%py-io-cell! v w) + (%py-io-decode-as! v all (%py-io-held w all last?) enc last?))) + ; utf-8-sig: a file that is the start of a BOM and no more of it + ; reads as nothing, and one with no BOM is utf-8 + ((= k 3) + (if (if last? (ps-bom8-start? all) #f) + (%seq (%py-io-st! v 1 ()) ()) + (%seq (%py-io-cell! v "utf-8") + (%py-io-decode-as! v all (%py-io-held "utf-8" all last?) "utf-8" last?)))) + (#t (%py-io-no-bom! v all last? enc k))))))))) + +; utf-16 or utf-32 with no BOM: the file is read to its end, what there is +; decodes little-endian, which is where an error in it is reported, and then +; a stream of a whole unit or more is refused -- the order CPython's read() +; finds them in. +(def %py-io-no-bom! + (fn (_ v all last? enc k) + (if (not last?) + (%seq (%py-io-st! v 1 all) ()) + (let ((plain (ps-stream-plain enc))) + (let ((held (%py-io-held plain all last?))) + (let ((out (%py-io-decode-as! v all held plain last?))) + (if (< (- (python-length all) held) k) + out + (Err raise (lit unicode-decode) + (if (= k 2) + "'utf-16' codec can't decode bytes in position 0-1: Stream does not start with BOM" + "'utf-32' codec can't decode bytes in position 0-3: Stream does not start with BOM") + ())))))))) + ; \r\n and a lone \r read as \n when the newline argument is None, the ; default. A \r a read ends on waits for the next, which may begin with its ; \n. @@ -311,11 +398,20 @@ (def %py-io-restart! (fn (_ v p at) (%seq (%py-io-lseek! v p (lit set)) + (%seq (if (python-io-text? v) (%py-io-reread! v p) ()) (%seq (%py-io-buf! v ()) (%seq (%py-io-st! v 1 ()) (%seq (%py-io-st! v 2 #f) (%seq (%py-io-st! v 3 at) - (%py-io-st! v 4 #f)))))))) + (%py-io-st! v 4 #f))))))))) + +; A text file read again from its first byte looks for its BOM again; from +; anywhere else it reads as it would with none. +(def %py-io-reread! + (fn (_ v p) + (let ((enc (first (%py-io-st v 5)))) + (%seq (%py-io-cell! v (if (if (= p 0) (ps-bom-codec? enc) #f) () (ps-stream-plain enc))) + (%set-first! (rest (rest (%py-io-cell v))) 0))))) ; Reads on, keeping nothing, until the buffer reaches the position or the ; file ends. @@ -400,16 +496,32 @@ (Str8 append (python-class-name (python-type-of x)) "'")) ()))))) ; The bytes text goes out as: each \n as the newline argument when that is -; \r or \r\n, and the whole in the file's encoding. +; \r or \r\n, and the whole in the file's encoding -- with its BOM only when +; the write is at the file's first byte. (def %py-io-encoded (fn (_ v cps) (let ((d (%py-io-st v 5))) (let ((nl (first (rest (rest d))))) - (ps-encode-as + (%py-io-encode-for v (if (null? nl) cps (let ((to (python-str-cps nl))) (if (%py-io-member? 13 to) (%py-io-turn cps to ()) cps))) (first d) (first (rest d))))))) +(def %py-io-encode-for + (fn (_ v cps enc errors) + (if (%py-io-bom-now? v) + (ps-encode-as cps enc errors) + (ps-encode-rest cps enc errors)))) + +; A descriptor that cannot say where it is owes the BOM to its first write. +(def %py-io-bom-now? + (fn (_ v) + (if (ps-bom-codec? (first (%py-io-st v 5))) + (let ((p (File seek (%py-io-st v 0) 0 (lit cur))) (c (%py-io-cell v))) + (let ((now (if (< p 0) (first (rest c)) (= p 0)))) + (%seq (%set-first! (rest c) #f) now))) + #f))) + (def %py-io-turn (fn (self cps to acc) (match @@ -826,7 +938,7 @@ (Err raise (lit type) "open() takes a path as a str here" ()))) (def enc (%py-open-text encoding "utf-8")) ; an encoding nothing here knows is refused before the file is opened - (if binary? () (ps-stream-codec enc)) + (if binary? () (ps-codec enc)) (def both? (%py-open-has? mode "+")) (def reads? (if both? #t (%py-open-has? mode "r"))) (def writes? (if both? #t (not (%py-open-has? mode "r")))) @@ -837,7 +949,8 @@ (%py-io-file-new #f (list path (%py-open-bytes-mode mode both?) ()) fd () reads? writes? at) (%py-io-file-new #t (list path mode enc) fd - (list enc (%py-open-text (python-opt a 3 ()) "strict") (python-opt a 4 ())) + (list enc (%py-open-text (python-opt a 3 ()) "strict") (python-opt a 4 ()) + (list (if (if (= at 0) (ps-bom-codec? enc) #f) () (ps-stream-plain enc)) #t 0)) reads? writes? at))) "open" (list "file" "mode" "buffering" "encoding" "errors" "newline" "closefd" "opener") 1 #f)) diff --git a/lib/python/str.x b/lib/python/str.x index 89b8543..1deef70 100644 --- a/lib/python/str.x +++ b/lib/python/str.x @@ -197,17 +197,6 @@ (Err raise (lit lookup) (Str8 append "unknown encoding: " name) ()) c)))) -; A file reads and writes in pieces, and only utf-8 and ascii are codecs a -; piece can be decoded in without the rest: utf-16's code units and its BOM -; would need the stream to carry them across. io asks this one. -(def ps-stream-codec - (fn (_ name) - (let ((c (ps-codec name))) - (if (if (eq? c (lit utf-8)) #t (eq? c (lit ascii))) - c - (Err raise (lit lookup) - (Str8 append "a file can't be read or written as " (Str8 append name " here")) ()))))) - ; --- the steps --------------------------------------------------------------- ; ; A step reads one code point from the front of the bytes and answers it and @@ -387,7 +376,7 @@ ; 'utf-16-le' codec can't decode bytes in position 0-1: illegal encoding (def %ps-dec-raise (fn (_ name all bs r) - (let ((at (- (pb-len all) (pb-len bs))) (k (first (rest (rest r))))) + (let ((at (- all (pb-len bs))) (k (first (rest (rest r))))) (Err raise (lit unicode-decode) (Str8 append "'" (Str8 append name @@ -452,30 +441,33 @@ ; utf-16 and utf-32 read their byte order from a BOM and drop it, and take ; little-endian when there is none; a position in a message still counts the ; BOM's bytes. utf-8-sig drops one utf-8 BOM and is utf-8 after it, so there -; a position counts from past the BOM, as CPython's does. -(def ps-decode-as - (fn (_ bs name errors) - (let ((c (ps-codec name))) +; a position counts from past the BOM, as CPython's does. OFF is added to +; every position: where in a file the bytes begin. +(def ps-decode-from + (fn (_ bs name errors off) + (let ((c (ps-codec name)) (n (+ off (pb-len bs)))) (match - ((eq? c (lit utf-8)) (%ps-dec bs bs %ps-utf8 %ps-pass8 #t "utf-8" errors)) - ((eq? c (lit ascii)) (%ps-dec bs bs %ps-ascii %ps-pass-none #t "ascii" errors)) + ((eq? c (lit utf-8)) (%ps-dec n bs %ps-utf8 %ps-pass8 #t "utf-8" errors)) + ((eq? c (lit ascii)) (%ps-dec n bs %ps-ascii %ps-pass-none #t "ascii" errors)) ((eq? c (lit utf-8-sig)) (let ((t8 (if (%ps-starts? %ps-bom8 bs) (%pb-drop 3 bs) bs))) - (%ps-dec t8 t8 %ps-utf8 %ps-pass8 #t "utf-8" errors))) - ((eq? c (lit utf-16-le)) (%ps-dec16 bs bs #t errors)) - ((eq? c (lit utf-16-be)) (%ps-dec16 bs bs #f errors)) + (%ps-dec (+ off (pb-len t8)) t8 %ps-utf8 %ps-pass8 #t "utf-8" errors))) + ((eq? c (lit utf-16-le)) (%ps-dec16 n bs #t errors)) + ((eq? c (lit utf-16-be)) (%ps-dec16 n bs #f errors)) ((eq? c (lit utf-16)) (match - ((%ps-starts? %ps-bom16le bs) (%ps-dec16 bs (%pb-drop 2 bs) #t errors)) - ((%ps-starts? %ps-bom16be bs) (%ps-dec16 bs (%pb-drop 2 bs) #f errors)) - (#t (%ps-dec16 bs bs #t errors)))) - ((eq? c (lit utf-32-le)) (%ps-dec32 bs bs #t errors)) - ((eq? c (lit utf-32-be)) (%ps-dec32 bs bs #f errors)) + ((%ps-starts? %ps-bom16le bs) (%ps-dec16 n (%pb-drop 2 bs) #t errors)) + ((%ps-starts? %ps-bom16be bs) (%ps-dec16 n (%pb-drop 2 bs) #f errors)) + (#t (%ps-dec16 n bs #t errors)))) + ((eq? c (lit utf-32-le)) (%ps-dec32 n bs #t errors)) + ((eq? c (lit utf-32-be)) (%ps-dec32 n bs #f errors)) (#t (match - ((%ps-starts? %ps-bom32le bs) (%ps-dec32 bs (%pb-drop 4 bs) #t errors)) - ((%ps-starts? %ps-bom32be bs) (%ps-dec32 bs (%pb-drop 4 bs) #f errors)) - (#t (%ps-dec32 bs bs #t errors)))))))) + ((%ps-starts? %ps-bom32le bs) (%ps-dec32 n (%pb-drop 4 bs) #t errors)) + ((%ps-starts? %ps-bom32be bs) (%ps-dec32 n (%pb-drop 4 bs) #f errors)) + (#t (%ps-dec32 n bs #t errors)))))))) + +(def ps-decode-as (fn (_ bs name errors) (ps-decode-from bs name errors 0))) ; --- encoding ---------------------------------------------------------------- ; @@ -604,21 +596,77 @@ (%ps-enc cps %ps-not-surrogate? (%ps-put32 le?) #f #t name "surrogates not allowed" errors))) ; utf-16, utf-32 and utf-8-sig write their BOM first, even before nothing; -; utf-16 and utf-32 are little-endian after it. -(def ps-encode-as - (fn (_ cps name errors) +; utf-16 and utf-32 are little-endian after it. A file writes it only at its +; start, so it asks for the rest alone: BOM? is #f, and the codec's name in a +; message is still the one asked for. +(def %ps-encode-in + (fn (_ cps name errors bom?) (let ((c (ps-codec name))) (match ((eq? c (lit utf-8)) (%ps-enc8 cps errors)) ((eq? c (lit ascii)) (%ps-enc cps %ps-ascii? %ps-put-ascii #t #f "ascii" "ordinal not in range(128)" errors)) - ((eq? c (lit utf-8-sig)) (python-list-cat %ps-bom8 (%ps-enc8 cps errors))) + ((eq? c (lit utf-8-sig)) (%ps-bom-onto bom? %ps-bom8 (%ps-enc8 cps errors))) ((eq? c (lit utf-16-le)) (%ps-enc16 cps #t "utf-16-le" errors)) ((eq? c (lit utf-16-be)) (%ps-enc16 cps #f "utf-16-be" errors)) - ((eq? c (lit utf-16)) (python-list-cat %ps-bom16le (%ps-enc16 cps #t "utf-16" errors))) + ((eq? c (lit utf-16)) + (%ps-bom-onto bom? %ps-bom16le (%ps-enc16 cps #t "utf-16" errors))) ((eq? c (lit utf-32-le)) (%ps-enc32 cps #t "utf-32-le" errors)) ((eq? c (lit utf-32-be)) (%ps-enc32 cps #f "utf-32-be" errors)) - (#t (python-list-cat %ps-bom32le (%ps-enc32 cps #t "utf-32" errors))))))) + (#t (%ps-bom-onto bom? %ps-bom32le (%ps-enc32 cps #t "utf-32" errors))))))) + +(def %ps-bom-onto (fn (_ bom? bom bs) (if bom? (python-list-cat bom bs) bs))) + +(def ps-encode-as (fn (_ cps name errors) (%ps-encode-in cps name errors #t))) +(def ps-encode-rest (fn (_ cps name errors) (%ps-encode-in cps name errors #f))) + +; --- what a stream needs ----------------------------------------------------- +; +; A file is decoded a piece at a time, so it settles once, at its start, what +; a BOM codec reads as -- and every piece after is in that codec. These say +; which codecs have a BOM, what each reads as where there is none, and what a +; BOM at the front of the bytes says. + +(def ps-bom-codec? + (fn (_ name) + (let ((c (ps-codec name))) + (if (eq? c (lit utf-16)) #t (if (eq? c (lit utf-32)) #t (eq? c (lit utf-8-sig))))))) + +; How many bytes a BOM codec's BOM is. +(def ps-bom-len + (fn (_ name) + (let ((c (ps-codec name))) + (if (eq? c (lit utf-16)) 2 (if (eq? c (lit utf-32)) 4 3))))) + +(def ps-stream-plain + (fn (_ name) + (let ((c (ps-codec name))) + (match + ((eq? c (lit utf-16)) "utf-16-le") + ((eq? c (lit utf-32)) "utf-32-le") + ((eq? c (lit utf-8-sig)) "utf-8") + (#t name))))) + +; The codec the BOM at the front of BS names, or nil when there is none. +(def ps-stream-bom + (fn (_ name bs) + (let ((c (ps-codec name))) + (match + ((eq? c (lit utf-16)) + (match + ((%ps-starts? %ps-bom16le bs) "utf-16-le") + ((%ps-starts? %ps-bom16be bs) "utf-16-be") + (#t ()))) + ((eq? c (lit utf-32)) + (match + ((%ps-starts? %ps-bom32le bs) "utf-32-le") + ((%ps-starts? %ps-bom32be bs) "utf-32-be") + (#t ()))) + ((%ps-starts? %ps-bom8 bs) "utf-8") + (#t ()))))) + +; Whether BS is the start of utf-8's BOM and no more of it. +(def ps-bom8-start? (fn (_ bs) (%ps-starts? bs %ps-bom8))) ; --- the boundary ------------------------------------------------------------ (def %ps-of-x (fn (_ s) (%ps-decode (pb-of-str s) ()))) diff --git a/tests/modules/data/long-utf16.txt b/tests/modules/data/long-utf16.txt new file mode 100644 index 0000000000000000000000000000000000000000..4026e6b894b08dcf8b5c52449ed11798c4a58cf8 GIT binary patch literal 8226 zcmeIuu?fH+07TKN1CHb{(WR~IUBbixvOwT*e7O2O`;HMHK!5-N0t5&UAV7cs0RjXF g5FkK+009C72oNAZfB*pke-@}CbET!^N Date: Fri, 27 Nov 2026 10:48:47 -0500 Subject: [PATCH 2/2] test: number the file-codecs spec D15 D14 is taken by bisect's spec in jonruttan/x-python#269. Co-Authored-By: Claude Opus 5.5 --- tests/specs/{D14-file-codecs.spec.md => D15-file-codecs.spec.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/specs/{D14-file-codecs.spec.md => D15-file-codecs.spec.md} (100%) diff --git a/tests/specs/D14-file-codecs.spec.md b/tests/specs/D15-file-codecs.spec.md similarity index 100% rename from tests/specs/D14-file-codecs.spec.md rename to tests/specs/D15-file-codecs.spec.md