Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bc8c912

Browse files
committed
Fixed io types to make tests work on PY2 once again.
Now it's about going through PY3 issues
1 parent ae2ff0f commit bc8c912

File tree

16 files changed

+45
-41
lines changed

16 files changed

+45
-41
lines changed

git/compat.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616

1717
from gitdb.utils.encoding import (
1818
string_types,
19-
text_type
19+
text_type,
20+
force_bytes
2021
)
2122

2223
if PY3:

git/index/base.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import sys
99
import subprocess
1010
import glob
11-
from io import StringIO
11+
from io import BytesIO
1212

1313
from stat import S_ISLNK
1414

@@ -43,6 +43,7 @@
4343
izip,
4444
xrange,
4545
string_types,
46+
force_bytes
4647
)
4748

4849
from git.util import (
@@ -562,7 +563,8 @@ def _store_path(self, filepath, fprogress):
562563
st = os.lstat(filepath) # handles non-symlinks as well
563564
stream = None
564565
if S_ISLNK(st.st_mode):
565-
stream = StringIO(os.readlink(filepath))
566+
# in PY3, readlink is string, but we need bytes. In PY2, it's just OS encoded bytes, we assume UTF-8
567+
stream = BytesIO(force_bytes(os.readlink(filepath), encoding='utf-8'))
566568
else:
567569
stream = open(filepath, 'rb')
568570
# END handle stream

git/index/fun.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
S_IFGITLINK = S_IFLNK | S_IFDIR # a submodule
1414

15-
from io import StringIO
15+
from io import BytesIO
1616

1717
from git.util import IndexFileSHA1Writer
1818
from git.exc import UnmergedEntriesError
@@ -218,7 +218,7 @@ def write_tree_from_cache(entries, odb, sl, si=0):
218218
# END for each entry
219219

220220
# finally create the tree
221-
sio = StringIO()
221+
sio = BytesIO()
222222
tree_to_stream(tree_items, sio.write)
223223
sio.seek(0)
224224

git/objects/commit.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
altzone
3131
)
3232
import os
33-
from io import StringIO
33+
from io import BytesIO
3434
import logging
3535

3636
log = logging.getLogger('git.objects.commit')
@@ -133,7 +133,7 @@ def _set_cache_(self, attr):
133133
if attr in Commit.__slots__:
134134
# read the data in a chunk, its faster - then provide a file wrapper
135135
binsha, typename, self.size, stream = self.repo.odb.stream(self.binsha)
136-
self._deserialize(StringIO(stream.read()))
136+
self._deserialize(BytesIO(stream.read()))
137137
else:
138138
super(Commit, self)._set_cache_(attr)
139139
# END handle attrs
@@ -345,7 +345,7 @@ def create_from_tree(cls, repo, tree, message, parent_commits=None, head=False,
345345
committer, committer_time, committer_offset,
346346
message, parent_commits, conf_encoding)
347347

348-
stream = StringIO()
348+
stream = BytesIO()
349349
new_commit._serialize(stream)
350350
streamlen = stream.tell()
351351
stream.seek(0)

