Interactive Python 3.5+ sys.stdout.write() AND sys.stderr.write() bug?

(Apologies for any typos.)
OSX 10.12.3 AND Windows 10.
This is for the serious Python experts on at least 3.5.x and above...
In script format sys.stdout.write() AND sys.stderr.write() seems to work correctly.
Have I found a serious bug in the interactive sys.stdout.write() AND sys.stderr.write() functions in at least Python 3.5.x ?
This is the same in Windows 10 using Python 3.6.0 too, though not tried it in Linux as yet but WIndows 10 and OSX 10.12.3 should suffice.

# echo.py

import sys

echo=sys.stdout.write

newline="\n"
char="!"
num=1
strng1="Python Version "
strng2=".4.0 for the AMIGA to 3.5.x"

echo("This works from %s%u%s on any platform%c%c" %(strng1, num, strng2, char, newline))
echo("A continuous ")
echo("line of 'echo's ")
echo("to show that no newline occurs, ")
echo("until now.\n")

# sys.stdout.flush()
Last login: Sat Mar  4 19:25:37 on ttys000
AMIGA:amiga~> cd Desktop/Code/Python
AMIGA:amiga~/Desktop/Code/Python> python
Python 2.7.10 (default, Jul 30 2016, 19:40:32) 
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> exec(open("echo.py").read())
This works from Python Version 1.4.0 for the AMIGA to 3.5.x on any platform!
A continuous line of 'echo's to show that no newline occurs, until now.
>>> import sys
>>> echo=sys.stdout.write
>>> echo("\tHello World!\n")
	Hello World!
>>> sys.exit()
AMIGA:amiga~/Desktop/Code/Python> python3.5
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 26 2016, 10:47:25) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> exec(open("echo.py").read())
This works from Python Version 1.4.0 for the AMIGA to 3.5.x on any platform!
A continuous line of 'echo's to show that no newline occurs, until now.
>>> import sys
>>> echo=sys.stdout.write
>>> echo("\tHello World!\n")
	Hello World!
14
>>> sys.exit()
AMIGA:amiga~/Desktop/Code/Python> _

Where is the '14' coming from?
Well, it looks like it is coming from 'sys.stderr' and corresponds to the length of any string; see below.

Note that the code below is all longhand and my 'echo' does not exist.

Last login: Sat Mar  4 21:13:51 on ttys000
AMIGA:amiga~> python3.5
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 26 2016, 10:47:25) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.stdout.write("Hello World!\n")
Hello World!
13
>>> sys.stderr.write("Hello World!\n")
Hello World!
13
>>> ERROR=sys.stderr.write("Hello World!\n")
Hello World!
>>> print(ERROR)
13
>>> sys.stdout.write("Hello!\n")
Hello!
7
>>> sys.stderr.write("Hello!\n")
Hello!
7
>>> ERROR=sys.stderr.write("Hello!\n")
Hello!
>>> print(ERROR)
7
>>> sys.stdout.write("\n")

1
>>> sys.stderr.write("\n")

1
>>> ERROR=sys.stderr.write("\n")

>>> print(ERROR)
1
>>> sys.stderr.write("")
0
>>> sys.stdout.write("")
0
>>> ERROR=sys.stderr.write("")
>>> print(ERROR)
0
>>> _

Is this a bug or am I missing something?
Can others try this out on various platforms too just to put my sanity at rest. ;o)

After searching the 3.5.x and 3.6.0 documentation and finding absolutely nothing about this someone pointed me to the Python builtin 'help()' function. I never even considered this.
So this is the response:-

Help on built-in function write:

write(text, /) method of _io.TextIOWrapper instance
    Write string to stream.
    Returns the number of characters written (which is always equal to
    the length of the string).

So it is the 'write' attribute that causes this event and IS NOT an error.
If this number is not stored into a _variable_ it is printed to 'stdout'.

Last login: Tue Mar  7 08:59:58 on ttys000
AMIGA:amiga~> python3.5
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 26 2016, 10:47:25) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> import sys
>>> TEXT="Some string.\n"
>>> sys.stdout.write(TEXT)
Some string.
13
>>> help(sys.stdout.write)

>>> STRLENGTH=sys.stdout.write(TEXT)
Some string.
>>> print(STRLENGTH)
13
>>> len(TEXT)
13
>>> exit()
AMIGA:amiga~> _

Now solved and my mind is at rest.

1 Like

Thanks. I'd noticed Python did that but had no idea why.