Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

string.Formatter accepts empty fields but displays wrong when nested #69222

Closed
AvdN mannequin opened this issue Sep 8, 2015 · 6 comments
Closed

string.Formatter accepts empty fields but displays wrong when nested #69222

AvdN mannequin opened this issue Sep 8, 2015 · 6 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@AvdN
Copy link
Mannequin

AvdN mannequin commented Sep 8, 2015

BPO 25034
Nosy @AvdN, @ericvsmith, @jayvdb
Files
  • 25034.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/ericvsmith'
    closed_at = <Date 2015-09-29.14:34:36.217>
    created_at = <Date 2015-09-08.19:23:13.767>
    labels = ['type-bug', 'library']
    title = 'string.Formatter accepts empty fields but displays wrong when nested'
    updated_at = <Date 2015-12-16.22:16:03.112>
    user = 'https://github.com/AvdN'

    bugs.python.org fields:

    activity = <Date 2015-12-16.22:16:03.112>
    actor = 'jayvdb'
    assignee = 'eric.smith'
    closed = True
    closed_date = <Date 2015-09-29.14:34:36.217>
    closer = 'eric.smith'
    components = ['Library (Lib)']
    creation = <Date 2015-09-08.19:23:13.767>
    creator = 'anthon'
    dependencies = []
    files = ['40419']
    hgrepos = []
    issue_num = 25034
    keywords = ['patch']
    message_count = 6.0
    messages = ['250253', '250254', '250291', '251860', '251861', '255258']
    nosy_count = 5.0
    nosy_names = ['anthon', 'eric.smith', 'python-dev', 'jayvdb', 'Bill Tutt']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue25034'
    versions = ['Python 3.4', 'Python 3.5', 'Python 3.6']

    @AvdN
    Copy link
    Mannequin Author

    AvdN mannequin commented Sep 8, 2015

    Since 3.4.1, string.Formatter() accepts empty keys {}. If these are nested they give different results from explicitly numbered, where the same arguments applied "".format() give the expected results:

        from string import Formatter
    
        f = Formatter()
    
        fmt0 = "X {:<{}} {} X"
        fmt1 = "X {0:<{1}} {2} X"
    
        for fmt in [fmt0, fmt1]:
            x = f.format(fmt, 'ab', 5, 1)
            y = fmt.format(   'ab', 5, 1)
            print(x)
            print(y)
    gives:
    X ab    5 X
    X ab    1 X
    X ab    1 X
    X ab    1 X
    

    of which the first line is incorrect.

    @AvdN AvdN mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Sep 8, 2015
    @AvdN
    Copy link
    Mannequin Author

    AvdN mannequin commented Sep 8, 2015

    Here is a patch for Python-3.5.0rc3/Lib/test/test_string.py unittests fail:

    --- /opt/python/3.5/lib/python3.5/test/test_string.py	2015-09-08 17:06:07.099197904 +0200
    +++ test_string.py	2015-09-08 21:47:01.471634309 +0200
    @@ -58,6 +58,8 @@
                              'foo{1}{num}{1}'.format(None, 'bar', num=6))
             self.assertEqual(fmt.format('{:^{}}', 'bar', 6),
                              '{:^{}}'.format('bar', 6))
    +        self.assertEqual(fmt.format('{:^{}} {}', 'bar', 6, 'X'),
    +                         '{:^{}} {}'.format('bar', 6, 'X'))
             self.assertEqual(fmt.format('{:^{pad}}{}', 'foo', 'bar', pad=6),
                              '{:^{pad}}{}'.format('foo', 'bar', pad=6))

    @ericvsmith ericvsmith self-assigned this Sep 8, 2015
    @AvdN
    Copy link
    Mannequin Author

    AvdN mannequin commented Sep 9, 2015

    The problem lies in the recursive call to _vformat which might consume fields without name ({}) and increment auto_arg_index, but that
    incremented value was not returned to the parent.

    Since the line became longer than PEP-8 allowed I wrapped all of the method call arguments to the next line, hope that that's ok.

    The patch is against the mercurial repository and works for 3.4.1 upwards.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Sep 29, 2015

    New changeset 9eae18e8af66 by Eric V. Smith in branch '3.4':
    Fixed issue bpo-25034: Fix string.Formatter problem with auto-numbering
    https://hg.python.org/cpython/rev/9eae18e8af66

    New changeset 65d7b4fd0332 by Eric V. Smith in branch '3.5':
    Issue bpo-25034: Merge from 3.4.
    https://hg.python.org/cpython/rev/65d7b4fd0332

    New changeset aef6365294c8 by Eric V. Smith in branch 'default':
    Issue bpo-25034: Merge from 3.5.
    https://hg.python.org/cpython/rev/aef6365294c8

    @ericvsmith
    Copy link
    Member

    Fixed in 3.4, 3.5, and 3.6. Thanks for the bug report and patch! I added you to the Misc/ACKS file.

    @BillTutt
    Copy link
    Mannequin

    BillTutt mannequin commented Nov 24, 2015

    I don't suppose this change could make it into 2.7.11 as well?

    Thanks,
    Bill

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant