Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit df95149

Browse files
committed
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
1 parent d5054fd commit df95149

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

git/config.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
string_types,
2424
FileType,
2525
defenc,
26+
force_text,
2627
with_metaclass,
2728
PY3
2829
)
@@ -412,7 +413,7 @@ def write_section(name, section_dict):
412413
fp.write(("[%s]\n" % name).encode(defenc))
413414
for (key, value) in section_dict.items():
414415
if key != "__name__":
415-
fp.write(("\t%s = %s\n" % (key, str(value).replace('\n', '\n\t'))).encode(defenc))
416+
fp.write(("\t%s = %s\n" % (key, self._value_to_string(value).replace('\n', '\n\t'))).encode(defenc))
416417
# END if key is not __name__
417418
# END section writing
418419

@@ -529,6 +530,11 @@ def get_value(self, section, option, default=None):
529530

530531
return valuestr
531532

533+
def _value_to_string(self, value):
534+
if isinstance(value, (int, float, bool)):
535+
return str(value)
536+
return force_text(value)
537+
532538
@needs_values
533539
@set_dirty_and_flush_changes
534540
def set_value(self, section, option, value):
@@ -543,7 +549,7 @@ def set_value(self, section, option, value):
543549
to a string"""
544550
if not self.has_section(section):
545551
self.add_section(section)
546-
self.set(section, option, str(value))
552+
self.set(section, option, self._value_to_string(value))
547553

548554
def rename_section(self, section, new_name):
549555
"""rename the given section to new_name
@@ -558,7 +564,7 @@ def rename_section(self, section, new_name):
558564

559565
super(GitConfigParser, self).add_section(new_name)
560566
for k, v in self.items(section):
561-
self.set(new_name, k, str(v))
567+
self.set(new_name, k, self._value_to_string(v))
562568
# end for each value to copy
563569

564570
# This call writes back the changes, which is why we don't have the respective decorator

git/test/test_index.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,12 +381,13 @@ def test_index_mutation(self, rw_repo):
381381
num_entries = len(index.entries)
382382
cur_head = rw_repo.head
383383

384-
uname = "Some Developer"
384+
uname = u"Thomas Müller"
385385
386386
writer = rw_repo.config_writer()
387387
writer.set_value("user", "name", uname)
388388
writer.set_value("user", "email", umail)
389389
writer.release()
390+
assert writer.get_value("user", "name") == uname
390391

391392
# remove all of the files, provide a wild mix of paths, BaseIndexEntries,
392393
# IndexEntries

git/util.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717
# NOTE: Some of the unused imports might be used/imported by others.
1818
# Handle once test-cases are back up and running.
1919
from .exc import GitCommandError
20-
from .compat import MAXSIZE
20+
from .compat import (
21+
MAXSIZE,
22+
defenc,
23+
PY3
24+
)
2125

2226
# Most of these are unused here, but are for use by git-python modules so these
2327
# don't see gitdb all the time. Flake of course doesn't like it.
@@ -364,7 +368,11 @@ def _main_actor(cls, env_name, env_email, config_reader=None):
364368
for attr, evar, cvar, default in (('name', env_name, cls.conf_name, default_name),
365369
('email', env_email, cls.conf_email, default_email)):
366370
try:
367-
setattr(actor, attr, os.environ[evar])
371+
val = os.environ[evar]
372+
if not PY3:
373+
val = val.decode(defenc)
374+
# end assure we don't get 'invalid strings'
375+
setattr(actor, attr, val)
368376
except KeyError:
369377
if config_reader is not None:
370378
setattr(actor, attr, config_reader.get_value('user', cvar, default))

0 commit comments

Comments
 (0)