Skip to content

Commit

Permalink
Support both import names of PyPI package python-multipart. (#17932)
Browse files Browse the repository at this point in the history
  • Loading branch information
renaudallard authored Nov 20, 2024
1 parent 1092a35 commit 8291aa8
Showing 2 changed files with 18 additions and 4 deletions.
1 change: 1 addition & 0 deletions changelog.d/17932.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Support new package name of PyPI package `python-multipart` 0.0.13 so that distro packagers do not need to work around name conflict with PyPI package `multipart`.
21 changes: 17 additions & 4 deletions synapse/http/client.py
Original file line number Diff line number Diff line change
@@ -36,7 +36,6 @@
)

import attr
import multipart
import treq
from canonicaljson import encode_canonical_json
from netaddr import AddrFormatError, IPAddress, IPSet
@@ -93,6 +92,20 @@
if TYPE_CHECKING:
from synapse.server import HomeServer

# Support both import names for the `python-multipart` (PyPI) library,
# which renamed its package name from `multipart` to `python_multipart`
# in 0.0.13 (though supports the old import name for compatibility).
# Note that the `multipart` package name conflicts with `multipart` (PyPI)
# so we should prefer importing from `python_multipart` when possible.
try:
from python_multipart import MultipartParser

if TYPE_CHECKING:
from python_multipart import multipart
except ImportError:
from multipart import MultipartParser # type: ignore[no-redef]


logger = logging.getLogger(__name__)

outgoing_requests_counter = Counter("synapse_http_client_requests", "", ["method"])
@@ -1039,7 +1052,7 @@ def __init__(
self.deferred = deferred
self.boundary = boundary
self.max_length = max_length
self.parser: Optional[multipart.MultipartParser] = None
self.parser: Optional[MultipartParser] = None
self.multipart_response = MultipartResponse()
self.has_redirect = False
self.in_json = False
@@ -1097,12 +1110,12 @@ def on_part_data(data: bytes, start: int, end: int) -> None:
self.deferred.errback()
self.file_length += end - start

callbacks: "multipart.multipart.MultipartCallbacks" = {
callbacks: "multipart.MultipartCallbacks" = {
"on_header_field": on_header_field,
"on_header_value": on_header_value,
"on_part_data": on_part_data,
}
self.parser = multipart.MultipartParser(self.boundary, callbacks)
self.parser = MultipartParser(self.boundary, callbacks)

self.total_length += len(incoming_data)
if self.max_length is not None and self.total_length >= self.max_length:

0 comments on commit 8291aa8

Please sign in to comment.