Disable DSUSP in Python

The OSX user sent an error that CTRL+ Ycauses the python terminal application to be suspended through dsusp , which will send SIGTSTP when the Python program tries to read on stdin. Code below to solve the problem: ( 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)
  • How can this function be detected (dsusp)? Is there any heuristic I could use based on os.uname()or similar?
  • termios.VDSUSPdoesn't even exist on systems that have it. Is there a reason it is missing?
  • How common is this behavior when it turns off? Programs using readline seem to ignore CTRL+ Yon OSX, so they are at least reasonably common. I added stty dsusp undefto my .bashrc for a long time, so I did not notice it.

To see this paused behavior, run catand type CTRL+ Y Returnin OSX or something else with this feature.

$ cat
^Y

[1]+ Stopped             cat
$ fg
cat
cat: stdin: Resource temporarily unavailable
+4
source share
1 answer

, , DSUSP. , , issue 7695 Python bug tracker. , VDSUSP termios 3.4.


Glibc,

: int VDSUSP

DSUSP . termios.c_cc[VDSUSP] .

DSUSP (suspend) , (. " " ). SIGTSTP, SUSP, - , . DSUSP; BSD- ( GNU/Hurd).


, , VDSUSP termios - 3.4;

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

BSD X; Hurd - , , ( , BSD).

:

import subprocess
import time
from distutils.spawn import find_executable

def exec_cmd(*args):
    p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    stdout, _ = p.communicate()
    return stdout

if find_executable('stty'):
    modesave = exec_cmd('stty', '-g').strip()
    exec_cmd('stty', 'dsusp', 'undef')
    print("disabled ctrl-y")
    time.sleep(2)
    print("enabled ctrl-y")
    exec_cmd('stty', modesave)
    time.sleep(2)
    print("exiting")

, Linux, stty -g .. - POSIX.

+3

All Articles