# HG changeset patch # User Pierre-Yves David # Date 2023-12-20 19:13:22 # Node ID 81224afd938dba86159ff739ca990fb17e5edece # Parent 9d3721552b6cbf613f0089e9dd9fa87e88919a22 lock: properly convert error to bytes Flagged by pytype when a later changeset is applied moving typing comment to annotation. We fix this ahead of the annotation change to make sure pytype remains happy after the change. We have to do fairly crazy dance for pytype to be happy. This probably comes from the fact IOError.filename probably claims to be `str` while it is actually `bytes` if the filename raising that `IOError` is bytes. At the same time, `IOError.strerror` is consistently `str` and should be passed as `str` everywhere. diff --git a/mercurial/error.py b/mercurial/error.py --- a/mercurial/error.py +++ b/mercurial/error.py @@ -498,8 +498,7 @@ class UnknownVersion(Abort): class LockError(IOError): def __init__(self, errno, strerror, filename, desc): - # TODO: figure out if this should be bytes or str - # _type: (int, str, str, bytes) -> None + # _type: (int, str, bytes, bytes) -> None IOError.__init__(self, errno, strerror, filename) self.desc = desc @@ -508,7 +507,7 @@ class LockError(IOError): class LockHeld(LockError): def __init__(self, errno, filename, desc, locker): - LockError.__init__(self, errno, b'Lock held', filename, desc) + LockError.__init__(self, errno, 'Lock held', filename, desc) self.locker = locker diff --git a/mercurial/lock.py b/mercurial/lock.py --- a/mercurial/lock.py +++ b/mercurial/lock.py @@ -12,6 +12,7 @@ import os import signal import socket import time +import typing import warnings from .i18n import _ @@ -154,8 +155,12 @@ def trylock(ui, vfs, lockname, timeout, if delay == warningidx: printwarning(ui.warn, inst.locker) if timeout <= delay: + assert isinstance(inst.filename, bytes) raise error.LockHeld( - errno.ETIMEDOUT, inst.filename, l.desc, inst.locker + errno.ETIMEDOUT, + typing.cast(bytes, inst.filename), + l.desc, + inst.locker, ) time.sleep(1) delay += 1 @@ -290,8 +295,13 @@ class lock: locker, ) else: + assert isinstance(why.filename, bytes) + assert isinstance(why.strerror, str) raise error.LockUnavailable( - why.errno, why.strerror, why.filename, self.desc + why.errno, + why.strerror, + typing.cast(bytes, why.filename), + self.desc, ) if not self.held: diff --git a/mercurial/statichttprepo.py b/mercurial/statichttprepo.py --- a/mercurial/statichttprepo.py +++ b/mercurial/statichttprepo.py @@ -243,7 +243,7 @@ class statichttprepository( def wlock(self, wait=True): raise error.LockUnavailable( 0, - _(b'lock not available'), + pycompat.sysstr(_(b'lock not available')), b'lock', _(b'cannot lock static-http repository'), ) @@ -251,7 +251,7 @@ class statichttprepository( def lock(self, wait=True): raise error.LockUnavailable( 0, - _(b'lock not available'), + pycompat.sysstr(_(b'lock not available')), b'lock', _(b'cannot lock static-http repository'), )