Skip to content

Commit

Permalink
Fixed io types to make tests work on PY2 once again.
Browse files Browse the repository at this point in the history
Now it's about going through PY3 issues
  • Loading branch information
Byron committed Jan 5, 2015
1 parent ae2ff0f commit bc8c912
Show file tree
Hide file tree
Showing 16 changed files with 45 additions and 41 deletions.
3 changes: 2 additions & 1 deletion git/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

from gitdb.utils.encoding import (
string_types,
text_type
text_type,
force_bytes
)

if PY3:
Expand Down
6 changes: 4 additions & 2 deletions git/index/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import sys
import subprocess
import glob
from io import StringIO
from io import BytesIO

from stat import S_ISLNK

Expand Down Expand Up @@ -43,6 +43,7 @@
izip,
xrange,
string_types,
force_bytes
)

from git.util import (
Expand Down Expand Up @@ -562,7 +563,8 @@ def _store_path(self, filepath, fprogress):
st = os.lstat(filepath) # handles non-symlinks as well
stream = None
if S_ISLNK(st.st_mode):
stream = StringIO(os.readlink(filepath))
# in PY3, readlink is string, but we need bytes. In PY2, it's just OS encoded bytes, we assume UTF-8
stream = BytesIO(force_bytes(os.readlink(filepath), encoding='utf-8'))
else:
stream = open(filepath, 'rb')
# END handle stream
Expand Down
4 changes: 2 additions & 2 deletions git/index/fun.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

S_IFGITLINK = S_IFLNK | S_IFDIR # a submodule

from io import StringIO
from io import BytesIO

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

# finally create the tree
sio = StringIO()
sio = BytesIO()
tree_to_stream(tree_items, sio.write)
sio.seek(0)

Expand Down
6 changes: 3 additions & 3 deletions git/objects/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
altzone
)
import os
from io import StringIO
from io import BytesIO
import logging

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

stream = StringIO()
stream = BytesIO()
new_commit._serialize(stream)
streamlen = stream.tell()
stream.seek(0)
Expand Down
6 changes: 3 additions & 3 deletions git/objects/submodule/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
find_first_remote_branch
)
from git.objects.util import Traversable
from io import StringIO # need a dict to set bloody .name field
from io import BytesIO # need a dict to set bloody .name field
from git.util import (
Iterable,
join_path_native,
Expand Down Expand Up @@ -187,8 +187,8 @@ def _clear_cache(self):

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

Expand Down
4 changes: 2 additions & 2 deletions git/objects/submodule/util.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import git
from git.exc import InvalidGitRepositoryError
from git.config import GitConfigParser
from io import StringIO
from io import BytesIO
import weakref

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

sm = self._smref()
if sm is not None:
Expand Down
2 changes: 1 addition & 1 deletion git/refs/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def from_line(cls, line):
:param line: line without trailing newline
:raise ValueError: If line could not be parsed"""
try:
info, msg = line.split('\t', 2)
info, msg = line.split('\t', 1)
except ValueError:
raise ValueError("line is missing tab separator")
# END handle first plit
Expand Down
1 change: 1 addition & 0 deletions git/test/fixtures/git_config_global
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# just a comment
[alias]
st = status
ci = commit
Expand Down
4 changes: 2 additions & 2 deletions git/test/lib/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ class StringProcessAdapter(object):
Its tailored to work with the test system only"""

def __init__(self, input_string):
self.stdout = io.StringIO(input_string)
self.stderr = io.StringIO()
self.stdout = io.BytesIO(input_string)
self.stderr = io.BytesIO()

def wait(self):
return 0
Expand Down
4 changes: 2 additions & 2 deletions git/test/performance/test_commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# This module is part of GitPython and is released under
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
from __future__ import print_function
from io import StringIO
from io import BytesIO
from time import time
import sys

Expand Down Expand Up @@ -93,7 +93,7 @@ def test_commit_serialization(self):
hc.committer, hc.committed_date, hc.committer_tz_offset,
str(i), parents=hc.parents, encoding=hc.encoding)

stream = StringIO()
stream = BytesIO()
cm._serialize(stream)
slen = stream.tell()
stream.seek(0)
Expand Down
12 changes: 6 additions & 6 deletions git/test/test_commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
text_type
)

