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

Incorrect handling of backslashes and quotes in GitConfigParser#46

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Byron merged 3 commits intogitpython-developers:masterfromereOn:master
May 30, 2012
Merged
Show file tree
Hide file tree
Changes fromall 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
30 changes: 27 additions & 3 deletionsgit/config.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -245,8 +245,28 @@ def _read(self, fp, fpname):
if pos != -1 and optval[pos-1].isspace():
optval = optval[:pos]
optval = optval.strip()
if optval == '""':
optval = ''

# Remove paired unescaped-quotes
unquoted_optval = ''
escaped = False
in_quote = False
for c in optval:
if not escaped and c == '"':
in_quote = not in_quote
else:
escaped = (c == '\\') and not escaped
unquoted_optval += c

if in_quote:
if not e:
e = cp.ParsingError(fpname)
e.append(lineno, repr(line))

optval = unquoted_optval

optval = optval.replace('\\\\', '\\') # Unescape backslashes
optval = optval.replace(r'\"', '"') # Unescape quotes

optname = self.optionxform(optname.rstrip())
cursect[optname] = optval
else:
Expand DownExpand Up@@ -303,7 +323,11 @@ def write_section(name, section_dict):
fp.write("[%s]\n" % name)
for (key, value) in section_dict.items():
if key != "__name__":
fp.write("\t%s = %s\n" % (key, str(value).replace('\n', '\n\t')))
value = str(value)
value = value.replace('\\', '\\\\') # Escape backslashes
value = value.replace('"', r'\"') # Escape quotes
value = value.replace('\n', '\n\t')
fp.write("\t%s = %s\n" % (key, value))
# END if key is not __name__
# END section writing

Expand Down
4 changes: 4 additions & 0 deletionsgit/test/fixtures/git_config_values
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
[values]
backslash = some\\data
quote = this is a \"quoted value\"
quoted = "all" "your \"quotes\" a"re bel"ong to """"us"
26 changes: 25 additions & 1 deletiongit/test/test_config.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -101,4 +101,28 @@ def test_base(self):
# it raises if there is no default though
self.failUnlessRaises(NoSectionError, r_config.get_value, "doesnt", "exist")


def test_values(self):
file_obj = self._to_memcache(fixture_path("git_config_values"))
w_config = GitConfigParser(file_obj, read_only = False)
w_config.write() # enforce writing
orig_value = file_obj.getvalue()

# Reading must unescape backslashes
backslash = w_config.get('values', 'backslash')
assert backslash == r'some\data'

# Reading must unescape quotes
quote = w_config.get('values', 'quote')
assert quote == 'this is a "quoted value"'

# Reading must remove surrounding quotes
quoted = w_config.get('values', 'quoted')
assert quoted == 'all your "quotes" are belong to us'

# Writing must escape backslashes and quotes
w_config.set('values', 'backslash', backslash)
w_config.set('values', 'quote', quote)
w_config.write() # enforce writing

# Contents shouldn't differ
assert file_obj.getvalue() == orig_value

[8]ページ先頭

©2009-2025 Movatter.jp