From: Peter Otten on
Bill McClain wrote:

> On 2008-12-09, Peter Otten <__peter__(a)web.de> wrote:
>
>> >>> out = io.StringIO()
>> >>> print(u"hello", file=out, end=u"\n")
>> >>> out.getvalue()
>> u'hello\n'
>
> That has the benefit of working. Thank you!
>
> That can't be the intended behavior of print(), can it? Insering
> non-unicode spaces and line terminators? I thought all text was unicode
> now. Or is that only in 3.0?

Yes it's 3.0 only. I have no clear idea of the implications of using the
print() function in 2.6 yet; maybe changing the defaults for end/sep to
unicode would suffice to make it work smoothly. I will probably defer the
transition from statement to function until I move to 3.x.

One benefit of the function is that you can do things like

from functools import partial
print = partial(print, sep=u" ", end=u"\n")

Peter
From: ajaksu on
On Dec 9, 5:24 pm, Bill McClain <20080915.20.wmccl...(a)spamgourmet.com>
wrote:
> On 2008-12-09, MRAB <goo...(a)mrabarnett.plus.com> wrote:
>
> > In Python 2.x unmarked string literals are bytestrings. In Python 3.x
> > they're Unicode. The intention is to make the transition from 2.x to 3.x
> > easier by adding some features of 3.x to 2.x, but without breaking
> > backwards compatibility (not entirely successfully!).
>
> It is a bit ugly. In 2.6 StringIO won't take bytestrings, so I apply u'x'.. But
> in 3.0 u'x' will be gone and I'll have to change the code again.

Try:

from __future__ import unicode_literals
From: Bill McClain on
On 2008-12-10, ajaksu <ajaksu(a)gmail.com> wrote:
> On Dec 9, 5:24 pm, Bill McClain <20080915.20.wmccl...(a)spamgourmet.com>
> wrote:
> > On 2008-12-09, MRAB <goo...(a)mrabarnett.plus.com> wrote:
> >
> > > In Python 2.x unmarked string literals are bytestrings. In Python 3.x
> > > they're Unicode. The intention is to make the transition from 2.x to 3.x
> > > easier by adding some features of 3.x to 2.x, but without breaking
> > > backwards compatibility (not entirely successfully!).
> >
> > It is a bit ugly. In 2.6 StringIO won't take bytestrings, so I apply u'x'. But
> > in 3.0 u'x' will be gone and I'll have to change the code again.

> Try:

> from __future__ import unicode_literals

That works for:

output.write('First line.\n')

....but not for:

print('Second line.', file=output)

Maybe a combination of this and functools.partial as was suggested before. At
least the necessary edits would be at the top of the program.

-Bill
--
Sattre Press Tales of War
http://sattre-press.com/ by Lord Dunsany
info(a)sattre-press.com http://sattre-press.com/tow.html
From: Jean-Paul Calderone on
On 10 Dec 2008 11:58:37 GMT, Bill McClain <20080915.20.wmcclain(a)spamgourmet..com> wrote:
>On 2008-12-10, ajaksu <ajaksu(a)gmail.com> wrote:
>> On Dec 9, 5:24 pm, Bill McClain <20080915.20.wmccl...(a)spamgourmet.com>
>> wrote:
>> > On 2008-12-09, MRAB <goo...(a)mrabarnett.plus.com> wrote:
>> >
>> > > In Python 2.x unmarked string literals are bytestrings. In Python 3.x
>> > > they're Unicode. The intention is to make the transition from 2.x to 3.x
>> > > easier by adding some features of 3.x to 2.x, but without breaking
>> > > backwards compatibility (not entirely successfully!).
>> >
>> > It is a bit ugly. In 2.6 StringIO won't take bytestrings, so I apply u'x'. But
>> > in 3.0 u'x' will be gone and I'll have to change the code again.
>
>> Try:
>
>> from __future__ import unicode_literals
>
>That works for:
>
> output.write('First line.\n')
>
>...but not for:
>
> print('Second line.', file=output)
>
>Maybe a combination of this and functools.partial as was suggested before. At
>least the necessary edits would be at the top of the program.

See http://bugs.python.org/issue4618, there's a comment with a workaround
for this problem.

Jean-Paul
From: pruebauno on
On Dec 10, 6:58 am, Bill McClain
<20080915.20.wmccl...(a)spamgourmet.com> wrote:
> On 2008-12-10, ajaksu <aja...(a)gmail.com> wrote:
>
> > On Dec 9, 5:24 pm, Bill McClain <20080915.20.wmccl...(a)spamgourmet.com>
> > wrote:
> > > On 2008-12-09, MRAB <goo...(a)mrabarnett.plus.com> wrote:
>
> > > > In Python 2.x unmarked string literals are bytestrings. In Python 3..x
> > > > they're Unicode. The intention is to make the transition from 2.x to 3.x
> > > > easier by adding some features of 3.x to 2.x, but without breaking
> > > > backwards compatibility (not entirely successfully!).
>
> > > It is a bit ugly. In 2.6 StringIO won't take bytestrings, so I apply u'x'. But
> > > in 3.0 u'x' will be gone and I'll have to change the code again.
> > Try:
> > from __future__ import unicode_literals
>
> That works for:
>
>     output.write('First line.\n')
>
> ...but not for:
>
>    print('Second line.', file=output)
>
> Maybe a combination of this and functools.partial as was suggested before.. At
> least the necessary edits would be at the top of the program.
>
> -Bill
> --
> Sattre Press                                      Tales of Warhttp://sattre-press.com/                      by Lord Dunsany
> i...(a)sattre-press.com        http://sattre-press.com/tow.html

I think this combination might do the trick (I don't have 2.6 to test
it right now):

from __future__ import print_function
from __future__ import unicode_literals
from functools import partial
import io
print = partial(print, sep=" ", end="\n")
out = io.StringIO()
print("hello", file=out)

What puzzles me is the documentation in 2.6 and 3.0:
In 2.6 it says: "The StringIO object can accept either Unicode or 8-
bit strings". Why does it fail with old str objects then?
Why is there no documentation for StringIO in 3.0?