Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitdf95149

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.Fixesgitpython-developers#237
1 parentd5054fd commitdf95149

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)insection_dict.items():
414415
ifkey!="__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
returnvaluestr
531532

533+
def_value_to_string(self,value):
534+
ifisinstance(value, (int,float,bool)):
535+
returnstr(value)
536+
returnforce_text(value)
537+
532538
@needs_values
533539
@set_dirty_and_flush_changes
534540
defset_value(self,section,option,value):
@@ -543,7 +549,7 @@ def set_value(self, section, option, value):
543549
to a string"""
544550
ifnotself.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
defrename_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
fork,vinself.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
umail="sd@company.com"
386386
writer=rw_repo.config_writer()
387387
writer.set_value("user","name",uname)
388388
writer.set_value("user","email",umail)
389389
writer.release()
390+
assertwriter.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 .excimportGitCommandError
20-
from .compatimportMAXSIZE
20+
from .compatimport (
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
forattr,evar,cvar,defaultin (('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+
ifnotPY3:
373+
val=val.decode(defenc)
374+
# end assure we don't get 'invalid strings'
375+
setattr(actor,attr,val)
368376
exceptKeyError:
369377
ifconfig_readerisnotNone:
370378
setattr(actor,attr,config_reader.get_value('user',cvar,default))

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp