tests/logexceptions.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:
35190
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     1
# logexceptions.py - Write files containing info about Mercurial exceptions
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     2
#
46819
d4ba4d51f85f contributor: change mentions of mpm to olivia
Raphaël Gomès <rgomes@octobus.net>
parents: 43076
diff changeset
     3
# Copyright 2017 Olivia Mackall <olivia@selenic.com>
35190
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     4
#
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     5
# This software may be used and distributed according to the terms of the
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     6
# GNU General Public License version 2 or any later version.
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     7
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     8
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     9
import inspect
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    10
import os
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    11
import sys
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    12
import traceback
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    13
import uuid
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    14
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    15
from mercurial import (
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    16
    dispatch,
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    17
    extensions,
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    18
)
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    19
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 36037
diff changeset
    20
35190
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    21
def handleexception(orig, ui):
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    22
    res = orig(ui)
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    23
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    24
    if not ui.environ.get(b'HGEXCEPTIONSDIR'):
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    25
        return res
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    26
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 36037
diff changeset
    27
    dest = os.path.join(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 36037
diff changeset
    28
        ui.environ[b'HGEXCEPTIONSDIR'], str(uuid.uuid4()).encode('ascii')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 36037
diff changeset
    29
    )
35190
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    30
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    31
    exc_type, exc_value, exc_tb = sys.exc_info()
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    32
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    33
    stack = []
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    34
    tb = exc_tb
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    35
    while tb:
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    36
        stack.append(tb)
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    37
        tb = tb.tb_next
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    38
    stack.reverse()
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    39
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    40
    hgframe = 'unknown'
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    41
    hgline = 'unknown'
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    42
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    43
    # Find the first Mercurial frame in the stack.
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    44
    for tb in stack:
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    45
        mod = inspect.getmodule(tb)
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    46
        if not mod.__name__.startswith(('hg', 'mercurial')):
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    47
            continue
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    48
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    49
        frame = tb.tb_frame
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    50
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    51
        try:
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    52
            with open(inspect.getsourcefile(tb), 'r') as fh:
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    53
                hgline = fh.readlines()[frame.f_lineno - 1].strip()
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    54
        except (IndexError, OSError):
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    55
            pass
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    56
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    57
        hgframe = '%s:%d' % (frame.f_code.co_filename, frame.f_lineno)
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    58
        break
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    59
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    60
    primary = traceback.extract_tb(exc_tb)[-1]
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    61
    primaryframe = '%s:%d' % (primary.filename, primary.lineno)
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    62
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    63
    with open(dest, 'wb') as fh:
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    64
        parts = [
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    65
            str(exc_value),
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    66
            primaryframe,
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    67
            hgframe,
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    68
            hgline,
36037
8de90e006c78 run-tests: report tests that exception occurred in
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35190
diff changeset
    69
            ui.environ[b'TESTNAME'].decode('utf-8', 'replace'),
35190
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    70
        ]
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    71
        fh.write(b'\0'.join(p.encode('utf-8', 'replace') for p in parts))
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    72
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 36037
diff changeset
    73
35190
bd8875b6473c run-tests: mechanism to report exceptions during test execution
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    74
def extsetup(ui):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 36037
diff changeset
    75
    extensions.wrapfunction(dispatch, 'handlecommandexception', handleexception)