[issue3777] PyNumber_Long fails from Float

Barry Alan Scott report at bugs.python.org
Sat Sep 6 20:36:05 CEST 2008


Barry Alan Scott <barry-scott at users.sourceforge.net> added the comment:

O.k. I know what is going on.

Here is the description from abstracts.h for PyNumber_Long:

     PyAPI_FUNC(PyObject *) PyNumber_Long(PyObject *o);

       /*
	 Returns the o converted to a long integer object on success,
	 or NULL on failure.  This is the equivalent of the Python
	 expression: long(o).

       */

Its says that I can expect a long integer. However PyNumber_long
can return an int or a long.

PyCXX checks for a long, but an int is not a long and I raise
a type error.

This is a contract break on the Python API.

The change that causes this break is in floatobject.c
>From 2.5.2 code:

static PyNumberMethods float_as_number = {
...
	float_int, 	/*nb_int*/
	float_long, 	/*nb_long*/

>From 2.6b3 code:

static PyNumberMethods float_as_number = {
...
	float_trunc, 	/*nb_int*/
	float_trunc, 	/*nb_long*/

float_trunc returns either an int or a long.
Which is not what is required for the API.

Here is the same bug at the pure python level.

 $ python2.6
Python 2.6b3 (r26b3:65922, Aug 25 2008, 15:44:46) 
[GCC 4.0.1 (Apple Computer, Inc. build 5370)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> long(4)
4L
>>> long(4.3)
4
>>> long("6")
6L
>>> type(long(4.3))
<type 'int'>
>>> 

Barry

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue3777>
_______________________________________


More information about the Python-bugs-list mailing list