diff mercurial/interfaces/modules.py @ 51976: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
line wrap: on
line diff
--- a/mercurial/interfaces/modules.py	Fri Oct 04 23:23:24 2024 -0400
+++ b/mercurial/interfaces/modules.py	Sat Oct 05 15:00:37 2024 -0400
@@ -50,3 +50,26 @@
 
     xdiffblocks: Optional[BDiffBlocksFnc]
     """This method is currently only available in the ``cext`` module."""
+
+
+class CharEncoding(Protocol):
+    """A Protocol class for the various charencoding module implementations."""
+
+    def isasciistr(self, s: bytes) -> bool:
+        """Can the byte string be decoded with the ``ascii`` codec?"""
+
+    def asciilower(self, s: bytes) -> bytes:
+        """convert a string to lowercase if ASCII
+
+        Raises UnicodeDecodeError if non-ASCII characters are found."""
+
+    def asciiupper(self, s: bytes) -> bytes:
+        """convert a string to uppercase if ASCII
+
+        Raises UnicodeDecodeError if non-ASCII characters are found."""
+
+    def jsonescapeu8fast(self, u8chars: bytes, paranoid: bool) -> bytes:
+        """Convert a UTF-8 byte string to JSON-escaped form (fast path)
+
+        Raises ValueError if non-ASCII characters have to be escaped.
+        """