git/objects/submodule/base.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
find_first_remote_branch
99
)
1010
from git.objects.util import Traversable
11-
from io import StringIO # need a dict to set bloody .name field
11+
from io import BytesIO # need a dict to set bloody .name field
1212
from git.util import (
1313
Iterable,
1414
join_path_native,
@@ -187,8 +187,8 @@ def _clear_cache(self):
187187

188188
@classmethod
189189
def _sio_modules(cls, parent_commit):
190-
""":return: Configuration file as StringIO - we only access it through the respective blob's data"""
191-
sio = StringIO(parent_commit.tree[cls.k_modules_file].data_stream.read())
190+
""":return: Configuration file as BytesIO - we only access it through the respective blob's data"""
191+
sio = BytesIO(parent_commit.tree[cls.k_modules_file].data_stream.read())
192192
sio.name = cls.k_modules_file
193193
return sio
194194

git/objects/submodule/util.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import git
22
from git.exc import InvalidGitRepositoryError
33
from git.config import GitConfigParser
4-
from io import StringIO
4+
from io import BytesIO
55
import weakref
66

77
__all__ = ('sm_section', 'sm_name', 'mkhead', 'unbare_repo', 'find_first_remote_branch',
@@ -83,7 +83,7 @@ def flush_to_index(self):
8383
"""Flush changes in our configuration file to the index"""
8484
assert self._smref is not None
8585
# should always have a file here
86-
assert not isinstance(self._file_or_files, StringIO)
86+
assert not isinstance(self._file_or_files, BytesIO)
8787

8888
sm = self._smref()
8989
if sm is not None:

git/refs/log.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def from_line(cls, line):
8585
:param line: line without trailing newline
8686
:raise ValueError: If line could not be parsed"""
8787
try:
88-
info, msg = line.split('\t', 2)
88+
info, msg = line.split('\t', 1)
8989
except ValueError:
9090
raise ValueError("line is missing tab separator")
9191
# END handle first plit

git/test/fixtures/git_config_global

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# just a comment
12
[alias]
23
st = status
34
ci = commit

git/test/lib/helper.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ class StringProcessAdapter(object):
4848
Its tailored to work with the test system only"""
4949

5050
def __init__(self, input_string):
51-
self.stdout = io.StringIO(input_string)
52-
self.stderr = io.StringIO()
51+
self.stdout = io.BytesIO(input_string)
52+
self.stderr = io.BytesIO()
5353

5454
def wait(self):
5555
return 0

git/test/performance/test_commit.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# This module is part of GitPython and is released under
55
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
66
from __future__ import print_function
7-
from io import StringIO
7+
from io import BytesIO
88
from time import time
99
import sys
1010

@@ -93,7 +93,7 @@ def test_commit_serialization(self):
9393
hc.committer, hc.committed_date, hc.committer_tz_offset,
9494
str(i), parents=hc.parents, encoding=hc.encoding)
9595

96-
stream = StringIO()
96+
stream = BytesIO()
9797
cm._serialize(stream)
9898
slen = stream.tell()
9999
stream.seek(0)

git/test/test_commit.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
text_type
2626
)
2727

28-
from io import StringIO
28+
from io import BytesIO
2929
import time
3030
import sys
3131
import re
@@ -44,7 +44,7 @@ def assert_commit_serialization(rwrepo, commit_id, print_performance_info=False)
4444

4545
# assert that we deserialize commits correctly, hence we get the same
4646
# sha on serialization
47-
stream = StringIO()
47+
stream = BytesIO()
4848
cm._serialize(stream)
4949
ns += 1
5050
streamlen = stream.tell()
@@ -59,7 +59,7 @@ def assert_commit_serialization(rwrepo, commit_id, print_performance_info=False)
5959
cm.message, cm.parents, cm.encoding)
6060

6161
assert nc.parents == cm.parents
62-
stream = StringIO()
62+
stream = BytesIO()
6363
nc._serialize(stream)
6464
ns += 1
6565
streamlen = stream.tell()
@@ -276,7 +276,7 @@ def test_serialization_unicode_support(self):
276276
cmt.author.name = "äüß".decode("utf-8")
277277
assert len(cmt.author.name) == 3
278278

279-
cstream = StringIO()
279+
cstream = BytesIO()
280280
cmt._serialize(cstream)
281281
cstream.seek(0)
282282
assert len(cstream.getvalue())
@@ -316,7 +316,7 @@ def test_gpgsig(self):
316316
cmt.gpgsig = "<test\ndummy\nsig>"
317317
assert cmt.gpgsig != fixture_sig
318318

319-
cstream = StringIO()
319+
cstream = BytesIO()
320320
cmt._serialize(cstream)
321321
assert re.search(r"^gpgsig <test\n dummy\n sig>$", cstream.getvalue(), re.MULTILINE)
322322

@@ -326,6 +326,6 @@ def test_gpgsig(self):
326326
assert cmt.gpgsig == "<test\ndummy\nsig>"
327327

328328
cmt.gpgsig = None
329-
cstream = StringIO()
329+
cstream = BytesIO()
330330
cmt._serialize(cstream)
331331
assert not re.search(r"^gpgsig ", cstream.getvalue(), re.MULTILINE)

