Mercurial > public > mercurial-scm > hg-stable
annotate mercurial/rcutil.py @ 49263:63fd0282ad40
node: stop converting binascii.Error to TypeError in bin()
Changeset f574cc00831a introduced the wrapper, to make bin() behave like on
Python 2, where it raised TypeError in many cases. Another previous approach,
changing callers to catch binascii.Error in addition to TypeError, was backed
out after negative review feedback [1].
However, I think it?s worth reconsidering the approach. Now that we?re on
Python 3 only, callers have to catch only binascii.Error instead of both.
Catching binascii.Error instead of TypeError has the advantage that it?s less
likely to cover a programming error (e.g. passing an int to bin() raises
TypeError). Also, raising TypeError never made sense semantically when bin()
got an argument of valid type.
As a side-effect, this fixed an exception in test-http-bad-server.t. The TODO
was outdated: it was not an uncaught ValueError in batch.results() but uncaught
TypeError from the now removed wrapper. Now that bin() raises binascii.Error
instead of TypeError, it gets converted to a proper error in
wirepeer.heads.<locals>.decode() that catches ValueError (superclass of
binascii.Error). This is a good example of why this changeset is a good idea.
Catching TypeError instead of ValueError there would not make much sense.
[1] https://phab.mercurial-scm.org/D2244
author | Manuel Jacob <me@manueljacob.de> |
---|---|
date | Mon, 30 May 2022 16:18:12 +0200 |
parents | 6000f5b25c9b |
children | f4733654f144 |
rev | line source |
---|---|
31684
0f8ba0bc1154
rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff
changeset
|
1 # rcutil.py - utilities about config paths, special config sections etc. |
0f8ba0bc1154
rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff
changeset
|
2 # |
0f8ba0bc1154
rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff
changeset
|
3 # Copyright Mercurial Contributors |
0f8ba0bc1154
rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff
changeset
|
4 # |
0f8ba0bc1154
rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff
changeset
|
5 # This software may be used and distributed according to the terms of the |
0f8ba0bc1154
rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff
changeset
|
6 # GNU General Public License version 2 or any later version. |
0f8ba0bc1154
rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff
changeset
|
7 |
0f8ba0bc1154
rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff
changeset
|
8 |
0f8ba0bc1154
rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff
changeset
|
9 import os |
0f8ba0bc1154
rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff
changeset
|
10 |
0f8ba0bc1154
rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff
changeset
|
11 from . import ( |
0f8ba0bc1154
rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff
changeset
|
12 encoding, |
0f8ba0bc1154
rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff
changeset
|
13 pycompat, |
0f8ba0bc1154
rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff
changeset
|
14 util, |
0f8ba0bc1154
rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff
changeset
|
15 ) |
0f8ba0bc1154
rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff
changeset
|
16 |
43715
5be909dbe385
util: remove datapath and swith users over to resourceutil
Martin von Zweigbergk <martinvonz@google.com>
parents:
43711
diff
changeset
|
17 from .utils import resourceutil |
5be909dbe385
util: remove datapath and swith users over to resourceutil
Martin von Zweigbergk <martinvonz@google.com>
parents:
43711
diff
changeset
|
18 |
34645 | 19 if pycompat.iswindows: |
31684
0f8ba0bc1154
rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff
changeset
|
20 from . import scmwindows as scmplatform |
0f8ba0bc1154
rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff
changeset
|
21 else: |
0f8ba0bc1154
rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff
changeset
|
22 from . import scmposix as scmplatform |
0f8ba0bc1154
rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff
changeset
|
23 |
32078
bf5e13e38390
pager: use less as a fallback on Unix
Yuya Nishihara <yuya@tcha.org>
parents:
31954
diff
changeset
|
24 fallbackpager = scmplatform.fallbackpager |
31684
0f8ba0bc1154
rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff
changeset
|
25 systemrcpath = scmplatform.systemrcpath |
0f8ba0bc1154
rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff
changeset
|
26 userrcpath = scmplatform.userrcpath |
0f8ba0bc1154
rcutil: move scmutil.*rcpath to rcutil (API)
Jun Wu <quark@fb.com>
parents:
diff
changeset
|
27 |
43075
57875cf423c9
style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents:
42093
diff
changeset
|
28 |
31686
294728f2a908
rcutil: extract rc directory listing logic
Jun Wu <quark@fb.com>
parents:
31685
diff
changeset
|
29 def _expandrcpath(path): |
294728f2a908
rcutil: extract rc directory listing logic
Jun Wu <quark@fb.com>
parents:
31685
diff
changeset
|
30 '''path could be a file or a directory. return a list of file paths''' |
294728f2a908
rcutil: extract rc directory listing logic
Jun Wu <quark@fb.com>
parents:
31685
diff
changeset
|
31 p = util.expandpath(path) |
294728f2a908
rcutil: extract rc directory listing logic
Jun Wu <quark@fb.com>
parents:
31685
diff
changeset
|
32 if os.path.isdir(p): |
294728f2a908
rcutil: extract rc directory listing logic
Jun Wu <quark@fb.com>
parents:
31685
diff
changeset
|
33 join = os.path.join |
43075
57875cf423c9
style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents:
42093
diff
changeset
|
34 return sorted( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43075
diff
changeset
|
35 join(p, f) for f, k in util.listdir(p) if f.endswith(b'.rc') |
43075
57875cf423c9
style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents:
42093
diff
changeset
|
36 ) |
31686
294728f2a908
rcutil: extract rc directory listing logic
Jun Wu <quark@fb.com>
parents:
31685
diff
changeset
|
37 return [p] |
294728f2a908
rcutil: extract rc directory listing logic
Jun Wu <quark@fb.com>
parents:
31685
diff
changeset
|
38 |
43075
57875cf423c9
style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents:
42093
diff
changeset
|
39 |
31689
0be96ac9199a
rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents:
31688
diff
changeset
|
40 def envrcitems(env=None): |
45957
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
44160
diff
changeset
|
41 """Return [(section, name, value, source)] config items. |
31689
0be96ac9199a
rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents:
31688
diff
changeset
|
42 |
0be96ac9199a
rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents:
31688
diff
changeset
|
43 The config items are extracted from environment variables specified by env, |
0be96ac9199a
rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents:
31688
diff
changeset
|
44 used to override systemrc, but not userrc. |
0be96ac9199a
rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents:
31688
diff
changeset
|
45 |
0be96ac9199a
rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents:
31688
diff
changeset
|
46 If env is not provided, encoding.environ will be used. |
45957
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
44160
diff
changeset
|
47 """ |
31689
0be96ac9199a
rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents:
31688
diff
changeset
|
48 if env is None: |
0be96ac9199a
rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents:
31688
diff
changeset
|
49 env = encoding.environ |
0be96ac9199a
rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents:
31688
diff
changeset
|
50 checklist = [ |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43075
diff
changeset
|
51 (b'EDITOR', b'ui', b'editor'), |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43075
diff
changeset
|
52 (b'VISUAL', b'ui', b'editor'), |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43075
diff
changeset
|
53 (b'PAGER', b'pager', b'pager'), |
31689
0be96ac9199a
rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents:
31688
diff
changeset
|
54 ] |
0be96ac9199a
rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents:
31688
diff
changeset
|
55 result = [] |
0be96ac9199a
rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents:
31688
diff
changeset
|
56 for envname, section, configname in checklist: |
0be96ac9199a
rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents:
31688
diff
changeset
|
57 if envname not in env: |
0be96ac9199a
rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents:
31688
diff
changeset
|
58 continue |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43075
diff
changeset
|
59 result.append((section, configname, env[envname], b'$%s' % envname)) |
31689
0be96ac9199a
rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents:
31688
diff
changeset
|
60 return result |
0be96ac9199a
rcutil: add a method to convert environment variables to config items
Jun Wu <quark@fb.com>
parents:
31688
diff
changeset
|
61 |
43075
57875cf423c9
style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents:
42093
diff
changeset
|
62 |
44033
1864efbe90d9
ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents:
43924
diff
changeset
|
63 def default_rc_resources(): |
1864efbe90d9
ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents:
43924
diff
changeset
|
64 """return rc resource IDs in defaultrc""" |
1864efbe90d9
ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents:
43924
diff
changeset
|
65 rsrcs = resourceutil.contents(b'mercurial.defaultrc') |
1864efbe90d9
ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents:
43924
diff
changeset
|
66 return [ |
1864efbe90d9
ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents:
43924
diff
changeset
|
67 (b'mercurial.defaultrc', r) |
1864efbe90d9
ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents:
43924
diff
changeset
|
68 for r in sorted(rsrcs) |
1864efbe90d9
ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents:
43924
diff
changeset
|
69 if resourceutil.is_resource(b'mercurial.defaultrc', r) |
1864efbe90d9
ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents:
43924
diff
changeset
|
70 and r.endswith(b'.rc') |
1864efbe90d9
ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents:
43924
diff
changeset
|
71 ] |
1864efbe90d9
ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents:
43924
diff
changeset
|
72 |
1864efbe90d9
ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents:
43924
diff
changeset
|
73 |
31687
07d62fa518a4
rcutil: rename rcpath to rccomponents (API)
Jun Wu <quark@fb.com>
parents:
31686
diff
changeset
|
74 def rccomponents(): |
45957
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
44160
diff
changeset
|
75 """return an ordered [(type, obj)] about where to load configs. |
31688
00e569a2da97
rcutil: let rccomponents return different types of configs (API)
Jun Wu <quark@fb.com>
parents:
31687
diff
changeset
|
76 |
00e569a2da97
rcutil: let rccomponents return different types of configs (API)
Jun Wu <quark@fb.com>
parents:
31687
diff
changeset
|
77 respect $HGRCPATH. if $HGRCPATH is empty, only .hg/hgrc of current repo is |
00e569a2da97
rcutil: let rccomponents return different types of configs (API)
Jun Wu <quark@fb.com>
parents:
31687
diff
changeset
|
78 used. if $HGRCPATH is not set, the platform default will be used. |
00e569a2da97
rcutil: let rccomponents return different types of configs (API)
Jun Wu <quark@fb.com>
parents:
31687
diff
changeset
|
79 |
00e569a2da97
rcutil: let rccomponents return different types of configs (API)
Jun Wu <quark@fb.com>
parents:
31687
diff
changeset
|
80 if a directory is provided, *.rc files under it will be used. |
00e569a2da97
rcutil: let rccomponents return different types of configs (API)
Jun Wu <quark@fb.com>
parents:
31687
diff
changeset
|
81 |
44033
1864efbe90d9
ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents:
43924
diff
changeset
|
82 type could be either 'path', 'items' or 'resource'. If type is 'path', |
1864efbe90d9
ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents:
43924
diff
changeset
|
83 obj is a string, and is the config file path. if type is 'items', obj is a |
1864efbe90d9
ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents:
43924
diff
changeset
|
84 list of (section, name, value, source) that should fill the config directly. |
1864efbe90d9
ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents:
43924
diff
changeset
|
85 If type is 'resource', obj is a tuple of (package name, resource name). |
45957
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
44160
diff
changeset
|
86 """ |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43075
diff
changeset
|
87 envrc = (b'items', envrcitems()) |
31690
d83e51654c8a
rcutil: let environ override system configs (BC)
Jun Wu <quark@fb.com>
parents:
31689
diff
changeset
|
88 |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43075
diff
changeset
|
89 if b'HGRCPATH' in encoding.environ: |
31698 | 90 # assume HGRCPATH is all about user configs so environments can be |
91 # overridden. | |
92 _rccomponents = [envrc] | |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43075
diff
changeset
|
93 for p in encoding.environ[b'HGRCPATH'].split(pycompat.ospathsep): |
31698 | 94 if not p: |
95 continue | |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43075
diff
changeset
|
96 _rccomponents.extend((b'path', p) for p in _expandrcpath(p)) |
31698 | 97 else: |
44033
1864efbe90d9
ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents:
43924
diff
changeset
|
98 _rccomponents = [(b'resource', r) for r in default_rc_resources()] |
1864efbe90d9
ui: add the ability to apply `defaultrc` configs from resources
Matt Harbison <matt_harbison@yahoo.com>
parents:
43924
diff
changeset
|
99 |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43075
diff
changeset
|
100 normpaths = lambda paths: [ |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43075
diff
changeset
|
101 (b'path', os.path.normpath(p)) for p in paths |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43075
diff
changeset
|
102 ] |
44034
2d4cad94d08a
rcutil: drop the `defaultrcpath()` method (API)
Matt Harbison <matt_harbison@yahoo.com>
parents:
44033
diff
changeset
|
103 _rccomponents.extend(normpaths(systemrcpath())) |
31698 | 104 _rccomponents.append(envrc) |
31699
10d88dc7c010
rcutil: extract duplicated logic to a lambda
Jun Wu <quark@fb.com>
parents:
31698
diff
changeset
|
105 _rccomponents.extend(normpaths(userrcpath())) |
31687
07d62fa518a4
rcutil: rename rcpath to rccomponents (API)
Jun Wu <quark@fb.com>
parents:
31686
diff
changeset
|
106 return _rccomponents |
31954
e518192d6bac
pager: set some environment variables if they're not set
Jun Wu <quark@fb.com>
parents:
31699
diff
changeset
|
107 |
43075
57875cf423c9
style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents:
42093
diff
changeset
|
108 |
31954
e518192d6bac
pager: set some environment variables if they're not set
Jun Wu <quark@fb.com>
parents:
31699
diff
changeset
|
109 def defaultpagerenv(): |
45957
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
44160
diff
changeset
|
110 """return a dict of default environment variables and their values, |
31954
e518192d6bac
pager: set some environment variables if they're not set
Jun Wu <quark@fb.com>
parents:
31699
diff
changeset
|
111 intended to be set before starting a pager. |
45957
89a2afe31e82
formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents:
44160
diff
changeset
|
112 """ |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43075
diff
changeset
|
113 return {b'LESS': b'FRX', b'LV': b'-c'} |
44160
238790674d69
config: add a function in `rcutil` to abstract HGRCSKIPREPO
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
44034
diff
changeset
|
114 |
238790674d69
config: add a function in `rcutil` to abstract HGRCSKIPREPO
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
44034
diff
changeset
|
115 |
238790674d69
config: add a function in `rcutil` to abstract HGRCSKIPREPO
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
44034
diff
changeset
|
116 def use_repo_hgrc(): |
238790674d69
config: add a function in `rcutil` to abstract HGRCSKIPREPO
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
44034
diff
changeset
|
117 """True if repositories `.hg/hgrc` config should be read""" |
238790674d69
config: add a function in `rcutil` to abstract HGRCSKIPREPO
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
44034
diff
changeset
|
118 return b'HGRCSKIPREPO' not in encoding.environ |