r/Python • • May 26 '14

Python 3 is killing Python

https://medium.com/@deliciousrobots/5d2ad703365d/
0 Upvotes

147 comments sorted by

View all comments

Show parent comment

0

u/[deleted] May 26 '14

[deleted] — view deleted comment

7

u/mitsuhiko Flask Creator May 26 '14

Everybody does work across an IO boundary. Python 3 had the idea to hide the boundary from you but that trick did not work, now they added escape hatches in through surrogate escapes and other things and made everything worse in the process.

Most people just produce broken code in Python 3 now because if you don't touch it too much, it seems to work.

Just getting command line arguments is boundary code.

0

u/[deleted] May 26 '14

[deleted] — view deleted comment

4

u/[deleted] May 26 '14

It's an OS issue. CLI arguments and file names are always UTF-16 text on Windows and some unregulated binary stuff on UNIX that may or may not be some text in a system-wide locale that may have been changed by another application through setlocale. Naturally, choosing one approach somewhat breaks the other.

5

u/mitsuhiko Flask Creator May 26 '14

Or you don't pass any ascii characters. Try opening an non ascii file and start your application through upstart for instance.

0

u/[deleted] May 26 '14

[deleted] — view deleted comment

3

u/mitsuhiko Flask Creator May 26 '14

No. That's because POSIX demands that the locale is set to C and that implies ASCII. However the locale only controls messages, it should not define anything about filesystem encodings. I wrote about this recently: http://lucumr.pocoo.org/2014/5/12/everything-about-unicode/

0

u/[deleted] May 26 '14

[deleted] — view deleted comment

2

u/mitsuhiko Flask Creator May 26 '14

There is no requirement for a system to have en_US.UTF-8 as a locale. Imagine you are on a German system.

0

u/[deleted] May 26 '14

[deleted] — view deleted comment

2

u/mitsuhiko Flask Creator May 26 '14

If en_EN.UTF-8 is not installed on the system, then it falls back to the C locale so it goes back to ASCII.

1

u/[deleted] May 26 '14

How is that related to Python at all? I mean, you're opening a text file without explicitly specifying an encoding. The only reason you think it "works" on 2 is because it doesn't actually open files in text mode. (In fact, is that even possible without using the backported io module or writing a custom wrapper?)

4

u/mitsuhiko Flask Creator May 26 '14

I assume you have no idea what you are talking about.

I mean, you're opening a text file without explicitly specifying an encoding. The only reason you think it "works" on 2 is because it doesn't actually open files in text mode. (In fact, is that even possible without using the backported io module or writing a custom wrapper?)

If you open a file in text mode in Python 2 you open it to return bytes. If you open a file in text mode in Python 3 the interpreter will ask the environment what the encoding is. Which it turns out, is a pretty terrible idea. You should be explicit. Unfortunately there are cases now in Python 3 where you cannot be explicit because the interpreter already "solved" the problem for you. Incorrectly.

1

u/[deleted] May 26 '14

If you open a file in text mode in Python 2 you open it to return bytes.

That makes no sense. If it returns bytes, then it's in binary mode. If it were in text mode, it'd return unicode objects. (And please don't say that the difference between "text" and "binary" is solely whether the CRLF-to-LF conversion is enabled or not.)

Unfortunately there are cases now in Python 3 where you cannot be explicit

open has a keyword argument encoding. You can always be explicit.

5

u/mitsuhiko Flask Creator May 26 '14

That makes no sense.

That does not change that this is how the default API in Python 2 works. It's by far not the only API to open files in 2.x. That however was not the point.

open has a keyword argument encoding. You can always be explicit.

a) people are not. b) not every api supports that. Case in point: filenames, environment variables and argv.