Skip to content

Handling unicode arguments #175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 25, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions git/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,12 +421,16 @@ def transform_kwargs(self, **kwargs):
@classmethod
def __unpack_args(cls, arg_list):
if not isinstance(arg_list, (list,tuple)):
if isinstance(arg_list, unicode):
return [arg_list.encode('utf-8')]
return [ str(arg_list) ]

outlist = list()
for arg in arg_list:
if isinstance(arg_list, (list, tuple)):
outlist.extend(cls.__unpack_args( arg ))
elif isinstance(arg_list, unicode):
outlist.append(arg_list.encode('utf-8'))
# END recursion
else:
outlist.append(str(arg))
Expand Down
32 changes: 20 additions & 12 deletions git/test/test_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

import os, sys
from git.test.lib import (
TestBase,
patch,
TestBase,
patch,
raises,
assert_equal,
assert_true,
Expand All @@ -17,7 +17,7 @@
from git import Git, GitCommandError

class TestGit(TestBase):

@classmethod
def setUp(cls):
super(TestGit, cls).setUp()
Expand All @@ -30,6 +30,14 @@ def test_call_process_calls_execute(self, git):
assert_true(git.called)
assert_equal(git.call_args, ((['git', 'version'],), {}))

def test_call_unpack_args_unicode(self):
args = Git._Git__unpack_args(u'Unicode' + unichr(40960))
assert_equal(args, ['Unicode\xea\x80\x80'])

def test_call_unpack_args(self):
args = Git._Git__unpack_args(['git', 'log', '--', u'Unicode' + unichr(40960)])
assert_equal(args, ['git', 'log', '--', 'Unicode\xea\x80\x80'])

@raises(GitCommandError)
def test_it_raises_errors(self):
self.git.this_does_not_exist()
Expand Down Expand Up @@ -59,7 +67,7 @@ def test_it_ignores_false_kwargs(self, git):
# this_should_not_be_ignored=False implies it *should* be ignored
output = self.git.version(pass_this_kwarg=False)
assert_true("pass_this_kwarg" not in git.call_args[1])

def test_persistent_cat_file_command(self):
# read header only
import subprocess as sp
Expand All @@ -68,37 +76,37 @@ def test_persistent_cat_file_command(self):
g.stdin.write("b2339455342180c7cc1e9bba3e9f181f7baa5167\n")
g.stdin.flush()
obj_info = g.stdout.readline()

# read header + data
g = self.git.cat_file(batch=True, istream=sp.PIPE,as_process=True)
g.stdin.write("b2339455342180c7cc1e9bba3e9f181f7baa5167\n")
g.stdin.flush()
obj_info_two = g.stdout.readline()
assert obj_info == obj_info_two

# read data - have to read it in one large chunk
size = int(obj_info.split()[2])
data = g.stdout.read(size)
terminating_newline = g.stdout.read(1)

# now we should be able to read a new object
g.stdin.write("b2339455342180c7cc1e9bba3e9f181f7baa5167\n")
g.stdin.flush()
assert g.stdout.readline() == obj_info


# same can be achived using the respective command functions
hexsha, typename, size = self.git.get_object_header(hexsha)
hexsha, typename_two, size_two, data = self.git.get_object_data(hexsha)
assert typename == typename_two and size == size_two

def test_version(self):
v = self.git.version_info
assert isinstance(v, tuple)
for n in v:
assert isinstance(n, int)
#END verify number types

def test_cmd_override(self):
prev_cmd = self.git.GIT_PYTHON_GIT_EXECUTABLE
try:
Expand All @@ -108,7 +116,7 @@ def test_cmd_override(self):
finally:
type(self.git).GIT_PYTHON_GIT_EXECUTABLE = prev_cmd
#END undo adjustment

def test_output_strip(self):
import subprocess as sp
hexsha = "b2339455342180c7cc1e9bba3e9f181f7baa5167"
Expand Down