Platform specific behavior of multiple carriage returns.

Answered

When you attempt to print lines containing two carriage returns it will clear the entire line but not consistently across platforms . I discovered it while looking into some odd behavior of the CSV library that gave me such lines.

The following code when run PyCharm will clear the double carriage return line on every example in windows and only on the character by character example on mac. Running it on powershell, cmd, bash or PyCharm with "emulate terminal in output console" will produce the expected results and print the lines without issue. 

import sys
import time


line = 'Here is a line with a unix style new line ending \n'
line += 'lets try a new line with a windows carriage return \r\n'
line += 'this is a new line with a extraneous carriage return. \r\r\n'
line += 'this is the last line with a unix style ending \n '

print('** write with stdout')
sys.stdout.write(line)
print('** write character by character')
for char in line:
time.sleep(.01)
sys.stdout.write(char)
sys.stdout.flush()
print('** write with print')
print(line)

 

I guess the question is. Why would a second carriage return clear the output in PyCharm console rather than returning the cursor to the start? And is the implementation different in pycharm in Mac vs PC or is it the underlying architecture? Just trying to understand the cause of this. Thanks!

PC Details:

Python- 3.6.2

PyCharmCE2018.1PyCharm 2018.1 (Community Edition)
Build #PC-181.4203.547, built on March 26, 2018
JRE: 1.8.0_152-release-1136-b20 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Windows 10 10.0

Mac Details:

Python- 3.6.5

PyCharm 2018.1.4 (Community Edition)
Build #PC-181.5087.37, built on May 24, 2018
JRE: 1.8.0_152-release-1136-b39 x86_64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.12.6

 

1 comment

Hi,

I am able to reproduce it. Here is a bug report in our issue tracker about it https://youtrack.jetbrains.com/issue/PY-33627, please follow it for future updates.

0

Please sign in to leave a comment.