[I18n-sig] Modified open() builtin (Re: Python Character Model)

Fredrik Lundh fredrik@effbot.org
Sun, 11 Feb 2001 14:34:26 +0100


mal wrote:
> > We (at least some subset of the people in the i18n-sig) want every
> > single new instance of the "open" function to declare an encoding. 
> 
> This doesn't make sense: not all uses of open() target text 
> information. What encoding information would you put into an
> open() which wants to read a JPEG image from a file ?

how about:

    file = open("image.jpg", encoding="image/jpeg")
    image = file.read() # return a PIL image object

or perhaps better:

    file = open("image.jpg", encoding="image/*")
    image = file.read()  

> We cannot turn override the mode parameter with an encoding
> parameter... why do you believe that this is backwards compatible
> in any way ? (Note that mode is an optional parameter!)

instead of overriding, why not append the encoding to
the mode parameter:

    "r" # default, read text file, unknown encoding
    "rb" # read binary file, no encoding"
    "r,utf-8" # read text file, utf-8 encoding
    "rb,ascii" # illegal mode

(this is in line with C's fopen)

Cheers /F