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
--- a/hgext/histedit.py Mon May 30 00:45:00 2022 +0200
+++ b/hgext/histedit.py Mon May 30 16:18:12 2022 +0200
@@ -199,6 +199,7 @@
fcntl = None
termios = None
+import binascii
import functools
import os
import pickle
@@ -504,7 +505,7 @@
# Check for validation of rule ids and get the rulehash
try:
rev = bin(ruleid)
- except TypeError:
+ except binascii.Error:
try:
_ctx = scmutil.revsingle(state.repo, ruleid)
rulehash = _ctx.hex()
--- a/hgext/largefiles/lfcommands.py Mon May 30 00:45:00 2022 +0200
+++ b/hgext/largefiles/lfcommands.py Mon May 30 16:18:12 2022 +0200
@@ -8,6 +8,7 @@
'''High-level command function for lfconvert, plus the cmdtable.'''
+import binascii
import errno
import os
import shutil
@@ -384,7 +385,7 @@
continue
try:
newid = bin(id)
- except TypeError:
+ except binascii.Error:
ui.warn(_(b'skipping incorrectly formatted id %s\n') % id)
continue
try:
--- a/mercurial/bookmarks.py Mon May 30 00:45:00 2022 +0200
+++ b/mercurial/bookmarks.py Mon May 30 16:18:12 2022 +0200
@@ -101,8 +101,8 @@
if nrefs[-2] > refspec:
# bookmarks weren't sorted before 4.5
nrefs.sort()
- except (TypeError, ValueError):
- # TypeError:
+ except ValueError:
+ # binascii.Error (ValueError subclass):
# - bin(...)
# ValueError:
# - node in nm, for non-20-bytes entry
--- a/mercurial/debugcommands.py Mon May 30 00:45:00 2022 +0200
+++ b/mercurial/debugcommands.py Mon May 30 16:18:12 2022 +0200
@@ -2691,9 +2691,9 @@
# local repository.
n = bin(s)
if len(n) != repo.nodeconstants.nodelen:
- raise TypeError()
+ raise ValueError
return n
- except TypeError:
+ except ValueError:
raise error.InputError(
b'changeset references must be full hexadecimal '
b'node identifiers'
--- a/mercurial/node.py Mon May 30 00:45:00 2022 +0200
+++ b/mercurial/node.py Mon May 30 16:18:12 2022 +0200
@@ -10,14 +10,7 @@
# This ugly style has a noticeable effect in manifest parsing
hex = binascii.hexlify
-# Adapt to Python 3 API changes. If this ends up showing up in
-# profiles, we can use this version only on Python 3, and forward
-# binascii.unhexlify like we used to on Python 2.
-def bin(s):
- try:
- return binascii.unhexlify(s)
- except binascii.Error as e:
- raise TypeError(e)
+bin = binascii.unhexlify
def short(node):
--- a/mercurial/obsolete.py Mon May 30 00:45:00 2022 +0200
+++ b/mercurial/obsolete.py Mon May 30 16:18:12 2022 +0200
@@ -68,6 +68,7 @@
"""
+import binascii
import errno
import struct
@@ -244,7 +245,7 @@
if len(p) != 20:
parents = None
break
- except TypeError:
+ except binascii.Error:
# if content cannot be translated to nodeid drop the data.
parents = None
--- a/mercurial/revlog.py Mon May 30 00:45:00 2022 +0200
+++ b/mercurial/revlog.py Mon May 30 16:18:12 2022 +0200
@@ -1487,7 +1487,7 @@
node = bin(id)
self.rev(node)
return node
- except (TypeError, error.LookupError):
+ except (binascii.Error, error.LookupError):
pass
def _partialmatch(self, id):
@@ -1529,7 +1529,7 @@
l = len(id) // 2 * 2 # grab an even number of digits
try:
prefix = bin(id[:l])
- except TypeError:
+ except binascii.Error:
pass
else:
nl = [e[7] for e in self.index if e[7].startswith(prefix)]
--- a/mercurial/revset.py Mon May 30 00:45:00 2022 +0200
+++ b/mercurial/revset.py Mon May 30 16:18:12 2022 +0200
@@ -6,6 +6,7 @@
# GNU General Public License version 2 or any later version.
+import binascii
import re
from .i18n import _
@@ -1728,7 +1729,7 @@
rn = repo.changelog.rev(bin(n))
except error.WdirUnsupported:
rn = wdirrev
- except (LookupError, TypeError):
+ except (binascii.Error, LookupError):
rn = None
else:
try:
--- a/mercurial/scmutil.py Mon May 30 00:45:00 2022 +0200
+++ b/mercurial/scmutil.py Mon May 30 16:18:12 2022 +0200
@@ -6,6 +6,7 @@
# GNU General Public License version 2 or any later version.
+import binascii
import errno
import glob
import os
@@ -639,7 +640,7 @@
return repo[rev]
except error.FilteredLookupError:
raise
- except (TypeError, LookupError):
+ except (binascii.Error, LookupError):
pass
# look up bookmarks through the name interface
--- a/mercurial/shelve.py Mon May 30 00:45:00 2022 +0200
+++ b/mercurial/shelve.py Mon May 30 16:18:12 2022 +0200
@@ -238,7 +238,7 @@
d[b'nodestoremove'] = [
bin(h) for h in d[b'nodestoremove'].split(b' ')
]
- except (ValueError, TypeError, KeyError) as err:
+ except (ValueError, KeyError) as err:
raise error.CorruptedState(stringutil.forcebytestr(err))
@classmethod
--- a/mercurial/tags.py Mon May 30 00:45:00 2022 +0200
+++ b/mercurial/tags.py Mon May 30 16:18:12 2022 +0200
@@ -11,6 +11,7 @@
# tags too.
+import binascii
import errno
import io
@@ -303,7 +304,7 @@
name = recode(name)
try:
nodebin = bin(nodehex)
- except TypeError:
+ except binascii.Error:
dbg(b"node '%s' is not well formed" % nodehex)
continue
--- a/mercurial/templatefuncs.py Mon May 30 00:45:00 2022 +0200
+++ b/mercurial/templatefuncs.py Mon May 30 16:18:12 2022 +0200
@@ -6,6 +6,7 @@
# GNU General Public License version 2 or any later version.
+import binascii
import re
from .i18n import _
@@ -769,7 +770,7 @@
elif len(hexnode) == hexnodelen:
try:
node = bin(hexnode)
- except TypeError:
+ except binascii.Error:
return hexnode
else:
try:
--- a/tests/test-http-bad-server.t Mon May 30 00:45:00 2022 +0200
+++ b/tests/test-http-bad-server.t Mon May 30 16:18:12 2022 +0200
@@ -386,14 +386,10 @@
> -p $HGPORT -d --pid-file=hg.pid -E error.log
$ cat hg.pid > $DAEMON_PIDS
-TODO client spews a stack due to uncaught ValueError in batch.results()
-#if no-chg
- $ hg clone http://localhost:$HGPORT/ clone 2> /dev/null
- [1]
-#else
- $ hg clone http://localhost:$HGPORT/ clone 2> /dev/null
+ $ hg clone http://localhost:$HGPORT/ clone
+ abort: unexpected response:
+ '96ee1d7354c4ad7372047672'
[255]
-#endif
$ killdaemons.py $DAEMON_PIDS