فهرست منبع

Port the minimal needed to get the test suite to run in python3.

Gary van der Merwe 10 سال پیش
والد
کامیت
51a8d2ae77
10فایلهای تغییر یافته به همراه60 افزوده شده و 36 حذف شده
  1. 2 1
      dulwich/_compat.py
  2. 7 2
      dulwich/client.py
  3. 10 10
      dulwich/contrib/swift.py
  4. 16 16
      dulwich/contrib/test_swift.py
  5. 2 1
      dulwich/diff_tree.py
  6. 2 1
      dulwich/pack.py
  7. 5 1
      dulwich/server.py
  8. 8 2
      dulwich/tests/compat/test_client.py
  9. 2 1
      dulwich/tests/utils.py
  10. 6 1
      dulwich/web.py

+ 2 - 1
dulwich/_compat.py

@@ -1,9 +1,10 @@
 # _compat.py -- For dealing with python2.6 oddness
+# Copyright (C) 2012-2014 Jelmer Vernooij and others.
 #
 # This program is free software; you can redistribute it and/or
 # modify it under the terms of the GNU General Public License
 # as published by the Free Software Foundation; version 2
-# of the License or (at your option) a later version.
+# of the License or (at your option) a later version of the License.
 #
 # This program is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of

+ 7 - 2
dulwich/client.py

@@ -44,8 +44,13 @@ import select
 import socket
 import subprocess
 import sys
-import urllib2
-import urlparse
+
+try:
+    import urllib2
+    import urlparse
+except ImportError:
+    import urllib.request as urllib2
+    import urllib.parse as urlparse
 
 from dulwich.errors import (
     GitProtocolError,

+ 10 - 10
dulwich/contrib/swift.py

@@ -32,7 +32,7 @@ import tempfile
 import posixpath
 
 from urlparse import urlparse
-from cStringIO import StringIO
+from io import BytesIO
 from ConfigParser import ConfigParser
 from geventhttpclient import HTTPClient
 
@@ -453,7 +453,7 @@ class SwiftConnector(object):
 
         if range:
             return content
-        return StringIO(content)
+        return BytesIO(content)
 
     def del_object(self, name):
         """Delete an object
@@ -718,7 +718,7 @@ class SwiftObjectStore(PackBasedObjectStore):
         :return: Fileobject to write to and a commit function to
             call when the pack is finished.
         """
-        f = StringIO()
+        f = BytesIO()
 
         def commit():
             f.seek(0)
@@ -729,7 +729,7 @@ class SwiftObjectStore(PackBasedObjectStore):
                                           "pack-%s" %
                                           iter_sha1(entry[0] for
                                                     entry in entries))
-                index = StringIO()
+                index = BytesIO()
                 write_pack_index_v2(index, entries, pack.get_stored_checksum())
                 self.scon.put_object(basename + ".pack", f)
                 f.close()
@@ -808,7 +808,7 @@ class SwiftObjectStore(PackBasedObjectStore):
 
         # Write the index.
         filename = pack_base_name + '.idx'
-        index_file = StringIO()
+        index_file = BytesIO()
         write_pack_index_v2(index_file, entries, pack_sha)
         self.scon.put_object(filename, index_file)
 
@@ -820,7 +820,7 @@ class SwiftObjectStore(PackBasedObjectStore):
         serialized_pack_info = pack_info_create(pack_data, pack_index)
         f.close()
         index_file.close()
-        pack_info_file = StringIO(serialized_pack_info)
+        pack_info_file = BytesIO(serialized_pack_info)
         filename = pack_base_name + '.info'
         self.scon.put_object(filename, pack_info_file)
         pack_info_file.close()
@@ -842,7 +842,7 @@ class SwiftInfoRefsContainer(InfoRefsContainer):
         self.store = store
         f = self.scon.get_object(self.filename)
         if not f:
-            f = StringIO('')
+            f = BytesIO('')
         super(SwiftInfoRefsContainer, self).__init__(f)
 
     def _load_check_ref(self, name, old_ref):
@@ -857,7 +857,7 @@ class SwiftInfoRefsContainer(InfoRefsContainer):
         return refs
 
     def _write_refs(self, refs):
-        f = StringIO()
+        f = BytesIO()
         f.writelines(write_info_refs(refs, self.store))
         self.scon.put_object(self.filename, f)
 
