The UNIX and Linux Forums
Login or Register to Ask a Question and Join Our Community
Search Forums
Tag Search
Advanced Search
Quick Links
Contact Us
Forum Rules
Today's Posts
FAQ
Pictures & Albums
All Albums
Miscellaneous
What is My IP
Whois
Mark Forums Read
Programming
Interactive Python 3.5+ sys.stdout.write() AND sys.stderr.write() bug?
Tags
programming, solved
Login to Discuss or Reply to this Discussion in Our Community
Thread Tools
Search this Thread
Top Forums
Programming
Interactive Python 3.5+ sys.stdout.write() AND sys.stderr.write() bug?
#
1
03-04-2017
wisecracker
Registered User
1,709,
666
Join Date: Jan 2013
Last Activity: 20 May 2020, 1:43 PM EDT
Location: Loughborough
Posts: 1,709
Thanks Given: 838
Thanked 666 Times in 467 Posts
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)
wisecracker
View Public Profile for wisecracker
Find all posts by wisecracker
#
2
03-07-2017
wisecracker
Registered User
1,709,
666
Join Date: Jan 2013
Last Activity: 20 May 2020, 1:43 PM EDT
Location: Loughborough
Posts: 1,709
Thanks Given: 838
Thanked 666 Times in 467 Posts
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:
Corona688
wisecracker
View Public Profile for wisecracker
Find all posts by wisecracker
#
3
03-07-2017
Corona688
Registered User
23,310,
4,623
Join Date: Aug 2005
Last Activity: 7 July 2020, 11:47 AM EDT
Location: Saskatchewan
Posts: 23,310
Thanks Given: 1,331
Thanked 4,623 Times in 4,217 Posts
Thanks. I'd noticed Python did that but had no idea why.
Corona688
View Public Profile for Corona688
Visit Corona688's homepage!
Find all posts by Corona688
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 :
Login or Register to Ask a Question
Member Badges and Information Modal
Featured Tech Videos
All times are GMT -4. The time now is 05:06 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy
|