git/test/test_config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
GitConfigParser
1313
)
1414
from git.compat import string_types
15-
import StringIO
15+
import io
1616
from copy import copy
1717
from ConfigParser import NoSectionError
1818

@@ -21,7 +21,7 @@ class TestBase(TestCase):
2121

2222
def _to_memcache(self, file_path):
2323
fp = open(file_path, "r")
24-
sio = StringIO.StringIO(fp.read())
24+
sio = io.BytesIO(fp.read())
2525
sio.name = file_path
2626
return sio
2727

git/test/test_fun.py

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

2626
from git.index import IndexFile
27-
from io import StringIO
27+
from io import BytesIO
2828

2929

3030
class TestFun(TestBase):
@@ -72,7 +72,7 @@ def test_aggressive_tree_merge(self):
7272

7373
def mktree(self, odb, entries):
7474
"""create a tree from the given tree entries and safe it to the database"""
75-
sio = StringIO()
75+
sio = BytesIO()
7676
tree_to_stream(entries, sio.write)
7777
sio.seek(0)
7878
istream = odb.store(IStream(str_tree_type, len(sio.getvalue()), sio))

git/test/test_index.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
ST_MODE
3232
)
3333

34-
from io import StringIO
34+
from io import BytesIO
3535
from gitdb.base import IStream
3636
from git.objects import Blob
3737
from git.index.typ import (
@@ -698,9 +698,9 @@ def test_index_bare_add(self, rw_bare_repo):
698698
# instead of throwing the Exception we are expecting. This is
699699
# a quick hack to make this test fail when expected.
700700
rw_bare_repo._working_tree_dir = None
701-
contents = 'This is a StringIO file'
701+
contents = b'This is a BytesIO file'
702702
filesize = len(contents)
703-
fileobj = StringIO(contents)
703+
fileobj = BytesIO(contents)
704704
filename = 'my-imaginary-file'
705705
istream = rw_bare_repo.odb.store(
706706
IStream(Blob.type, filesize, fileobj))

git/test/test_repo.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
import sys
3737
import tempfile
3838
import shutil
39-
from io import StringIO
39+
from io import BytesIO
4040

4141

4242
class TestRepo(TestBase):
@@ -362,22 +362,22 @@ def test_comparison_and_hash(self):
362362
def test_git_cmd(self):
363363
# test CatFileContentStream, just to be very sure we have no fencepost errors
364364
# last \n is the terminating newline that it expects
365-
l1 = "0123456789\n"
366-
l2 = "abcdefghijklmnopqrstxy\n"
367-
l3 = "z\n"
368-
d = "%s%s%s\n" % (l1, l2, l3)
365+
l1 = b"0123456789\n"
366+
l2 = b"abcdefghijklmnopqrstxy\n"
367+
l3 = b"z\n"
368+
d = b"%s%s%s\n" % (l1, l2, l3)
369369

370370
l1p = l1[:5]
371371

372372
# full size
373373
# size is without terminating newline
374374
def mkfull():
375-
return Git.CatFileContentStream(len(d) - 1, StringIO(d))
375+
return Git.CatFileContentStream(len(d) - 1, BytesIO(d))
376376

377377
ts = 5
378378

379379
def mktiny():
380-
return Git.CatFileContentStream(ts, StringIO(d))
380+
return Git.CatFileContentStream(ts, BytesIO(d))
381381

382382
# readlines no limit
383383
s = mkfull()

git/test/test_tree.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
Blob
1212
)
1313

14-
from io import StringIO
14+
from io import BytesIO
1515

1616

1717
class TestTree(TestBase):
@@ -30,7 +30,7 @@ def test_serializable(self):
3030
orig_data = tree.data_stream.read()
3131
orig_cache = tree._cache
3232

33-
stream = StringIO()
33+
stream = BytesIO()
3434
tree._serialize(stream)
3535
assert stream.getvalue() == orig_data
3636

@@ -82,7 +82,7 @@ def test_serializable(self):
8282
mod.set_done() # multiple times are okay
8383

8484
# serialize, its different now
85-
stream = StringIO()
85+
stream = BytesIO()
8686
testtree._serialize(stream)
8787
stream.seek(0)
8888
assert stream.getvalue() != orig_data

0 commit comments

Comments
 (0)