[issue1716] String format operator '%i' fails for large floats

Christopher Tur Lesniewski-Laas report at bugs.python.org
Tue Jan 1 03:38:29 CET 2008

New submission from Christopher Tur Lesniewski-Laas:

To reproduce:
>>> '%i' % 12345678901.0
TypeError: int argument required

Contrast with:
>>> '%i' % 1234567890.0

Previous experience led me to expect that the '%i' format code would
work for all numerical types.  Admittedly, there's nothing in the docs
promising this, but it works *almost* all the time.

In fact, the operator fails to convert floats which are too big to fit
into a machine long.  The code for the '%i' operator handles objects of
type long specially, and then uses PyInt_AsLong to handle all other
objects (in formatint).

The ideal solution would be to ask the object to convert itself into an
int *before* the special test for type long; this would give the
expected behavior.

An acceptable solution would be to make the behavior consistent by
refusing all floats passed as the argument to '%i', but I expect this
would break a lot of code.  Of course, right now most of that code is
broken anyway, since it will throw a TypeError when the input float
happens to be large.

components: Interpreter Core
messages: 59068
nosy: ctl
severity: normal
status: open
title: String format operator '%i' fails for large floats
versions: Python 2.5, Python 2.6, Python 3.0

Tracker <report at bugs.python.org>

More information about the Python-bugs-list mailing list