News for hackers. Stuff that matters.
Re:The History and rationale of the Python 3 Unicode model f... by zgrxat on Wednesday December 14, 2022 @10:56AM (#33985302)
The Unicode model is complex and overengineered. Like many Python features, it requires permanent marketing until people believe it is good. It provides a steady income stream for those paid to "work" on Python.

The right way would have been utf-8, which is the only reasonable representation. Indexing could have been broken early in Python 3 or outsourced to a special type.

Re:The History and rationale of the Python 3 Unicode model f... by jerf on Wednesday December 14, 2022 @11:28AM (#33985827)
Do you mean the right way would be for Python to use utf-8 for file names, or for the filesystem? I'm not sure how "utf-8" is the solution in this case for the problem.

The problem here is that file names aren't any kind of Unicode. You can have a utf-8 filename right next to a utf-16 file name right next to a Shift-JIS filename right next to one that's just binary gibberish that isn't a legal string in any encoding (that has any restrictions at all). They're just bytes with no indication of what coding scheme they are at all. You can't solve this problem with any Unicode solution. (Even the one that tries to wrap bad bytes in a recoverable way, whose name escapes me and I can't recall if it's actually a standard or not, would still make me nervous. You really need code that just treats the filenames as opaque bytes, and only later at display time tries to figure out if they're UTF-8 or not.)

It doesn't matter what Unicode model Python used. That may solve Python's other problems, sure. But filenames would always be some sort of problem. Even if you do just treat them as bytes, all that really does is make your potential errors deterministic, which is still better than a crash probably, but the filesystem's lack of encoding standards is a fundamental problem that can't be solved by higher code levels.

Re:The History and rationale of the Python 3 Unicode model f... by StefanKarpinski on Wednesday December 14, 2022 @12:06PM (#33986421)
On UNIX, paths are UTF-8 by convention, but not forced to be valid. Treating paths as UTF-8 works very well as long as you hadn't also make the mistake of requiring your UTF-8 strings to be valid (which Python did, unfortunately).

On Windows, paths are UTF-16 by convention, but also not forced to be valid. However, invalid UTF-16 can be faithfully converted to WTF-8 and converted back losslessly, so you can translate Windows paths to WTF-16 and everything Just Works™ [1].

There aren't any operating systems I'm aware of where paths are actually Shift-JIS by convention, so that seems like a non-issue. Using "UTF-8 by convention" strings works on all modern OSes.

[1] Ok, here's why the WTF-8 thing works so well. If we write WTF-16 for potentially invalid UTF-16 (just arbitrary sequences of 16-bit code units), then the mapping between WTF-16 and WTF-8 space is a bijection because it's losslessly round-trippable. But more importantly, this WTF-8/16 bijection is also a homomorphism with respect to pretty much any string operation you can think of. For example `utf16_concat(a, b) == utf8_concat(wtf8(a), wtf8(b))` for arbitrary UTF-16 strings a and b. Similar identities hold for other string operations like searching for substrings or splitting on specific strings.

Re:The History and rationale of the Python 3 Unicode model f... by ChrisSD on Wednesday December 14, 2022 @12:41PM (#33986895)
Just to clarify further, note that actually preserving the "as if" behaviour does involve some complexity in the implementation. E.g. appending to WTF-8 has to be handled carefully to ensure it remains truly the same as doing so with WTF-16. This is because any newly paired surrogate has to be converted to its proper UTF-8 encoding. Similarly splitting WTF-8 can potentially break apart what was valid UTF-8 (though I'm not totally convinced that there's a good use case for actually doing this, at least for Windows paths).

Of course the implementation details are something that can and should be handled by a library instead of doing it manually.

Re:The History and rationale of the Python 3 Unicode model f... by msla on Wednesday December 14, 2022 @01:24PM (#33987561)
> On UNIX, paths are UTF-8 by convention, but not forced to be valid.

On UNIX, paths are a sequence of bytes, with two bytes being sacred to the kernel (0x2F, used to separate path elements, and 0x00, used to terminate paths) and no other bytes being interpreted in any way. Any character encoding which respects the sacred bytes by not using them to encode any other characters is therefore usable to make UNIX paths; in fact, a UNIX path can contain multiple encodings, as long as they're all suitably respectful.

That requirement for respect means that UTF-16 and UCS-2 and UCS-4 are not suitable. UTF-7 is, however, as is UTF-8, and all of the ISO/IEC 8859 encodings are as well, not to mention a whole raft of non-standard "extended ASCII" character sets. In theory, UTF-16 in some suitably respectful encoding would work, too, but gouge my eyes out with a goddamned spoon.

Re:The History and rationale of the Python 3 Unicode model f... by deathanatos on Wednesday December 14, 2022 @01:29PM (#33987668)
They know all that. The operative words there are "by convention", not "by requirement".

I.e., their comment is an RFC "OUGHT TO", not an RFC "MUST".

Re:The History and rationale of the Python 3 Unicode model f... by msla on Wednesday December 14, 2022 @02:04PM (#33988376)
My point is that assuming UNIX paths are UTF-8 by default is fragile, and that assuming UNIX paths have any consistent character encoding is also somewhat fragile. You can check to see if the path is UTF-8, but, otherwise, mitts off unless the user explicitly tells you an encoding.
Re:The History and rationale of the Python 3 Unicode model f... by deathanatos on Wednesday December 14, 2022 @03:18PM (#33989777)
> assuming UNIX paths are UTF-8 by default is fragile, and that assuming UNIX paths have any consistent character encoding is also somewhat fragile.

Again, we're both aware that it's not guaranteed. But the convention these days is nonetheless UTF-8.

> mitts off unless the user explicitly tells you an encoding.

This doesn't adequately solve the problem, though. Typed languages have to emit some type of value: having that be the "string" type of the language is useful, as you can do things like printf a message that includes the filename. (Or display an "Open file…" dialog. Or…)

There are middle-grounds, such as byte smuggling and escaping. But if you take the stance that filenames are arbitrary bags of bytes (which is actually a subset: the reality is even worse) — then anything that returns a filename is stuck: it can't return a string ("mitts off"). You can take Rust's approach with Path (a type specific to Paths) but people wail about that all the time too ("why are there so many types?"), and you can't print it because it's not text!

"Error out if the file name isn't conventional" is a pragmatic tradeoff: bad file names will cause errors, but it makes basically all other operations much more tractable. It's not worth supporting insane file names.

There are workarounds, of course (such as just replacement-charactering "�" anything that can't be understood), and finding a format that can encode Paths when transmitting them, but these all take more time and effort. Allowing non-text files introduces unnecessary complexity and bugs into every single program that needs to deal with the file system.

Re:The History and rationale of the Python 3 Unicode model f... by lmm on Wednesday December 14, 2022 @05:50PM (#33991899)
> There aren't any operating systems I'm aware of where paths are actually Shift-JIS by convention, so that seems like a non-issue. Using "UTF-8 by convention" strings works on all modern OSes.

Nonsense. Unix paths use the system locale by convention, and it's entirely normal for that to be Shift-JIS.

Re:The History and rationale of the Python 3 Unicode model f... by StefanKarpinski on Wednesday December 14, 2022 @11:07AM (#33985510)
Absolutely right. Deprecating direct string indexing would have been the right move. Require writing `str.chars()` to get something that lets you slice by Unicode characters (i.e. code points); provide `str.graphemes()` and `str.grapheme_clusters()` to get something that lets you slice by graphemes and grapheme clusters, respectively. Cache an index structure that lets you do that kind of indexing efficiently once you've asked for it the first time. Provide an API to clear the caches.

Not allowing strings to represent invalid Unicode is also a huge mistake (and essentially forced by the representation strategy that they adopted). It forces any programmer who wants to robustly handle potentially invalid string data to use byte vectors instead. Which is exactly what they did with OS paths, but that's far from the only place you can get invalid strings. You can get invalid strings almost anywhere! Worse, since it's incredibly inconvenient to work with byte vectors when you want to do stringlike stuff, no one does it unless forced to, so this design choice effectively guarantees that all Python code that works with strings will blow up if it encounters anything invalid—which is a very common occurrence.

If only there was a type that behaves like a string and supports all the handy string operations but which handles invalid data gracefully. Then you could write robust string code conveniently. But at that point, you should just make that the standard string type! This isn't hypothetical, it's exactly how Burnt Sushi's bstr type [1] works in Rust and how the standard String type works in Julia.

[1] https://github.com/BurntSushi/bstr

Re:The History and rationale of the Python 3 Unicode model f... by Jasper_ on Wednesday December 14, 2022 @11:27AM (#33985812)
It's worth noting that Python str's are sequences of code points, not scalar values. This was a truly horrendous mistake made mostly out of ignorance, but now they rely upon it in surrogateescape to hide "invalid" data, so...

I have ranted for long hours go friends about the insanity of Python 3's text model before. It's mostly the blind leading the blind.

Re:The History and rationale of the Python 3 Unicode model f... by 323 on Wednesday December 14, 2022 @11:37AM (#33985979)
I've seen this happen multiple times in Python, where they implement a feature without a thorough survey of what other languages are doing. Then when it turns out the new feature has a major broken part, they fix that, but again, without looking how it's done in other places. asyncio is a major example.
Re:The History and rationale of the Python 3 Unicode model f... by Spivak on Wednesday December 14, 2022 @12:15PM (#33986552)
> It forces any programmer who wants to robustly handle potentially invalid string data to use byte vectors instead.

Is this not the only thing you can really do? If strings could hold invalid unicode then they effectively become bytes and you now have to be wary of every possible string. I would rather Python just do away with strings entirely in the integration points with the OS and make you either keep them as opaque bytes or decode them.

> behaves like a string and supports all the handy string operations but which handles invalid data gracefully

Unless Guido was feeling particularly practical I can't imagine this ever making it into the stdlib because the choice of "gracefully" is somewhat arbitrary and application dependent.

Re:The History and rationale of the Python 3 Unicode model f... by wk_end on Wednesday December 14, 2022 @02:00PM (#33988301)
Deprecating direct string indexing might have been the "right" move in some sense but it's hard to imagine it ever happening in Python; it's such a natural and frequently-used operation in the language that you would've broken an enormous amount of code. Because indexing strings is syntactically identical to indexing lists, and because there's no* real static typing, there'd be no* good and robust way to automate the conversion.

Like, the changes Python 3 made were honestly pretty subtle, and nearly 15 years later there's still people reluctant to upgrade. If they broke string indexing I'm pretty sure Python 3 adoption would make Perl 6 uptake look impressive.

Re:The History and rationale of the Python 3 Unicode model f... by Animats on Wednesday December 14, 2022 @02:53PM (#33989258)
Unicode string indexing should have been made lazy, rather than deprecated. Random access to strings is rare. Mostly, operations are moving forward linearly or using saved positions.

So, only build the index for random access if needed. Optimize "advance one glyph" and "back up one glyph" expressed as indexing, and you'll get most of the frequently used cases. Have the "index" functions that return a string index return an opaque type that's a byte index. Attempting to convert that to an integer forces creation of the string index.

This preserves the user visible semantics but keeps performance.

PyPy does something like this.

Re:The History and rationale of the Python 3 Unicode model f... by burntsushi on Thursday December 15, 2022 @09:39AM (#34000141)
And note that bstr's design was based on Go's 'string' type, which has the same property: it is conventionally UTF-8.
Re:The History and rationale of the Python 3 Unicode model f... by zokier on Wednesday December 14, 2022 @11:24AM (#33985777)
Utf8 would not help with the issue in the article in any way.
Re:The History and rationale of the Python 3 Unicode model f... by StefanKarpinski on Wednesday December 14, 2022 @12:21PM (#33986646)
It's not at all obvious how it helps, but it does.

First, why is Python unable to represent invalid path names as strings? Because internally it converts strings from UTF-8, UTF-16, or any other encoding, to a fixed-with array of decoded Unicode code points. The width of integer used to represent code points is determined by the largest code point in the string: if the string is ASCII, it can use a byte (uint8) per character; if the string is non-ASCII but all BMP, then it can use a uint16 per character; otherwise it has to use uint32 per character.

Why does Python do all this? So that you can have O(1) character indexing. If you gave up on that, you wouldn't need to convert the string at all, you could just leave it as (potentially invalid) UTF-8 data.

Suppose you get an invalid path on UNIX where paths are UTF-8 by convention? What does Python do with this string? It can't convert it to an array of code points because invalid UTF-8 doesn't correspond to a code point (well, it can if it's just illegal, not malformed, but in general, we have to consider completely malformed strings that don't even follow the basic UTF-8 format). So Python is stuck: it can only replace the invalid data with something like the Unicode replacement character. But then you can't do anything useful with that because it's not the correct name of the path you're trying to work with.

How does using UTF-8 to represent strings help? Because you can represent invalid strings: just leave them as-is and don't try to decode them unless you have to. Sure, you can't decode them as code points, but that's actually a pretty unusual thing to do. If someone asks for decoding, _then_ you can give an error. What about Windows where paths are UTF-16 by convention? You can convert them to WTF-8 and everything works out. (Described in way more detail here: https://news.ycombinator.com/item?id=33984308).

Re:The History and rationale of the Python 3 Unicode model f... by masklinn on Wednesday December 14, 2022 @12:27PM (#33986726)
> How does using UTF-8 to represent strings help? Because you can represent invalid strings: just leave them as-is and don't try to decode them unless you have to.

That’s not UTF8. That’s a bag’o bytes which might be UTF8. Very different thing.

> Sure, you can't decode them as code points, but that's actually a pretty unusual thing to do.

It’s not, any unicode-aware text processing does it implicitly. This means any such processing has to either perform its own validation that the input is valid, or it may fly off the rails entirely if fed nonsense. This also increases risks if security issues, either outright UBs, or the ability to smuggle payloads through overlong encoding.

Re:The History and rationale of the Python 3 Unicode model f... by StefanKarpinski on Wednesday December 14, 2022 @12:40PM (#33986883)
> That’s not UTF8.

True; I was careful not to call it that, but treating strings as UTF-8 by convention does make sense.

> It’s not, any unicode-aware text processing does it implicitly. This means any such things processing has to either perform its own validation that the input is valid, or it may fly off the rails entirely if fed nonsense.

In theory, but that's just not how most string operations actually work. If you have two UTF-8 strings and you want to concatenate them, you just concatenate the bytes. It would be ridiculously inefficient to decode the code points in each string and then re-encode them back into a destination buffer. If you have two UTF-8 strings and you want to see if one is a substring of the other and at what byte index, you just look for the bytes of one as a "substring" of the bytes of the other. Again, it would be ridiculously inefficient to decode the code points in each and do matching on code points. But what if the strings aren't valid UTF-8?! Both of those operations work just fine even if the strings aren't valid and produce sensible, intuitive results.

If you're implementing a browser or a terminal that has to actually display UTF-8 as characters then sure, you have to actually decode characters. Similarly, if you're parsing text somehow, then you have to decode characters. But many program only do concatenation and search and other operations like that which are actually implemented in terms of byte sequences, not characters.

Re:The History and rationale of the Python 3 Unicode model f... by masklinn on Wednesday December 14, 2022 @02:53PM (#33989254)
> True; I was careful not to call it that

You specifically called it UTF8, repeatedly. The very comment I quoted asserts that "Utf8 would help deal with the issue [of garbage inputs]" (in its denial of the opposite assertion). You also did it in https://news.ycombinator.com/item?id=33986421

> If you have two UTF-8 strings and you want to concatenate them, you just concatenate the bytes.

That's not a unicode-aware operation, it's mostly a unicode-irrelevant operation (though unicode awareness can be useful in edge cases because of special grapheme clusters, but that's very task-specific).

> But what if the strings aren't valid UTF-8?! Both of those operations work just fine even if the strings aren't valid and produce sensible, intuitive results.

If your content is not actually UTF-8, you can end up with UTF-8, thus changing the semantics of the content. You can also end up with overlong UTF-8, which also changes the semantics of the content in a worse way.

Re:The History and rationale of the Python 3 Unicode model f... by StefanKarpinski on Wednesday December 14, 2022 @03:58PM (#33990461)
The comment that you're quoting wasn't mine. In the comment you link to says "UTF-8 by convention". If either string is valid, then the result is as expected. If you're concatenating two strings that are both invalid UTF-8, there's not much you can do that's better than just concatenating the bytes together... which is exactly what treating them as byte arrays would end up doing (but it's less convenient). If you're worried about invalid UTF-8 you can check for validity (which again, is exactly what you end up doing if you use byte arrays).
Re:The History and rationale of the Python 3 Unicode model f... by masklinn on Thursday December 15, 2022 @04:54AM (#33997333)
> The comment that you're quoting wasn't mine.

The comment I originally quoted was yours. The second quote is adapted from a statement you denied. It is thus your statement. Let me put both sections together since you seem unwilling to do so:

>> Utf8 would not help with the issue in the article in any way.

> It's not at all obvious how it helps, but it does.

So you are stating, unambiguously, that "Utf8 would help deal with the issue [of garbage inputs]".

> In the comment you link to says "UTF-8 by convention".

The comment I link says:

> Treating paths as UTF-8 works very well

Which is either

1. wrong

or

2. nonsensical, given the later statement that you should not "require your UTF-8 strings to be valid", which would make them not UTF-8

> If you're concatenating two strings that are both invalid UTF-8, there's not much you can do that's better than just concatenating the bytes together... which is exactly what treating them as byte arrays would end up doing

But that's the point innit? You're asserting semantics which don't hold and which you break with no regard.

> (but it's less convenient).

Is it now? Here's the concatenation of two strings:

    a + b
here's the concatenation of two byte arrays:

    a + b
You're right, the inconvenience makes me shudder. What horror. What indignity.
Re:The History and rationale of the Python 3 Unicode model f... by adgjlsfhk1 on Wednesday December 14, 2022 @12:42PM (#33986915)
The problem with using strict UTF-8 for paths is that paths aren't guaranteed to be valid UTF-8. How do you want to write a program that opens a path who's name is invalid UTF-8?
Re:The History and rationale of the Python 3 Unicode model f... by masklinn on Wednesday December 14, 2022 @01:28PM (#33987648)
> The problem with using strict UTF-8 for paths is that paths aren't guaranteed to be valid UTF-8.

Ok but I’m not saying to do that. I’m saying if you have not-utf8 strings don’t call them UTF8.

> How do you want to write a program that opens a path who's name is invalid UTF-8?

That’s not my problem given I’m not advocating for that.

Re:The History and rationale of the Python 3 Unicode model f... by StefanKarpinski on Wednesday December 14, 2022 @04:09PM (#33990601)
The issue is that when you're implementing something like a programming language or a robust general purpose utility, then simply not being able to open—or list or remove or stat—paths with invalid names is not really acceptable.
Re:The History and rationale of the Python 3 Unicode model f... by masklinn on Thursday December 15, 2022 @04:47AM (#33997283)
Do you actually read comments before replying?
Re:The History and rationale of the Python 3 Unicode model f... by zokier on Wednesday December 14, 2022 @03:02PM (#33989451)
> How does using UTF-8 to represent strings help? Because you can represent invalid strings: just leave them as-is and don't try to decode them unless you have to. Sure, you can't decode them as code points, but that's actually a pretty unusual thing to do. If someone asks for decoding, _then_ you can give an error

How is that better than just handling paths as `bytes`?

Re:The History and rationale of the Python 3 Unicode model f... by ilyt on Wednesday December 14, 2022 @05:09PM (#33991333)
> Why does Python do all this? So that you can have O(1) character indexing. If you gave up on that, you wouldn't need to convert the string at all, you could just leave it as (potentially invalid) UTF-8 data.

Seems like "giving up" would've been better choice, considering just how rare operation that is. Or alternatively doing the conversion lazily the first time operation needing runes instead of bytes happen.

Most string operations are not accessing string by index and most of them even at O(n) would be fast enough because n is small. Like in typical "get a file name, extract some info from it", you're doing extraction once and anything after that doesn't need character indexing, because you already got the relevant data.

Re:The History and rationale of the Python 3 Unicode model f... by mistrial9 on Wednesday December 14, 2022 @11:14AM (#33985604)
> requires permanent marketing

LOL this is great, so true

Re:The History and rationale of the Python 3 Unicode model f... by froh on Wednesday December 14, 2022 @11:18AM (#33985677)
utf-8 needs 24bit per glyph for the logographic unicode scripts, while utf-16 needs 16 bits. So if you're into selling silicon or don't care about these languages, or you only care about heavily mixed content (like html), then yes, utf-8 would be The Right Way (TM).