mercurial/node.py
author Manuel Jacob <me@manueljacob.de>
Mon, 30 May 2022 16:18:12 +0200
changeset 49248 63fd0282ad40
parent 48946 642e31cb55f0
permissions -rw-r--r--
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

# node.py - basic nodeid manipulation for mercurial
#
# Copyright 2005, 2006 Olivia Mackall <olivia@selenic.com>
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.


import binascii

# This ugly style has a noticeable effect in manifest parsing
hex = binascii.hexlify
bin = binascii.unhexlify


def short(node):
    return hex(node[:6])


nullrev = -1

# pseudo identifier for working directory
# (experimental, so don't add too many dependencies on it)
wdirrev = 0x7FFFFFFF


class sha1nodeconstants:
    nodelen = 20

    # In hex, this is '0000000000000000000000000000000000000000'
    nullid = b"\0" * nodelen
    nullhex = hex(nullid)

    # Phony node value to stand-in for new files in some uses of
    # manifests.
    # In hex, this is '2121212121212121212121212121212121212121'
    newnodeid = b'!!!!!!!!!!!!!!!!!!!!'
    # In hex, this is '3030303030303030303030303030306164646564'
    addednodeid = b'000000000000000added'
    # In hex, this is '3030303030303030303030306d6f646966696564'
    modifiednodeid = b'000000000000modified'

    wdirfilenodeids = {newnodeid, addednodeid, modifiednodeid}

    # pseudo identifier for working directory
    # (experimental, so don't add too many dependencies on it)
    # In hex, this is 'ffffffffffffffffffffffffffffffffffffffff'
    wdirid = b"\xff" * nodelen
    wdirhex = hex(wdirid)


# legacy starting point for porting modules
nullid = sha1nodeconstants.nullid
nullhex = sha1nodeconstants.nullhex
newnodeid = sha1nodeconstants.newnodeid
addednodeid = sha1nodeconstants.addednodeid
modifiednodeid = sha1nodeconstants.modifiednodeid
wdirfilenodeids = sha1nodeconstants.wdirfilenodeids
wdirid = sha1nodeconstants.wdirid
wdirhex = sha1nodeconstants.wdirhex