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

str.format_map raises a SystemError for format strings with positional arguments #56788

Closed
Julian mannequin opened this issue Jul 17, 2011 · 13 comments
Closed

str.format_map raises a SystemError for format strings with positional arguments #56788

Julian mannequin opened this issue Jul 17, 2011 · 13 comments
Assignees
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs)

Comments

@Julian
Copy link
Mannequin

Julian mannequin commented Jul 17, 2011

BPO 12579
Nosy @ericvsmith, @Julian
Files
  • format_map_err.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 2011-07-18.18:03:52.738>
    created_at = <Date 2011-07-17.14:43:42.190>
    labels = ['interpreter-core']
    title = 'str.format_map raises a SystemError for format strings with positional arguments'
    updated_at = <Date 2011-07-18.18:03:52.736>
    user = 'https://github.com/Julian'

    bugs.python.org fields:

    activity = <Date 2011-07-18.18:03:52.736>
    actor = 'python-dev'
    assignee = 'eric.smith'
    closed = True
    closed_date = <Date 2011-07-18.18:03:52.738>
    closer = 'python-dev'
    components = ['Interpreter Core']
    creation = <Date 2011-07-17.14:43:42.190>
    creator = 'Julian'
    dependencies = []
    files = ['22683']
    hgrepos = []
    issue_num = 12579
    keywords = ['patch']
    message_count = 13.0
    messages = ['140528', '140530', '140537', '140539', '140540', '140541', '140542', '140546', '140547', '140549', '140551', '140553', '140610']
    nosy_count = 3.0
    nosy_names = ['eric.smith', 'Julian', 'python-dev']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue12579'
    versions = ['Python 3.2', 'Python 3.3']

    @Julian
    Copy link
    Mannequin Author

    Julian mannequin commented Jul 17, 2011

    Attached is just a failing test case (just print("{}".format_map(12))), haven't been able to decipher the chain of calls in the unicodeobject.c code yet to see where the best place to put the fix would be (inside do_format_map before the pass back upwards maybe?).

    Assuming this should be a TypeError obviously, soon as I can figure out where to put the fix I'll update the patch.

    @Julian Julian mannequin added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Jul 17, 2011
    @ericvsmith ericvsmith self-assigned this Jul 17, 2011
    @ericvsmith
    Copy link
    Member

    If you want to look at this, I think there's a missing check for args being non-null in string_format.h, line 515.

    I'd have to think to see if there are other possible failure scenarios.

    Thanks for the report.

    @ericvsmith
    Copy link
    Member

    Actually that's probably not the place to catch it. Let me look at it closer. Of course, patches welcome!

    @Julian
    Copy link
    Mannequin Author

    Julian mannequin commented Jul 17, 2011

    Yeah, I saw the line you suggested and didn't think that was it.

    To expand on this, perhaps "{foo}".format(12) should raise a TypeError as well? I'd expect that more than the current behavior which is a KeyError. That KeyError is getting raised in get_field_object, which is where the fix needs to be, or at least needs to be changed, I'm just trying not to duplicate code.

    @Julian
    Copy link
    Mannequin Author

    Julian mannequin commented Jul 17, 2011

    Sorry for the double post, meant to say, in line 506.

    @ericvsmith
    Copy link
    Member

    I think KeyError for "{foo}".format(12) is correct. It's looking up "foo" in the empty dict of **kwargs.

    @Julian
    Copy link
    Mannequin Author

    Julian mannequin commented Jul 17, 2011

    Fair enough. I suppose I take .format_map to mean, here is a mapping, call __getitem__ on it for each of the keys inside the format string, to which calling 12["foo"] would get me a TypeError.

    I suppose I see both as appropriate, but the fact that it's implemented by passing the mapping as kwargs seemed less relevant. Less concerned about that part though I guess.

    @ericvsmith
    Copy link
    Member

    I think the issue is that it should be an error in any string used for format_map() to have a positional argument. So the right thing to do is detect this case in get_field_object (index != -1 and args is NULL). So I think a new test near line 515 really is the right thing to do. With a good comment explaining what's going on.

    I'm pretty sure the only case where args is NULL is when used with format_map(), but that needs to be verified.

    @ericvsmith
    Copy link
    Member

    Changing the title to reflect the real problem.

    You get a SystemError even when using a mapping, if you have a format string with positional arguments:
    >>> '{}'.format_map({'a':0})
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    SystemError: null argument to internal routine

    @ericvsmith ericvsmith changed the title str.format_map raises a SystemError for non-mapping str.format_map raises a SystemError for format strings with positional arguments Jul 17, 2011
    @Julian
    Copy link
    Mannequin Author

    Julian mannequin commented Jul 18, 2011

    Well you're right :). I appreciate you taking more time to help me with this than you could have yourself. I made the change (and changed the TypeError to a ValueError as per your discovery that this was just a positional value issue, hope you agree with that).

    Ran the whole test suite, all passes, other than the one test that fails for me without the patch too.

    I'm not too happy with the wording on the error, "Format string contained positional fields" doesn't sound nearly specific enough, but I was weary to put something specifically referencing str.format_map in a function that handles str.format as well?

    Anyways, appreciate the help again, please let me know if anything needs fixing :).

    @ericvsmith
    Copy link
    Member

    I definitely agree it should be a ValueError.

    How about including in your patch adding your name to Misc/ACKS, if it isn't already there? I don't have your full name.

    I might play with the exception wording and add a few more comments. Thanks for your work on this.

    @Julian
    Copy link
    Mannequin Author

    Julian mannequin commented Jul 18, 2011

    Added, updated the patch :). Thank you!

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Jul 18, 2011

    New changeset f6d074a7bbd4 by Eric V. Smith in branch '3.2':
    Closes bpo-12579. Positional fields with str.format_map() now raise a ValueError instead of SystemError.
    http://hg.python.org/cpython/rev/f6d074a7bbd4

    @python-dev python-dev mannequin closed this as completed Jul 18, 2011
    @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
    interpreter-core (Objects, Python, Grammar, and Parser dirs)
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant