[issue18512] sys.stdout.write does not allow bytes in Python 3.x

2013-08-18 Thread Juan Luis Boya García

Juan Luis Boya García added the comment:

Sorry for the late response, GMail's SPAM filter ate the replies.

The main issue is sys.stdout being opened as text instead of binary. This fact 
is stated in the docs. http://docs.python.org/3/library/sys.html#sys.stdout

In any case, there are some caveats worth noting:

> You can do
>sys.stdout.buffer.write(b"hello")
This is problematic if both buffer and IOTextWrapper are used. For example:

  print("Hello", end=""); sys.stdout.buffer.write(b"World")

That line may write `WorldHello` instead of `HelloWorld` (and it does indeed, 
at least in Linux implementation).

Yes, an application should not do this in Python3, but using print() and 
writing to stdout were OK in Python2, which makes porting programs harder.

A workaround is to perform sys.stdout.flush() before sys.stdout.buffer.write().

> (from the docs)
> Using io.TextIOBase.detach(), streams can be made binary by default.
> sys.stdout = sys.stdout.detach()

This should help in cases where most output is binary, but it's worth noting 
that interactive shells (such as the builtin python or IPython) and debuggers 
(both pdb and ipdb) stop working when this is used. Also, it will probably 
break every function there that relies on sys.stdout being Unicode or binary 
depending on only the Python version.

--

___
Python tracker 
<http://bugs.python.org/issue18512>
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18512] sys.stdout.write does not allow bytes in Python 3.x

2013-07-19 Thread Juan Luis Boya García

New submission from Juan Luis Boya García:

Sometimes developers need to write text to stdout, and it's nice to have on the 
fly Unicode to UTF-8 conversion (or what matches the platform), but sometimes 
they also need to output binary blobs, like text encoded in other codifications 
than the system default, binary files, etc.

Python2 does the thing more-or-less right and allows writing both text and 
binary. I think Python3 should also accept both.

--
components: Library (Lib)
messages: 193394
nosy: ntrrgc
priority: normal
severity: normal
status: open
title: sys.stdout.write does not allow bytes in Python 3.x
type: behavior
versions: Python 3.3

___
Python tracker 
<http://bugs.python.org/issue18512>
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com