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)

0 commit comments

Comments
 (0)