Carriage return ignored/forgotten
I am trying to run this code:
print("This text will be replaced\rWith this text")
This code outputs as:
This text will be replacedWith this text
The output should be:
With this text be replaced
I then ran this code:
import requests
code = """print('This text will be replaced\rWith this text')"""
resp = requests.post("https://emkc.org/api/v2/piston/execute", {"language": f"python", "version": "3.10.0", "files": [code]}).json()
output = "\n".join(line for line in resp['run']['output'].splitlines())
print(output)
Which gave me this output:
File "/piston/jobs/8c28a39a-0386-4f93-86f5-1e5cc2ac494e/file0.code", line 1
print("This text will be replaced
^
SyntaxError: unterminated string literal (detected at line 1)
This is an issue as I am trying to create a Python challenge for my Discord bot that contains the carriage return character.
The terminal emulator is responsible for the replacement behavior of carriage returns. This means that the expected behavior is to print out the whole string, including the carriage return.
You can see this with the following commands:
# Replacement happens
python -c 'print("This text will be replaced\rWith this text")'
# No replacement, file is 42 bytes
python -c 'print("This text will be replaced\rWith this text")' > file
du --bytes file
As for the second issue, the carriage return is interpreted before being sent to Piston so python sees a carriage return (functionally a new line) instead of \r.
You can see this by doing the following:
len("""test""")
len("""\r""")
If you want to keep the escape code intact you should escape the escape character, like so: string = """\\r"""