comparison mercurial/interfaces/modules.py @ 51940:54d9f496f07a

interfaces: introduce and use a protocol class for the `charencoding` module See f2832de2a46c for details when this was done for the `bdiff` module. This lets us dump the hack where the `pure` implementation was imported during the type checking phase to provide signatures for the module methods it provides. Now the protocol classes are starting to shine, because these methods are provided by `pure.charencoding` and `cext.parsers`, and references to `cffi.charencoding` and `cext.charencoding` are forwarded to them as appropriate by the `policy` module. But none of that matters, as long as the module returned provides the listed methods. The interface was copy/pasted from the `pure` module, but `jsonescapeu8fallback` is omitted because it is accessed from the `pure` module directly when the escaping fails in the primary module's `jsonescapeu8()`.
author Matt Harbison <matt_harbison@yahoo.com>
date Sat, 05 Oct 2024 15:00:37 -0400
parents fa7059f031a9
children d7f17819ae9e
comparison
equal deleted inserted replaced
51939:8d9767bf4adb 51940:54d9f496f07a
48 def fixws(self, text: bytes, allws: bool) -> bytes: 48 def fixws(self, text: bytes, allws: bool) -> bytes:
49 ... 49 ...
50 50
51 xdiffblocks: Optional[BDiffBlocksFnc] 51 xdiffblocks: Optional[BDiffBlocksFnc]
52 """This method is currently only available in the ``cext`` module.""" 52 """This method is currently only available in the ``cext`` module."""
53
54
55 class CharEncoding(Protocol):
56 """A Protocol class for the various charencoding module implementations."""
57
58 def isasciistr(self, s: bytes) -> bool:
59 """Can the byte string be decoded with the ``ascii`` codec?"""
60
61 def asciilower(self, s: bytes) -> bytes:
62 """convert a string to lowercase if ASCII
63
64 Raises UnicodeDecodeError if non-ASCII characters are found."""
65
66 def asciiupper(self, s: bytes) -> bytes:
67 """convert a string to uppercase if ASCII
68
69 Raises UnicodeDecodeError if non-ASCII characters are found."""
70
71 def jsonescapeu8fast(self, u8chars: bytes, paranoid: bool) -> bytes:
72 """Convert a UTF-8 byte string to JSON-escaped form (fast path)
73
74 Raises ValueError if non-ASCII characters have to be escaped.
75 """