David-Sarah Hopwood <david-sa...@jacaranda.org> added the comment:

(For anyone wondering about the hold-up on this bug, I ended up switching to 
Ubuntu. Not to worry, I now have Python 3 building in XP under VirtualBox -- 
which is further than I ever got with my broken Vista install :-/ It seems to 
behave identically to native XP as far as this bug is concerned.)

Victor STINNER wrote:
> The question is now how to integrate WriteConsoleW() into Python without 
> breaking the API, for example:
> - Should sys.stdout be a TextIOWrapper or not?

It pretty much has to be a TextIOWrapper for compatibility. Also it's easier to 
implement it that way, because the text stream object has to be able to fall 
back to using the buffer if the fd is redirected.

> - Should sys.stdout.fileno() returns 1 or raise an error?

Return sys.stdout.buffer.fileno(), which is 1 unless redirected.

This is the Right Thing because in Windows, fds are an abstraction of the C 
runtime library, and the C runtime allows an fd to be associated with a 
console. In that case, from the application's point of view it is still writing 
to the same fd. In fact, we'd be implementing this by calling the WriteConsoleW 
win32 API directly in order to avoid bugs in the CRT's Unicode support, but 
that's an implementation detail.

> - What about sys.stdout.buffer: should sys.stdout.buffer.write() calls 
> WriteConsoleA() or sys.stdout should not have a buffer attribute?

I was thinking that sys.std{out,err}.buffer would still be set up exactly as 
they are now. Then if an app writes to that buffer, it will get interleaved 
with any writes via the text stream. (The writes to the buffer go to the 
underlying fd, which probably ends up calling WriteFile at the win32 level.)

> I think that many modules and programs now rely on sys.stdout.buffer to write 
> directly bytes into stdout. There is at least python -m base64.

That would just work. The only caveat would be that if you write a partial line 
to the buffer object (or if you set the buffer object to be fully buffered and 
write to it), and then write to the text stream, the buffer wouldn't be flushed 
before the text is written. I think that is fine as long as it is documented.

If an app sets the .buffer attribute of sys.std{out,err}, it would fall back to 
using that buffer in the same way as when the fd is redirected.

> - Should we use ReadConsoleW() for stdin?

Yes. I'll probably start with a patch that just handles std{out,err}, though.

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue1602>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to