This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: String formatting bug in interactive mode
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: amaury.forgeotdarc, arigo, benjamin.peterson, eric.smith, ezio.melotti, flox, jayanth, mark.dickinson, python-dev, ronaldoussoren, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2011-11-15 22:04 by jayanth, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
issue13410.patch amaury.forgeotdarc, 2011-11-15 23:35 review
issue13410_2.patch amaury.forgeotdarc, 2011-11-15 23:46 review
issue13410_3.patch amaury.forgeotdarc, 2011-11-15 23:50 review
Messages (14)
msg147711 - (view) Author: Jayanth Raman (jayanth) Date: 2011-11-15 22:04
With file xx.py:

class Foo(object):
    def __init__(self, x):
        self.x = x
    def __long__(self):
        return long(self.x)
    def __float__(self):
        return float(self.x)
y = Foo(22)
print '%d' % y
print '%d' % y


Interactive mode:

$ python -V
Python 2.6.7
$ python -i xx.py
22
22
>>>
TypeError: int() argument must be a string or a number, not 'Foo'
>>> '%d' % y
'22'
>>> '%d' % y
TypeError: int() argument must be a string or a number, not 'Foo'
>>> '%d' % y
'22'
>>> '%d' % y
TypeError: int() argument must be a string or a number, not 'Foo'

It alternates between printing '22' and print a TypeError message.  Expected it to be consistent.

Also, the first carraige-return (with no input) results in an error message.  Did not expect an error message.  In fact, typing pretty much anything (e.g. an undefined variable or raise(Exception())) results in the same error message.

On Mac OS X Darwin Kernel Version 10.8.0
Python 2.6.7 (r267:88850, Jul  6 2011, 13:57:37) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin

Thanks.
msg147716 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2011-11-15 22:42
I think this must be a change between 2.5 and 2.6.

In 2.5.1 non-debug (Linux) I consistently see:
>>> print '%d' % y
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: int argument required

In 2.6.5 (Windows via activestate) I see the alternating errors reported here.

In 2.7.2+ debug (Linux) I consistently see:
>>> print '%d' % y
22
XXX undetected error
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: int() argument must be a string or a number, not 'Foo'
[38749 refs]

Note the "XXX undetected error".

In 2.7.2+ non-debug (Linux), I get the alternating errors reported here.

I don't think anything will be done about the problem in 2.6. For 2.7, the "XXX undetected error" should be investigated, and will likely point to the problem.
msg147722 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2011-11-15 22:55
With Darwin 10.8.0 x86_64, I confirm the behavior described in first message.


$ python2.7 -V
Python 2.7.2
$ python2.7 -i xx.py
22
22
>>> 
TypeError: int() argument must be a string or a number, not 'Foo'
>>> '%d' % y
'22'
>>> '%d' % y
TypeError: int() argument must be a string or a number, not 'Foo'
>>> '%d' % y
'22'
>>> '%d' % y
TypeError: int() argument must be a string or a number, not 'Foo'
>>>
msg147732 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2011-11-15 23:35
A debug build displays "XXX undetected error".  An error condition was not correctly cleared, see attached patch.
msg147735 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2011-11-15 23:46
New patch with a unit test.
msg147737 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2011-11-15 23:50
Sorry I found that u'%d' is equally affected, here is a new version.
msg147738 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2011-11-15 23:56
I don't think you're going to want those print statements in a test. You could just evaluate '%d' % y.
msg147741 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2011-11-16 00:04
Unfortunately without the "print" the test does not fail.
msg147742 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2011-11-16 00:10
With an unpatched 2.7, this fails for me:

diff --git a/Lib/test/test_format.py b/Lib/test/test_format.py
--- a/Lib/test/test_format.py
+++ b/Lib/test/test_format.py
@@ -289,6 +289,17 @@
             else:
                 raise TestFailed, '"%*d"%(maxsize, -127) should fail'
 
+    def test_issue13410(self):
+        class Foo(object):
+            def __init__(self, x):
+                self.x = x
+            def __long__(self):
+                return long(self.x)
+            def __float__(self):
+                return float(self.x)
+        '%d' % Foo(22)
+
 def test_main():
     test_support.run_unittest(FormatTest)
 

