tests/seq.py
author Manuel Jacob <me@manueljacob.de>
Mon, 11 Jul 2022 01:51:20 +0200
branchstable
changeset 49378 094a5fa3cf52
parent 49285 56f98406831b
permissions -rwxr-xr-x
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:
45830
c102b704edb5 global: use python3 in shebangs
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
     1
#!/usr/bin/env python3
24360
f554f89a2038 tests: introduce 'seq.py' as a portable replacement for 'seq'
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
     2
#
f554f89a2038 tests: introduce 'seq.py' as a portable replacement for 'seq'
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
     3
# A portable replacement for 'seq'
f554f89a2038 tests: introduce 'seq.py' as a portable replacement for 'seq'
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
     4
#
f554f89a2038 tests: introduce 'seq.py' as a portable replacement for 'seq'
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
     5
# Usage:
f554f89a2038 tests: introduce 'seq.py' as a portable replacement for 'seq'
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
     6
#   seq STOP              [1, STOP] stepping by 1
f554f89a2038 tests: introduce 'seq.py' as a portable replacement for 'seq'
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
     7
#   seq START STOP        [START, STOP] stepping by 1
f554f89a2038 tests: introduce 'seq.py' as a portable replacement for 'seq'
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
     8
#   seq START STEP STOP   [START, STOP] stepping by STEP
f554f89a2038 tests: introduce 'seq.py' as a portable replacement for 'seq'
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
     9
40773
0605726179a0 tests: apply binary mode to output in seq.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 35150
diff changeset
    10
import os
24360
f554f89a2038 tests: introduce 'seq.py' as a portable replacement for 'seq'
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    11
import sys
f554f89a2038 tests: introduce 'seq.py' as a portable replacement for 'seq'
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    12
40773
0605726179a0 tests: apply binary mode to output in seq.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 35150
diff changeset
    13
try:
0605726179a0 tests: apply binary mode to output in seq.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 35150
diff changeset
    14
    import msvcrt
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40773
diff changeset
    15
40773
0605726179a0 tests: apply binary mode to output in seq.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 35150
diff changeset
    16
    msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
0605726179a0 tests: apply binary mode to output in seq.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 35150
diff changeset
    17
    msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
0605726179a0 tests: apply binary mode to output in seq.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 35150
diff changeset
    18
    msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY)
0605726179a0 tests: apply binary mode to output in seq.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 35150
diff changeset
    19
except ImportError:
0605726179a0 tests: apply binary mode to output in seq.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 35150
diff changeset
    20
    pass
0605726179a0 tests: apply binary mode to output in seq.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 35150
diff changeset
    21
24360
f554f89a2038 tests: introduce 'seq.py' as a portable replacement for 'seq'
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    22
start = 1
f554f89a2038 tests: introduce 'seq.py' as a portable replacement for 'seq'
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    23
if len(sys.argv) > 2:
f554f89a2038 tests: introduce 'seq.py' as a portable replacement for 'seq'
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    24
    start = int(sys.argv[1])
f554f89a2038 tests: introduce 'seq.py' as a portable replacement for 'seq'
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    25
f554f89a2038 tests: introduce 'seq.py' as a portable replacement for 'seq'
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    26
step = 1
f554f89a2038 tests: introduce 'seq.py' as a portable replacement for 'seq'
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    27
if len(sys.argv) > 3:
f554f89a2038 tests: introduce 'seq.py' as a portable replacement for 'seq'
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    28
    step = int(sys.argv[2])
f554f89a2038 tests: introduce 'seq.py' as a portable replacement for 'seq'
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    29
f554f89a2038 tests: introduce 'seq.py' as a portable replacement for 'seq'
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    30
stop = int(sys.argv[-1]) + 1
f554f89a2038 tests: introduce 'seq.py' as a portable replacement for 'seq'
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    31
49285
56f98406831b py3: remove xrange() compatibility code
Manuel Jacob <me@manueljacob.de>
parents: 48875
diff changeset
    32
for i in range(start, stop, step):
28722
2cd8c3b0bd11 py3: use print_function in seq.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 28721
diff changeset
    33
    print(i)