contrib/showstack.py
author Manuel Jacob <me@manueljacob.de>
Mon, 11 Jul 2022 01:51:20 +0200
branchstable
changeset 49378 094a5fa3cf52
parent 48875 6000f5b25c9b
permissions -rw-r--r--
procutil: make stream detection in make_line_buffered more correct and strict In make_line_buffered(), we don’t want to wrap the stream if we know that lines get flushed to the underlying raw stream already. Previously, the heuristic was too optimistic. It assumed that any stream which is not an instance of io.BufferedIOBase doesn’t need wrapping. However, there are buffered streams that aren’t instances of io.BufferedIOBase, like Mercurial’s own winstdout. The new logic is different in two ways: First, only for the check, if unwraps any combination of WriteAllWrapper and winstdout. Second, it skips wrapping the stream only if it is an instance of io.RawIOBase (or already wrapped). If it is an instance of io.BufferedIOBase, it gets wrapped. In any other case, the function raises an exception. This ensures that, if an unknown stream is passed or we add another wrapper in the future, we don’t wrap the stream if it’s already line buffered or not wrap the stream if it’s not line buffered. In fact, this was already helpful during development of this change. Without it, I possibly would have forgot that WriteAllWrapper needs to be ignored for the check, leading to unnecessary wrapping if stdout is unbuffered. The alternative would have been to always wrap unknown streams. However, I don’t think that anyone would benefit from being less strict. We can expect streams from the standard library to be subclassing either io.RawIOBase or io.BufferedIOBase, so running Mercurial in the standard way should not regress by this change. Py2exe might replace sys.stdout and sys.stderr, but that currently breaks Mercurial anyway and also these streams don’t claim to be interactive, so this function is not called for them.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
26123
bdac264e5ed4 contrib: add showstack extension
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     1
# showstack.py - extension to dump a Python stack trace on signal
bdac264e5ed4 contrib: add showstack extension
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     2
#
bdac264e5ed4 contrib: add showstack extension
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     3
# binds to both SIGQUIT (Ctrl-\) and SIGINFO (Ctrl-T on BSDs)
41548
6dae1f31c6c9 showstack: use raw docstring
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40036
diff changeset
     4
r"""dump stack trace when receiving SIGQUIT (Ctrl-\) or SIGINFO (Ctrl-T on BSDs)
35656
c9eb92fb87b7 showstack: add an extension docstring
Boris Feld <boris.feld@octobus.net>
parents: 28522
diff changeset
     5
"""
26123
bdac264e5ed4 contrib: add showstack extension
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     6
28522
f2fe7b199bb4 showstack: use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 26123
diff changeset
     7
import signal
f2fe7b199bb4 showstack: use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 26123
diff changeset
     8
import sys
f2fe7b199bb4 showstack: use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 26123
diff changeset
     9
import traceback
26123
bdac264e5ed4 contrib: add showstack extension
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    10
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41548
diff changeset
    11
26123
bdac264e5ed4 contrib: add showstack extension
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    12
def sigshow(*args):
bdac264e5ed4 contrib: add showstack extension
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    13
    sys.stderr.write("\n")
bdac264e5ed4 contrib: add showstack extension
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    14
    traceback.print_stack(args[1], limit=10, file=sys.stderr)
bdac264e5ed4 contrib: add showstack extension
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    15
    sys.stderr.write("----\n")
bdac264e5ed4 contrib: add showstack extension
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    16
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41548
diff changeset
    17
40036
acf5dbe39478 showstack: also handle SIGALRM
Augie Fackler <augie@google.com>
parents: 35656
diff changeset
    18
def sigexit(*args):
acf5dbe39478 showstack: also handle SIGALRM
Augie Fackler <augie@google.com>
parents: 35656
diff changeset
    19
    sigshow(*args)
acf5dbe39478 showstack: also handle SIGALRM
Augie Fackler <augie@google.com>
parents: 35656
diff changeset
    20
    print('alarm!')
acf5dbe39478 showstack: also handle SIGALRM
Augie Fackler <augie@google.com>
parents: 35656
diff changeset
    21
    sys.exit(1)
acf5dbe39478 showstack: also handle SIGALRM
Augie Fackler <augie@google.com>
parents: 35656
diff changeset
    22
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41548
diff changeset
    23
26123
bdac264e5ed4 contrib: add showstack extension
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    24
def extsetup(ui):
bdac264e5ed4 contrib: add showstack extension
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    25
    signal.signal(signal.SIGQUIT, sigshow)
40036
acf5dbe39478 showstack: also handle SIGALRM
Augie Fackler <augie@google.com>
parents: 35656
diff changeset
    26
    signal.signal(signal.SIGALRM, sigexit)
26123
bdac264e5ed4 contrib: add showstack extension
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    27
    try:
bdac264e5ed4 contrib: add showstack extension
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    28
        signal.signal(signal.SIGINFO, sigshow)
bdac264e5ed4 contrib: add showstack extension
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    29
    except AttributeError:
bdac264e5ed4 contrib: add showstack extension
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    30
        pass