[Tutor] Limitation of int() in converting strings
eryksun
eryksun at gmail.com
Mon Dec 17 16:39:51 CET 2012
On Mon, Dec 17, 2012 at 3:55 AM, Alan Gauld <alan.gauld at btinternet.com> wrote:
>
> So you are right, there is an inconsistency between how int() converts
> floating point numbers and how it converts strings. Even stranger since the
> underlying atoi() C function appears to handle float strings quite
> happily...
If you're using strtol in C it's up to you how to interpret an
incomplete conversion due to an out-of-range number or bad literal for
the given base. Python, on the other hand, automatically switches the
type for big integers to a multiprecision long (2.x long) and raises
ValueError for bad literals. Where you go from there is up to you.
BTW, 2.x int() isn't using the libc atoi or strtol/strtoul functions.
It has its own implementation in mystrtoul.c. PyOS_strtol wraps the
main workhorse PyOS_strtoul (unsigned):
http://hg.python.org/cpython/file/70274d53c1dd/Python/mystrtoul.c#l80
The conversion loop proceeds up to the first non-base character, as
defined by the table _PyLong_DigitValue in longobject.c:
http://hg.python.org/cpython/file/70274d53c1dd/Objects/longobject.c#l1604
A pointer to the last scanned character is set. In PyInt_FromString,
if this points to the first character, or the previous character isn't
alphanumeric, or removing trailing whitespace starting from this point
doesn't end on a NUL terminator (e.g. *end == '.'), then the
conversion raises a ValueError. See lines 364-383 in intobject.c:
http://hg.python.org/cpython/file/70274d53c1dd/Objects/intobject.c#l340
More information about the Tutor
mailing list