If I run the following program (using Python 3.8.3 on a Windows 10 laptop): import sys, time for i in range(1,11): sys.stdout.write('\r%d' % i) time.sleep(1) As intended, it displays '1', replacing it at 1-second intervals with '2', '3' ... '10'. Now run the same code inside the REPL: Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:20:19) [MSC v.1925 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information.
import sys, time for i in range(1,11): ... sys.stdout.write('\r%d' % i) ... time.sleep(1) ... 12 22 32 42 52 62 72 82 92 103
It appears that the requested characters are output, *followed by* the number of characters output (which is the value returned by sys.stdout.write) and a newline. Surely this is not the intended behaviour. sys.stderr behaves the same as sys.stdout. Python 2 does NOT exhibit this behaviour: Python 2.7.18 (v2.7.18:8d21aa21f2, Apr 20 2020, 13:19:08) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information.
import sys, time for i in range(1,11): ... sys.stdout.write('\r%d' % i) ... time.sleep(1) ... 10>>> # displays '1', '2' ... '10' as intended.
Best wishes Rob Cliffe
Now run the same code inside the REPL:
Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:20:19) [MSC v.1925 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information.
import sys, time for i in range(1,11): ... sys.stdout.write('\r%d' % i) ... time.sleep(1) ... 12 22 32 42 52 62 72 82 92 103
It appears that the requested characters are output, *followed by* the number of characters output (which is the value returned by sys.stdout.write) and a newline. Surely this is not the intended behaviour. sys.stderr behaves the same as sys.stdout.
Why not? I suppose it's intended this way. A behavior change like this does not happen by accident.
for i in range(3): ... (lambda: 2)() ... 2 2 2
On 6/12/20 4:01 AM, Rob Cliffe via Python-Dev wrote:
If I run the following program (using Python 3.8.3 on a Windows 10 laptop):
import sys, time for i in range(1,11): sys.stdout.write('\r%d' % i) time.sleep(1)
As intended, it displays '1', replacing it at 1-second intervals with '2', '3' ... '10'.
Now run the same code inside the REPL:
Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:20:19) [MSC v.1925 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information.
import sys, time for i in range(1,11): ... sys.stdout.write('\r%d' % i) ... time.sleep(1) ... 12 22 32 42 52 62 72 82 92 103
It appears that the requested characters are output, *followed by* the number of characters output (which is the value returned by sys.stdout.write) and a newline. Surely this is not the intended behaviour. sys.stderr behaves the same as sys.stdout.
Python 2 does NOT exhibit this behaviour:
Python 2.7.18 (v2.7.18:8d21aa21f2, Apr 20 2020, 13:19:08) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information.
import sys, time for i in range(1,11): ... sys.stdout.write('\r%d' % i) ... time.sleep(1) ... 10>>> # displays '1', '2' ... '10' as intended.
Best wishes Rob Cliffe Is that what the REPL is defined to do? print the results of each statement (unless it is None), so when working on it you don't need to say print(...) everywhere.
-- Richard Damon
On 12.06.2020 11:01, Rob Cliffe via Python-Dev wrote:
If I run the following program (using Python 3.8.3 on a Windows 10 laptop):
import sys, time for i in range(1,11): sys.stdout.write('\r%d' % i) time.sleep(1)
As intended, it displays '1', replacing it at 1-second intervals with '2', '3' ... '10'.
Now run the same code inside the REPL:
Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:20:19) [MSC v.1925 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information.
import sys, time for i in range(1,11): ... sys.stdout.write('\r%d' % i) ... time.sleep(1) ... 12 22 32 42 52 62 72 82 92 103
It appears that the requested characters are output, *followed by* the number of characters output (which is the value returned by sys.stdout.write) and a newline. Surely this is not the intended behaviour. sys.stderr behaves the same as sys.stdout.
3.7.4 win64 works as expected (i.e. prints and overwrites only the numbers) and I see nothing relevant in https://docs.python.org/3/whatsnew/3.8.html So I'd say this is a bug.
Python 2 does NOT exhibit this behaviour:
Python 2.7.18 (v2.7.18:8d21aa21f2, Apr 20 2020, 13:19:08) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information.
import sys, time for i in range(1,11): ... sys.stdout.write('\r%d' % i) ... time.sleep(1) ... 10>>> # displays '1', '2' ... '10' as intended.
Best wishes Rob Cliffe _______________________________________________ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-leave@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/T3BVZMBL... Code of Conduct: http://python.org/psf/codeofconduct/ -- Regards, Ivan
On 2020-06-15 15:26, Ivan Pozdeev via Python-Dev wrote:
On 12.06.2020 11:01, Rob Cliffe via Python-Dev wrote:
If I run the following program (using Python 3.8.3 on a Windows 10 laptop):
import sys, time for i in range(1,11): sys.stdout.write('\r%d' % i) time.sleep(1)
As intended, it displays '1', replacing it at 1-second intervals with '2', '3' ... '10'.
Now run the same code inside the REPL:
Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:20:19) [MSC v.1925 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information.
import sys, time for i in range(1,11): ... sys.stdout.write('\r%d' % i) ... time.sleep(1) ... 12 22 32 42 52 62 72 82 92 103
It appears that the requested characters are output, *followed by* the number of characters output (which is the value returned by sys.stdout.write) and a newline. Surely this is not the intended behaviour. sys.stderr behaves the same as sys.stdout.
3.7.4 win64 works as expected (i.e. prints and overwrites only the numbers) and I see nothing relevant in https://docs.python.org/3/whatsnew/3.8.html
So I'd say this is a bug.
Python 2 does NOT exhibit this behaviour:
Python 2.7.18 (v2.7.18:8d21aa21f2, Apr 20 2020, 13:19:08) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information.
import sys, time for i in range(1,11): ... sys.stdout.write('\r%d' % i) ... time.sleep(1) ... 10>>> # displays '1', '2' ... '10' as intended.
Best wishes Rob Cliffe _______________________________________________ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-leave@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/T3BVZMBL... Code of Conduct: http://python.org/psf/codeofconduct/ -- Regards, Ivan
Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-leave@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/6BS3GXCS... Code of Conduct: http://python.org/psf/codeofconduct/
I just tested with 3.7.3 and got the same results as originally described. Is there something different about 3.7.4 on win7? Python 3.7.3 (default, Dec 20 2019, 18:57:59) [GCC 8.3.0] on linux Type "help", "copyright", "credits" or "license" for more information.
import sys import time for i in range(1,11): ... sys.stdout.write('\r%d' % i) ... time.sleep(1) ... 12 22 32 42 52 62 72 82 92 103 (I also tested with 3.8.3 with the same results)
Joseph J
On Fri, Jun 12, 2020 at 09:01:33AM +0100, Rob Cliffe via Python-Dev wrote:
If I run the following program (using Python 3.8.3 on a Windows 10 laptop):
import sys, time for i in range(1,11): sys.stdout.write('\r%d' % i)
In Python 2, the 'write()` method returns None, which is suppressed in the REPR. In Python 3, the `write` method returns the number of bytes (or characters, I forget which...) actually written, which is not suppressed. I've been bitten by this myself, forgetting that in a script any result not bound to a variable gets silently thrown away, but in the REPL it gets printed.
It appears that the requested characters are output, *followed by* the number of characters output (which is the value returned by sys.stdout.write) and a newline. Surely this is not the intended behaviour.
Of course it is. The whole point of the REPL is to evaluate an expression and have the result printed. (That's the P in REPL :-) `stdout.write(...)` is an expression that returns a value, so the REPL prints it. -- Steven
On Mon, Jun 15, 2020 at 8:31 PM Steven D'Aprano
On Fri, Jun 12, 2020 at 09:01:33AM +0100, Rob Cliffe via Python-Dev wrote:
It appears that the requested characters are output, *followed by* the number of characters output (which is the value returned by sys.stdout.write) and a newline. Surely this is not the intended behaviour.
Of course it is. The whole point of the REPL is to evaluate an expression and have the result printed. (That's the P in REPL :-)
`stdout.write(...)` is an expression that returns a value, so the REPL prints it.
The expected behavior, at least for me until I read this thread, was that the REPL would print the result of a *bare* expression, meaning when the complete statement entered is an expression when taken as a whole. In other words, I would expect the following input and output in Python 3:
sys.stdout.write('\r1') 12 for i in range(1, 11): ... sys.stdout.write('\r%d' % i) ... time.sleep(1) 10>>>
because the first statement is, as a whole, a single expression and thus should print its result, but a for statement and block is not a single expression taken as a whole and therefore should not print intermediate (or final) results.
On 16/06/20 12:20 pm, Steven D'Aprano wrote:
The whole point of the REPL is to evaluate an expression and have the result printed. (That's the P in REPL :-)
Still, it's a bit surprising that it prints results of expressions within a compound statement, not just at the top level. -- Greg
On 16 Jun 2020, at 08:51, Greg Ewing
wrote: On 16/06/20 12:20 pm, Steven D'Aprano wrote:
The whole point of the REPL is to evaluate an expression and have the result printed. (That's the P in REPL :-)
Still, it's a bit surprising that it prints results of expressions within a compound statement, not just at the top level.
For what that’s worth, 2.7 seems to have the same behaviour, every statement with a non-None result gets echoed even if it is not the top level statement e.g. Python 2.7.17 (default, April 15 2020, 17:20:14) [GCC 7.5.0] on linux2 Type “help”, “copyright”, “credits” or “license” for more information.
for i in range(3): i ... 0 1 2
On 16.06.2020 1:40, Joseph Jenne via Python-Dev wrote:
On 2020-06-15 15:26, Ivan Pozdeev via Python-Dev wrote:
On 12.06.2020 11:01, Rob Cliffe via Python-Dev wrote:
If I run the following program (using Python 3.8.3 on a Windows 10 laptop):
import sys, time for i in range(1,11): sys.stdout.write('\r%d' % i) time.sleep(1)
As intended, it displays '1', replacing it at 1-second intervals with '2', '3' ... '10'.
Now run the same code inside the REPL:
Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:20:19) [MSC v.1925 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information.
import sys, time for i in range(1,11): ... sys.stdout.write('\r%d' % i) ... time.sleep(1) ... 12 22 32 42 52 62 72 82 92 103
It appears that the requested characters are output, *followed by* the number of characters output (which is the value returned by sys.stdout.write) and a newline. Surely this is not the intended behaviour. sys.stderr behaves the same as sys.stdout.
3.7.4 win64 works as expected (i.e. prints and overwrites only the numbers) and I see nothing relevant in https://docs.python.org/3/whatsnew/3.8.html
So I'd say this is a bug.
Python 2 does NOT exhibit this behaviour:
Python 2.7.18 (v2.7.18:8d21aa21f2, Apr 20 2020, 13:19:08) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information.
import sys, time for i in range(1,11): ... sys.stdout.write('\r%d' % i) ... time.sleep(1) ... 10>>> # displays '1', '2' ... '10' as intended.
Best wishes Rob Cliffe _______________________________________________ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-leave@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/T3BVZMBL... Code of Conduct: http://python.org/psf/codeofconduct/ -- Regards, Ivan
Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-leave@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/6BS3GXCS... Code of Conduct: http://python.org/psf/codeofconduct/
I just tested with 3.7.3 and got the same results as originally described. Is there something different about 3.7.4 on win7?
"Something different" is that I ran this in IPython :\
Python 3.7.3 (default, Dec 20 2019, 18:57:59) [GCC 8.3.0] on linux Type "help", "copyright", "credits" or "license" for more information.
import sys import time for i in range(1,11): ... sys.stdout.write('\r%d' % i) ... time.sleep(1) ... 12 22 32 42 52 62 72 82 92 103 (I also tested with 3.8.3 with the same results)
Joseph J _______________________________________________ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-leave@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/TOSIE4NB... Code of Conduct: http://python.org/psf/codeofconduct/ -- Regards, Ivan
participants (9)
-
Greg Ewing
-
Ivan Pozdeev
-
Jonathan Goble
-
Joseph Jenne
-
Richard Damon
-
Rob Cliffe
-
Stefan Ring
-
Steven D'Aprano
-
Xavier Morel