py3: make stdout line-buffered if connected to a TTY
Status messages that are to be shown on the terminal should be written to the
file descriptor before anything further is done, to keep the user updated.
One common way to achieve this is to make stdout line-buffered if it is
connected to a TTY. This is done on Python 2 (except on Windows, where libc,
which the CPython 2 streams depend on, does not properly support this).
Python 3 rolls it own I/O streams. On Python 3, buffered binary streams can't be
set line-buffered. The previous code (added in
227ba1afcb65) incorrectly
assumed that on Python 3, pycompat.stdout (sys.stdout.buffer) is already
line-buffered. However the interpreter initializes it with a block-buffered
stream or an unbuffered stream (when the -u option or the PYTHONUNBUFFERED
environment variable is set), never with a line-buffered stream.
One example where the current behavior is unacceptable is when running
`hg pull https://www.mercurial-scm.org/repo/hg` on Python 3, where the line
"pulling from https://www.mercurial-scm.org/repo/hg" does not appear on the
terminal before the hg process blocks while waiting for the server.
Various approaches to fix this problem are possible, including:
1. Weaken the contract of procutil.stdout to not give any guarantees about
buffering behavior. In this case, users of procutil.stdout need to be
changed to do enough flushes. In particular,
1. either ui must insert enough flushes for ui.write() and friends, or
2. ui.write() and friends get split into flushing and fully buffered
methods, or
3. users of ui.write() and friends must flush explicitly.
2. Make stdout unbuffered.
3. Make stdout line-buffered. Since Python 3 does not natively support that for
binary streams, we must implement it ourselves.
(2.) is problematic because using unbuffered I/O changes the performance
characteristics significantly compared to line-buffered (which is used on
Python 2) and this would be a regression.
(1.2.) and (1.3) are a substantial amount of work. It’s unclear whether the
added complexity would be justified, given that raw performance doesn’t matter
that much when writing to a terminal much faster than the user could read it.
(1.1.) pushes complexity into the ui class instead of separating the concern of
how stdout is buffered. Other users of procutil.stdout would still need to take
care of the flushes.
This patch implements (3.). The general performance considerations are very
similar to (1.1.). The extra method invocation and method forwarding add a
little more overhead if the class is used. In exchange, it doesn’t add overhead
if not used.
For the benchmarks, I compared the previous implementation (incorrect on Python
3), (1.1.), (3.) and (2.). The command was chosen so that the streams were
configured as if they were writing to a TTY, but actually write to a pager,
which is also the default:
HGRCPATH=/dev/null python3 ./hg --cwd ~/vcs/mozilla-central --time --pager yes --config pager.pager='cat > /dev/null' status --all
previous:
time: real 7.880 secs (user 7.290+0.050 sys 0.580+0.170)
time: real 7.830 secs (user 7.220+0.070 sys 0.590+0.140)
time: real 7.800 secs (user 7.210+0.050 sys 0.570+0.170)
(1.1.) using Yuya Nishihara’s patch:
time: real 9.860 secs (user 8.670+0.350 sys 1.160+0.830)
time: real 9.540 secs (user 8.430+0.370 sys 1.100+0.770)
time: real 9.830 secs (user 8.630+0.370 sys 1.180+0.840)
(3.) using this patch:
time: real 9.580 secs (user 8.480+0.350 sys 1.090+0.770)
time: real 9.670 secs (user 8.480+0.330 sys 1.170+0.860)
time: real 9.640 secs (user 8.500+0.350 sys 1.130+0.810)
(2.) using a previous patch by me:
time: real 10.480 secs (user 8.850+0.720 sys 1.590+1.500)
time: real 10.490 secs (user 8.750+0.750 sys 1.710+1.470)
time: real 10.240 secs (user 8.600+0.700 sys 1.590+1.510)
As expected, there’s no difference on Python 2, as exactly the same code paths
are used:
previous:
time: real 6.950 secs (user 5.870+0.330 sys 1.070+0.770)
time: real 7.040 secs (user 6.040+0.360 sys 0.980+0.750)
time: real 7.070 secs (user 5.950+0.360 sys 1.100+0.760)
this patch:
time: real 7.010 secs (user 5.900+0.390 sys 1.070+0.730)
time: real 7.000 secs (user 5.850+0.350 sys 1.120+0.760)
time: real 7.000 secs (user 5.790+0.380 sys 1.170+0.710)
$ echo "[extensions]" >> $HGRCPATH
$ echo "mq=" >> $HGRCPATH
$ hg init
$ hg qinit
$ echo x > x
$ hg ci -Ama
adding x
$ hg qnew a.patch
$ echo a > a
$ hg add a
$ hg qrefresh
$ hg qnew b.patch
$ echo b > b
$ hg add b
$ hg qrefresh
$ hg qnew c.patch
$ echo c > c
$ hg add c
$ hg qrefresh
$ hg qpop -a
popping c.patch
popping b.patch
popping a.patch
patch queue now empty
should fail
$ hg qguard does-not-exist.patch +bleh
abort: no patch named does-not-exist.patch
[255]
should fail
$ hg qguard +fail
abort: no patches applied
[255]
$ hg qpush
applying a.patch
now at: a.patch
should guard a.patch
$ hg qguard +a
should print +a
$ hg qguard
a.patch: +a
$ hg qpop
popping a.patch
patch queue now empty
should fail
$ hg qpush a.patch
cannot push 'a.patch' - guarded by '+a'
[1]
$ hg qguard a.patch
a.patch: +a
should push b.patch
$ hg qpush
applying b.patch
now at: b.patch
$ hg qpop
popping b.patch
patch queue now empty
test selection of an empty guard
$ hg qselect ""
abort: guard cannot be an empty string
[255]
$ hg qselect a
number of unguarded, unapplied patches has changed from 2 to 3
should push a.patch
$ hg qpush
applying a.patch
now at: a.patch
$ hg qguard -- c.patch -a
should print -a
$ hg qguard c.patch
c.patch: -a
should skip c.patch
$ hg qpush -a
applying b.patch
skipping c.patch - guarded by '-a'
now at: b.patch
$ hg qnext
all patches applied
[1]
should display b.patch
$ hg qtop
b.patch
$ hg qguard -n c.patch
should push c.patch
$ hg qpush -a
applying c.patch
now at: c.patch
$ hg qpop -a
popping c.patch
popping b.patch
popping a.patch
patch queue now empty
$ hg qselect -n
guards deactivated
number of unguarded, unapplied patches has changed from 3 to 2
should push all
$ hg qpush -a
applying b.patch
applying c.patch
now at: c.patch
$ hg qpop -a
popping c.patch
popping b.patch
patch queue now empty
$ hg qguard a.patch +1
$ hg qguard b.patch +2
$ hg qselect 1
number of unguarded, unapplied patches has changed from 1 to 2
should push a.patch, not b.patch
$ hg qpush
applying a.patch
now at: a.patch
$ hg qpush
applying c.patch
now at: c.patch
$ hg qpop -a
popping c.patch
popping a.patch
patch queue now empty
$ hg qselect 2
should push b.patch
$ hg qpush
applying b.patch
now at: b.patch
$ hg qpush -a
applying c.patch
now at: c.patch
$ hg qprev
b.patch
Used to be an issue with holes in the patch sequence
So, put one hole on the base and ask for topmost patch.
$ hg qtop
c.patch
$ hg qpop -a
popping c.patch
popping b.patch
patch queue now empty
$ hg qselect 1 2
number of unguarded, unapplied patches has changed from 2 to 3
should push a.patch, b.patch
$ hg qpush
applying a.patch
now at: a.patch
$ hg qpush
applying b.patch
now at: b.patch
$ hg qpop -a
popping b.patch
popping a.patch
patch queue now empty
$ hg qguard -- a.patch +1 +2 -3
$ hg qselect 1 2 3
number of unguarded, unapplied patches has changed from 3 to 2
list patches and guards
$ hg qguard -l
a.patch: +1 +2 -3
b.patch: +2
c.patch: unguarded
have at least one patch applied to test coloring
$ hg qpush
applying b.patch
now at: b.patch
list patches and guards with color
$ hg --config extensions.color= qguard --config color.mode=ansi \
> -l --color=always
\x1b[0;30;1ma.patch\x1b[0m: \x1b[0;33m+1\x1b[0m \x1b[0;33m+2\x1b[0m \x1b[0;31m-3\x1b[0m (esc)
\x1b[0;34;1;4mb.patch\x1b[0m: \x1b[0;33m+2\x1b[0m (esc)
\x1b[0;30;1mc.patch\x1b[0m: \x1b[0;32munguarded\x1b[0m (esc)
should pop b.patch
$ hg qpop
popping b.patch
patch queue now empty
list series
$ hg qseries -v
0 G a.patch
1 U b.patch
2 U c.patch
list guards
$ hg qselect
1
2
3
should push b.patch
$ hg qpush
applying b.patch
now at: b.patch
$ hg qpush -a
applying c.patch
now at: c.patch
$ hg qselect -n --reapply -v
guards deactivated
popping guarded patches
popping c.patch
popping b.patch
patch queue now empty
reapplying unguarded patches
skipping a.patch - guarded by '+1' '+2'
skipping b.patch - guarded by '+2'
skipping a.patch - guarded by '+1' '+2'
skipping b.patch - guarded by '+2'
applying c.patch
patching file c
adding c
committing files:
c
committing manifest
committing changelog
now at: c.patch
guards in series file: +1 +2 -3
$ hg qselect -s
+1
+2
-3
should show c.patch
$ hg qapplied
c.patch
$ hg qrename a.patch new.patch
should show :
new.patch: +1 +2 -3
b.patch: +2
c.patch: unguarded
$ hg qguard -l
new.patch: +1 +2 -3
b.patch: +2
c.patch: unguarded
$ hg qnew d.patch
$ hg qpop
popping d.patch
now at: c.patch
should show new.patch and b.patch as Guarded, c.patch as Applied
and d.patch as Unapplied
$ hg qseries -v
0 G new.patch
1 G b.patch
2 A c.patch
3 U d.patch
qseries again, but with color
$ hg --config extensions.color= --config color.mode=ansi qseries -v --color=always
0 G \x1b[0;30;1mnew.patch\x1b[0m (esc)
1 G \x1b[0;30;1mb.patch\x1b[0m (esc)
2 A \x1b[0;34;1;4mc.patch\x1b[0m (esc)
3 U \x1b[0;30;1md.patch\x1b[0m (esc)
$ hg qguard d.patch +2
new.patch, b.patch: Guarded. c.patch: Applied. d.patch: Guarded.
$ hg qseries -v
0 G new.patch
1 G b.patch
2 A c.patch
3 G d.patch
$ qappunappv()
> {
> for command in qapplied "qapplied -v" qunapplied "qunapplied -v"; do
> echo % hg $command
> hg $command
> done
> }
$ hg qpop -a
popping c.patch
patch queue now empty
$ hg qguard -l
new.patch: +1 +2 -3
b.patch: +2
c.patch: unguarded
d.patch: +2
$ qappunappv
% hg qapplied
% hg qapplied -v
% hg qunapplied
c.patch
% hg qunapplied -v
0 G new.patch
1 G b.patch
2 U c.patch
3 G d.patch
$ hg qselect 1
number of unguarded, unapplied patches has changed from 1 to 2
$ qappunappv
% hg qapplied
% hg qapplied -v
% hg qunapplied
new.patch
c.patch
% hg qunapplied -v
0 U new.patch
1 G b.patch
2 U c.patch
3 G d.patch
$ hg qpush -a
applying new.patch
skipping b.patch - guarded by '+2'
applying c.patch
skipping d.patch - guarded by '+2'
now at: c.patch
$ qappunappv
% hg qapplied
new.patch
c.patch
% hg qapplied -v
0 A new.patch
1 G b.patch
2 A c.patch
% hg qunapplied
% hg qunapplied -v
3 G d.patch
$ hg qselect 2
number of unguarded, unapplied patches has changed from 0 to 1
$ qappunappv
% hg qapplied
new.patch
c.patch
% hg qapplied -v
0 A new.patch
1 U b.patch
2 A c.patch
% hg qunapplied
d.patch
% hg qunapplied -v
3 U d.patch
$ for patch in `hg qseries`; do
> echo % hg qapplied $patch
> hg qapplied $patch
> echo % hg qunapplied $patch
> hg qunapplied $patch
> done
% hg qapplied new.patch
new.patch
% hg qunapplied new.patch
b.patch
d.patch
% hg qapplied b.patch
new.patch
% hg qunapplied b.patch
d.patch
% hg qapplied c.patch
new.patch
c.patch
% hg qunapplied c.patch
d.patch
% hg qapplied d.patch
new.patch
c.patch
% hg qunapplied d.patch
hg qseries -m: only b.patch should be shown
the guards file was not ignored in the past
$ hg qdelete -k b.patch
$ hg qseries -m
b.patch
hg qseries -m with color
$ hg --config extensions.color= --config color.mode=ansi qseries -m --color=always
\x1b[0;31;1mb.patch\x1b[0m (esc)
excercise corner cases in "qselect --reapply"
$ hg qpop -a
popping c.patch
popping new.patch
patch queue now empty
$ hg qguard -- new.patch -not-new
$ hg qguard -- c.patch -not-c
$ hg qguard -- d.patch -not-d
$ hg qpush -a
applying new.patch
applying c.patch
applying d.patch
patch d.patch is empty
now at: d.patch
$ hg qguard -l
new.patch: -not-new
c.patch: -not-c
d.patch: -not-d
$ hg qselect --reapply not-d
popping guarded patches
popping d.patch
now at: c.patch
reapplying unguarded patches
cannot push 'd.patch' - guarded by '-not-d'
$ hg qser -v
0 A new.patch
1 A c.patch
2 G d.patch
$ hg qselect --reapply -n
guards deactivated
$ hg qpush
applying d.patch
patch d.patch is empty
now at: d.patch
$ hg qser -v
0 A new.patch
1 A c.patch
2 A d.patch
$ hg qselect --reapply not-c
popping guarded patches
popping d.patch
popping c.patch
now at: new.patch
reapplying unguarded patches
applying d.patch
patch d.patch is empty
now at: d.patch
$ hg qser -v
0 A new.patch
1 G c.patch
2 A d.patch
$ hg qselect --reapply not-new
popping guarded patches
popping d.patch
popping new.patch
patch queue now empty
reapplying unguarded patches
applying c.patch
applying d.patch
patch d.patch is empty
now at: d.patch
$ hg qser -v
0 G new.patch
1 A c.patch
2 A d.patch
test that qselect shows "number of guarded, applied patches" correctly
$ hg qimport -q -e b.patch
adding b.patch to series file
$ hg qguard -- b.patch -not-b
$ hg qpop -a -q
patch queue now empty
$ hg qunapplied -v
0 G new.patch
1 U c.patch
2 U d.patch
3 U b.patch
$ hg qselect not-new not-c
number of unguarded, unapplied patches has changed from 3 to 2
$ hg qpush -q -a
patch d.patch is empty
now at: b.patch
$ hg qapplied -v
0 G new.patch
1 G c.patch
2 A d.patch
3 A b.patch
$ hg qselect --none
guards deactivated
$ hg qselect not-new not-c not-d
number of guarded, applied patches has changed from 0 to 1
test that "qselect --reapply" reapplies patches successfully when the
already applied patch becomes unguarded and it follows the already
guarded (= not yet applied) one.
$ hg qpop -q -a
patch queue now empty
$ hg qselect not-new not-c
number of unguarded, unapplied patches has changed from 1 to 2
$ hg qpush -q -a
patch d.patch is empty
now at: b.patch
$ hg qapplied -v
0 G new.patch
1 G c.patch
2 A d.patch
3 A b.patch
$ hg qselect -q --reapply not-c not-b
now at: d.patch
cannot push 'b.patch' - guarded by '-not-b'
$ hg qseries -v
0 U new.patch
1 G c.patch
2 A d.patch
3 G b.patch
test that "qselect --reapply" checks applied patches correctly when no
applied patches becomes guarded but some of unapplied ones become
unguarded.
$ hg qpop -q -a
patch queue now empty
$ hg qselect not-new not-c not-d
number of unguarded, unapplied patches has changed from 2 to 1
$ hg qpush -q -a
now at: b.patch
$ hg qapplied -v
0 G new.patch
1 G c.patch
2 G d.patch
3 A b.patch
$ hg qselect -q --reapply not-new not-c
$ hg qseries -v
0 G new.patch
1 G c.patch
2 U d.patch
3 A b.patch