Skip to content

Commit 16a639e

Browse files
authored
Remove some obsolete Twisted version checks. (#18729)
Follows: #18727 --------- Signed-off-by: Olivier 'reivilibre <oliverw@matrix.org>
1 parent a2ba909 commit 16a639e

3 files changed

Lines changed: 5 additions & 62 deletions

File tree

changelog.d/18729.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Bump minimum version bound on Twisted to 21.2.0.

synapse/__init__.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,6 @@
4545

4646
# Allow using the asyncio reactor via env var.
4747
if strtobool(os.environ.get("SYNAPSE_ASYNC_IO_REACTOR", "0")):
48-
from incremental import Version
49-
50-
import twisted
51-
52-
# We need a bugfix that is included in Twisted 21.2.0:
53-
# https://twistedmatrix.com/trac/ticket/9787
54-
if twisted.version < Version("Twisted", 21, 2, 0):
55-
print("Using asyncio reactor requires Twisted>=21.2.0")
56-
sys.exit(1)
57-
5848
import asyncio
5949

6050
from twisted.internet import asyncioreactor

synapse/handlers/send_email.py

Lines changed: 4 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,13 @@
2424
from email.mime.multipart import MIMEMultipart
2525
from email.mime.text import MIMEText
2626
from io import BytesIO
27-
from typing import TYPE_CHECKING, Any, Dict, Optional
27+
from typing import TYPE_CHECKING, Dict, Optional
2828

29-
from pkg_resources import parse_version
30-
31-
import twisted
3229
from twisted.internet.defer import Deferred
3330
from twisted.internet.endpoints import HostnameEndpoint
34-
from twisted.internet.interfaces import IOpenSSLContextFactory, IProtocolFactory
31+
from twisted.internet.interfaces import IProtocolFactory
3532
from twisted.internet.ssl import optionsForClientTLS
36-
from twisted.mail.smtp import ESMTPSender, ESMTPSenderFactory
33+
from twisted.mail.smtp import ESMTPSenderFactory
3734
from twisted.protocols.tls import TLSMemoryBIOFactory
3835

3936
from synapse.logging.context import make_deferred_yieldable
@@ -44,49 +41,6 @@
4441

4542
logger = logging.getLogger(__name__)
4643

47-
_is_old_twisted = parse_version(twisted.__version__) < parse_version("21")
48-
49-
50-
class _BackportESMTPSender(ESMTPSender):
51-
"""Extend old versions of ESMTPSender to configure TLS.
52-
53-
Unfortunately, before Twisted 21.2, ESMTPSender doesn't give an easy way to
54-
disable TLS, or to configure the hostname used for TLS certificate validation.
55-
This backports the `hostname` parameter for that functionality.
56-
"""
57-
58-
__hostname: Optional[str]
59-
60-
def __init__(self, *args: Any, **kwargs: Any) -> None:
61-
""""""
62-
self.__hostname = kwargs.pop("hostname", None)
63-
super().__init__(*args, **kwargs)
64-
65-
def _getContextFactory(self) -> Optional[IOpenSSLContextFactory]:
66-
if self.context is not None:
67-
return self.context
68-
elif self.__hostname is None:
69-
return None # disable TLS if hostname is None
70-
return optionsForClientTLS(self.__hostname)
71-
72-
73-
class _BackportESMTPSenderFactory(ESMTPSenderFactory):
74-
"""An ESMTPSenderFactory for _BackportESMTPSender.
75-
76-
This backports the `hostname` parameter, to disable or configure TLS.
77-
"""
78-
79-
__hostname: Optional[str]
80-
81-
def __init__(self, *args: Any, **kwargs: Any) -> None:
82-
self.__hostname = kwargs.pop("hostname", None)
83-
super().__init__(*args, **kwargs)
84-
85-
def protocol(self, *args: Any, **kwargs: Any) -> ESMTPSender: # type: ignore
86-
# this overrides ESMTPSenderFactory's `protocol` attribute, with a Callable
87-
# instantiating our _BackportESMTPSender, providing the hostname parameter
88-
return _BackportESMTPSender(*args, **kwargs, hostname=self.__hostname)
89-
9044

9145
async def _sendmail(
9246
reactor: ISynapseReactor,
@@ -129,9 +83,7 @@ async def _sendmail(
12983
elif tlsname is None:
13084
tlsname = smtphost
13185

132-
factory: IProtocolFactory = (
133-
_BackportESMTPSenderFactory if _is_old_twisted else ESMTPSenderFactory
134-
)(
86+
factory: IProtocolFactory = ESMTPSenderFactory(
13587
username,
13688
password,
13789
from_addr,

0 commit comments

Comments
 (0)