Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/node.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 | 642e31cb55f0 |
children | f4733654f144 |
comparison
equal
deleted
inserted
replaced
49262:3e5f1fb2aec7 | 49263:63fd0282ad40 |
---|---|
8 | 8 |
9 import binascii | 9 import binascii |
10 | 10 |
11 # This ugly style has a noticeable effect in manifest parsing | 11 # This ugly style has a noticeable effect in manifest parsing |
12 hex = binascii.hexlify | 12 hex = binascii.hexlify |
13 # Adapt to Python 3 API changes. If this ends up showing up in | 13 bin = binascii.unhexlify |
14 # profiles, we can use this version only on Python 3, and forward | |
15 # binascii.unhexlify like we used to on Python 2. | |
16 def bin(s): | |
17 try: | |
18 return binascii.unhexlify(s) | |
19 except binascii.Error as e: | |
20 raise TypeError(e) | |
21 | 14 |
22 | 15 |
23 def short(node): | 16 def short(node): |
24 return hex(node[:6]) | 17 return hex(node[:6]) |
25 | 18 |