4

Here's a simple Python program:

s = input('Name: ')

I ran the program, typed a few letters, and then pressed Ctrld:

$ python3 myscript.py
Name: myverylongna

Nothing happens, so I press Ctrld again. Nothing happens. Then I press Ctrld for the third time. Python finally exits. I observe the same behavior using Python 2.7.

I don't understand why I had to press Ctrld three times to quit Python. For comparison, I wrote a C program to ask for user input (warning: the code has many mistakes):

#include <stdlib.h>
#include <stdio.h>

/* Asks the user for string input.
 * Returns a pointer to the string entered by the user.
 * The pointer must be freed.
 */
char* input()
{
    char *s = malloc(sizeof(char));
    if (s == NULL) {
        fprintf(stderr, "Error: malloc\n");
        exit(1);
    }
    char ch;
    size_t s_len = 0;
    while((ch = (char) getchar()) != '\n' && ch != EOF) {
        s[s_len] = ch;
        s_len++;
        s = realloc(s, (s_len + 1) * sizeof(char));
        if (s == NULL) {
            fprintf(stderr, "Error: realloc\n");
            exit(1);
        }
    }
    s[s_len] = '\0';
    return s;
}

int main()
{
    printf("Name: ");
    char *s = input();
    free(s);
    return 0;
}

The C program only requires two presses of Ctrld for a similar input. Why?

Why does python need three presses of Ctrld to quit the program when the user is in the middle of entering characters at an input prompt?

(I am running Python 3.6.8 on Ubuntu 18.04)

Flux
  • 9,805
  • 5
  • 46
  • 92
  • I can't reproduce this; It takes me one `Ctrl-d` to exit the input, and possibly a second `Ctrl-d` to exit the interpreter (if I am running one). What shell are you using? (I'm running zsh 5.0.2 on Centos 7) – 0x5453 Sep 13 '19 at 15:24
  • I don't see how the comparison with C is relevant, why would you expect the number of times you have to press Ctrl-d to be the same with 2 different code snippets in different languages? – Chris_Rands Sep 13 '19 at 15:26
  • Note that help(input) mentions: "If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError. On *nix systems, readline is used if available.". So your C code is far from equivalent to your python line, esp. if readline is used by ubuntu. – Demi-Lune Sep 13 '19 at 15:27
  • 1
    if this is a practical issue for you, use Ctrl-c once – Chris_Rands Sep 13 '19 at 15:28
  • 2
    It's not relevant to the behavior you're seeing, but there are many things wrong with your C version. – jamesdlin Sep 13 '19 at 15:29
  • @0x5453 I'm running zsh 5.4.2 on Ubuntu 18.04. – Flux Sep 13 '19 at 15:29
  • @jamesdlin I'm new to C, so it would really help if you could drop a few comments about the code: https://codereview.stackexchange.com/q/228979 – Flux Sep 13 '19 at 15:41
  • 1
    Related question for `fgets()` in C: [Why do i have to input EOF 3 times when using fgets?](https://stackoverflow.com/questions/29759979/why-do-i-have-to-input-eof-3-times-when-using-fgets) – Wojciech Morawiec Sep 13 '19 at 16:57
  • 1
    Per Wojciech Morawiec: possible duplicate of [Why do i have to input EOF 3 times when using fgets?](https://stackoverflow.com/questions/29759979/why-do-i-have-to-input-eof-3-times-when-using-fgets) – Davis Herring Sep 13 '19 at 23:23
  • Note that this is arguably a bug in Python: `input` should return after the second ^D since that does count as an EOF (rather than just a short read). – Davis Herring Sep 13 '19 at 23:58

0 Answers0