News for hackers. Stuff that matters.
Re:How UTF-8 Works by BiteCode_dev on Tuesday February 08, 2022 @12:28PM (#30261391)
Like anything new, people had a hard time with it at the beginning.

I remember that I got a home assignment in an interview for a PHP job. The person evaluating my code said I should not have used UTF8, which causes "compatibility problems". At the time, I didn't know better, and I answered that no, it was explicitly created to solve compatibility problems, and that they just didn't understand how to deal with encoding properly.

Needing-less to say, I didn't get the job :)

Same with Python 2 code. So many people, when migrating to Python 3, suddenly though python 3 encoding management was broken, since it was raising so many UnicodeDecodingError.

Only much later people realize the huge number of programs that couldn't deal with non ASCII characters in file paths, html attributes or user names, because they just implicitly assume ASCII. "My code used to work fine", they said. But it worked fine on their machine, set to an english locale, tested only using ascii plain text files on their ascii named directories with their ascii last name.

Re:How UTF-8 Works by andrepd on Tuesday February 08, 2022 @01:01PM (#30261948)
That's in general a problem with dynamic languages with weak type systems. How "Your code runs without crashing" is really really != "your code works". How do people even manage production python! A bug could be lurking anywhere, undetected until it's actually run. Whereas in a compiled language with a strong type system, "your code compiles" is much closer to "your code is correct".
Re:How UTF-8 Works by BiteCode_dev on Tuesday February 08, 2022 @02:42PM (#30263483)
I don't think a type system can help you with decoding a file with the wrong charset.
Re:How UTF-8 Works by morelisp on Wednesday February 09, 2022 @07:02AM (#30271669)
A type system can refuse to turn a `bytes` into a `utf8str` until it's been appropriately parsed.

(It doesn't even need to be a very good or strongly-enforced type system - Go makes it dangerously easy to convert between `[]byte` and `string` by other-type-system standards, and yet everything works pretty well. It's enough to hitch your thinking and make you realize you need another step.)

Re:How UTF-8 Works by BiteCode_dev on Wednesday February 09, 2022 @09:01AM (#30272686)
How does the type system knows the bytes are utf8 and not CP850?
Re:How UTF-8 Works by morelisp on Wednesday February 09, 2022 @11:39AM (#30274715)
The same way you can parse a string into an integer and it knows you got an either[integer, err].
Re:How UTF-8 Works by BiteCode_dev on Wednesday February 09, 2022 @12:04PM (#30275008)
So it doesn't, and it's not about the type system. Any decode operation in python 3 raise an exception or return string.

It's about the runtime error handling making it mandatory to deal with the error and won't let you panic at run time for this specific error.

Re:How UTF-8 Works by morelisp on Wednesday February 09, 2022 @12:07PM (#30275050)
I'm sorry you don't see exceptions as part of a type system yet.
Re:How UTF-8 Works by pvorb on Tuesday February 08, 2022 @06:30PM (#30266390)
The compiler can raise a warning about bad characters. It can't detect all problems, but it certainly can help with some.

It's not the type system, but many dynamic languages are interpreted instead of compiled.

Re:How UTF-8 Works by dotancohen on Wednesday February 09, 2022 @03:09AM (#30270201)
Those bad characters are fed into the program long long after the compiler has done its job.
Re:How UTF-8 Works by pvorb on Wednesday February 09, 2022 @06:06PM (#30279996)
Not necessarily? I'm talking about constant string literals in source code which can be validated at compile time.
Re:How UTF-8 Works by cafard on Wednesday February 09, 2022 @10:33AM (#30273799)
But this is not a matter of UTF-8 in the code, rather a matter of UTF-8 in the input or output. How does compiling a program ensure that it is robust on a range of inputs?
Re:How UTF-8 Works by morelisp on Wednesday February 09, 2022 @11:46AM (#30274808)
> How does compiling a program ensure that it is robust on a range of inputs?

This is quite literally the job of a type system: to impose a semantic interpretation on sequences of "raw bits" and let you specify legal (and only legal) operations in terms of the semantic interpretation rather than the bits.

Re:How UTF-8 Works by digisign on Tuesday February 08, 2022 @01:49PM (#30262658)
There are a number of mitigations, so those kind of bugs are quite rare. In our large code base, about 98% of bugs we find are of the "we need to handle another case" variety. Pyflakes quickly finds typos which eliminates most of the rest.
Re:How UTF-8 Works by wk_end on Tuesday February 08, 2022 @06:26PM (#30266349)
This is the difference between people who embrace static typing and everyone else. A static type lover hears that 98% of your bugs are of the "we need to handle another case" variety and says, "well, that means you could have gotten rid of 98% of your bugs with better typing".
Re:How UTF-8 Works by digisign on Tuesday February 08, 2022 @07:38PM (#30266995)
No, what I mean is that an additional key comes in (with the json or similar hash) and we now need to do some thing with it, or something different than we thought we were supposed to with it. Typing is not going to fix it because the full cases were unknown at development time.
Re:How UTF-8 Works by mekster on Wednesday February 09, 2022 @12:14AM (#30269125)
> "your code compiles" is much closer to "your code is correct".

That is so far from the truth.

Re:How UTF-8 Works by ben-schaaf on Wednesday February 09, 2022 @01:05AM (#30269481)
How is it anything but the truth? The express purpose of static analysis, like a type system, is to catch bugs before running your code. That pretty clearly means that code that successfully compiles is closer to being correct than code that doesn't.
Re:How UTF-8 Works by mekster on Wednesday February 09, 2022 @01:13AM (#30269528)
I mean, correct as in logically correct and that's just a different matter than being grammatically correct.
Re:How UTF-8 Works by morelisp on Wednesday February 09, 2022 @12:01PM (#30274980)
The parser assures your code is grammatically correct; the type system assures your code is semantically consistent, which is usually a much stronger guarantee, and by most practical measures will be closer - often much closer, and for total functions on total types, sometimes all the way - to "logically correct".
Re:How UTF-8 Works by morelisp on Tuesday February 08, 2022 @03:54PM (#30264550)
Python 3 encoding management was broken, because it tried to impose Unicode semantics on things that were actually byte streams. For anyone actually correctly handling encodings in Python 2 it was awful because suddenly the language runtime was hiding half the data you needed.
Re:How UTF-8 Works by BiteCode_dev on Tuesday February 08, 2022 @05:29PM (#30265765)
Nowadays, passing bytes to any os function returns bytes objects, not unicode. You'll get string if you pass string objects though, and they will be using utf8 surrogate escaping.
Re:How UTF-8 Works by morelisp on Wednesday February 09, 2022 @06:58AM (#30271639)
And nowadays, lots of people left the Python ecosystem completely because the 3 upgrade was broken and raising so many UnicodeDecodingErrors for so long. I'm glad it's fixed but it cost too much.

(And it had nothing to do with UTF-8. Actually, it's at least partially caused by the CPython developers avoiding UTF-8 for poor reasons.)

Re:How UTF-8 Works by mark-r on Wednesday February 09, 2022 @04:57PM (#30279128)
I'd argue that the people correctly handling encodings in Python 2 were vastly outnumbered by the people that weren't, but were getting away with it because the code didn't outright crash. Now in Python 3 it crashes, which is a pain in the short term but better in the long run.
Re:How UTF-8 Works by morelisp on Thursday February 10, 2022 @03:06AM (#30283526)
I don't really think it's better - certainly it was not better until many years after release when they started admitting their big mistakes.

But even today, codepoint indexing is too easy and doesn't crash so lots of code is still subtly wrong. Memory usage of a string grows 3x if you add a single emoji. Most libraries are still agnostic about whether they take bytes or str so you still get exceptions thrown with no easy solution. (The growing popularity of type hints is fixing this, but that's not really related to Python 3.)

Re:How UTF-8 Works by SAI_Peregrinus on Tuesday February 08, 2022 @04:02PM (#30264644)
My Slack name at work is "Τĥιs ñåmè įß ą váĺîδ POSIX paτĥ". My hope is that it serves as an amusing reminder to consider things like spaces and non-ASCII characters.
Re:How UTF-8 Works by tialaramex on Tuesday February 08, 2022 @08:33PM (#30267489)
One of my friends is these days a colleague, with an utterly ordinary English name but his identity management data is full of spurious accents to check APIs do the Right Thing™.

I was delighted recently to stumble on a history of modern women course HIST1158 named Liberté Egalité Beyoncé and I immediately thought two things: 1. Why are our Computer Science courses given unimaginative names? and 2. What a useful test input, I bet some of our systems don't work correctly for this input even though an acute accent is hardly a bleeding edge feature.

I haven't been able to interest any Computer Science professors in fun names for their courses, but I was able in my test environment to name a COMP series course "Untitled Course Name" with a description explaining that "It is a lovely day in the village and there are only two hard problems in Computer Science".

Re:How UTF-8 Works by bear8642 on Tuesday February 08, 2022 @09:24PM (#30267905)
>only two hard problems in Computer Science".

You mean cache invalidation and naming things? :)

Re:How UTF-8 Works by mark-r on Tuesday February 08, 2022 @10:10PM (#30268265)
And off-by-one errors.
Re:How UTF-8 Works by kergonath on Wednesday February 09, 2022 @07:26AM (#30271833)
2 just means that the counter overflowed.
Re:How UTF-8 Works by xorcist on Saturday February 12, 2022 @11:59AM (#30314230)
Much too trivial. Not even an RTL marker? How about some of the more exotic blank steps? There are some interesting combining characters too!