Skip to content

Commit cdac0a5

Browse files
authored
Merge pull request #2796 from lunkwill42/upgrade/twisted
Upgrade Twisted to a version compatible with both Python 3.7 and 3.11
2 parents 106ef20 + 68e0641 commit cdac0a5

File tree

4 files changed

+21
-7
lines changed

4 files changed

+21
-7
lines changed

python/nav/ipdevpoll/pool.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,25 @@ def __init__(self, is_worker, **kwargs):
158158
self.lost_handler = None
159159

160160
def makeConnection(self, transport):
161-
"""Called when a connection has been made to the AMP endpoint"""
162-
if not hasattr(transport, 'getPeer'):
161+
"""Overrides the base implementation to fake the required getPeer() and
162+
getHost() methods on the incoming process transport object, if needed ( the
163+
base AMP class was not really designed with process pipe transports in mind,
164+
but with IP transports).
165+
166+
Process transports in Twisted<21 did not implement these methods at all,
167+
while in Twisted>=21 they resolve to base methods that raise
168+
`NotImplementError`.
169+
"""
170+
try:
171+
transport.getPeer()
172+
except (AttributeError, NotImplementedError):
163173
setattr(transport, 'getPeer', lambda: "peer")
164-
if not hasattr(transport, 'getHost'):
174+
175+
try:
176+
transport.getHost()
177+
except (AttributeError, NotImplementedError):
165178
setattr(transport, 'getHost', lambda: "host")
179+
166180
super(ProcessAMP, self).makeConnection(transport)
167181

168182
def connectionLost(self, reason):

python/nav/mibs/mibretriever.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
from twisted.internet import defer, reactor
3838
from twisted.internet.defer import returnValue
3939
from twisted.internet.error import TimeoutError
40+
from twisted.python.failure import Failure
4041

4142
from nav.Snmp import safestring
4243
from nav.ipdevpoll import ContextLogger
@@ -428,7 +429,7 @@ def _result_formatter(result):
428429

429430
return formatted_result
430431

431-
def _snmp_timeout_handler(failure: defer.failure.Failure):
432+
def _snmp_timeout_handler(failure: Failure):
432433
"""Transforms SnmpTimeoutErrors into "regular" TimeoutErrors"""
433434
failure.trap(SnmpTimeoutError)
434435
raise TimeoutError(failure.value)

requirements/base.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ psycopg2==2.9.9 # requires libpq to build
66
IPy==1.01
77
pyaml
88

9-
twisted>=20.0.0,<21
10-
9+
twisted~=23.8.0 # last version that still supports Python 3.7
1110

1211
networkx==2.6.3
1312
Pillow>3.3.2

tests/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pytest-metadata<2.0.0
99
pytest-cov==2.7.1
1010
pytest-selenium==2.0.1
1111
pytest-timeout
12-
pytest-twisted==1.12
12+
pytest-twisted~=1.13.0
1313
pytidylib==0.3.2
1414
selenium==3.141.0
1515
whisper>=0.9.9

0 commit comments

Comments
 (0)