diff options
author | Sebastian Thiel <byronimo@gmail.com> | 2015-01-19 18:10:16 +0100 |
---|---|---|
committer | Sebastian Thiel <byronimo@gmail.com> | 2015-01-19 18:10:16 +0100 |
commit | df95149198744c258ed6856044ac5e79e6b81404 (patch) | |
tree | e583207230b9ddfbd170c01d218168ca12fea07b /git/config.py | |
parent | d5054fdb1766cb035a1186c3cef4a14472fee98d (diff) | |
download | gitpython-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
Diffstat (limited to 'git/config.py')
-rw-r--r-- | git/config.py | 12 |
1 files changed, 9 insertions, 3 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 |