dev-python/twisted: Port to py39
authorMichał Górny <mgorny@gentoo.org>
Wed, 27 May 2020 15:10:26 +0000 (17:10 +0200)
committerMichał Górny <mgorny@gentoo.org>
Wed, 27 May 2020 15:22:13 +0000 (17:22 +0200)
Signed-off-by: Michał Górny <mgorny@gentoo.org>
dev-python/twisted/files/twisted-19.10.0-py39-b64.patch [new file with mode: 0644]
dev-python/twisted/files/twisted-20.3.0-py39-b64.patch [new file with mode: 0644]
dev-python/twisted/files/twisted-20.3.0-py39-combined.patch [new file with mode: 0644]
dev-python/twisted/twisted-19.10.0.ebuild
dev-python/twisted/twisted-20.3.0.ebuild

diff --git a/dev-python/twisted/files/twisted-19.10.0-py39-b64.patch b/dev-python/twisted/files/twisted-19.10.0-py39-b64.patch
new file mode 100644 (file)
index 0000000..f67d624
--- /dev/null
@@ -0,0 +1,165 @@
+From f56133a2e0d7ddf9ee6e43bf9e1d62e970cb0b3a Mon Sep 17 00:00:00 2001
+From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= <mgorny@gentoo.org>
+Date: Wed, 27 May 2020 13:23:37 +0200
+Subject: [PATCH] Replace base64.*string() functions to fix py3.9 support
+
+Replace base64.decodestring() and .encodestring() functions as they
+were deprecated since Python 3.1 in favor of (equivalent) .decodebytes()
+and .encodebytes(), and were eventually removed in Python 3.9.
+
+While at it, replace most of their uses with base64.b64encode()
+and .b64decode() that are preferable to the former wrt ticket #6446,
+and they do not introduce line breaks that the twisted code usually
+discarded.
+
+Use .decodebytes() and .encodebytes() in DirDBM as it seems to rely
+on the exact presence of newlines, and changing that would break
+backwards compatibility.
+
+Fixes: ticket:6446
+Fixes: ticket:9831
+---
+ src/twisted/conch/scripts/tkconch.py          |  2 +-
+ src/twisted/conch/test/test_keys.py           |  2 +-
+ src/twisted/mail/pop3.py                      |  4 ++--
+ src/twisted/mail/test/test_pop3.py            |  4 ++--
+ src/twisted/persisted/dirdbm.py               | 10 ++++++++--
+ src/twisted/web/http.py                       |  2 +-
+ src/twisted/web/test/test_http.py             |  6 +++---
+ 14 files changed, 18 insertions(+), 12 deletions(-)
+
+diff --git a/src/twisted/conch/scripts/tkconch.py b/src/twisted/conch/scripts/tkconch.py
+index 9c48e8a7f..5e007ebdc 100644
+--- a/src/twisted/conch/scripts/tkconch.py
++++ b/src/twisted/conch/scripts/tkconch.py
+@@ -409,7 +409,7 @@ class SSHClientTransport(transport.SSHClientTransport):
+                 "known hosts.\r\n" %
+                 (khHost, {b'ssh-dss':'DSA', b'ssh-rsa':'RSA'}[keyType]))
+             with open(os.path.expanduser('~/.ssh/known_hosts'), 'a') as known_hosts:
+-                encodedKey = base64.encodestring(pubKey).replace(b'\n', b'')
++                encodedKey = base64.b64encode(pubKey)
+                 known_hosts.write('\n%s %s %s' % (khHost, keyType, encodedKey))
+         except:
+             log.deferr()
+diff --git a/src/twisted/conch/test/test_keys.py b/src/twisted/conch/test/test_keys.py
+index 41e49f415..795e7b8d7 100644
+--- a/src/twisted/conch/test/test_keys.py
++++ b/src/twisted/conch/test/test_keys.py
+@@ -352,7 +352,7 @@ SUrCyZXsNh6VXwjs3gKQ
+         self.assertRaises(
+             keys.BadKeyError,
+-            keys.Key.fromString, data=b'{' + base64.encodestring(sexp) + b'}',
++            keys.Key.fromString, data=b'{' + base64.b64encode(sexp) + b'}',
+             )
+diff --git a/src/twisted/mail/pop3.py b/src/twisted/mail/pop3.py
+index ffe9714c9..057389e3a 100644
+--- a/src/twisted/mail/pop3.py
++++ b/src/twisted/mail/pop3.py
+@@ -728,7 +728,7 @@ class POP3(basic.LineOnlyReceiver, policies.TimeoutMixin):
+         self._auth = auth()
+         chal = self._auth.getChallenge()
+-        self.sendLine(b'+ ' + base64.encodestring(chal).rstrip(b'\n'))
++        self.sendLine(b'+ ' + base64.b64encode(chal))
+         self.state = 'AUTH'
+@@ -747,7 +747,7 @@ class POP3(basic.LineOnlyReceiver, policies.TimeoutMixin):
+         """
+         self.state = "COMMAND"
+         try:
+-            parts = base64.decodestring(line).split(None, 1)
++            parts = base64.b64decode(line).split(None, 1)
+         except binascii.Error:
+             self.failResponse(b"Invalid BASE64 encoding")
+         else:
+diff --git a/src/twisted/mail/test/test_pop3.py b/src/twisted/mail/test/test_pop3.py
+index ea513487c..36780d9c9 100644
+--- a/src/twisted/mail/test/test_pop3.py
++++ b/src/twisted/mail/test/test_pop3.py
+@@ -1097,12 +1097,12 @@ class SASLTests(unittest.TestCase):
+         p.lineReceived(b"AUTH CRAM-MD5")
+         chal = s.getvalue().splitlines()[-1][2:]
+-        chal = base64.decodestring(chal)
++        chal = base64.b64decode(chal)
+         response = hmac.HMAC(b'testpassword', chal,
+                              digestmod=md5).hexdigest().encode("ascii")
+         p.lineReceived(
+-            base64.encodestring(b'testuser ' + response).rstrip(b'\n'))
++            base64.b64encode(b'testuser ' + response))
+         self.assertTrue(p.mbox)
+         self.assertTrue(s.getvalue().splitlines()[-1].find(b"+OK") >= 0)
+         p.connectionLost(failure.Failure(Exception("Test harness disconnect")))
+diff --git a/src/twisted/persisted/dirdbm.py b/src/twisted/persisted/dirdbm.py
+index f97c526d0..d9f29cce2 100644
+--- a/src/twisted/persisted/dirdbm.py
++++ b/src/twisted/persisted/dirdbm.py
+@@ -81,14 +81,20 @@ class DirDBM:
+         Encode a key so it can be used as a filename.
+         """
+         # NOTE: '_' is NOT in the base64 alphabet!
+-        return base64.encodestring(k).replace(b'\n', b'_').replace(b"/", b"-")
++        try:
++            return base64.encodebytes(k).replace(b'\n', b'_').replace(b"/", b"-")
++        except AttributeError:
++            return base64.encodestring(k).replace(b'\n', b'_').replace(b"/", b"-")
+     def _decode(self, k):
+         """
+         Decode a filename to get the key.
+         """
+-        return base64.decodestring(k.replace(b'_', b'\n').replace(b"-", b"/"))
++        try:
++            return base64.decodebytes(k.replace(b'_', b'\n').replace(b"-", b"/"))
++        except AttributeError:
++            return base64.decodestring(k.replace(b'_', b'\n').replace(b"-", b"/"))
+     def _readFile(self, path):
+diff --git a/src/twisted/web/http.py b/src/twisted/web/http.py
+index fe88d3373..602a58f31 100644
+--- a/src/twisted/web/http.py
++++ b/src/twisted/web/http.py
+@@ -1540,7 +1540,7 @@ class Request:
+             bas, upw = authh.split()
+             if bas.lower() != b"basic":
+                 raise ValueError()
+-            upw = base64.decodestring(upw)
++            upw = base64.b64decode(upw)
+             self.user, self.password = upw.split(b':', 1)
+         except (binascii.Error, ValueError):
+             self.user = self.password = ""
+diff --git a/src/twisted/web/test/test_http.py b/src/twisted/web/test/test_http.py
+index 6001d1e40..70065e232 100644
+--- a/src/twisted/web/test/test_http.py
++++ b/src/twisted/web/test/test_http.py
+@@ -1513,7 +1513,7 @@ class ParsingTests(unittest.TestCase):
+                 requests.append(self)
+         for u, p in [(b"foo", b"bar"), (b"hello", b"there:z")]:
+-            s = base64.encodestring(b":".join((u, p))).strip()
++            s = base64.b64encode(b":".join((u, p)))
+             f = b"GET / HTTP/1.0\nAuthorization: Basic " + s + b"\n\n"
+             self.runRequest(f, Request, 0)
+             req = requests.pop()
+@@ -2139,9 +2139,9 @@ Hello,
+         u = b"foo"
+         p = b"bar"
+-        s = base64.encodestring(b":".join((u, p))).strip()
++        s = base64.b64encode(b":".join((u, p)))
+         f = b"GET / HTTP/1.0\nAuthorization: Basic " + s + b"\n\n"
+-        self.patch(base64, 'decodestring', lambda x: [])
++        self.patch(base64, 'b64decode', lambda x: [])
+         self.runRequest(f, Request, 0)
+         req = requests.pop()
+         self.assertEqual(('', ''), req.credentials)
+-- 
+2.26.2
+
diff --git a/dev-python/twisted/files/twisted-20.3.0-py39-b64.patch b/dev-python/twisted/files/twisted-20.3.0-py39-b64.patch
new file mode 100644 (file)
index 0000000..f475614
--- /dev/null
@@ -0,0 +1,158 @@
+From f44c2ff111a8961d295409186cc07aaf414c76bc Mon Sep 17 00:00:00 2001
+From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= <mgorny@gentoo.org>
+Date: Wed, 27 May 2020 13:23:37 +0200
+Subject: [PATCH 1/4] Replace base64.*string() functions to fix py3.9 support
+
+Replace base64.decodestring() and .encodestring() functions as they
+were deprecated since Python 3.1 in favor of (equivalent) .decodebytes()
+and .encodebytes(), and were eventually removed in Python 3.9.
+
+While at it, replace most of their uses with base64.b64encode()
+and .b64decode() that are preferable to the former wrt ticket #6446,
+and they do not introduce line breaks that the twisted code usually
+discarded.
+
+Use .decodebytes() and .encodebytes() in DirDBM as it seems to rely
+on the exact presence of newlines, and changing that would break
+backwards compatibility.
+
+Fixes: ticket:6446
+Fixes: ticket:9831
+---
+ src/twisted/conch/scripts/tkconch.py          | 2 +-
+ src/twisted/conch/test/test_keys.py           | 2 +-
+ src/twisted/mail/pop3.py                      | 4 ++--
+ src/twisted/mail/test/test_pop3.py            | 4 ++--
+ src/twisted/persisted/dirdbm.py               | 4 ++--
+ src/twisted/web/http.py                       | 2 +-
+ src/twisted/web/test/test_http.py             | 6 +++---
+ 14 files changed, 12 insertions(+), 12 deletions(-)
+
+diff --git a/src/twisted/conch/scripts/tkconch.py b/src/twisted/conch/scripts/tkconch.py
+index a662cabc8..744734343 100644
+--- a/src/twisted/conch/scripts/tkconch.py
++++ b/src/twisted/conch/scripts/tkconch.py
+@@ -412,7 +412,7 @@ class SSHClientTransport(transport.SSHClientTransport):
+                 "known hosts.\r\n" %
+                 (khHost, {b'ssh-dss':'DSA', b'ssh-rsa':'RSA'}[keyType]))
+             with open(os.path.expanduser('~/.ssh/known_hosts'), 'a') as known_hosts:
+-                encodedKey = base64.encodestring(pubKey).replace(b'\n', b'')
++                encodedKey = base64.b64encode(pubKey)
+                 known_hosts.write('\n%s %s %s' % (khHost, keyType, encodedKey))
+         except:
+             log.deferr()
+diff --git a/src/twisted/conch/test/test_keys.py b/src/twisted/conch/test/test_keys.py
+index 650a19bfb..f76cbd1b4 100644
+--- a/src/twisted/conch/test/test_keys.py
++++ b/src/twisted/conch/test/test_keys.py
+@@ -404,7 +404,7 @@ SUrCyZXsNh6VXwjs3gKQ
+         self.assertRaises(
+             keys.BadKeyError,
+-            keys.Key.fromString, data=b'{' + base64.encodestring(sexp) + b'}',
++            keys.Key.fromString, data=b'{' + base64.b64encode(sexp) + b'}',
+             )
+diff --git a/src/twisted/mail/pop3.py b/src/twisted/mail/pop3.py
+index ffe9714c9..057389e3a 100644
+--- a/src/twisted/mail/pop3.py
++++ b/src/twisted/mail/pop3.py
+@@ -728,7 +728,7 @@ class POP3(basic.LineOnlyReceiver, policies.TimeoutMixin):
+         self._auth = auth()
+         chal = self._auth.getChallenge()
+-        self.sendLine(b'+ ' + base64.encodestring(chal).rstrip(b'\n'))
++        self.sendLine(b'+ ' + base64.b64encode(chal))
+         self.state = 'AUTH'
+@@ -747,7 +747,7 @@ class POP3(basic.LineOnlyReceiver, policies.TimeoutMixin):
+         """
+         self.state = "COMMAND"
+         try:
+-            parts = base64.decodestring(line).split(None, 1)
++            parts = base64.b64decode(line).split(None, 1)
+         except binascii.Error:
+             self.failResponse(b"Invalid BASE64 encoding")
+         else:
+diff --git a/src/twisted/mail/test/test_pop3.py b/src/twisted/mail/test/test_pop3.py
+index f7fbfaf1e..af335ab2d 100644
+--- a/src/twisted/mail/test/test_pop3.py
++++ b/src/twisted/mail/test/test_pop3.py
+@@ -1096,12 +1096,12 @@ class SASLTests(unittest.TestCase):
+         p.lineReceived(b"AUTH CRAM-MD5")
+         chal = s.getvalue().splitlines()[-1][2:]
+-        chal = base64.decodestring(chal)
++        chal = base64.b64decode(chal)
+         response = hmac.HMAC(b'testpassword', chal,
+                              digestmod=md5).hexdigest().encode("ascii")
+         p.lineReceived(
+-            base64.encodestring(b'testuser ' + response).rstrip(b'\n'))
++            base64.b64encode(b'testuser ' + response))
+         self.assertTrue(p.mbox)
+         self.assertTrue(s.getvalue().splitlines()[-1].find(b"+OK") >= 0)
+         p.connectionLost(failure.Failure(Exception("Test harness disconnect")))
+diff --git a/src/twisted/persisted/dirdbm.py b/src/twisted/persisted/dirdbm.py
+index 3ba7a59d4..7659ff765 100644
+--- a/src/twisted/persisted/dirdbm.py
++++ b/src/twisted/persisted/dirdbm.py
+@@ -77,14 +77,14 @@ class DirDBM:
+         Encode a key so it can be used as a filename.
+         """
+         # NOTE: '_' is NOT in the base64 alphabet!
+-        return base64.encodestring(k).replace(b'\n', b'_').replace(b"/", b"-")
++        return base64.encodebytes(k).replace(b'\n', b'_').replace(b"/", b"-")
+     def _decode(self, k):
+         """
+         Decode a filename to get the key.
+         """
+-        return base64.decodestring(k.replace(b'_', b'\n').replace(b"-", b"/"))
++        return base64.decodebytes(k.replace(b'_', b'\n').replace(b"-", b"/"))
+     def _readFile(self, path):
+diff --git a/src/twisted/web/http.py b/src/twisted/web/http.py
+index 0e115741e..e9a080d21 100644
+--- a/src/twisted/web/http.py
++++ b/src/twisted/web/http.py
+@@ -1544,7 +1544,7 @@ class Request:
+             bas, upw = authh.split()
+             if bas.lower() != b"basic":
+                 raise ValueError()
+-            upw = base64.decodestring(upw)
++            upw = base64.b64decode(upw)
+             self.user, self.password = upw.split(b':', 1)
+         except (binascii.Error, ValueError):
+             self.user = self.password = b''
+diff --git a/src/twisted/web/test/test_http.py b/src/twisted/web/test/test_http.py
+index 112e56f46..02a4674a7 100644
+--- a/src/twisted/web/test/test_http.py
++++ b/src/twisted/web/test/test_http.py
+@@ -1604,7 +1604,7 @@ class ParsingTests(unittest.TestCase):
+                 requests.append(self)
+         for u, p in [(b"foo", b"bar"), (b"hello", b"there:z")]:
+-            s = base64.encodestring(b":".join((u, p))).strip()
++            s = base64.b64encode(b":".join((u, p)))
+             f = b"GET / HTTP/1.0\nAuthorization: Basic " + s + b"\n\n"
+             self.runRequest(f, Request, 0)
+             req = requests.pop()
+@@ -2209,9 +2209,9 @@ Hello,
+         u = b"foo"
+         p = b"bar"
+-        s = base64.encodestring(b":".join((u, p))).strip()
++        s = base64.b64encode(b":".join((u, p)))
+         f = b"GET / HTTP/1.0\nAuthorization: Basic " + s + b"\n\n"
+-        self.patch(base64, 'decodestring', lambda x: [])
++        self.patch(base64, 'b64decode', lambda x: [])
+         self.runRequest(f, Request, 0)
+         req = requests.pop()
+         self.assertEqual((b'', b''), req.credentials)
+-- 
+2.26.2
diff --git a/dev-python/twisted/files/twisted-20.3.0-py39-combined.patch b/dev-python/twisted/files/twisted-20.3.0-py39-combined.patch
new file mode 100644 (file)
index 0000000..0ed1f7b
--- /dev/null
@@ -0,0 +1,115 @@
+From 2d30860a8b71e90513ead9958f5dd312802b0d36 Mon Sep 17 00:00:00 2001
+From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= <mgorny@gentoo.org>
+Date: Wed, 27 May 2020 14:40:53 +0200
+Subject: [PATCH 2/4] Fix imap4-utf-7 codec lookup function for Python 3.9
+
+Python 3.9 normalizes the codec name into 'imap4_utf_7' rather than
+'imap4-utf-7', and therefore the lookup function needs to account
+for the former name.  Transform the latter locally to preserve support
+for all Python versions.
+
+Fixes: ticket: 9832
+---
+ src/twisted/mail/imap4.py                | 2 +-
+ 2 files changed, 1 insertion(+), 1 deletion(-)
+
+diff --git a/src/twisted/mail/imap4.py b/src/twisted/mail/imap4.py
+index 736ef111d..3f32982ca 100644
+--- a/src/twisted/mail/imap4.py
++++ b/src/twisted/mail/imap4.py
+@@ -6369,7 +6369,7 @@ _codecInfo = codecs.CodecInfo(encoder, decoder, StreamReader, StreamWriter)
+ def imap4_utf_7(name):
+-    if name == 'imap4-utf-7':
++    if name.replace('-', '_') == 'imap4_utf_7':
+         return _codecInfo
+ codecs.register(imap4_utf_7)
+-- 
+2.26.2
+
+From daf928bf0f0371816dddbd4929948c4213d0cdcb Mon Sep 17 00:00:00 2001
+From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= <mgorny@gentoo.org>
+Date: Wed, 27 May 2020 15:12:54 +0200
+Subject: [PATCH 3/4] Fix verifyCryptedPassword() for crypt.crypt() throwing in
+ py3.9
+
+In Python 3.9, the crypt.crypt() function may throw an exception
+if the underlying crypt() function fails.  Update
+verifyCryptedPassword() to account for that, and preserve the existing
+behavior of returning False in that case.
+
+Fixes: ticket:9833
+---
+ src/twisted/conch/checkers.py             | 5 ++++-
+ src/twisted/plugins/cred_unix.py          | 5 ++++-
+ 4 files changed, 8 insertions(+), 2 deletions(-)
+
+diff --git a/src/twisted/conch/checkers.py b/src/twisted/conch/checkers.py
+index 917567a39..e4e327b16 100644
+--- a/src/twisted/conch/checkers.py
++++ b/src/twisted/conch/checkers.py
+@@ -53,7 +53,10 @@ def verifyCryptedPassword(crypted, pw):
+     @rtype: L{bool}
+     """
+-    return crypt.crypt(pw, crypted) == crypted
++    try:
++        return crypt.crypt(pw, crypted) == crypted
++    except OSError:
++        return False
+diff --git a/src/twisted/plugins/cred_unix.py b/src/twisted/plugins/cred_unix.py
+index 211b4ccbc..a662719b6 100644
+--- a/src/twisted/plugins/cred_unix.py
++++ b/src/twisted/plugins/cred_unix.py
+@@ -43,7 +43,10 @@ def verifyCryptedPassword(crypted, pw):
+         pw = pw.decode('utf-8')
+     if not isinstance(crypted, StringType):
+         crypted = crypted.decode('utf-8')
+-    return crypt.crypt(pw, crypted) == crypted
++    try:
++        return crypt.crypt(pw, crypted) == crypted
++    except OSError:
++        return False
+-- 
+2.26.2
+
+From 4fc435df0d1eba3e5d6416a2b86d39d3404f82fe Mon Sep 17 00:00:00 2001
+From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= <mgorny@gentoo.org>
+Date: Wed, 27 May 2020 15:37:10 +0200
+Subject: [PATCH 4/4] Use xml.etree.ElementTree instead of deprecated
+ cElementTree
+
+The xml.etree.cElementTree is deprecated, and has been removed in Python
+3.9.  At the same time, xml.etree.ElementTree has already been using
+cElementTree implicitly since Python 3.3.  Update test_flatten to use
+the latter to provide compatibility with newer Python versions.
+
+Fixes: ticket:9834
+---
+ src/twisted/web/test/test_flatten.py    | 2 +-
+ 2 files changed, 1 insertion(+), 1 deletion(-)
+
+diff --git a/src/twisted/web/test/test_flatten.py b/src/twisted/web/test/test_flatten.py
+index 677401c55..61d50e20a 100644
+--- a/src/twisted/web/test/test_flatten.py
++++ b/src/twisted/web/test/test_flatten.py
+@@ -9,7 +9,7 @@ L{twisted.web._flatten}.
+ import sys
+ import traceback
+-from xml.etree.cElementTree import XML
++from xml.etree.ElementTree import XML
+ from collections import OrderedDict
+-- 
+2.26.2
+
index 6ed6b63c9d3526827372a5fef0adee154934cfe4..720e3b98f6471b7d41521bcb7c0706e28a33babf 100644 (file)
@@ -3,7 +3,7 @@
 
 EAPI=7
 
-PYTHON_COMPAT=( python2_7 python3_{6,7,8} )
+PYTHON_COMPAT=( python2_7 python3_{6,7,8,9} )
 PYTHON_REQ_USE="threads(+)"
 
 inherit distutils-r1 virtualx
@@ -85,6 +85,8 @@ python_prepare_all() {
                "${FILESDIR}"/${P}-py38.patch
                "${FILESDIR}"/twisted-19.10.0-py38-cgi.patch
                "${FILESDIR}"/twisted-20.3.0-py38-hmac.patch
+               "${FILESDIR}"/twisted-19.10.0-py39-b64.patch
+               "${FILESDIR}"/twisted-20.3.0-py39-combined.patch
        )
 
        # upstream test for making releases; not very useful and requires
index 47624031dc763de8abf4cf19d102d244d6ea0d03..3ee3d08b812ab5ca8db26b463626fc116967dac9 100644 (file)
@@ -3,7 +3,7 @@
 
 EAPI=7
 
-PYTHON_COMPAT=( python3_{6,7,8} )
+PYTHON_COMPAT=( python3_{6,7,8,9} )
 PYTHON_REQ_USE="threads(+)"
 
 inherit distutils-r1 virtualx
@@ -84,6 +84,8 @@ python_prepare_all() {
        local PATCHES=(
                "${FILESDIR}"/twisted-20.3.0-py38-cgi.patch
                "${FILESDIR}"/twisted-20.3.0-py38-hmac.patch
+               "${FILESDIR}"/twisted-20.3.0-py39-b64.patch
+               "${FILESDIR}"/twisted-20.3.0-py39-combined.patch
        )
 
        # upstream test for making releases; not very useful and requires