Skip to content

Commit 087803e

Browse files
committed
Making a bit of progress...
This changes the internals to use BytesIO over StringIO, which fixed a few of the failing tests in Python 3. We are only importing from `io` now, instead of the entire chain, as this is available in Python 2.6+.
1 parent a19a169 commit 087803e

File tree

8 files changed

+20
-62
lines changed

8 files changed

+20
-62
lines changed

gitdb/db/mem.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,7 @@
2424
DecompressMemMapReader,
2525
)
2626

27-
try:
28-
from cStringIO import StringIO
29-
except ImportError:
30-
try:
31-
from StringIO import StringIO
32-
except ImportError:
33-
from io import StringIO
27+
from io import StringIO
3428

3529
__all__ = ("MemoryDB", )
3630

gitdb/fun.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,7 @@
2121
except ImportError:
2222
izip = zip
2323

24-
try:
25-
from cStringIO import StringIO
26-
except ImportError:
27-
try:
28-
from StringIO import StringIO
29-
except ImportError:
30-
from io import StringIO
24+
from io import StringIO
3125

3226
# INVARIANTS
3327
OFS_DELTA = 6
@@ -453,7 +447,7 @@ def msb_size(data, offset=0):
453447
l = len(data)
454448
hit_msb = False
455449
while i < l:
456-
c = ord(data[i+offset])
450+
c = byte_ord(data[i+offset])
457451
size |= (c & 0x7f) << i*7
458452
i += 1
459453
if not c & 0x80:

gitdb/stream.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,7 @@
33
# This module is part of GitDB and is released under
44
# the New BSD License: http://www.opensource.org/licenses/bsd-license.php
55

6-
try:
7-
from cStringIO import StringIO
8-
except ImportError:
9-
try:
10-
from StringIO import StringIO
11-
except ImportError:
12-
from io import StringIO
6+
from io import BytesIO, StringIO
137

148
import errno
159
import mmap
@@ -106,20 +100,20 @@ def _parse_header_info(self):
106100
self._s = maxb
107101
hdr = self.read(maxb)
108102
hdrend = hdr.find("\0".encode("ascii"))
109-
type, size = hdr[:hdrend].split(" ".encode("ascii"))
103+
typ, size = hdr[:hdrend].split(" ".encode("ascii"))
110104
size = int(size)
111105
self._s = size
112106

113107
# adjust internal state to match actual header length that we ignore
114108
# The buffer will be depleted first on future reads
115109
self._br = 0
116-
hdrend += 1 # count terminating \0
117-
self._buf = StringIO(hdr[hdrend:])
110+
hdrend += 1
111+
self._buf = BytesIO(hdr[hdrend:])
118112
self._buflen = len(hdr) - hdrend
119113

120114
self._phi = True
121115

122-
return type, size
116+
return typ.decode("ascii"), size
123117

124118
#{ Interface
125119

@@ -133,8 +127,8 @@ def new(self, m, close_on_deletion=False):
133127
:param close_on_deletion: if True, the memory map will be closed once we are
134128
being deleted"""
135129
inst = DecompressMemMapReader(m, close_on_deletion, 0)
136-
type, size = inst._parse_header_info()
137-
return type, size, inst
130+
typ, size = inst._parse_header_info()
131+
return typ, size, inst
138132

139133
def data(self):
140134
""":return: random access compatible data we are working on"""
@@ -211,14 +205,14 @@ def read(self, size=-1):
211205
# END clamp size
212206

213207
if size == 0:
214-
return str()
208+
return bytes()
215209
# END handle depletion
216210

217211

218212
# deplete the buffer, then just continue using the decompress object
219213
# which has an own buffer. We just need this to transparently parse the
220214
# header from the zlib stream
221-
dat = str()
215+
dat = bytes()
222216
if self._buf:
223217
if self._buflen >= size:
224218
# have enough data
@@ -588,7 +582,7 @@ class ZippedStoreShaWriter(Sha1Writer):
588582
__slots__ = ('buf', 'zip')
589583
def __init__(self):
590584
Sha1Writer.__init__(self)
591-
self.buf = StringIO()
585+
self.buf = BytesIO()
592586
self.zip = zlib.compressobj(zlib.Z_BEST_SPEED)
593587

594588
def __getattr__(self, attr):

gitdb/test/db/lib.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,7 @@
2424

2525
from async import IteratorReader
2626

27-
try:
28-
from cStringIO import StringIO
29-
except ImportError:
30-
try:
31-
from StringIO import StringIO
32-
except ImportError:
33-
from io import StringIO
27+
from io import StringIO
3428

3529
from struct import pack
3630

@@ -41,7 +35,7 @@ class TestDBBase(TestBase):
4135
"""Base class providing testing routines on databases"""
4236

4337
# data
44-
two_lines = "1234\nhello world"
38+
two_lines = "1234\nhello world".encode("ascii")
4539
all_data = (two_lines, )
4640

4741
def _assert_object_writing_simple(self, db):

gitdb/test/lib.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,7 @@
1717
import random
1818
from array import array
1919

20-
try:
21-
from cStringIO import StringIO
22-
except ImportError:
23-
try:
24-
from StringIO import StringIO
25-
except ImportError:
26-
from io import StringIO
20+
from io import StringIO
2721

2822
import glob
2923
import unittest

gitdb/test/test_example.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,7 @@
88
from gitdb.db import LooseObjectDB
99
from gitdb.util import pool
1010

11-
try:
12-
from cStringIO import StringIO
13-
except ImportError:
14-
try:
15-
from StringIO import StringIO
16-
except ImportError:
17-
from io import StringIO
11+
from io import StringIO
1812

1913
from async import IteratorReader
2014

gitdb/test/test_stream.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ def test_decompress_reader(self):
8282
if with_size:
8383
# need object data
8484
zdata = zlib.compress(make_object(str_blob_type, cdata))
85-
type, size, reader = DecompressMemMapReader.new(zdata, close_on_deletion)
85+
typ, size, reader = DecompressMemMapReader.new(zdata, close_on_deletion)
8686
assert size == len(cdata)
87-
assert type == str_blob_type
87+
assert typ == str_blob_type
8888

8989
# even if we don't set the size, it will be set automatically on first read
9090
test_reader = DecompressMemMapReader(zdata, close_on_deletion=False)

gitdb/util.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,7 @@
88
import sys
99
import errno
1010

11-
try:
12-
from cStringIO import StringIO
13-
except ImportError:
14-
try:
15-
from StringIO import StringIO
16-
except ImportError:
17-
from io import StringIO
11+
from io import StringIO
1812

1913
try:
2014
import async.mod.zlib as zlib

0 commit comments

Comments
 (0)