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


 
Thread Tools Search this Thread
Top Forums Programming Interactive Python 3.5+ sys.stdout.write() AND sys.stderr.write() bug?
# 1  
Old 03-04-2017
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.

Code:
# 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()

Code:
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.
Code:
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)
# 2  
Old 03-07-2017
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:-
Code:
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'.
Code:
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.
This User Gave Thanks to wisecracker For This Post:
# 3  
Old 03-07-2017
Thanks. I'd noticed Python did that but had no idea why.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Sed: couldn't write 1378 items to stdout: No space left on device

Hi , In file first line start with "",when trying to remove using sed i am getting the below error .Please advise sed -e 's///g' -e 's/$]//g' segment1.txt >>segment_m1 sed: couldn't write 1378 items to stdout: No space left on device Thanks, Mohan (1 Reply)
Discussion started by: mohan705
1 Replies

2. Solaris

SMF in Solaris wont write to stderr/stdout ?

Hi, I got a process (c written) that we usually run this way : EmsChkQu >> /EMS/log/EmsChkQu.log 2>&1 When trying to use it as a service I defined it this way : <?xml version='1.0'?> <!DOCTYPE service_bundle SYSTEM '/usr/share/lib/xml/dtd/service_bundle.dtd.1'> <service_bundle... (6 Replies)
Discussion started by: zionassedo
6 Replies

3. Shell Programming and Scripting

Have each subshell write stderr and stdout to its own logfile

Hello, As stated in the title, I do some hacked parallel processing by running multiple instances of bash scripts, each in their own subshell. The code looks like this, # launch one batch-train script in background for each value in fold group list for FOLD_GROUP in "${FOLD_GROUP_LIST}" do ... (5 Replies)
Discussion started by: LMHmedchem
5 Replies

4. UNIX and Linux Applications

How to write automated interactive shell script?

Hello everyone, I just want to write a shell script for automatic feeding the username and password prompts when running my commands, I tried this one but it did not work. Please help me for any way out. #!/bin/bash #!/usr/bin/expect cd ~/workspace/mimosanetworks_mimosa-nms ls -ltr ... (5 Replies)
Discussion started by: sandy-sm
5 Replies

5. Programming

read and write stdin/stdout in unix

Hi, i am using the below program to read from the standard input or to write to standard out put. i know that using highlevel functions this can be done better than what i have done here. i just want to know is there any other method by which i find the exact number of characters ( this... (3 Replies)
Discussion started by: MrUser
3 Replies

6. IP Networking

read/write,write/write lock with smbclient fails

Hi, We have smb client running on two of the linux boxes and smb server on another linux system. During a backup operation which uses smb, read of a file was allowed while write to the same file was going on.Also simultaneous writes to the same file were allowed.Following are the settings in the... (1 Reply)
Discussion started by: swatidas11
1 Replies

7. Homework & Coursework Questions

How to write script that behaves both in interactive and non interactive mode

Q. Write a script that behaves both in interactive and non interactive mode. When no arguments are supplied it picks up each C program from the directory and prints first 10 lines. It then prompts for deletion of the file. If user supplies arguments with the script , then it works on those files... (8 Replies)
Discussion started by: rits
8 Replies

8. Shell Programming and Scripting

how to write a shell script to login to a system which is interactive.

Can anybody help me to write a shell script to login interactive system once u open a connection using telnet it will ask for USERCODE: PASSWORD: DOMAIN: (1 Reply)
Discussion started by: sudhakaryadav
1 Replies

9. Shell Programming and Scripting

Cisco TCL script write stdout to tftpboot

I want to log the following stdout: router(config)#scripting tcl init tftp://192.168.1.1/remote-ping.tcl ===================================== tclsh proc rping {} { foreach address { 192.168.1.1 192.168.2.1 } { ping $address re 50 si 1500 } ===================================== ... (0 Replies)
Discussion started by: research3
0 Replies

10. Shell Programming and Scripting

how to write stderr in a subroutine log file?..

perl 5.6.1 in win2000: my program uses a subroutine which will write log file. i will call that sub routine like this &log_message("the file $inputfile opened"); what should i do to write the stderr on the same log file itself.. i tried to pass the stderr to the subroutine like ... (1 Reply)
Discussion started by: sekar sundaram
1 Replies
Login or Register to Ask a Question