大家好,我想在win7 x64的python控制台程序中禁用键盘ctrl-c。我的脚本使用winpexect来运行一个运行几个小时的外部程序。我已经试过了
signal.signal(signal.SIGINT,signal.SIG_IGN)
它在我遇到 winpexpect 部分之前工作,如果 winpexpect 产生的进程已经开始,那么我按 ctrl-c 。发生以下错误。
File "xxx.py", line 605, in myFunc
i = thread.expect_list(cpl,timeout=timeout)
File "C:\Python27\lib\site-packages\winpexpect-1.6-py2.7.egg\pexpect.py", line
1325, in expect_list
return self.expect_loop(searcher_re(pattern_list), timeout, searchwindowsize
)
File "C:\Python27\lib\site-packages\winpexpect-1.6-py2.7.egg\pexpect.py", line
1378, in expect_loop
c = self.read_nonblocking (self.maxread, timeout)
File "C:\Python27\lib\site-packages\winpexpect-1.6-py2.7.egg\winpexpect.py", l
ine 955, in read_nonblocking
handle, status, data = self.child_output.get(timeout=timeout)
File "C:\Python27\lib\Queue.py", line 177, in get
self.not_empty.wait(remaining)
File "C:\Python27\lib\threading.py", line 358, in wait
_sleep(delay)
IOError: [Errno 4] Interrupted function call
我检查了winpexpect源,它在扫描程序的输出时捕获了这个异常,有没有办法告诉它忽略这个异常并在我的主python脚本中处理它?(必须更改 winpexpect 代码?)
所以换句话说,问题可能是“如何忽略模块中捕获的异常并重新引发它,以便外部代码可以稍后捕获它?”