[Python-bugs-list] [ python-Bugs-466125 ] PyLong_AsLongLong works for any integer

noreply@sourceforge.net noreply@sourceforge.net
Fri, 28 Sep 2001 09:45:15 -0700


Bugs item #466125, was opened at 2001-09-28 09:45
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=466125&group_id=5470

Category: Python Interpreter Core
Group: Feature Request
Status: Open
Resolution: None
Priority: 5
Submitted By: Tom Epperly (tepperly)
Assigned to: Nobody/Anonymous (nobody)
Summary: PyLong_AsLongLong works for any integer

Initial Comment:
It would be nice if PyLong_AsLongLong worked more like
PyInt_AsLong. When the value can be coerced into an
integer, it should be.

The current behavior is that PyLong_AsLongLong only
works (without raising an exception) on instances of a
PyLongObject.  I would be far better for my
application, http://www.llnl.gov/casc/components/, if
PyLong_AsLongLong called PyInt_AsLong in cases where
the argument is not a PyLongObject. The return value of
PyInt_AsLong can safely be promoted to a long long.

The end result that I would like is that an 'L' in a
PyArg_ParseTuple call return any integer value (int or
long) as a C long long. PyArg_ParseTuple ultimately
uses PyLong_AsLongLong to handle the L format code.

It would be nice to have this behavior in past versions
too (i.e. 1.5.2, 2.0 and 2.1).

Here is an example of how one might do this.  This
patch also changes the behavior of PyLong_AsLong to
fall back on PyInt_AsLong.

$ diff -c longobject.c~ longobject.c
*** longobject.c~	Wed Jan 17 07:33:18 2001
--- longobject.c	Fri Sep 28 09:42:45 2001
***************
*** 144,153 ****
  	unsigned long x, prev;
  	int i, sign;
  
! 	if (vv == NULL || !PyLong_Check(vv)) {
  		PyErr_BadInternalCall();
  		return -1;
  	}
  	v = (PyLongObject *)vv;
  	i = v->ob_size;
  	sign = 1;
--- 144,156 ----
  	unsigned long x, prev;
  	int i, sign;
  
! 	if (vv == NULL) {
  		PyErr_BadInternalCall();
  		return -1;
  	}
+ 	if (!PyLong_Check(vv)) {
+ 		return PyInt_AsLong(vv);
+         }
  	v = (PyLongObject *)vv;
  	i = v->ob_size;
  	sign = 1;
***************
*** 387,395 ****
  	LONG_LONG x, prev;
  	int i, sign;
  	
! 	if (vv == NULL || !PyLong_Check(vv)) {
  		PyErr_BadInternalCall();
  		return -1;
  	}
  
  	v = (PyLongObject *)vv;
--- 390,401 ----
  	LONG_LONG x, prev;
  	int i, sign;
  	
! 	if (vv == NULL) {
  		PyErr_BadInternalCall();
  		return -1;
+ 	}
+ 	if (!PyLong_Check(vv)) {
+ 		return PyInt_AsLong(vv);
  	}
  
  	v = (PyLongObject *)vv;


----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=466125&group_id=5470