Author larry
Recipients Sworddragon, a.badger, bkabrda, larry, lemburg, loewis, ncoghlan, pitrou, r.david.murray, serhiy.storchaka, terry.reedy, vstinner
Date 2013-12-13.11:58:51
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1386935931.95.0.013903044656.issue19846@psf.upfronthosting.co.za>
In-reply-to
Content
> "The fact that write() -> open() relies on sys.getfilesystemencoding()
> (respectively locale.getpreferredencoding()) at default as encoding is
> either a defect or a bad design (I leave the decision to you)."
>
> Or am I overlooking something?

First, you should probably just drop mentioning write() or print() or any of the functions that actually perform I/O.  The crucial decisions about decoding are made inside open().

Second, open() is implemented in C.  It cannot "rely on sys.getfilesystemencoding()" as it never calls it.  Internally, sys.getfilesystemencoding() simply returns a C global called Py_FileSystemDefaultEncoding.  But open() doesn't examine that, either.

Instead, open() determines the default encoding by calling the same function that's used to initialize Py_FileSystemDefaultEncoding: get_locale_encoding() in Python/pythonrun.c.  Which on POSIX systems calls the POSIX function nl_langinfo().

If you want to see the actual mechanisms involved, you should read the C source code in Modules/_io in the Python trunk.  open() is implemented as the C function io_open() in _iomodule.c.  When it opens a file in text mode without an explicit encoding, it wraps it in a TextIOWrapper object; the __init__ function for this class is the C function textiowrapper_init() in textio.c.

As for your assertion that this is "either a defect or a bad design": I leave the critique of that to others.
History
Date User Action Args
2013-12-13 11:58:52larrysetrecipients: + larry, lemburg, loewis, terry.reedy, ncoghlan, pitrou, vstinner, a.badger, r.david.murray, Sworddragon, serhiy.storchaka, bkabrda
2013-12-13 11:58:51larrysetmessageid: <1386935931.95.0.013903044656.issue19846@psf.upfronthosting.co.za>
2013-12-13 11:58:51larrylinkissue19846 messages
2013-12-13 11:58:51larrycreate