[issue12014] str.format parses replacement field incorrectly

Ben Wolfson report at bugs.python.org
Wed May 11 00:07:56 CEST 2011


Ben Wolfson <wolfson at gmail.com> added the comment:

Actually, that's the wrong place in MarkupIterator_next to include that loop. The attached diff has it in the right place. The results of "make test" here are:

328 tests OK.
1 test failed:
    test_unicode
25 tests skipped:
    test_codecmaps_cn test_codecmaps_hk test_codecmaps_jp
    test_codecmaps_kr test_codecmaps_tw test_curses test_dbm_gnu
    test_epoll test_gdb test_largefile test_msilib test_ossaudiodev
    test_readline test_smtpnet test_socketserver test_startfile
    test_timeout test_tk test_ttk_guionly test_urllib2net
    test_urllibnet test_winreg test_winsound test_xmlrpc_net
    test_zipfile64
1 skip unexpected on darwin:
    test_readline
make: [test] Error 1 (ignored)

test_unicode fails because it expects "{0[}".format() to raise an IndexError; instead, it raises a ValueError ("unmatched '{' in format") because it interprets the "}" as an *index*.

This can be avoided by changing the line 

                while (self->str.ptr < self->str.end && *self->str.ptr != ']') {

to 

                while (self->str.ptr < self->str.end-1 && *self->str.ptr != ']') {

In which case the test passes as is, or, obviously, by changing the expected exception in test_unicode.py.

----------
keywords: +patch
versions: +Python 2.6, Python 3.4
Added file: http://bugs.python.org/file21963/strformat.diff

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue12014>
_______________________________________


More information about the Python-bugs-list mailing list