[Python-3000] New io system and binary data
Christian Heimes
lists at cheimes.de
Thu Sep 20 12:12:48 CEST 2007
Greg Ewing wrote:
> Christian Heimes wrote:
>> With Python 2.5 the input and output data isn't implicitly
>> converted
>
> Are you sure that's always true? What about systems
> where newlines aren't \n?
Windows is a strange beast. As far as I can remember the OS converts the
incoming and outgoing standard streams to Unix line endings \n. A true
binary standard stream on Windows needs some effort - unfortunately. :(
>> I recommend that the problem and fix gets documented. Maybe stdin,
>> stdout and stderr should get a method that disables the implicit
>> conversion like setMode("b") / setMode("t").
>
> Or maybe another set of objects called stdbin, stdbout, stdberr.
I have given some thoughts to it while I was writing the initial mail. I
had the names stdinb, stdoutb and stderrb in mind but your names are
better. The problem with the binary stream lies in the fine detail. We
can't simply assign sys.stdout.buffer to sys.stdbout. I - as a Python
user - would expect that stdbout will always use the same backend as stdout:
Python sets
>>> sys.stdbout = sys.stdout.buffer
Now the user assigns a new file to stdout
>>> sys.stdout = file("myoutput", "w")
and blindly expects that
>>> sys.stdbout.write("data\ndata\n")
does the right thing.
A proxy like following (untested) class might do the trick.
import sys
class StdBinaryFacade:
def __init__(self, name):
self._name = name
def __getattr__(self, key):
buffer = getattr(sys, self._name).buffer
return getattr(buffer, key)
def __repr__(self):
return "<%s for sys.%s at %i>" % (self.__name__, self._name,
id(self))
>>> sys.stdbout = StdBinaryFacade("stdout")
Christian
More information about the Python-3000
mailing list