summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian Thiel <byronimo@gmail.com>2015-01-19 18:10:16 +0100
committerSebastian Thiel <byronimo@gmail.com>2015-01-19 18:10:16 +0100
commitdf95149198744c258ed6856044ac5e79e6b81404 (patch)
treee583207230b9ddfbd170c01d218168ca12fea07b
parentd5054fdb1766cb035a1186c3cef4a14472fee98d (diff)
downloadgitpython-df95149198744c258ed6856044ac5e79e6b81404.tar.gz
Improved unicode handling when using os.environ or GitConfigParser
Assured unicode values are supported when reading the configuration, and when getting author/committer information from the environment. Fixes #237
-rw-r--r--git/config.py12
-rw-r--r--git/test/test_index.py3
-rw-r--r--git/util.py12
3 files changed, 21 insertions, 6 deletions
diff --git a/git/config.py b/git/config.py
index f41a86e6..ed61bf93 100644
--- a/git/config.py
+++ b/git/config.py
@@ -23,6 +23,7 @@ from git.compat import (
string_types,
FileType,
defenc,
+ force_text,
with_metaclass,
PY3
)
@@ -412,7 +413,7 @@ class GitConfigParser(with_metaclass(MetaParserBuilder, cp.RawConfigParser, obje
fp.write(("[%s]\n" % name).encode(defenc))
for (key, value) in section_dict.items():
if key != "__name__":
- fp.write(("\t%s = %s\n" % (key, str(value).replace('\n', '\n\t'))).encode(defenc))
+ fp.write(("\t%s = %s\n" % (key, self._value_to_string(value).replace('\n', '\n\t'))).encode(defenc))
# END if key is not __name__
# END section writing
@@ -529,6 +530,11 @@ class GitConfigParser(with_metaclass(MetaParserBuilder, cp.RawConfigParser, obje
return valuestr
+ def _value_to_string(self, value):
+ if isinstance(value, (int, float, bool)):
+ return str(value)
+ return force_text(value)
+
@needs_values
@set_dirty_and_flush_changes
def set_value(self, section, option, value):
@@ -543,7 +549,7 @@ class GitConfigParser(with_metaclass(MetaParserBuilder, cp.RawConfigParser, obje
to a string"""
if not self.has_section(section):
self.add_section(section)
- self.set(section, option, str(value))
+ self.set(section, option, self._value_to_string(value))
def rename_section(self, section, new_name):
"""rename the given section to new_name
@@ -558,7 +564,7 @@ class GitConfigParser(with_metaclass(MetaParserBuilder, cp.RawConfigParser, obje
super(GitConfigParser, self).add_section(new_name)
for k, v in self.items(section):
- self.set(new_name, k, str(v))
+ self.set(new_name, k, self._value_to_string(v))
# end for each value to copy
# This call writes back the changes, which is why we don't have the respective decorator
diff --git a/git/test/test_index.py b/git/test/test_index.py
index 2be776cd..0569f40f 100644
--- a/git/test/test_index.py
+++ b/git/test/test_index.py
@@ -381,12 +381,13 @@ class TestIndex(TestBase):
num_entries = len(index.entries)
cur_head = rw_repo.head
- uname = "Some Developer"
+ uname = u"Thomas Müller"
umail = "sd@company.com"
writer = rw_repo.config_writer()
writer.set_value("user", "name", uname)
writer.set_value("user", "email", umail)
writer.release()
+ assert writer.get_value("user", "name") == uname
# remove all of the files, provide a wild mix of paths, BaseIndexEntries,
# IndexEntries
diff --git a/git/util.py b/git/util.py
index 010130cb..5385455a 100644
--- a/git/util.py
+++ b/git/util.py
@@ -17,7 +17,11 @@ import threading
# NOTE: Some of the unused imports might be used/imported by others.
# Handle once test-cases are back up and running.
from .exc import GitCommandError
-from .compat import MAXSIZE
+from .compat import (
+ MAXSIZE,
+ defenc,
+ PY3
+)
# Most of these are unused here, but are for use by git-python modules so these
# don't see gitdb all the time. Flake of course doesn't like it.
@@ -364,7 +368,11 @@ class Actor(object):
for attr, evar, cvar, default in (('name', env_name, cls.conf_name, default_name),
('email', env_email, cls.conf_email, default_email)):
try:
- setattr(actor, attr, os.environ[evar])
+ val = os.environ[evar]
+ if not PY3:
+ val = val.decode(defenc)
+ # end assure we don't get 'invalid strings'
+ setattr(actor, attr, val)
except KeyError:
if config_reader is not None:
setattr(actor, attr, config_reader.get_value('user', cvar, default))