单行 while 语句的奇怪之处

问题描述 投票:0回答:1

我有一段带有单行 while 和 try- except 语句的代码,其行为很奇怪。

这会在 Ctrl+C 上打印“a”:

try:
    while True:
        pass
except KeyboardInterrupt:
    print("a")

还有这个:

try:
    i = 0
    while True: pass
except KeyboardInterrupt:
    print("a")

但这不是,它会引发回溯:

try:
    while True: pass
except KeyboardInterrupt:
    print("a")

这段代码也没有:

try:
    while True: pass
    i = 0
except KeyboardInterrupt:
    print("a")
python try-catch
1个回答
0
投票

不是答案,但为了帮助诊断,3.12

dis()

未能捕捉到:

foo = """
try:
    while True: pass
except KeyboardInterrupt:
    print("a")
"""

  0           0 RESUME                   0

  2           2 NOP

  3     >>    4 JUMP_BACKWARD            1 (to 4)
        >>    6 PUSH_EXC_INFO

  4           8 LOAD_NAME                0 (KeyboardInterrupt)
             10 CHECK_EXC_MATCH
             12 POP_JUMP_IF_FALSE       11 (to 36)
             14 POP_TOP

  5          16 PUSH_NULL
             18 LOAD_NAME                1 (print)
             20 LOAD_CONST               1 ('a')
             22 CALL                     1
             30 POP_TOP
             32 POP_EXCEPT
             34 RETURN_CONST             2 (None)

  4     >>   36 RERAISE                  0
        >>   38 COPY                     3
             40 POP_EXCEPT
             42 RERAISE                  1
ExceptionTable:
  4 to 4 -> 6 [0]
  6 to 30 -> 38 [1] lasti
  36 to 36 -> 38 [1] lasti
None

然而,捕获:

bar = """
try:
    while True:
        pass
except KeyboardInterrupt:
    print("a")
"""

  0           0 RESUME                   0

  2           2 NOP

  3           4 NOP

  4     >>    6 NOP

  3           8 JUMP_BACKWARD            2 (to 6)
        >>   10 PUSH_EXC_INFO

  5          12 LOAD_NAME                0 (KeyboardInterrupt)
             14 CHECK_EXC_MATCH
             16 POP_JUMP_IF_FALSE       11 (to 40)
             18 POP_TOP

  6          20 PUSH_NULL
             22 LOAD_NAME                1 (print)
             24 LOAD_CONST               1 ('a')
             26 CALL                     1
             34 POP_TOP
             36 POP_EXCEPT
             38 RETURN_CONST             2 (None)

  5     >>   40 RERAISE                  0
        >>   42 COPY                     3
             44 POP_EXCEPT
             46 RERAISE                  1
ExceptionTable:
  4 to 8 -> 10 [0]
  10 to 34 -> 42 [1] lasti
  40 to 40 -> 42 [1] lasti
None

代码块之间的区别是

bar
在新行上传递。反汇编的区别是两个额外的
NOP
语句。我假设这是“无操作”。

© www.soinside.com 2019 - 2024. All rights reserved.