10

I've found that

input('some\x00 text')

will prompt for some instead of some text.

From sources, I've figured out that this function uses C function PyOS_Readline, which ignores everything in prompt after NULL byte.

From PyOS_StdioReadline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt):

fprintf(stderr, "%s", prompt);

https://github.com/python/cpython/blob/3.6/Python/bltinmodule.c#L1989 https://github.com/python/cpython/blob/3.6/Parser/myreadline.c#L251

Is this a bug or there is a reason for that?

Issue: http://bugs.python.org/issue30431

Raymond Hettinger
  • 182,864
  • 54
  • 321
  • 419
  • Most like it is a bug. – ivand58 May 22 '17 at 13:24
  • 2
    It's calling `readline`, and `readline` itself uses the C NUL-terminated string data type, so there is little else Python *could* do, other than completely re-coding `readline`. – torek May 22 '17 at 13:26
  • Thank you @torek. I edited post – Kostya Cholak May 22 '17 at 13:40
  • since , you can not expect any chars (data) after a NULL byte in almost all use cases , it does not seems to have a bug , also considering that why anyone wants to read after knowing that the NULL character has been reached ! might be a waste of computation for almost all use cases except some like this , correct me if I m wrong ? – tom May 22 '17 at 13:44
  • 4
    As far as I know in Python NULL byte is not terminating, that is why input is supposed to print whole string – Kostya Cholak May 22 '17 at 13:46
  • 2
    For example `print('some\0 text')` will print `some text` – Kostya Cholak May 22 '17 at 13:46
  • 3
    @Костя Чолак, you miss torek's point. It isn't a terminating character in Python, but Python is calling `readline()` and `readline()` is written in C, not in Python, so the C rule applies. Python passes the entire string `"some\x00 text"` to `readline()`, and then `readline()` truncates it. – BoarGules May 22 '17 at 14:25
  • 1
    Yes, I see. I have no troubles with understanding what goes on. But I'm not sure if it is ok to be so. – Kostya Cholak May 22 '17 at 14:29
  • 2
    Created issue: http://bugs.python.org/issue30431 – Kostya Cholak May 22 '17 at 15:10
  • @torek re-code `readline`? Why could Python not `print` the prompt and call `readline` with the empty string? – timgeb May 22 '17 at 18:10
  • 4
    @timgeb: The OS-supplied `readline` must know the prompt so that `readline` can re-print it whenever `readline` wants to (which is frequently). The other option for the prompt itself is for Python to strip out NULs and pass whatever remains to `readline`, but it's not at all clear to me that this is an *improvement*. – torek May 22 '17 at 18:20
  • @torek thanks for the explanation, I was not aware of the fact that `readline` could make use of the prompt multiple times. – timgeb May 22 '17 at 18:33

1 Answers1

2

The function signature pretty much requires a NUL terminated C-string, PyOS_StdioReadline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt), so there isn't much than can be done about this without changing the API and breaking interoperability with GNU readline.

Raymond Hettinger
  • 182,864
  • 54
  • 321
  • 419