On 1/22/2010 2:04 PM, M.-A. Lemburg wrote:
> Karen Tracey wrote:
>> On Fri, Jan 22, 2010 at 7:38 AM, Michael Foord 
>> <fuzzy...@voidspace.org.uk>wrote:
>>> The encoding I'm talking about is the
>>> encoding that Python uses to decode a file (or encode a string) when you do
>>> the following in Python 3:
>>>
>>>    text = open(filename).read()
>>>    open(filename, 'w').write(some_string)
>>>
>>> It isn't the default encoding (always utf-8 by default in Python 3
>>> apparently), it isn't the file system encoding which is the system encoding
>>> used for file names. What is the correct terminology for this platform
>>> dependent encoding that Python uses here?
>>>
>>>
>> The doc here:
>> http://docs.python.org/3.1/library/functions.html?highlight=open#open just
>> calls it default encoding and clarifies that is "whatever
>> locale.getpreferredencoding() returns".
> 
> ... which is a pretty poor guess, since the locale setting
> is usually set to what the user wants to see in user interfaces,
> not what the application want to use as file content.
[...]
> Applications should always provide a well-defined encoding
> for use with files - they know best what encoding to use and
> they also know best when to apply guessing and which set
> of encodings to use as basis for such guessing.

This all begs the question: why is there a default? and why is the
default a guess?

I have to admit that I was completely oblivious to this potential
pitfall, and mostly that's because in the most common case, I am working
with ASCII files. It's just serendipity that most systems specify (if
not require) the locale encoding be an ASCII superset.

"""
locale.getpreferredencoding([do_setlocale])

    Return the encoding used for text data, according to user
preferences. User preferences are expressed differently on different
systems, and might not be available programmatically on some systems, so
this function only returns a guess.
"""

I already know that this suggestion will not get any following because,
for most people, it just works. However: "In the face of ambiguity,
refuse the temptation to guess." Would it really be that unfortunate to
force everyone to reconsider what they are doing when they open() files?

-- 
Scott Dial
sc...@scottdial.com
scod...@cs.indiana.edu
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to