在Python中禁用DSUSP

2024-10-06 11:21:58 发布

您现在位置:Python中文网/ 问答频道 /正文

一个OSX用户submitted a bugCTRL+Y通过dsusp导致在python程序尝试读取stdin时发送sigtsp。下面的代码可以解决这个问题:(context

import sys
import termios
if sys.platform == 'darwin':
    attrs = termios.tcgetattr(0)
    VDSUSP = termios.VSUSP + 1
    attrs[-1][VDSUSP] = 0
    termios.tcsetattr(0, termios.TCSANOW, attrs)
  • 如何检测此功能(dsusp)?有没有一个启发我可以使用基于os.uname()或类似的方法?在
  • termios.VDSUSP不存在,即使在有它的系统上也是如此。它失踪有什么原因吗?在
  • 这种关掉它的行为有多普遍?在OSX上,使用readline的程序似乎忽略了CTRL+Y,因此这至少是相当常见的。我很久以前在我的.bashrc中添加了stty dsusp undef,所以没有注意到。在

要查看此挂起行为,请运行cat并在OSX或其他具有此功能的程序上输入CTRL+YReturn。在

^{pr2}$

Tags: 用户import程序功能stdinsysbugattrs
1条回答
网友
1楼 · 发布于 2024-10-06 11:21:58

{1}我不需要检查计算机的实际行为。 然而,在调查这个问题时,我遇到了issue 7695 in Python bug tracker。似乎只有从3.4以后,VDSUSP才在termios中可用。在


Glibc documentation,意思是

Macro: int VDSUSP

This is the subscript for the DSUSP character in the special control character array. termios.c_cc[VDSUSP] holds the character itself.

The DSUSP (suspend) character is recognized only if the implementation supports job control (see Job Control). It sends a SIGTSTP signal, like the SUSP character, but not right away—only when the program tries to read it as input. Not all systems with job control support DSUSP; only BSD-compatible systems (including GNU/Hurd systems).


因此,我建议您检查VDSUSPtermios中的存在性,它将从3.4开始一直存在;否则将返回到

if any(i in sys.platform for i in ['bsd', 'darwin']):

它应该与BSD和您的OS X相匹配;Hurd是一个很大的问号,因为我无法确定您的修复是否完全正确(我想它在所有BSD版本上都会起类似的作用)。在

更简单的方法是:

^{pr2}$

至少这不会破坏我的Linux,stty命令本身和-g等都是POSIX标准。在

相关问题 更多 >