aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/Twisted/py3/twisted/protocols
diff options
context:
space:
mode:
authorrobot-piglet <robot-piglet@yandex-team.com>2024-03-16 14:04:41 +0300
committerrobot-piglet <robot-piglet@yandex-team.com>2024-03-16 14:15:11 +0300
commitd83e7ea91cbc5e5d4ea49195eeab84c364dba4bb (patch)
tree5ffdbed1c2a932a8d41089a6ac2766dd783ad15b /contrib/python/Twisted/py3/twisted/protocols
parentf7d1b63f8a2e18da4372628a2917fa0c508ba43a (diff)
downloadydb-d83e7ea91cbc5e5d4ea49195eeab84c364dba4bb.tar.gz
Intermediate changes
Diffstat (limited to 'contrib/python/Twisted/py3/twisted/protocols')
-rw-r--r--contrib/python/Twisted/py3/twisted/protocols/amp.py65
-rw-r--r--contrib/python/Twisted/py3/twisted/protocols/haproxy/_v2parser.py2
-rw-r--r--contrib/python/Twisted/py3/twisted/protocols/tls.py34
3 files changed, 59 insertions, 42 deletions
diff --git a/contrib/python/Twisted/py3/twisted/protocols/amp.py b/contrib/python/Twisted/py3/twisted/protocols/amp.py
index a8c35754a9..ac28a92bfa 100644
--- a/contrib/python/Twisted/py3/twisted/protocols/amp.py
+++ b/contrib/python/Twisted/py3/twisted/protocols/amp.py
@@ -202,7 +202,18 @@ from io import BytesIO
from itertools import count
from struct import pack
from types import MethodType
-from typing import Any, Callable, Dict, List, Optional, Tuple, Type, TypeVar, Union
+from typing import (
+ Any,
+ Callable,
+ ClassVar,
+ Dict,
+ List,
+ Optional,
+ Tuple,
+ Type,
+ TypeVar,
+ Union,
+)
from zope.interface import Interface, implementer
@@ -1686,18 +1697,23 @@ class Descriptor(Integer):
return outString
+_Self = TypeVar("_Self")
+
+
class _CommandMeta(type):
"""
Metaclass hack to establish reverse-mappings for 'errors' and
'fatalErrors' as class vars.
"""
- def __new__(cls, name, bases, attrs):
+ def __new__(
+ cls: type[_Self], name: str, bases: tuple[type], attrs: dict[str, object]
+ ) -> Type[Command]:
reverseErrors = attrs["reverseErrors"] = {}
er = attrs["allErrors"] = {}
if "commandName" not in attrs:
attrs["commandName"] = name.encode("ascii")
- newtype = type.__new__(cls, name, bases, attrs)
+ newtype: Type[Command] = type.__new__(cls, name, bases, attrs) # type:ignore
if not isinstance(newtype.commandName, bytes):
raise TypeError(
@@ -1705,12 +1721,12 @@ class _CommandMeta(type):
newtype.commandName
)
)
- for name, _ in newtype.arguments:
- if not isinstance(name, bytes):
- raise TypeError(f"Argument names must be byte strings, got: {name!r}")
- for name, _ in newtype.response:
- if not isinstance(name, bytes):
- raise TypeError(f"Response names must be byte strings, got: {name!r}")
+ for bname, _ in newtype.arguments:
+ if not isinstance(bname, bytes):
+ raise TypeError(f"Argument names must be byte strings, got: {bname!r}")
+ for bname, _ in newtype.response:
+ if not isinstance(bname, bytes):
+ raise TypeError(f"Response names must be byte strings, got: {bname!r}")
errors: Dict[Type[Exception], bytes] = {}
fatalErrors: Dict[Type[Exception], bytes] = {}
@@ -1718,9 +1734,9 @@ class _CommandMeta(type):
accumulateClassDict(newtype, "fatalErrors", fatalErrors)
if not isinstance(newtype.errors, dict):
- newtype.errors = dict(newtype.errors)
+ newtype.errors = dict(newtype.errors) # type:ignore[unreachable]
if not isinstance(newtype.fatalErrors, dict):
- newtype.fatalErrors = dict(newtype.fatalErrors)
+ newtype.fatalErrors = dict(newtype.fatalErrors) # type:ignore[unreachable]
for v, k in errors.items():
reverseErrors[k] = v
@@ -1729,13 +1745,13 @@ class _CommandMeta(type):
reverseErrors[k] = v
er[v] = k
- for _, name in newtype.errors.items():
- if not isinstance(name, bytes):
- raise TypeError(f"Error names must be byte strings, got: {name!r}")
- for _, name in newtype.fatalErrors.items():
- if not isinstance(name, bytes):
+ for _, bname in newtype.errors.items():
+ if not isinstance(bname, bytes):
+ raise TypeError(f"Error names must be byte strings, got: {bname!r}")
+ for _, bname in newtype.fatalErrors.items():
+ if not isinstance(bname, bytes):
raise TypeError(
- f"Fatal error names must be byte strings, got: {name!r}"
+ f"Fatal error names must be byte strings, got: {bname!r}"
)
return newtype
@@ -1784,14 +1800,15 @@ class Command(metaclass=_CommandMeta):
want one.
"""
- arguments: List[Tuple[bytes, Argument]] = []
- response: List[Tuple[bytes, Argument]] = []
- extra: List[Any] = []
- errors: Dict[Type[Exception], bytes] = {}
- fatalErrors: Dict[Type[Exception], bytes] = {}
+ commandName: ClassVar[bytes]
+ arguments: ClassVar[List[Tuple[bytes, Argument]]] = []
+ response: ClassVar[List[Tuple[bytes, Argument]]] = []
+ extra: ClassVar[List[Any]] = []
+ errors: ClassVar[Dict[Type[Exception], bytes]] = {}
+ fatalErrors: ClassVar[Dict[Type[Exception], bytes]] = {}
- commandType: "Union[Type[Command], Type[Box]]" = Box
- responseType: Type[AmpBox] = Box
+ commandType: "ClassVar[Union[Type[Command], Type[Box]]]" = Box
+ responseType: ClassVar[Type[AmpBox]] = Box
requiresAnswer = True
diff --git a/contrib/python/Twisted/py3/twisted/protocols/haproxy/_v2parser.py b/contrib/python/Twisted/py3/twisted/protocols/haproxy/_v2parser.py
index 5b8e587401..cfcf7c99bc 100644
--- a/contrib/python/Twisted/py3/twisted/protocols/haproxy/_v2parser.py
+++ b/contrib/python/Twisted/py3/twisted/protocols/haproxy/_v2parser.py
@@ -13,7 +13,7 @@ from typing import Callable, Tuple, Type, Union
from zope.interface import implementer
-from constantly import ValueConstant, Values # type: ignore[import]
+from constantly import ValueConstant, Values
from typing_extensions import Literal
from twisted.internet import address
diff --git a/contrib/python/Twisted/py3/twisted/protocols/tls.py b/contrib/python/Twisted/py3/twisted/protocols/tls.py
index d2ac2d2cf8..be7a58ce4d 100644
--- a/contrib/python/Twisted/py3/twisted/protocols/tls.py
+++ b/contrib/python/Twisted/py3/twisted/protocols/tls.py
@@ -708,7 +708,7 @@ class _AggregateSmallWrites:
self._write = write
self._clock = clock
self._buffer: list[bytes] = []
- self._bufferLen = 0
+ self._bufferLeft = self.MAX_BUFFER_SIZE
self._scheduled: Optional[IDelayedCall] = None
def write(self, data: bytes) -> None:
@@ -719,9 +719,9 @@ class _AggregateSmallWrites:
Accumulating too much data can result in higher memory usage.
"""
self._buffer.append(data)
- self._bufferLen += len(data)
+ self._bufferLeft -= len(data)
- if self._bufferLen > self.MAX_BUFFER_SIZE:
+ if self._bufferLeft < 0:
# We've accumulated enough we should just write it out. No need to
# schedule a flush, since we just flushed everything.
self.flush()
@@ -744,7 +744,7 @@ class _AggregateSmallWrites:
def flush(self) -> None:
"""Flush any buffered writes."""
if self._buffer:
- self._bufferLen = 0
+ self._bufferLeft = self.MAX_BUFFER_SIZE
self._write(b"".join(self._buffer))
del self._buffer[:]
@@ -764,19 +764,20 @@ def _get_default_clock() -> IReactorTime:
class BufferingTLSTransport(TLSMemoryBIOProtocol):
"""
A TLS transport implemented by wrapping buffering around a
- ``TLSMemoryBIOProtocol``.
+ L{TLSMemoryBIOProtocol}.
- Doing many small writes directly to a ``OpenSSL.SSL.Connection``, as
- implemented in ``TLSMemoryBIOProtocol``, can add significant CPU and
+ Doing many small writes directly to a L{OpenSSL.SSL.Connection}, as
+ implemented in L{TLSMemoryBIOProtocol}, can add significant CPU and
bandwidth overhead. Thus, even when writing is possible, small writes will
get aggregated and written as a single write at the next reactor iteration.
"""
- # Note: An implementation based on composition would be nicer, but there's
- # close integration between ``ProtocolWrapper`` subclasses like
- # ``TLSMemoryBIOProtocol`` and the corresponding factory. Composition broke
- # things like ``TLSMemoryBIOFactory.protocols`` having the correct
- # instances, whereas subclassing makes that work.
+ # Implementation Note: An implementation based on composition would be
+ # nicer, but there's close integration between L{ProtocolWrapper}
+ # subclasses like L{TLSMemoryBIOProtocol} and the corresponding factory. An
+ # attempt to implement this with broke things like
+ # L{TLSMemoryBIOFactory.protocols} having the correct instances, whereas
+ # subclassing makes that work.
def __init__(
self,
@@ -787,11 +788,10 @@ class BufferingTLSTransport(TLSMemoryBIOProtocol):
super().__init__(factory, wrappedProtocol, _connectWrapped)
actual_write = super().write
self._aggregator = _AggregateSmallWrites(actual_write, factory._clock)
-
- def write(self, data: bytes) -> None:
- if isinstance(data, str): # type: ignore[unreachable]
- raise TypeError("Must write bytes to a TLS transport, not str.")
- self._aggregator.write(data)
+ # This is kinda ugly, but speeds things up a lot in a hot path with
+ # lots of small TLS writes. May become unnecessary in Python 3.13 or
+ # later if JIT and/or inlining becomes a thing.
+ self.write = self._aggregator.write # type: ignore[method-assign]
def writeSequence(self, sequence: Iterable[bytes]) -> None:
self._aggregator.write(b"".join(sequence))