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 0000000..4026e6b Binary files /dev/null and b/tests/modules/data/long-utf16.txt differ diff --git a/tests/modules/data/long-utf16be.txt b/tests/modules/data/long-utf16be.txt new file mode 100644 index 0000000..5cc7ff6 Binary files /dev/null and b/tests/modules/data/long-utf16be.txt differ diff --git a/tests/modules/data/long-utf8sig.txt b/tests/modules/data/long-utf8sig.txt new file mode 100644 index 0000000..3a01451 --- /dev/null +++ b/tests/modules/data/long-utf8sig.txt @@ -0,0 +1,6 @@ +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa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aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa😀éb +c +b +c +b +c diff --git a/tests/specs/D15-file-codecs.spec.md b/tests/specs/D15-file-codecs.spec.md new file mode 100644 index 0000000..dd084f1 --- /dev/null +++ b/tests/specs/D15-file-codecs.spec.md @@ -0,0 +1,108 @@ +# codecs in files: utf-16, utf-32 and utf-8-sig through open() + +`lib/python/io.x`. Each expected output is a python3.14 run of the same +program, all ASCII. + +### a file written in a BOM codec has one BOM, at its first byte, and reads back + +```python +(python-run "p = \"/tmp/x-python-d14-codecs.txt\"\ndef raw():\n with open(p, 'rb') as f:\n return f.read()\nfor enc in ('utf-16', 'utf-32', 'utf-8-sig', 'utf-16-be', 'utf-32-le'):\n with open(p, 'w', encoding=enc) as f:\n f.write('')\n f.write('ab')\n f.write(chr(0x1f600))\n print(enc, raw())\n with open(p, 'a', encoding=enc) as f:\n f.write('c')\n print(raw())\n with open(p, 'w+', encoding=enc) as f:\n f.write('xy')\n f.seek(0)\n f.write('z')\n f.seek(0)\n print(ascii(f.read()))\n print(raw())\n with open(p, encoding=enc) as f:\n print(ascii(f.read(1)), ascii(f.read(1)), ascii(f.read()))\nwith open(p, 'w', encoding='utf-16') as f:\n f.write('')\nprint(raw())\nwith open(p, 'a', encoding='utf-16') as f:\n f.write('q')\nprint(raw())\n") +``` +--- +```output +utf-16 b'\xff\xfea\x00b\x00=\xd8\x00\xde' +b'\xff\xfea\x00b\x00=\xd8\x00\xdec\x00' +'zy' +b'\xff\xfez\x00y\x00' +'z' 'y' '' +utf-32 b'\xff\xfe\x00\x00a\x00\x00\x00b\x00\x00\x00\x00\xf6\x01\x00' +b'\xff\xfe\x00\x00a\x00\x00\x00b\x00\x00\x00\x00\xf6\x01\x00c\x00\x00\x00' +'zy' +b'\xff\xfe\x00\x00z\x00\x00\x00y\x00\x00\x00' +'z' 'y' '' +utf-8-sig b'\xef\xbb\xbfab\xf0\x9f\x98\x80' +b'\xef\xbb\xbfab\xf0\x9f\x98\x80c' +'zy' +b'\xef\xbb\xbfzy' +'z' 'y' '' +utf-16-be b'\x00a\x00b\xd8=\xde\x00' +b'\x00a\x00b\xd8=\xde\x00\x00c' +'zy' +b'\x00z\x00y' +'z' 'y' '' +utf-32-le b'a\x00\x00\x00b\x00\x00\x00\x00\xf6\x01\x00' +b'a\x00\x00\x00b\x00\x00\x00\x00\xf6\x01\x00c\x00\x00\x00' +'zy' +b'z\x00\x00\x00y\x00\x00\x00' +'z' 'y' '' +b'\xff\xfe' +b'\xff\xfeq\x00' +``` + +### reading a file: the BOM settles the byte order, and a utf-16 or utf-32 stream with none is refused + +```python +(python-run "p = \"/tmp/x-python-d14-codecs.txt\"\ndef read(data, enc, err='strict'):\n with open(p, 'wb') as f:\n f.write(data)\n try:\n with open(p, encoding=enc, errors=err) as f:\n print(ascii(f.read()))\n except UnicodeError as e:\n print(type(e).__name__, e)\nfor d in (b'', b'h', b'h\\x00', b'h\\x00\\x00', b'h\\x00\\x00\\x00', b'\\xff\\xfe', b'\\xff\\xfeh', b'\\xfe\\xff\\x00h\\x00i', b'\\x00\\x00\\xfe\\xff\\x00\\x00\\x00h'):\n for enc in ('utf-16', 'utf-32', 'utf-16-le'):\n print(d, enc, end=' ')\n read(d, enc)\nread(b'h\\x00i\\x00', 'utf-16', 'replace')\nread(b'\\xff\\xfe\\x00\\xd8h\\x00', 'utf-16', 'replace')\nread(b'\\xef\\xbb', 'utf-8-sig')\nread(b'\\xef', 'utf-8-sig')\nread(b'\\xef\\xbb\\xbfhi\\r\\nthere', 'utf-8-sig')\nread(b'hi', 'utf-8-sig')\nread(b'\\xef\\xbb\\xbf\\xef\\xbb\\xbfhi', 'utf-8-sig')\n") +``` +--- +```output +b'' utf-16 '' +b'' utf-32 '' +b'' utf-16-le '' +b'h' utf-16 UnicodeDecodeError 'utf-16-le' codec can't decode byte 0x68 in position 0: truncated data +b'h' utf-32 UnicodeDecodeError 'utf-32-le' codec can't decode byte 0x68 in position 0: truncated data +b'h' utf-16-le UnicodeDecodeError 'utf-16-le' codec can't decode byte 0x68 in position 0: truncated data +b'h\x00' utf-16 UnicodeDecodeError 'utf-16' codec can't decode bytes in position 0-1: Stream does not start with BOM +b'h\x00' utf-32 UnicodeDecodeError 'utf-32-le' codec can't decode bytes in position 0-1: truncated data +b'h\x00' utf-16-le 'h' +b'h\x00\x00' utf-16 UnicodeDecodeError 'utf-16-le' codec can't decode byte 0x00 in position 2: truncated data +b'h\x00\x00' utf-32 UnicodeDecodeError 'utf-32-le' codec can't decode bytes in position 0-2: truncated data +b'h\x00\x00' utf-16-le UnicodeDecodeError 'utf-16-le' codec can't decode byte 0x00 in position 2: truncated data +b'h\x00\x00\x00' utf-16 UnicodeDecodeError 'utf-16' codec can't decode bytes in position 0-1: Stream does not start with BOM +b'h\x00\x00\x00' utf-32 UnicodeDecodeError 'utf-32' codec can't decode bytes in position 0-3: Stream does not start with BOM +b'h\x00\x00\x00' utf-16-le 'h\x00' +b'\xff\xfe' utf-16 '' +b'\xff\xfe' utf-32 UnicodeDecodeError 'utf-32-le' codec can't decode bytes in position 0-1: truncated data +b'\xff\xfe' utf-16-le '\ufeff' +b'\xff\xfeh' utf-16 UnicodeDecodeError 'utf-16-le' codec can't decode byte 0x68 in position 2: truncated data +b'\xff\xfeh' utf-32 UnicodeDecodeError 'utf-32-le' codec can't decode bytes in position 0-2: truncated data +b'\xff\xfeh' utf-16-le UnicodeDecodeError 'utf-16-le' codec can't decode byte 0x68 in position 2: truncated data +b'\xfe\xff\x00h\x00i' utf-16 'hi' +b'\xfe\xff\x00h\x00i' utf-32 UnicodeDecodeError 'utf-32-le' codec can't decode bytes in position 0-3: code point not in range(0x110000) +b'\xfe\xff\x00h\x00i' utf-16-le '\ufffe\u6800\u6900' +b'\x00\x00\xfe\xff\x00\x00\x00h' utf-16 UnicodeDecodeError 'utf-16' codec can't decode bytes in position 0-1: Stream does not start with BOM +b'\x00\x00\xfe\xff\x00\x00\x00h' utf-32 'h' +b'\x00\x00\xfe\xff\x00\x00\x00h' utf-16-le '\x00\ufffe\x00\u6800' +UnicodeDecodeError 'utf-16' codec can't decode bytes in position 0-1: Stream does not start with BOM +'\ufffdh' +'' +'' +'hi\nthere' +'hi' +'\ufeffhi' +``` + +### a character split between two reads, newlines, seek, and surrogates in a file + +```python +(python-run "p = \"/tmp/x-python-d14-codecs.txt\"\nfor name, enc, n in (('long-utf16.txt', 'utf-16', 4094), ('long-utf16be.txt', 'utf-16-be', 4095), ('long-utf8sig.txt', 'utf-8-sig', 8188)):\n q = \"tests/modules/data/\" + name\n with open(q, encoding=enc) as f:\n t = f.read()\n print(enc, len(t), ascii(t[n - 2:n + 4]), ascii(t[-6:]))\n with open(q, encoding=enc) as f:\n print([len(line) for line in f])\n with open(q, encoding=enc, newline='') as f:\n print(ascii(f.readlines()[-2:]))\n f.seek(0)\n print(ascii(f.read(3)))\nwith open(p, 'w', encoding='utf-16-le', newline='\\r\\n') as f:\n f.write('x\\ny\\n')\nwith open(p, 'rb') as f:\n print(f.read())\nwith open(p, 'w', encoding='utf-16') as f:\n try:\n f.write('a' + chr(0xd800))\n except UnicodeError as e:\n print(type(e).__name__, e)\nwith open(p, 'w', encoding='utf-16', errors='surrogatepass') as f:\n f.write('a' + chr(0xd800))\nwith open(p, 'rb') as f:\n print(f.read())\nwith open(p, encoding='utf-16', errors='surrogatepass') as f:\n print(ascii(f.read()))\n") +``` +--- +```output +utf-16 4108 'aa\U0001f600\xe9b\n' 'c\nb\nc\n' +[4098, 2, 2, 2, 2, 2] +['b\r\n', 'c\n'] +'aaa' +utf-16-be 4109 'aa\U0001f600\xe9b\n' 'c\nb\nc\n' +[4099, 2, 2, 2, 2, 2] +['b\r\n', 'c\n'] +'aaa' +utf-8-sig 8202 'aa\U0001f600\xe9b\n' 'c\nb\nc\n' +[8192, 2, 2, 2, 2, 2] +['b\r\n', 'c\n'] +'aaa' +b'x\x00\r\x00\n\x00y\x00\r\x00\n\x00' +UnicodeEncodeError 'utf-16' codec can't encode character '\ud800' in position 1: surrogates not allowed +b'\xff\xfea\x00\x00\xd8' +'a\ud800' +```