Mercurial > hg
annotate hgext/commitextras.py @ 45095:8e04607023e5
procutil: ensure that procutil.std{out,err}.write() writes all bytes
Python 3 offers different kind of streams and it’s not guaranteed for all of
them that calling write() writes all bytes.
When Python is started in unbuffered mode, sys.std{out,err}.buffer are
instances of io.FileIO, whose write() can write less bytes for
platform-specific reasons (e.g. Linux has a 0x7ffff000 bytes maximum and could
write less if interrupted by a signal; when writing to Windows consoles, it’s
limited to 32767 bytes to avoid the "not enough space" error). This can lead to
silent loss of data, both when using sys.std{out,err}.buffer (which may in fact
not be a buffered stream) and when using the text streams sys.std{out,err}
(I’ve created a CPython bug report for that:
https://bugs.python.org/issue41221).
Python may fix the problem at some point. For now, we implement our own wrapper
for procutil.std{out,err} that calls the raw stream’s write() method until all
bytes have been written. We don’t use sys.std{out,err} for larger writes, so I
think it’s not worth the effort to patch them.
author | Manuel Jacob <me@manueljacob.de> |
---|---|
date | Fri, 10 Jul 2020 12:27:58 +0200 |
parents | 9f70512ae2cf |
children | 45a073af50a2 |
rev | line source |
---|---|
33546
77c0c36654c8
commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
1 # commitextras.py |
77c0c36654c8
commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
2 # |
77c0c36654c8
commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
3 # Copyright 2013 Facebook, Inc. |
77c0c36654c8
commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
4 # |
77c0c36654c8
commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
5 # This software may be used and distributed according to the terms of the |
77c0c36654c8
commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
6 # GNU General Public License version 2 or any later version. |
77c0c36654c8
commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
7 |
33562
3cfabb6cfd51
commitextras: mark the extension as ADVANCED
Pulkit Goyal <7895pulkit@gmail.com>
parents:
33547
diff
changeset
|
8 '''adds a new flag extras to commit (ADVANCED)''' |
33546
77c0c36654c8
commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
9 |
77c0c36654c8
commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
10 from __future__ import absolute_import |
77c0c36654c8
commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
11 |
33602
27fbca750b4d
commitextras: make sure keys contains ascii letters, numbers, '_' and '-' only
Pulkit Goyal <7895pulkit@gmail.com>
parents:
33562
diff
changeset
|
12 import re |
27fbca750b4d
commitextras: make sure keys contains ascii letters, numbers, '_' and '-' only
Pulkit Goyal <7895pulkit@gmail.com>
parents:
33562
diff
changeset
|
13 |
33546
77c0c36654c8
commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
14 from mercurial.i18n import _ |
77c0c36654c8
commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
15 from mercurial import ( |
77c0c36654c8
commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
16 commands, |
33547
a6af8560494e
commitextras: check the format of the arguments and no internal key is used
Pulkit Goyal <7895pulkit@gmail.com>
parents:
33546
diff
changeset
|
17 error, |
33546
77c0c36654c8
commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
18 extensions, |
77c0c36654c8
commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
19 registrar, |
39294
1cb7c9777852
commitextras: work nicely with other extensions
Valentin Gatien-Baron <vgatien-baron@janestreet.com>
parents:
36419
diff
changeset
|
20 util, |
33546
77c0c36654c8
commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
21 ) |
77c0c36654c8
commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
22 |
77c0c36654c8
commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
23 cmdtable = {} |
77c0c36654c8
commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
24 command = registrar.command(cmdtable) |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
25 testedwith = b'ships-with-hg-core' |
33546
77c0c36654c8
commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
26 |
33547
a6af8560494e
commitextras: check the format of the arguments and no internal key is used
Pulkit Goyal <7895pulkit@gmail.com>
parents:
33546
diff
changeset
|
27 usedinternally = { |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
28 b'amend_source', |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
29 b'branch', |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
30 b'close', |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
31 b'histedit_source', |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
32 b'topic', |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
33 b'rebase_source', |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
34 b'intermediate-source', |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
35 b'__touch-noise__', |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
36 b'source', |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
37 b'transplant_source', |
33547
a6af8560494e
commitextras: check the format of the arguments and no internal key is used
Pulkit Goyal <7895pulkit@gmail.com>
parents:
33546
diff
changeset
|
38 } |
a6af8560494e
commitextras: check the format of the arguments and no internal key is used
Pulkit Goyal <7895pulkit@gmail.com>
parents:
33546
diff
changeset
|
39 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41532
diff
changeset
|
40 |
33546
77c0c36654c8
commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
41 def extsetup(ui): |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
42 entry = extensions.wrapcommand(commands.table, b'commit', _commit) |
33546
77c0c36654c8
commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
43 options = entry[1] |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41532
diff
changeset
|
44 options.append( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
45 ( |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
46 b'', |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
47 b'extra', |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
48 [], |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
49 _(b'set a changeset\'s extra values'), |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
50 _(b"KEY=VALUE"), |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
51 ) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41532
diff
changeset
|
52 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41532
diff
changeset
|
53 |
33546
77c0c36654c8
commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
54 |
77c0c36654c8
commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff
changeset
|
55 def _commit(orig, ui, repo, *pats, **opts): |
43115
4aa72cdf616f
py3: delete b'' prefix from safehasattr arguments
Martin von Zweigbergk <martinvonz@google.com>
parents:
43077
diff
changeset
|
56 if util.safehasattr(repo, 'unfiltered'): |
39294
1cb7c9777852
commitextras: work nicely with other extensions
Valentin Gatien-Baron <vgatien-baron@janestreet.com>
parents:
36419
diff
changeset
|
57 repo = repo.unfiltered() |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41532
diff
changeset
|
58 |
39294
1cb7c9777852
commitextras: work nicely with other extensions
Valentin Gatien-Baron <vgatien-baron@janestreet.com>
parents:
36419
diff
changeset
|
59 class repoextra(repo.__class__): |
1cb7c9777852
commitextras: work nicely with other extensions
Valentin Gatien-Baron <vgatien-baron@janestreet.com>
parents:
36419
diff
changeset
|
60 def commit(self, *innerpats, **inneropts): |
43506
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43115
diff
changeset
|
61 extras = opts.get('extra') |
39295
3a60416c4fd8
commitextras: no need to special case extras=[]
Valentin Gatien-Baron <vgatien-baron@janestreet.com>
parents:
39294
diff
changeset
|
62 for raw in extras: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
63 if b'=' not in raw: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41532
diff
changeset
|
64 msg = _( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
65 b"unable to parse '%s', should follow " |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
66 b"KEY=VALUE format" |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41532
diff
changeset
|
67 ) |
39295
3a60416c4fd8
commitextras: no need to special case extras=[]
Valentin Gatien-Baron <vgatien-baron@janestreet.com>
parents:
39294
diff
changeset
|
68 raise error.Abort(msg % raw) |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
69 k, v = raw.split(b'=', 1) |
39295
3a60416c4fd8
commitextras: no need to special case extras=[]
Valentin Gatien-Baron <vgatien-baron@janestreet.com>
parents:
39294
diff
changeset
|
70 if not k: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
71 msg = _(b"unable to parse '%s', keys can't be empty") |
39295
3a60416c4fd8
commitextras: no need to special case extras=[]
Valentin Gatien-Baron <vgatien-baron@janestreet.com>
parents:
39294
diff
changeset
|
72 raise error.Abort(msg % raw) |
41532
bd3f03d8cc9f
global: use raw strings for regular expressions with escapes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39295
diff
changeset
|
73 if re.search(br'[^\w-]', k): |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41532
diff
changeset
|
74 msg = _( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
75 b"keys can only contain ascii letters, digits," |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
76 b" '_' and '-'" |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41532
diff
changeset
|
77 ) |
39295
3a60416c4fd8
commitextras: no need to special case extras=[]
Valentin Gatien-Baron <vgatien-baron@janestreet.com>
parents:
39294
diff
changeset
|
78 raise error.Abort(msg) |
3a60416c4fd8
commitextras: no need to special case extras=[]
Valentin Gatien-Baron <vgatien-baron@janestreet.com>
parents:
39294
diff
changeset
|
79 if k in usedinternally: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41532
diff
changeset
|
80 msg = _( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
81 b"key '%s' is used internally, can't be set " |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
82 b"manually" |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41532
diff
changeset
|
83 ) |
39295
3a60416c4fd8
commitextras: no need to special case extras=[]
Valentin Gatien-Baron <vgatien-baron@janestreet.com>
parents:
39294
diff
changeset
|
84 raise error.Abort(msg % k) |
43506
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43115
diff
changeset
|
85 inneropts['extra'][k] = v |
39294
1cb7c9777852
commitextras: work nicely with other extensions
Valentin Gatien-Baron <vgatien-baron@janestreet.com>
parents:
36419
diff
changeset
|
86 return super(repoextra, self).commit(*innerpats, **inneropts) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41532
diff
changeset
|
87 |
39294
1cb7c9777852
commitextras: work nicely with other extensions
Valentin Gatien-Baron <vgatien-baron@janestreet.com>
parents:
36419
diff
changeset
|
88 repo.__class__ = repoextra |
1cb7c9777852
commitextras: work nicely with other extensions
Valentin Gatien-Baron <vgatien-baron@janestreet.com>
parents:
36419
diff
changeset
|
89 return orig(ui, repo, *pats, **opts) |