为什么我不能跳出这个 itertools 无限循环?

Why can't I break out of this itertools infinite loop?

在REPL中,我们通常可以用一个sigint中断一个无限循环,即ctrl+c,并在口译员。

>>> while True: pass
... 
^CTraceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyboardInterrupt
>>>

但是在这个循环中,中断好像被阻塞了,必须杀掉父进程才能逃脱。

>>> *x, = itertools.repeat('x')
^C^C^C^C^C^C^C^C^\^\^\^\^\^Z^Z^Z^Z

这是为什么?

在每个 Python 指令后检查 KeyboardInterruptitertools.repeat 并且元组生成在 C 代码中处理。中断是事后处理的,即never。