Repository navigation
uv-fs: transparently support reading UTF-16 files - #2283
Conversation
|
cc @zanieb @mitsuhiko given discussion in #1666 about supporting UTF-16 encoded |
zanieb
left a comment
There was a problem hiding this comment.
👍 I agree with your principles
|
I think it's wise to support these as long as PowerShell is going to use UTF-16 by default when piping -- that's a very common use-case. |
|
Now I realize that this is because i use I'm not sure what to do here. It does seem weird to me that uv would support this encoding since nothing else does, but maybe this can be useful for the user. I do fully expect that this will create questions though about why those files don't work with other tools. |
This is interesting because I think Rust is the one who is writing UTF-16 to the console. Is that somehow getting transcoded back to UTF-8 for you? |
|
@BurntSushi this is what it does on my machine: I think on this machine the default code page is |
So this machine is a few months old and I'm really not sure how any of this works. What I can see on my machine:
No clue why it does that. I tried on a VM and it always shows me code page 850. No matter though how, powershell always transcodes to UTF-16 but that is also reconfigurable with a flag (that probably nobody ever sets). Whatever API Python and Rust use to write, it clearly transcodes somehow somewhere. EDIT: i unticked a beta box for utf-8 support in the |
I did 😆 |
This PR tweaks uv to support reading `requirements.txt` regardless of whether it is encoded as UTF-8 or UTF-16. This is particularly relevant on Windows where `uv pip freeze > requirements.txt` will likely write a UTF-16 encoded `requirements.txt` file. There is some discussion on #1666 where it's suggested that perhaps we should explicitly not support this. I didn't see that until I had already put this PR together, but even so, I think it's worth considering this. UTF-16 is predominant on Windows. It is very easy to produce a UTF-16 encoded file. Moreover, there is an easy and well specified way to recognize and transcode UTF-16 encoded data to UTF-8. I think the downside of this is that it could encourage the use UTF-16 encoded `requirements.txt` files *in addition* to UTF-8 encoded files, and it would probably be nice to converge and standardize on one encoding. One possible alternative to this PR is that we provide a better error message. Another alternative is to ensure that a `-o/--output` flag exists for all commands (neither `uv pip freeze` nor `pip freeze` have such a flag) so that users can always write output to a file without relying on their environment's piping behavior. (Although this last alternative seems a little sad to me.) It's also worth noting the [PEP-0508] doesn't seem to mention file encoding at all. So I think from a "do the standards allow this" perspective, this change is OK. Finally, `pip` itself seems to work with UTF-16 encoded `requirements.txt` files. I think I personally overall lean towards supporting UTF-16 for `requirements.txt` files. In part because I think it smoothes out the UX a little bit, in part because there is no obvious specification (that I'm aware of) that mandates that these files are UTF-8, and finally in part because `pip` supports it too. Fixes #1666, Fixes #2276 [PEP-0508]: https://peps.python.org/pep-0508/
This was causing `cargo test --doc` to fail for me locally.
8101e36 to
5e1b9e8
Compare
|
I'm going to bring this in given most folks seem to be in support of it. Moreover, it addresses an easy footgun for an existing common pip workflow: |
A UTF-16 requirements file containing only its byte-order mark was passed through by the decoder and rejected as invalid UTF-8. We now recognize both complete UTF-16 BOMs as empty input in the shared transcoding helper used by file and stdin reads. Every other input continues through the existing decoder. The in-module tests cover UTF-8 and UTF-16, including non-BMP text, interior BOMs, and empty input. Follow-up to #2283. --------- Co-authored-by: Zanie Blue <[email protected]> Co-authored-by: Charlie Marsh <[email protected]>
This PR tweaks uv to support reading
requirements.txtregardless ofwhether it is encoded as UTF-8 or UTF-16. This is particularly relevant
on Windows where
uv pip freeze > requirements.txtwill likely write aUTF-16 encoded
requirements.txtfile.There is some discussion on #1666 where it's suggested that perhaps
we should explicitly not support this. I didn't see that until I
had already put this PR together, but even so, I think it's worth
considering this. UTF-16 is predominant on Windows. It is very easy
to produce a UTF-16 encoded file. Moreover, there is an easy and well
specified way to recognize and transcode UTF-16 encoded data to UTF-8.
I think the downside of this is that it could encourage the use UTF-16
encoded
requirements.txtfiles in addition to UTF-8 encodedfiles, and it would probably be nice to converge and standardize on
one encoding. One possible alternative to this PR is that we provide
a better error message. Another alternative is to ensure that a
-o/--outputflag exists for all commands (neitheruv pip freezenorpip freezehave such a flag) so that users can always write outputto a file without relying on their environment's piping behavior.
(Although this last alternative seems a little sad to me.)
It's also worth noting the PEP-0508 doesn't seem to mention file
encoding at all. So I think from a "do the standards allow this"
perspective, this change is OK.
Finally,
pipitself seems to work with UTF-16 encodedrequirements.txtfiles.I think I personally overall lean towards supporting UTF-16 for
requirements.txtfiles. In part because I think it smoothes out theUX a little bit, in part because there is no obvious specification
(that I'm aware of) that mandates that these files are UTF-8, and
finally in part because
pipsupports it too.Fixes #1666, Fixes #2276