from io import StringIO
from io import BytesIO
import time
import sys
import re
Expand All @@ -44,7 +44,7 @@ def assert_commit_serialization(rwrepo, commit_id, print_performance_info=False)

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

assert nc.parents == cm.parents
stream = StringIO()
stream = BytesIO()
nc._serialize(stream)
ns += 1
streamlen = stream.tell()
Expand Down Expand Up @@ -276,7 +276,7 @@ def test_serialization_unicode_support(self):
cmt.author.name = "äüß".decode("utf-8")
assert len(cmt.author.name) == 3

cstream = StringIO()
cstream = BytesIO()
cmt._serialize(cstream)
cstream.seek(0)
assert len(cstream.getvalue())
Expand Down Expand Up @@ -316,7 +316,7 @@ def test_gpgsig(self):
cmt.gpgsig = "<test\ndummy\nsig>"
assert cmt.gpgsig != fixture_sig

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

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

cmt.gpgsig = None
cstream = StringIO()
cstream = BytesIO()
cmt._serialize(cstream)
assert not re.search(r"^gpgsig ", cstream.getvalue(), re.MULTILINE)
4 changes: 2 additions & 2 deletions git/test/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
GitConfigParser
)
from git.compat import string_types
import StringIO
import io
from copy import copy
from ConfigParser import NoSectionError

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

def _to_memcache(self, file_path):
fp = open(file_path, "r")
sio = StringIO.StringIO(fp.read())
sio = io.BytesIO(fp.read())
sio.name = file_path
return sio

Expand Down
4 changes: 2 additions & 2 deletions git/test/test_fun.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
)

from git.index import IndexFile
from io import StringIO
from io import BytesIO


class TestFun(TestBase):
Expand Down Expand Up @@ -72,7 +72,7 @@ def test_aggressive_tree_merge(self):

def mktree(self, odb, entries):
"""create a tree from the given tree entries and safe it to the database"""
sio = StringIO()
sio = BytesIO()
tree_to_stream(entries, sio.write)
sio.seek(0)
istream = odb.store(IStream(str_tree_type, len(sio.getvalue()), sio))
Expand Down
6 changes: 3 additions & 3 deletions git/test/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
ST_MODE
)

from io import StringIO
from io import BytesIO
from gitdb.base import IStream
from git.objects import Blob
from git.index.typ import (
Expand Down Expand Up @@ -698,9 +698,9 @@ def test_index_bare_add(self, rw_bare_repo):
# instead of throwing the Exception we are expecting. This is
# a quick hack to make this test fail when expected.
rw_bare_repo._working_tree_dir = None
contents = 'This is a StringIO file'
contents = b'This is a BytesIO file'
filesize = len(contents)
fileobj = StringIO(contents)
fileobj = BytesIO(contents)
filename = 'my-imaginary-file'
istream = rw_bare_repo.odb.store(
IStream(Blob.type, filesize, fileobj))
Expand Down
14 changes: 7 additions & 7 deletions git/test/test_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import sys
import tempfile
import shutil
from io import StringIO
from io import BytesIO


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

l1p = l1[:5]

# full size
# size is without terminating newline
def mkfull():
return Git.CatFileContentStream(len(d) - 1, StringIO(d))
return Git.CatFileContentStream(len(d) - 1, BytesIO(d))

ts = 5

def mktiny():
return Git.CatFileContentStream(ts, StringIO(d))
return Git.CatFileContentStream(ts, BytesIO(d))

# readlines no limit
s = mkfull()
Expand Down
6 changes: 3 additions & 3 deletions git/test/test_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
Blob
)

from io import StringIO
from io import BytesIO


class TestTree(TestBase):
Expand All @@ -30,7 +30,7 @@ def test_serializable(self):
orig_data = tree.data_stream.read()
orig_cache = tree._cache

stream = StringIO()
stream = BytesIO()
tree._serialize(stream)
assert stream.getvalue() == orig_data

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

# serialize, its different now
stream = StringIO()
stream = BytesIO()
testtree._serialize(stream)
stream.seek(0)
assert stream.getvalue() != orig_data
Expand Down

0 comments on commit bc8c912

Please sign in to comment.