Bugs item #1467929, was opened at 2006-04-10 15:39 Message generated for change (Comment added) made by akuchling You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1467929&group_id=5470
Please note that this message will contain a full copy of the comment thread, including the initial issue submission, for this request, not just the latest update. Category: Python Interpreter Core Group: Python 2.5 Status: Open Resolution: None Priority: 8 Private: No Submitted By: M.-A. Lemburg (lemburg) Assigned to: Anthony Baxter (anthonybaxter) Summary: %-formatting and dicts Initial Comment: This looks like a bug in the way the %-formatting code works or is it a feature ? >>> '%s %(a)s' % {'a': 'xyz'} "{'a': 'xyz'} xyz" >>> u'%s %(a)s' % {'a': 'xyz'} u"{'a': 'xyz'} xyz" Note that both strings and Unicode are affected. Python 2.3 and 2.4 also show this behavior. I would have expected an exception or the %-formatter simply ignoring the first %s. ---------------------------------------------------------------------- >Comment By: A.M. Kuchling (akuchling) Date: 2007-01-12 09:57 Message: Logged In: YES user_id=11375 Originator: NO The patch shouldn't be applied as it stands, though, because it's not complete; similiar changes need to be made to the Unicode type, for a start. To answer Skip's question: I don't remember the logic of the format code. I think the FORMAT_TYPE_UNKNOWN check may be unnecessary; the code could just always do format_type = _TUPLE, occasionally doing a redundant assignment (but who cares)? I don't think I'll have any chance to work on this; PyCon is keeping me busy, and the mailbox bugs will take priority for me. ---------------------------------------------------------------------- Comment By: Anthony Baxter (anthonybaxter) Date: 2007-01-12 04:10 Message: Logged In: YES user_id=29957 Originator: NO I'm happy for this to be applied for 2.5.1. I don't have time to do it myself for a few days, though, so feel free to beat me to it. ---------------------------------------------------------------------- Comment By: Georg Brandl (gbrandl) Date: 2006-08-19 01:25 Message: Logged In: YES user_id=849994 I'd say before 2.5 final... ---------------------------------------------------------------------- Comment By: M.-A. Lemburg (lemburg) Date: 2006-08-18 18:17 Message: Logged In: YES user_id=38388 Should this patch be applied to the 2.5 branch ? And if so, before or after the release of 2.5 ? ---------------------------------------------------------------------- Comment By: Jim Jewett (jimjjewett) Date: 2006-08-18 18:01 Message: Logged In: YES user_id=764593 Just a bit of encouragement for checking consistency like this; the explicit error message would have helped with a mistake I made earlier today. For one of several keys, I mistyped it as "(%key)s", and a message about "not enough values" just didn't make sense. ---------------------------------------------------------------------- Comment By: M.-A. Lemburg (lemburg) Date: 2006-08-04 09:26 Message: Logged In: YES user_id=38388 The patch looks OK. I'd make it a TypeError and use "cannot use positional and named formatting parameters at the same time" as message. Thanks. ---------------------------------------------------------------------- Comment By: Skip Montanaro (montanaro) Date: 2006-08-04 09:21 Message: Logged In: YES user_id=44345 Looks okay to me, though why is the FORMAT_TYPE_UNKNOWN test necessary in the second case but not the first? ---------------------------------------------------------------------- Comment By: A.M. Kuchling (akuchling) Date: 2006-07-25 08:35 Message: Logged In: YES user_id=11375 So, what should '%s' % {} do? Should it be 1) '{}' or 2) an error because the argument is a mapping but the format specifier doesn't have a '(key)'? I've attached a draft patch that fixes stringobject.c; if the approach is deemed OK, I'll apply it to unicodeobject.c, too. PyString_Format() records the type of argument being processed (a tuple or a mapping) and raises ValueError if you mix them, at the cost of two extra comparisons for each format specifier processed. This preserves the current behaviour of '%s' % dictionary. Questions: 1) is the approach reasonably clear? 2) are the additional two comparisons unacceptably slow? 3) Is ValueError the right exception? 4) can someone come up with a better exception message than "both keyed and unkeyed format specifiers used"? ---------------------------------------------------------------------- Comment By: Martin v. Löwis (loewis) Date: 2006-07-24 16:07 Message: Logged In: YES user_id=21627 IMO, it's correct to break backwards compatibility, as the current behaviour clearly violates the spec; I'm not sure whether it's good to break the behaviour *now* (i.e. with no further betas before the release of 2.5). Deferring to the release manager. ---------------------------------------------------------------------- Comment By: Georg Brandl (gbrandl) Date: 2006-07-24 09:37 Message: Logged In: YES user_id=849994 The library ref specifies that if a dict is supplied, the format specifiers MUST include a mapping key, so the right thing to do would be to raise an exception. Is it worth breaking backwards compatibility, Martin? ---------------------------------------------------------------------- Comment By: Hasan Diwan (hdiwan650) Date: 2006-04-14 04:33 Message: Logged In: YES user_id=1185570 It looks as though it's a feature... The first %s will print the whole dictionary as a string, the second, only that value looked up by the key. ---------------------------------------------------------------------- You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1467929&group_id=5470 _______________________________________________ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com