Title: Weird string interpolation behaviour
Type: behavior Stage: resolved
Components: Versions: Python 3.2, Python 2.7, Python 2.6
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Mike.Graham, Thomas.Waldmann, alex, benjamin.peterson, eallik, python-dev, r.david.murray
Priority: normal Keywords:

Created on 2012-08-28 21:06 by eallik, last changed 2013-03-24 03:37 by benjamin.peterson. This issue is now closed.

File name Uploaded Description Edit Thomas.Waldmann, 2013-03-24 03:04
Messages (11)
msg169300 - (view) Author: Erik Allik (eallik) Date: 2012-08-28 21:06
>>> class Foo(object):
...     pass
>>> "asdads" % Foo()

Doesn't look like it's supposed to be this way.

As per the documentation:

"If format requires a single argument, values may be a single non-tuple object. [5] Otherwise, values must be a tuple with exactly the number of items specified by the format string, or a single mapping object (for example, a dictionary)."

Tested and confirmed on 2.5, 2.6, 2.7 (both old and new style classes) and 3.2.
msg169301 - (view) Author: Alex Gaynor (alex) * (Python committer) Date: 2012-08-28 21:07
Additional detail, if and only if Foo defines an __str__ method, this raises an exception.
msg169302 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-08-28 21:16
That doesn't appear to be true in 3.2.  I happened to test this on email.message.Message, and it also did not raise an error, but it defines an __str__.

I suspect this is some oddball result of the coercion rules.
msg169303 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-08-28 21:19
Didn't get an error in 2.7, either, with Message().
msg169304 - (view) Author: Mike Graham (Mike.Graham) Date: 2012-08-28 21:27
Line 13464 of unicodeobject.c is

    if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) &&

Too lazy to check, but don't user-created types have a tp_as_mapping? If so, I think it mistakes Foo() for a dict.
msg169309 - (view) Author: Roundup Robot (python-dev) Date: 2012-08-28 22:02
New changeset 2801bf875a24 by Benjamin Peterson in branch '2.7':
use the stricter PyMapping_Check (closes #15801)

New changeset 4d431e719646 by Benjamin Peterson in branch '3.2':
use the stricter PyMapping_Check (closes #15801)

New changeset 263d09ce3e9e by Benjamin Peterson in branch 'default':
merge 3.2 (#15801)
msg169320 - (view) Author: Erik Allik (eallik) Date: 2012-08-28 23:29
Respectable reaction time.
msg171664 - (view) Author: Roundup Robot (python-dev) Date: 2012-09-30 19:49
New changeset 8f07ab82de92 by Ezio Melotti in branch '2.7':
#15923: fix a mistake in that resulted in a TypeError after 2801bf875a24 (see #15801).

New changeset cb988d601803 by Ezio Melotti in branch '3.2':
#15923: fix a mistake in that resulted in a TypeError after 2801bf875a24 (see #15801).
msg185110 - (view) Author: Thomas Waldmann (Thomas.Waldmann) Date: 2013-03-24 03:04
gave 2.7.4rc1 a try and was seeing a failing unit test that does not fail with 2.7.3.

see the attached file for some minimal code that succeeds on 2.7.3, but not on 2.7.4rc1.

it seems to have to do with being a subclass of Exception, it doesn't happen for subclass of object.
msg185111 - (view) Author: Roundup Robot (python-dev) Date: 2013-03-24 03:37
New changeset 391e3a7db1a3 by Benjamin Peterson in branch '2.7':
allow any type with __getitem__ to be a mapping for the purposes of % (#15801)
msg185112 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2013-03-24 03:37
Thanks for the report. Will be fixed in 2.7.4.
Date User Action Args
2013-03-24 03:37:40benjamin.petersonsetnosy: + benjamin.peterson
messages: + msg185112
2013-03-24 03:37:21python-devsetmessages: + msg185111
2013-03-24 03:04:26Thomas.Waldmannsetfiles: +
nosy: + Thomas.Waldmann
messages: + msg185110

2012-09-30 19:49:05python-devsetmessages: + msg171664
2012-08-28 23:29:25ealliksetmessages: + msg169320
2012-08-28 22:02:25python-devsetstatus: open -> closed

nosy: + python-dev
messages: + msg169309

resolution: fixed
stage: resolved
2012-08-28 21:27:59Mike.Grahamsetnosy: + Mike.Graham
messages: + msg169304
2012-08-28 21:19:38r.david.murraysetmessages: + msg169303
2012-08-28 21:16:51r.david.murraysetnosy: + r.david.murray
messages: + msg169302
2012-08-28 21:07:57alexsetnosy: + alex
messages: + msg169301
2012-08-28 21:06:21eallikcreate