Skip to content

input() recognize Carriage return (\r, 0x0D) but not Line Feed (\n, 0x0A) #9217

Open
@DeqingSun

Description

@DeqingSun

CircuitPython version

Adafruit CircuitPython 9.0.4 on 2024-04-16; Adafruit KB2040 with rp2040

Code/REPL

if supervisor.runtime.serial_bytes_available:
    data = input()
    print(data)

Behavior

On windows, enter will be encoded as \r\n (0x0D, 0x0A)
However, the input seems only recognize the \r, and the 0x0A cause the code to freeze at the input()

I've tried to send 0x0D only, the input has response.
Then I tried 0x0A, the input has no response.

Description

I tried to search why this happen on github. It seems the input is linked with mp_builtin_input in modbuiltins.c.
And it leads to readline_process_char.
It processes \r

} else if (c == '\r') {

but not \n

Additional information

It seems circuit python uses the same code as readline_process_char

The Unix port of Circuit python uses fgets to read line. And fgets actually look for \n

https://github.com/lattera/freebsd/blob/401a161083850a9a4ce916f37520c084cff1543b/lib/libc/stdio/fgets.c#L90

And python version of input() will take care \n or \r\n

https://github.com/python/cpython/blob/19d468a3ab1d57c07a4283d67d439908574aa0cc/Python/bltinmodule.c#L2317

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions