|
36 | 36 | ) |
37 | 37 |
|
38 | 38 | import attr |
39 | | -import multipart |
40 | 39 | import treq |
41 | 40 | from canonicaljson import encode_canonical_json |
42 | 41 | from netaddr import AddrFormatError, IPAddress, IPSet |
|
93 | 92 | if TYPE_CHECKING: |
94 | 93 | from synapse.server import HomeServer |
95 | 94 |
|
| 95 | +# Support both import names for the `python-multipart` (PyPI) library, |
| 96 | +# which renamed its package name from `multipart` to `python_multipart` |
| 97 | +# in 0.0.13 (though supports the old import name for compatibility). |
| 98 | +# Note that the `multipart` package name conflicts with `multipart` (PyPI) |
| 99 | +# so we should prefer importing from `python_multipart` when possible. |
| 100 | +try: |
| 101 | + from python_multipart import MultipartParser |
| 102 | + |
| 103 | + if TYPE_CHECKING: |
| 104 | + from python_multipart import multipart |
| 105 | +except ImportError: |
| 106 | + from multipart import MultipartParser # type: ignore[no-redef] |
| 107 | + |
| 108 | + |
96 | 109 | logger = logging.getLogger(__name__) |
97 | 110 |
|
98 | 111 | outgoing_requests_counter = Counter("synapse_http_client_requests", "", ["method"]) |
@@ -1039,7 +1052,7 @@ def __init__( |
1039 | 1052 | self.deferred = deferred |
1040 | 1053 | self.boundary = boundary |
1041 | 1054 | self.max_length = max_length |
1042 | | - self.parser: Optional[multipart.MultipartParser] = None |
| 1055 | + self.parser: Optional[MultipartParser] = None |
1043 | 1056 | self.multipart_response = MultipartResponse() |
1044 | 1057 | self.has_redirect = False |
1045 | 1058 | self.in_json = False |
@@ -1097,12 +1110,12 @@ def on_part_data(data: bytes, start: int, end: int) -> None: |
1097 | 1110 | self.deferred.errback() |
1098 | 1111 | self.file_length += end - start |
1099 | 1112 |
|
1100 | | - callbacks: "multipart.multipart.MultipartCallbacks" = { |
| 1113 | + callbacks: "multipart.MultipartCallbacks" = { |
1101 | 1114 | "on_header_field": on_header_field, |
1102 | 1115 | "on_header_value": on_header_value, |
1103 | 1116 | "on_part_data": on_part_data, |
1104 | 1117 | } |
1105 | | - self.parser = multipart.MultipartParser(self.boundary, callbacks) |
| 1118 | + self.parser = MultipartParser(self.boundary, callbacks) |
1106 | 1119 |
|
1107 | 1120 | self.total_length += len(incoming_data) |
1108 | 1121 | if self.max_length is not None and self.total_length >= self.max_length: |
|
0 commit comments