@@ -928,7 +928,7 @@ class SwiftRepo(BaseRepo):
         :param filename: the path to the object to put on Swift
         :param contents: the content as bytestring
         """
-        f = StringIO()
+        f = BytesIO()
         f.write(contents)
         self.scon.put_object(filename, f)
         f.close()
@@ -944,7 +944,7 @@ class SwiftRepo(BaseRepo):
         scon.create_root()
         for obj in [posixpath.join(OBJECTDIR, PACKDIR),
                     posixpath.join(INFODIR, 'refs')]:
-            scon.put_object(obj, StringIO(''))
+            scon.put_object(obj, BytesIO(''))
         ret = cls(scon.root, conf)
         ret._init_files(True)
         return ret

+ 16 - 16
dulwich/contrib/test_swift.py

@@ -24,7 +24,7 @@
 import posixpath
 
 from time import time
-from cStringIO import StringIO
+from io import BytesIO
 try:
     from unittest import skipIf
 except ImportError:
@@ -224,7 +224,7 @@ class FakeSwiftConnector(object):
         name = posixpath.join(self.root, name)
         if not range:
             try:
-                return StringIO(self.store[name])
+                return BytesIO(self.store[name])
             except KeyError:
                 return None
         else:
@@ -260,14 +260,14 @@ class TestSwiftObjectStore(TestCase):
 
     def setUp(self):
         super(TestSwiftObjectStore, self).setUp()
-        self.conf = swift.load_conf(file=StringIO(config_file %
+        self.conf = swift.load_conf(file=BytesIO(config_file %
                                                   def_config_file))
         self.fsc = FakeSwiftConnector('fakerepo', conf=self.conf)
 
     def _put_pack(self, sos, commit_amount=1, marker='Default'):
         odata = create_commits(length=commit_amount, marker=marker)
         data = [(d.type_num, d.as_raw_string()) for d in odata]
-        f = StringIO()
+        f = BytesIO()
         build_pack(f, data, store=sos)
         sos.add_thin_pack(f.read, None)
         return odata
@@ -368,7 +368,7 @@ class TestSwiftObjectStore(TestCase):
                 (tree.type_num, tree.as_raw_string()),
                 (cmt.type_num, cmt.as_raw_string()),
                 (tag.type_num, tag.as_raw_string())]
-        f = StringIO()
+        f = BytesIO()
         build_pack(f, data, store=sos)
         sos.add_thin_pack(f.read, None)
         self.assertEqual(len(self.fsc.store), 6)
@@ -379,7 +379,7 @@ class TestSwiftRepo(TestCase):
 
     def setUp(self):
         super(TestSwiftRepo, self).setUp()
-        self.conf = swift.load_conf(file=StringIO(config_file %
+        self.conf = swift.load_conf(file=BytesIO(config_file %
                                                   def_config_file))
 
     def test_init(self):
@@ -428,15 +428,15 @@ class TestSwiftRepo(TestCase):
 @skipIf(missing_libs, skipmsg)
 class TestPackInfoLoadDump(TestCase):
     def setUp(self):
-        conf = swift.load_conf(file=StringIO(config_file %
+        conf = swift.load_conf(file=BytesIO(config_file %
                                              def_config_file))
         sos = swift.SwiftObjectStore(
             FakeSwiftConnector('fakerepo', conf=conf))
         commit_amount = 10
         self.commits = create_commits(length=commit_amount, marker="m")
         data = [(d.type_num, d.as_raw_string()) for d in self.commits]
-        f = StringIO()
-        fi = StringIO()
+        f = BytesIO()
+        fi = BytesIO()
         expected = build_pack(f, data, store=sos)
         entries = [(sha, ofs, checksum) for
                    ofs, _, _, sha, checksum in expected]
@@ -455,14 +455,14 @@ class TestPackInfoLoadDump(TestCase):
 #            dump_time.append(time() - start)
 #        for i in xrange(0, 100):
 #            start = time()
-#            pack_infos = swift.load_pack_info('', file=StringIO(dumps))
+#            pack_infos = swift.load_pack_info('', file=BytesIO(dumps))
 #            load_time.append(time() - start)
 #        print sum(dump_time) / float(len(dump_time))
 #        print sum(load_time) / float(len(load_time))
 
     def test_pack_info(self):
         dumps = swift.pack_info_create(self.pack_data, self.pack_index)
-        pack_infos = swift.load_pack_info('', file=StringIO(dumps))
+        pack_infos = swift.load_pack_info('', file=BytesIO(dumps))
         for obj in self.commits:
             self.assertIn(obj.id, pack_infos)
 
@@ -476,7 +476,7 @@ class TestSwiftInfoRefsContainer(TestCase):
             "22effb216e3a82f97da599b8885a6cadb488b4c5\trefs/heads/master\n" + \
             "cca703b0e1399008b53a1a236d6b4584737649e4\trefs/heads/dev"
         self.store = {'fakerepo/info/refs': content}
-        self.conf = swift.load_conf(file=StringIO(config_file %
+        self.conf = swift.load_conf(file=BytesIO(config_file %
                                                   def_config_file))
         self.fsc = FakeSwiftConnector('fakerepo', conf=self.conf)
         self.object_store = {}
@@ -510,7 +510,7 @@ class TestSwiftConnector(TestCase):
 
     def setUp(self):
         super(TestSwiftConnector, self).setUp()
-        self.conf = swift.load_conf(file=StringIO(config_file %
+        self.conf = swift.load_conf(file=BytesIO(config_file %
                                                   def_config_file))
         with patch('geventhttpclient.HTTPClient.request',
                    fake_auth_request_v1):
@@ -594,7 +594,7 @@ class TestSwiftConnector(TestCase):
     def test_put_object(self):
         with patch('geventhttpclient.HTTPClient.request',
                    lambda *args, **kwargs: Response()):
-            self.assertEqual(self.conn.put_object('a', StringIO('content')),
+            self.assertEqual(self.conn.put_object('a', BytesIO('content')),
                              None)
 
     def test_put_object_fails(self):
@@ -602,7 +602,7 @@ class TestSwiftConnector(TestCase):
                    lambda *args, **kwargs: Response(status=400)):
             self.assertRaises(swift.SwiftException,
                               lambda: self.conn.put_object(
-                                  'a', StringIO('content')))
+                                  'a', BytesIO('content')))
 
     def test_get_object(self):
         with patch('geventhttpclient.HTTPClient.request',
@@ -638,7 +638,7 @@ class SwiftObjectStoreTests(ObjectStoreTests, TestCase):
 
     def setUp(self):
         TestCase.setUp(self)
-        conf = swift.load_conf(file=StringIO(config_file %
+        conf = swift.load_conf(file=BytesIO(config_file %
                                def_config_file))
         fsc = FakeSwiftConnector('fakerepo', conf=conf)
         self.store = swift.SwiftObjectStore(fsc)

+ 2 - 1
dulwich/diff_tree.py

@@ -24,9 +24,10 @@ from collections import (
     )
 
 from io import BytesIO
-from itertools import chain, izip
+from itertools import chain
 import stat
 
+from dulwich._compat import izip
 from dulwich.objects import (
     S_ISGITLINK,
     TreeEntry,

+ 2 - 1
dulwich/pack.py

@@ -39,7 +39,7 @@ from collections import (
     )
 import difflib
 
-from itertools import chain, imap, izip
+from itertools import chain
 
 try:
     import mmap
@@ -58,6 +58,7 @@ from struct import unpack_from
 import warnings
 import zlib
 
+from dulwich._compat import imap, izip
 from dulwich.errors import (
     ApplyDeltaError,
     ChecksumMismatch,

+ 5 - 1
dulwich/server.py

@@ -42,10 +42,14 @@ Currently supported capabilities:
 import collections
 import os
 import socket
-import SocketServer
 import sys
 import zlib
 
+try:
+    import SocketServer
+except ImportError:
+    import socketserver as SocketServer
+
 from dulwich.errors import (
     ApplyDeltaError,
     ChecksumMismatch,

+ 8 - 2
dulwich/tests/compat/test_client.py

@@ -20,8 +20,6 @@
 """Compatibilty tests between the Dulwich client and the cgit server."""
 
 from io import BytesIO
-import BaseHTTPServer
-import SimpleHTTPServer
 import copy
 import os
 import select
@@ -34,6 +32,14 @@ import tempfile
 import threading
 import urllib
 
+try:
+    import BaseHTTPServer
+    import SimpleHTTPServer
+except ImportError:
+    import http.server
+    BaseHTTPServer = http.server
+    SimpleHTTPServer = http.server
+
 if sys.platform == 'win32':
     import ctypes
 

+ 2 - 1
dulwich/tests/utils.py

@@ -29,6 +29,7 @@ import types
 
 import warnings
 
+from dulwich._compat import iteritems
 from dulwich.index import (
     commit_tree,
     )
@@ -98,7 +99,7 @@ def make_object(cls, **attrs):
         pass
 
     obj = TestObject()
-    for name, value in attrs.iteritems():
+    for name, value in iteritems(attrs):
         if name == 'id':
             # id property is read-only, so we overwrite sha instead.
             sha = FixedSha(value)

+ 6 - 1
dulwich/web.py

@@ -27,7 +27,6 @@ import os
 import re
 import sys
 import time
-from urlparse import parse_qs
 from wsgiref.simple_server import (
     WSGIRequestHandler,
     ServerHandler,
@@ -35,6 +34,12 @@ from wsgiref.simple_server import (
     make_server,
     )
 
+try:
+    from urlparse import parse_qs
+except ImportError:
+    from urllib.parse import parse_qs
+
+
 from dulwich import log_utils
 from dulwich.protocol import (
     ReceivableProtocol,