Title: locale.format_string fails on escaped percentage
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.1, Python 2.6
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: r.david.murray Nosy List: christoph, eric.smith, mtschopp, r.david.murray
Priority: normal Keywords: patch

Created on 2009-08-06 11:09 by christoph, last changed 2010-09-05 22:48 by r.david.murray. This issue is now closed.

File name Uploaded Description Edit
locale_percents_test.diff christoph, 2009-08-06 11:09 Test case for locale.format_string()
locale_percents.diff christoph, 2009-08-06 11:14 Patch fixing said bug
locale-tests.diff r.david.murray, 2009-08-11 08:01
locale-fix.diff r.david.murray, 2009-08-11 08:05
Messages (7)
msg91352 - (view) Author: Christoph Burgmer (christoph) Date: 2009-08-06 11:09
locale.format_string doesn't return same result as a normal
"string" % format
directive, but raises a TypeError. See attached test case for Python

>>> locale.format_string('%f%%', 1.0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.5/", line 195, in format_string
    return new_f % val
TypeError: not enough arguments for format string
>>> '%f%%' % 1.0
msg91353 - (view) Author: Christoph Burgmer (christoph) Date: 2009-08-06 11:14
This patch removes '%%' entities from the regex results and only
replaces other matches with '%s' which later then get replaced by
localized versions so that escaped percentage entities don't show up in
localized parsing anymore.

Removing case '%%' from the regex completely does not sound feasible
and will result in '%%d' having a match '%d', though d should be a
normal character.

The replacing of regex matches does not look that beautiful, feel free
to rewrite said part.
msg91470 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2009-08-11 08:01
I fixed this a different way that seems cleaner.  In the process, I
discovered that passing a mapping argument to format_string is
completely broken.  Here is an expanded set of tests that demonstrate
this.  This diff is against trunk.
msg91471 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2009-08-11 08:05
Here is a patch against trunk that fixes both the mapping handling and
the % escape handling.

The thing that worries me about this patch is that there are (obviously)
not a comprehensive set of tests proving that format_string actually
behaves like % formatting does.  So my refactoring could have broken
other behavior and I wouldn't know it.  However, since % formatting is
effectively depricated, I'm not sure how much effort it is worth putting
in to this....
msg100730 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2010-03-09 14:49
Eric, the patch for this issue contains a fix for issue 8096.  The only reason I haven't applied it is the fear of breaking existing correct behavior because there aren't enough tests.  Maybe you can see an easy way to reuse the % test suite to check local.format_string?  (I didn't look at that option very hard.)  Or maybe we just apply it anyway...
msg104373 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2010-04-27 22:05
Committed a very slightly modified version of the patch to trunk in r80512, and to py3k in 80521.  I'm leaving this issue open and, unless there is an objection, if no problems show up after the next beta has been out for a while, I'll backport the fix to 2.6 and 3.1.
msg115676 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2010-09-05 22:48
Well, I let this bake so long I missed 2.6, but I've backported the fix to 3.1 in r84543.
Date User Action Args
2010-09-05 22:48:40r.david.murraysetstatus: open -> closed

messages: + msg115676
2010-04-27 22:05:33r.david.murraysetversions: - Python 2.7, Python 3.2
messages: + msg104373

keywords: - needs review
resolution: fixed
stage: patch review -> resolved
2010-03-09 14:50:04r.david.murraysetmessages: - msg100729
2010-03-09 14:49:57r.david.murraysetmessages: - msg100727
2010-03-09 14:49:49r.david.murraysetmessages: + msg100730
2010-03-09 14:48:47r.david.murraysetnosy: + mtschopp
messages: + msg100729
2010-03-09 14:47:44r.david.murraysetmessages: + msg100727
2010-03-09 14:45:32r.david.murraylinkissue8096 superseder
2010-03-09 14:42:15r.david.murraysetnosy: + eric.smith
2009-08-11 08:05:41r.david.murraysetkeywords: + needs review
files: + locale-fix.diff
messages: + msg91471

stage: patch review
2009-08-11 08:01:52r.david.murraysetfiles: + locale-tests.diff
priority: normal
type: behavior

assignee: r.david.murray
versions: + Python 3.1, Python 2.7, Python 3.2, - Python 2.5
nosy: + r.david.murray

messages: + msg91470
2009-08-06 11:14:37christophsetfiles: + locale_percents.diff

messages: + msg91353
2009-08-06 11:09:36christophcreate