$ ./python Lib/test/regrtest.py test_format
test_format
test test_format crashed -- <type 'exceptions.TypeError'>: int() argument must be a string or a number, not 'Foo'
1 test failed:
    test_format
msg147750 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2011-11-16 07:16
$ ./python Lib/test/regrtest.py test_format
shows the error, but
$ ./python Lib/test/regrtest.py -v test_format
does not fail!
The "print" is needed, can something else have the same effect?
msg147759 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2011-11-16 09:47
Interesting! Same here.

Using eval() fails with or without -v:

--- a/Lib/test/test_format.py
+++ b/Lib/test/test_format.py
@@ -289,6 +289,18 @@
             else:
                 raise TestFailed, '"%*d"%(maxsize, -127) should fail'
 
+    def test_issue13410(self):
+        class Foo(object):
+            def __init__(self, x):
+                self.x = x
+            def __long__(self):
+                return long(self.x)
+            def __float__(self):
+                return float(self.x)
+        eval(u'%d' % Foo(22))
+        eval('%d' % Foo(22))
+
+
 def test_main():
     test_support.run_unittest(FormatTest)
 
I've put both '%d' and u'%d' here, but it also fails with just one of them.
msg147977 - (view) Author: Armin Rigo (arigo) * (Python committer) Date: 2011-11-20 09:13
Fwiw, a class with methods __long__ and __float__ but no method __int__ behaves strangely in many other places; the canonical example is that calling "int(Foo(42))" will not work.  In light of this, does it make sense for "'%d' % Foo(42)" to work?  Shouldn't the fix instead be to cleanly raise the TypeError instead?
msg263082 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-04-09 11:56
The issue for str was fixed in issue15516. The issue for unicode is not fixed yet.
msg263136 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-04-10 12:27
New changeset a06654ca0134 by Serhiy Storchaka in branch '2.7':
Issue #13410: Fixed a bug in PyUnicode_Format where it failed to properly
https://hg.python.org/cpython/rev/a06654ca0134
History
Date User Action Args
2022-04-11 14:57:23adminsetgithub: 57619
2016-04-10 12:28:36serhiy.storchakasetstatus: open -> closed
nosy: + benjamin.peterson

resolution: fixed
stage: patch review -> resolved
2016-04-10 12:27:36python-devsetnosy: + python-dev
messages: + msg263136
2016-04-09 11:56:05serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg263082
2011-11-20 09:13:37arigosetnosy: + arigo
messages: + msg147977
2011-11-16 09:47:20eric.smithsetmessages: + msg147759
2011-11-16 07:17:30ezio.melottisetnosy: + ezio.melotti
stage: needs patch -> patch review

versions: - Python 2.6
2011-11-16 07:16:03amaury.forgeotdarcsetmessages: + msg147750
2011-11-16 00:10:59eric.smithsetmessages: + msg147742
2011-11-16 00:04:17amaury.forgeotdarcsetmessages: + msg147741
2011-11-15 23:56:59eric.smithsetmessages: + msg147738
2011-11-15 23:50:16amaury.forgeotdarcsetfiles: + issue13410_3.patch

messages: + msg147737
2011-11-15 23:46:49amaury.forgeotdarcsetfiles: + issue13410_2.patch

messages: + msg147735
2011-11-15 23:35:24amaury.forgeotdarcsetfiles: + issue13410.patch

nosy: + amaury.forgeotdarc
messages: + msg147732

keywords: + patch
2011-11-15 22:55:07floxsetmessages: + msg147722
2011-11-15 22:51:10floxsetnosy: + mark.dickinson, flox

stage: needs patch
2011-11-15 22:42:17eric.smithsetassignee: ronaldoussoren ->
messages: + msg147716
components: + Interpreter Core, - macOS
versions: + Python 2.7
2011-11-15 22:20:32eric.smithsetnosy: + eric.smith
2011-11-15 22:04:50jayanthcreate