← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~cjwatson/turnip:remove-py2-compat-imports into turnip:master

 

Colin Watson has proposed merging ~cjwatson/turnip:remove-py2-compat-imports into turnip:master.

Commit message:
Remove various Python 2 compatibility imports

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~cjwatson/turnip/+git/turnip/+merge/404700

We're committed to running on Python 3 now.
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~cjwatson/turnip:remove-py2-compat-imports into turnip:master.
diff --git a/requirements.txt b/requirements.txt
index 6606fa3..5cea053 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -14,7 +14,6 @@ contextlib2==0.6.0.post1
 cornice==3.6.1
 cryptography==3.0
 docutils==0.14
-enum34==1.1.10; python_version < "3.4"
 envdir==0.7
 extras==1.0.0
 fixtures==3.0.0
@@ -38,7 +37,6 @@ m2r==0.1.14
 # lp1 Remove use of deprecated 'setup.py test'.
 mccabe==0.6.1+lp1
 mistune==0.8.3
-mock==3.0.5; python_version < "3"
 pathlib2==2.3.5
 Paste==3.5.0
 PasteDeploy==2.1.0
diff --git a/setup.py b/setup.py
index 6bc2e94..7e269ba 100755
--- a/setup.py
+++ b/setup.py
@@ -21,7 +21,6 @@ requires = [
     'celery',
     'contextlib2',
     'cornice',
-    'enum34; python_version < "3.4"',
     'gevent[monitor]',
     'lazr.sshserver>=0.1.7',
     'Paste',
@@ -37,7 +36,6 @@ test_requires = [
     'docutils',
     'fixtures',
     'flake8',
-    'mock; python_version < "3"',
     'testscenarios',
     'testtools',
     'webtest',
diff --git a/turnip/api/tests/test_api.py b/turnip/api/tests/test_api.py
index b84b8ff..a24c442 100644
--- a/turnip/api/tests/test_api.py
+++ b/turnip/api/tests/test_api.py
@@ -13,14 +13,15 @@ import subprocess
 from textwrap import dedent
 import time
 import unittest
+from unittest import mock
 import uuid
+from urllib.parse import quote
 
 from fixtures import (
     EnvironmentVariable,
     TempDir,
     )
 import six
-from six.moves.urllib.parse import quote
 from testtools import TestCase
 from testtools.matchers import (
     Equals,
@@ -38,7 +39,6 @@ from turnip.api.tests.test_helpers import (
     )
 from turnip.config import config
 from turnip.pack.tests.fake_servers import FakeVirtInfoService
-from turnip.tests.compat import mock
 from turnip.tests.tasks import CeleryWorkerFixture
 
 
diff --git a/turnip/api/tests/test_helpers.py b/turnip/api/tests/test_helpers.py
index d1a288c..6c6a4a1 100644
--- a/turnip/api/tests/test_helpers.py
+++ b/turnip/api/tests/test_helpers.py
@@ -8,6 +8,8 @@ import logging
 import os
 from subprocess import PIPE, Popen, CalledProcessError, STDOUT
 import uuid
+from urllib.parse import urljoin
+from urllib.request import pathname2url
 
 from pygit2 import (
     clone_repository,
@@ -18,7 +20,6 @@ from pygit2 import (
     Signature,
     )
 import six
-from six.moves import urllib
 
 log = logging.getLogger()
 
@@ -184,8 +185,7 @@ class RepoFactory(object):
 
     def clone_repo(self, repo_factory):
         """Return a pygit2 repo object cloned from an existing factory repo."""
-        clone_from_url = urllib.parse.urljoin(
-            'file:', urllib.request.pathname2url(repo_factory.repo.path))
+        clone_from_url = urljoin('file:', pathname2url(repo_factory.repo.path))
         return clone_repository(clone_from_url, self.repo_path, bare=False)
 
     def build(self):
diff --git a/turnip/compat/__init__.py b/turnip/compat/__init__.py
deleted file mode 100644
index e69de29..0000000
--- a/turnip/compat/__init__.py
+++ /dev/null
diff --git a/turnip/compat/files.py b/turnip/compat/files.py
deleted file mode 100644
index 3f8bb22..0000000
--- a/turnip/compat/files.py
+++ /dev/null
@@ -1,21 +0,0 @@
-import sys
-
-
-def fd_buffer(file_descriptor):
-    """Returns the raw bytes stream for the given file descriptor.
-
-    On Python2, it returns the file descriptor itself (since it reads raw
-    binary data by default). On Python3, it returns the
-    file_descriptor.buffer object (since py3, by default, opens files with
-    an encoding).
-
-    It's useful to read and write from sys.std{in,out,err} without reopening
-    those files, for example.
-
-    :param file_descriptor: The file descriptor to get raw buffer from.
-    :return: A BufferedReader or BufferedWriter object."""
-    PY3K = sys.version_info >= (3, 0)
-    if PY3K:
-        return file_descriptor.buffer
-    else:
-        return file_descriptor
diff --git a/turnip/helpers.py b/turnip/helpers.py
index b23e4a6..d0c3b39 100644
--- a/turnip/helpers.py
+++ b/turnip/helpers.py
@@ -8,9 +8,12 @@ from __future__ import (
     )
 
 import os.path
+from xmlrpc.client import (
+    ServerProxy,
+    Transport,
+    )
 
 import six
-from six.moves import xmlrpc_client
 
 
 def compose_path(root, path):
@@ -25,22 +28,22 @@ def compose_path(root, path):
     return full_path
 
 
-class TimeoutTransport(xmlrpc_client.Transport):
+class TimeoutTransport(Transport):
 
     def __init__(self, timeout, use_datetime=0):
         self.timeout = timeout
-        xmlrpc_client.Transport.__init__(self, use_datetime)
+        Transport.__init__(self, use_datetime)
 
     def make_connection(self, host):
-        connection = xmlrpc_client.Transport.make_connection(self, host)
+        connection = Transport.make_connection(self, host)
         connection.timeout = self.timeout
         return connection
 
 
-class TimeoutServerProxy(xmlrpc_client.ServerProxy):
+class TimeoutServerProxy(ServerProxy):
 
     def __init__(self, uri, timeout=10, transport=None, encoding=None,
                  verbose=0, allow_none=0, use_datetime=0):
         t = TimeoutTransport(timeout)
-        xmlrpc_client.ServerProxy.__init__(
+        ServerProxy.__init__(
             self, uri, t, encoding, verbose, allow_none, use_datetime)
diff --git a/turnip/pack/hookrpc.py b/turnip/pack/hookrpc.py
index 9ae29cb..cf1e329 100644
--- a/turnip/pack/hookrpc.py
+++ b/turnip/pack/hookrpc.py
@@ -22,9 +22,9 @@ from __future__ import (
 
 import base64
 import json
+from xmlrpc.client import Binary
 
 import six
-from six.moves import xmlrpc_client
 from twisted.internet import (
     defer,
     protocol,
@@ -173,7 +173,7 @@ class HookRPCHandler(object):
                 result = yield proxy.callRemote(
                     'checkRefPermissions',
                     six.ensure_str(ref_path),
-                    [xmlrpc_client.Binary(path) for path in missing],
+                    [Binary(path) for path in missing],
                     auth_params).addTimeout(
                         self.virtinfo_timeout, self.reactor)
             except xmlrpc.Fault as e:
@@ -194,9 +194,7 @@ class HookRPCHandler(object):
                         "checkRefPermissions virtinfo raised Unauthorized: "
                         "auth_params={auth_params}, ref_path={ref_path}",
                         auth_params=auth_params, ref_path=ref_path)
-                    result = [
-                        (xmlrpc_client.Binary(path), [])
-                        for path in missing]
+                    result = [(Binary(path), []) for path in missing]
                 else:
                     log_context.log.info(
                         "checkRefPermissions virtinfo raised "
diff --git a/turnip/pack/hooks/hook.py b/turnip/pack/hooks/hook.py
index a32490f..38a9c19 100755
--- a/turnip/pack/hooks/hook.py
+++ b/turnip/pack/hooks/hook.py
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/python3
 
 # Copyright 2015-2018 Canonical Ltd.  This software is licensed under the
 # GNU Affero General Public License version 3 (see the file LICENSE).
@@ -18,7 +18,6 @@ import sys
 
 import six
 
-from turnip.compat.files import fd_buffer
 from turnip.pack.helpers import get_repack_data
 
 # XXX twom 2018-10-23 This should be a pygit2 import, but
@@ -182,7 +181,7 @@ def send_mp_url(received_line):
                 {'key': rpc_key,
                  'branch': six.ensure_text(pushed_branch, "UTF-8")})
             if mp_url is not None:
-                stdout = fd_buffer(sys.stdout)
+                stdout = sys.stdout.buffer
                 stdout.write(b'      \n')
                 stdout.write(
                     b"Create a merge proposal for '%s' on Launchpad by"
@@ -194,8 +193,8 @@ def send_mp_url(received_line):
 if __name__ == '__main__':
     # Connect to the RPC server, authenticating using the random key
     # from the environment.
-    stdin = fd_buffer(sys.stdin)
-    stdout = fd_buffer(sys.stdout)
+    stdin = sys.stdin.buffer
+    stdout = sys.stdout.buffer
     rpc_key = os.environ['TURNIP_HOOK_RPC_KEY']
     sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
     sock.connect(os.environ['TURNIP_HOOK_RPC_SOCK'])
diff --git a/turnip/pack/http.py b/turnip/pack/http.py
index 3e627d0..0d11d91 100644
--- a/turnip/pack/http.py
+++ b/turnip/pack/http.py
@@ -13,10 +13,7 @@ import json
 import os.path
 import tempfile
 import textwrap
-try:
-    from urllib.parse import urlencode
-except ImportError:
-    from urllib import urlencode
+from urllib.parse import urlencode
 import uuid
 import zlib
 
diff --git a/turnip/pack/tests/fake_servers.py b/turnip/pack/tests/fake_servers.py
index 458c317..e1be1be 100644
--- a/turnip/pack/tests/fake_servers.py
+++ b/turnip/pack/tests/fake_servers.py
@@ -9,10 +9,10 @@ from __future__ import (
 
 from collections import defaultdict
 import hashlib
+from xmlrpc.client import Binary
 
 from lazr.sshserver.auth import NoSuchPersonWithName
 import six
-from six.moves import xmlrpc_client
 from twisted.web import xmlrpc
 
 __all__ = [
@@ -123,7 +123,7 @@ class FakeVirtInfoService(xmlrpc.XMLRPC):
         if self.ref_permissions_fault is not None:
             raise self.ref_permissions_fault
         return [
-            (xmlrpc_client.Binary(ref), permissions)
+            (Binary(ref), permissions)
             for ref, permissions in self.ref_permissions.items()]
 
     def xmlrpc_getMergeProposalURL(self, path, branch, auth_params):
diff --git a/turnip/pack/tests/test_functional.py b/turnip/pack/tests/test_functional.py
index a695168..cbc77f8 100644
--- a/turnip/pack/tests/test_functional.py
+++ b/turnip/pack/tests/test_functional.py
@@ -17,20 +17,13 @@ import re
 import shutil
 import stat
 import tempfile
+from urllib.parse import (
+    urlsplit,
+    urlunsplit,
+    )
 
 from turnip.config import config
 
-try:
-    from urllib.parse import (
-        urlsplit,
-        urlunsplit,
-        )
-except ImportError:
-    from urlparse import (
-        urlsplit,
-        urlunsplit,
-        )
-
 from fixtures import (
     EnvironmentVariable,
     TempDir,
diff --git a/turnip/pack/tests/test_git.py b/turnip/pack/tests/test_git.py
index 57be78c..c2c38a7 100644
--- a/turnip/pack/tests/test_git.py
+++ b/turnip/pack/tests/test_git.py
@@ -9,6 +9,7 @@ from __future__ import (
 
 import hashlib
 import os.path
+from unittest import mock
 
 from fixtures import TempDir, MonkeyPatch
 from pygit2 import init_repository
@@ -37,7 +38,6 @@ from turnip.pack import (
 from turnip.pack.tests.fake_servers import FakeVirtInfoService
 from turnip.pack.tests.test_helpers import MockStatsd
 from turnip.pack.tests.test_hooks import MockHookRPCHandler
-from turnip.tests.compat import mock
 
 
 class DummyPackServerProtocol(git.PackServerProtocol):
diff --git a/turnip/pack/tests/test_hookrpc.py b/turnip/pack/tests/test_hookrpc.py
index eea5d08..1bec197 100644
--- a/turnip/pack/tests/test_hookrpc.py
+++ b/turnip/pack/tests/test_hookrpc.py
@@ -10,8 +10,8 @@ from __future__ import (
 import base64
 import contextlib
 import uuid
+from xmlrpc.client import Binary
 
-from six.moves import xmlrpc_client
 from testtools import (
     ExpectedException,
     TestCase,
@@ -225,7 +225,7 @@ class TestHookRPCHandler(TestCase):
                 Equals(path),
                 MatchesListwise([
                     MatchesAll(
-                        IsInstance(xmlrpc_client.Binary),
+                        IsInstance(Binary),
                         MatchesStructure.byEquality(data=ref_path))
                     for ref_path in ref_paths
                     ]),
diff --git a/turnip/pack/tests/test_http.py b/turnip/pack/tests/test_http.py
index 18c5893..ef8f835 100644
--- a/turnip/pack/tests/test_http.py
+++ b/turnip/pack/tests/test_http.py
@@ -10,6 +10,7 @@ from __future__ import (
 from io import BytesIO
 import json
 import os
+from unittest import mock
 
 from fixtures import TempDir
 import six
@@ -39,7 +40,6 @@ from turnip.pack.http import (
     HTTPAuthLoginResource,
     )
 from turnip.pack.tests.fake_servers import FakeVirtInfoService
-from turnip.tests.compat import mock
 from turnip.version_info import version_info
 
 
diff --git a/turnip/tests/compat.py b/turnip/tests/compat.py
deleted file mode 100644
index daa21d8..0000000
--- a/turnip/tests/compat.py
+++ /dev/null
@@ -1,19 +0,0 @@
-# Copyright 2015 Canonical Ltd.  This software is licensed under the
-# GNU Affero General Public License version 3 (see the file LICENSE).
-
-
-from __future__ import (
-    absolute_import,
-    print_function,
-    unicode_literals,
-)
-
-try:
-    from unittest import mock
-except ImportError:
-    import mock
-
-
-__all__ = [
-    'mock'
-]