P3 weird sys.stdout.write()

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

P3 weird sys.stdout.write()

Jerzy Jalocha N
I've stumbled upon the following in Python 3:

Python 3.0.1+ (r301:69556, Apr 15 2009, 15:59:22)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.stdout.write("")
0
>>> sys.stdout.write("something")
something9
>>>

write() is appending the length of the string to it's output. That's
not how it worked in 2.6.

What's the reason for this? Is this intended? I couldn't find a bug
report for this.
--
http://mail.python.org/mailman/listinfo/python-list
Reply | Threaded
Open this post in threaded view
|

Re: P3 weird sys.stdout.write()

Diez B. Roggisch-2
Jerzy Jalocha N wrote:

> I've stumbled upon the following in Python 3:
>
> Python 3.0.1+ (r301:69556, Apr 15 2009, 15:59:22)
> [GCC 4.3.3] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> import sys
>>>> sys.stdout.write("")
> 0
>>>> sys.stdout.write("something")
> something9
>>>>
>
> write() is appending the length of the string to it's output. That's
> not how it worked in 2.6.
>
> What's the reason for this? Is this intended? I couldn't find a bug
> report for this.

Write returns the number of bytes written. And because you don't capture
that output into a variable, the interpreter puts it out as well.

If you do

>>> n = sys.stdout.write("")

instead, you won't see the behavior.

Diez
--
http://mail.python.org/mailman/listinfo/python-list
Reply | Threaded
Open this post in threaded view
|

Re: P3 weird sys.stdout.write()

André Roberge
In reply to this post by Jerzy Jalocha N
On Aug 24, 10:13 am, Jerzy Jalocha N <[hidden email]> wrote:

> I've stumbled upon the following in Python 3:
>
> Python 3.0.1+ (r301:69556, Apr 15 2009, 15:59:22)
> [GCC 4.3.3] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>
> >>> import sys
> >>> sys.stdout.write("")
> 0
> >>> sys.stdout.write("something")
> something9
>
> write() is appending the length of the string to it's output.


Not quite right, see below.

> That's
> not how it worked in 2.6.
>
> What's the reason for this? Is this intended? I couldn't find a bug
> report for this.

I don't know what the reason for the change, but try the following:

>>> out = sys.stdout.write("test\n")
test
>>> out
5


What you are seeing is the concatenation of the return value of the
write() method with its output.

André
--
http://mail.python.org/mailman/listinfo/python-list
Reply | Threaded
Open this post in threaded view
|

Re: P3 weird sys.stdout.write()

Jerzy Jalocha N
In reply to this post by Jerzy Jalocha N
>>> import sys
>>> n = sys.stdout.write('something')
something>>> n
9
>>>

Yes, that works as expected, now, similar to 2.6.
Thank you both, Diez and André!

-Jerzy
--
http://mail.python.org/mailman/listinfo/python-list
Reply | Threaded
Open this post in threaded view
|

Re: P3 weird sys.stdout.write()

Dave Angel
In reply to this post by Jerzy Jalocha N
Jerzy Jalocha N wrote:

> I've stumbled upon the following in Python 3:
>
> Python 3.0.1+ (r301:69556, Apr 15 2009, 15:59:22)
> [GCC 4.3.3] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>  
>>>> import sys
>>>> sys.stdout.write("")
>>>>        
> 0
>  
>>>> sys.stdout.write("something")
>>>>        
> something9
>  
>
> write() is appending the length of the string to it's output. That's
> not how it worked in 2.6.
>
> What's the reason for this? Is this intended? I couldn't find a bug
> report for this.
>
>  
(You probably should be using 3.1, but that's not your particular
problem here.)

The write() function changed in 3.0, but not in the way you're
describing.  It now (usually) has a return value, the count of the
number of characters written.  See the 3.1 docs:

file.write(/str/)

    Write a string to the file. Due to buffering, the string may not
    actually show up in the file until the flush() or close() method is
    called.

    The meaning of the return value is not defined for every file-like
    object. Some (mostly low-level) file-like objects may return the
    number of bytes actually written, others return None.

But because you're running from the interpreter, you're seeing the
return value(9), which is suppressed if it's None, which it was in
2.x.   This has nothing to do with how the language behaves in normal use.

DaveA

--
http://mail.python.org/mailman/listinfo/python-list
Reply | Threaded
Open this post in threaded view
|

Re: P3 weird sys.stdout.write()

Jerzy Jalocha N
On Mon, Aug 24, 2009 at 10:52 AM, Dave Angel<[hidden email]> wrote:
> The write() function changed in 3.0, but not in the way you're describing.
>  It now (usually) has a return value, the count of the number of characters
> written.
[...]
> But because you're running from the interpreter, you're seeing the return
> value(9), which is suppressed if it's None, which it was in 2.x.   This has
> nothing to do with how the language behaves in normal use.

This makes it much clearer!
You are right, output in a shell script is "normal", without the return value.
Thank you, Dave.

-Jerzy
--
http://mail.python.org/mailman/listinfo/python-list