xmlrpc: improve bytes handling (#9166)

This commit is contained in:
Jelle Zijlstra
2022-11-25 19:33:53 -08:00
committed by GitHub
parent 553e7d46f0
commit 9452ce539c
2 changed files with 32 additions and 19 deletions

View File

@@ -2,7 +2,7 @@ import gzip
import http.client
import sys
import time
from _typeshed import Self, SupportsRead, SupportsWrite
from _typeshed import ReadableBuffer, Self, SupportsRead, SupportsWrite, _BufferWithLen
from collections.abc import Callable, Iterable, Mapping
from datetime import datetime
from io import BytesIO
@@ -15,7 +15,20 @@ class _SupportsTimeTuple(Protocol):
_DateTimeComparable: TypeAlias = DateTime | datetime | str | _SupportsTimeTuple
_Marshallable: TypeAlias = (
bool | int | float | str | bytes | None | tuple[Any, ...] | list[Any] | dict[Any, Any] | datetime | DateTime | Binary
bool
| int
| float
| str
| bytes
| bytearray
| None
| tuple[_Marshallable, ...]
# Ideally we'd use _Marshallable for list and dict, but invariance makes that impractical
| list[Any]
| dict[str, Any]
| datetime
| DateTime
| Binary
)
_XMLDate: TypeAlias = int | datetime | tuple[int, ...] | time.struct_time
_HostType: TypeAlias = Union[tuple[str, dict[str, str]], str]
@@ -83,18 +96,18 @@ def _datetime_type(data: str) -> datetime: ... # undocumented
class Binary:
data: bytes
def __init__(self, data: bytes | None = ...) -> None: ...
def decode(self, data: bytes) -> None: ...
def __init__(self, data: bytes | bytearray | None = ...) -> None: ...
def decode(self, data: ReadableBuffer) -> None: ...
def encode(self, out: SupportsWrite[str]) -> None: ...
def __eq__(self, other: object) -> bool: ...
def _binary(data: bytes) -> Binary: ... # undocumented
def _binary(data: ReadableBuffer) -> Binary: ... # undocumented
WRAPPERS: tuple[type[DateTime], type[Binary]] # undocumented
class ExpatParser: # undocumented
def __init__(self, target: Unmarshaller) -> None: ...
def feed(self, data: str | bytes) -> None: ...
def feed(self, data: str | ReadableBuffer) -> None: ...
def close(self) -> None: ...
_WriteCallback: TypeAlias = Callable[[str], object]
@@ -115,7 +128,7 @@ class Marshaller:
def dump_int(self, value: int, write: _WriteCallback) -> None: ...
def dump_double(self, value: float, write: _WriteCallback) -> None: ...
def dump_unicode(self, value: str, write: _WriteCallback, escape: Callable[[str], str] = ...) -> None: ...
def dump_bytes(self, value: bytes, write: _WriteCallback) -> None: ...
def dump_bytes(self, value: ReadableBuffer, write: _WriteCallback) -> None: ...
def dump_array(self, value: Iterable[_Marshallable], write: _WriteCallback) -> None: ...
def dump_struct(
self, value: Mapping[str, _Marshallable], write: _WriteCallback, escape: Callable[[str], str] = ...
@@ -196,13 +209,13 @@ def dumps(
allow_none: bool = ...,
) -> str: ...
def loads(data: str, use_datetime: bool = ..., use_builtin_types: bool = ...) -> tuple[tuple[_Marshallable, ...], str | None]: ...
def gzip_encode(data: bytes) -> bytes: ... # undocumented
def gzip_decode(data: bytes, max_decode: int = ...) -> bytes: ... # undocumented
def gzip_encode(data: ReadableBuffer) -> bytes: ... # undocumented
def gzip_decode(data: ReadableBuffer, max_decode: int = ...) -> bytes: ... # undocumented
class GzipDecodedResponse(gzip.GzipFile): # undocumented
io: BytesIO
def __init__(self, response: SupportsRead[bytes]) -> None: ...
def __init__(self, response: SupportsRead[ReadableBuffer]) -> None: ...
class _Method: # undocumented
@@ -231,17 +244,21 @@ class Transport:
else:
def __init__(self, use_datetime: bool = ..., use_builtin_types: bool = ...) -> None: ...
def request(self, host: _HostType, handler: str, request_body: bytes, verbose: bool = ...) -> tuple[_Marshallable, ...]: ...
def request(
self, host: _HostType, handler: str, request_body: _BufferWithLen, verbose: bool = ...
) -> tuple[_Marshallable, ...]: ...
def single_request(
self, host: _HostType, handler: str, request_body: bytes, verbose: bool = ...
self, host: _HostType, handler: str, request_body: _BufferWithLen, verbose: bool = ...
) -> tuple[_Marshallable, ...]: ...
def getparser(self) -> tuple[ExpatParser, Unmarshaller]: ...
def get_host_info(self, host: _HostType) -> tuple[str, list[tuple[str, str]], dict[str, str]]: ...
def make_connection(self, host: _HostType) -> http.client.HTTPConnection: ...
def close(self) -> None: ...
def send_request(self, host: _HostType, handler: str, request_body: bytes, debug: bool) -> http.client.HTTPConnection: ...
def send_request(
self, host: _HostType, handler: str, request_body: _BufferWithLen, debug: bool
) -> http.client.HTTPConnection: ...
def send_headers(self, connection: http.client.HTTPConnection, headers: list[tuple[str, str]]) -> None: ...
def send_content(self, connection: http.client.HTTPConnection, request_body: bytes) -> None: ...
def send_content(self, connection: http.client.HTTPConnection, request_body: _BufferWithLen) -> None: ...
def parse_response(self, response: http.client.HTTPResponse) -> tuple[_Marshallable, ...]: ...
class SafeTransport(Transport):

View File

@@ -2,14 +2,10 @@ import http.server
import pydoc
import socketserver
from collections.abc import Callable, Iterable, Mapping
from datetime import datetime
from re import Pattern
from typing import Any, ClassVar, Protocol
from typing_extensions import TypeAlias
from xmlrpc.client import Fault
# TODO: Recursive type on tuple, list, dict
_Marshallable: TypeAlias = None | bool | int | float | str | bytes | tuple[Any, ...] | list[Any] | dict[Any, Any] | datetime
from xmlrpc.client import Fault, _Marshallable
# The dispatch accepts anywhere from 0 to N arguments, no easy way to allow this in mypy
class _DispatchArity0(Protocol):