[Python-checkins] python/dist/src/Objects longobject.c,1.157,1.158 intobject.c,2.103,2.104

theller@users.sourceforge.net theller@users.sourceforge.net
Thu, 17 Apr 2003 11:55:42 -0700


Update of /cvsroot/python/python/dist/src/Objects
In directory sc8-pr-cvs1:/tmp/cvs-serv7022

Modified Files:
	longobject.c intobject.c 
Log Message:
SF # 595026: support for masks in getargs.c.

New functions:
  unsigned long PyInt_AsUnsignedLongMask(PyObject *);
  unsigned PY_LONG_LONG) PyInt_AsUnsignedLongLongMask(PyObject *);
  unsigned long PyLong_AsUnsignedLongMask(PyObject *);
  unsigned PY_LONG_LONG) PyLong_AsUnsignedLongLongMask(PyObject *);

New and changed format codes:

b unsigned char 0..UCHAR_MAX
B unsigned char none **
h unsigned short 0..USHRT_MAX
H unsigned short none **
i int INT_MIN..INT_MAX
I * unsigned int 0..UINT_MAX
l long LONG_MIN..LONG_MAX
k * unsigned long none
L long long LLONG_MIN..LLONG_MAX
K * unsigned long long none

Notes:

* New format codes.

** Changed from previous "range-and-a-half" to "none"; the
range-and-a-half checking wasn't particularly useful.

New test test_getargs2.py, to verify all this.


Index: longobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/longobject.c,v
retrieving revision 1.157
retrieving revision 1.158
diff -C2 -d -r1.157 -r1.158
*** longobject.c	29 Mar 2003 10:04:55 -0000	1.157
--- longobject.c	17 Apr 2003 18:55:36 -0000	1.158
***************
*** 261,264 ****
--- 261,292 ----
  }
  
+ /* Get a C unsigned long int from a long int object, ignoring the high bits.
+    Returns -1 and sets an error condition if an error occurs. */
+ 
+ unsigned long
+ PyLong_AsUnsignedLongMask(PyObject *vv)
+ {
+ 	register PyLongObject *v;
+ 	unsigned long x;
+ 	int i, sign;
+ 
+ 	if (vv == NULL || !PyLong_Check(vv)) {
+ 		PyErr_BadInternalCall();
+ 		return (unsigned long) -1;
+ 	}
+ 	v = (PyLongObject *)vv;
+ 	i = v->ob_size;
+ 	sign = 1;
+ 	x = 0;
+ 	if (i < 0) {
+ 		sign = -1;
+ 		i = -i;
+ 	}
+ 	while (--i >= 0) {
+ 		x = (x << SHIFT) + v->ob_digit[i];
+ 	}
+ 	return x * sign;
+ }
+ 
  int
  _PyLong_Sign(PyObject *vv)
***************
*** 780,783 ****
--- 808,838 ----
  }
  
+ /* Get a C unsigned long int from a long int object, ignoring the high bits.
+    Returns -1 and sets an error condition if an error occurs. */
+ 
+ unsigned PY_LONG_LONG
+ PyLong_AsUnsignedLongLongMask(PyObject *vv)
+ {
+ 	register PyLongObject *v;
+ 	unsigned PY_LONG_LONG x;
+ 	int i, sign;
+ 
+ 	if (vv == NULL || !PyLong_Check(vv)) {
+ 		PyErr_BadInternalCall();
+ 		return (unsigned long) -1;
+ 	}
+ 	v = (PyLongObject *)vv;
+ 	i = v->ob_size;
+ 	sign = 1;
+ 	x = 0;
+ 	if (i < 0) {
+ 		sign = -1;
+ 		i = -i;
+ 	}
+ 	while (--i >= 0) {
+ 		x = (x << SHIFT) + v->ob_digit[i];
+ 	}
+ 	return x * sign;
+ }
  #undef IS_LITTLE_ENDIAN
  

Index: intobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/intobject.c,v
retrieving revision 2.103
retrieving revision 2.104
diff -C2 -d -r2.103 -r2.104
*** intobject.c	20 Feb 2003 20:32:11 -0000	2.103
--- intobject.c	17 Apr 2003 18:55:36 -0000	2.104
***************
*** 170,173 ****
--- 170,218 ----
  		else
  		{
+ 			Py_DECREF(io);
+ 			PyErr_SetString(PyExc_TypeError,
+ 					"nb_int should return int object");
+ 			return -1;
+ 		}
+ 	}
+ 
+ 	val = PyInt_AS_LONG(io);
+ 	Py_DECREF(io);
+ 
+ 	return val;
+ }
+ 
+ unsigned long
+ PyInt_AsUnsignedLongMask(register PyObject *op)
+ {
+ 	PyNumberMethods *nb;
+ 	PyIntObject *io;
+ 	unsigned long val;
+ 
+ 	if (op && PyInt_Check(op))
+ 		return PyInt_AS_LONG((PyIntObject*) op);
+ 	if (op && PyLong_Check(op))
+ 		return PyLong_AsUnsignedLongMask(op);
+ 
+ 	if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
+ 	    nb->nb_int == NULL) {
+ 		PyErr_SetString(PyExc_TypeError, "an integer is required");
+ 		return -1;
+ 	}
+ 
+ 	io = (PyIntObject*) (*nb->nb_int) (op);
+ 	if (io == NULL)
+ 		return -1;
+ 	if (!PyInt_Check(io)) {
+ 		if (PyLong_Check(io)) {
+ 			val = PyLong_AsUnsignedLongMask((PyObject *)io);
+ 			Py_DECREF(io);
+ 			if (PyErr_Occurred())
+ 				return -1;
+ 			return val;
+ 		}
+ 		else
+ 		{
+ 			Py_DECREF(io);
  			PyErr_SetString(PyExc_TypeError,
  					"nb_int should return int object");
***************
*** 181,184 ****
--- 226,275 ----
  	return val;
  }
+ 
+ #ifdef HAVE_LONG_LONG
+ unsigned PY_LONG_LONG
+ PyInt_AsUnsignedLongLongMask(register PyObject *op)
+ {
+ 	PyNumberMethods *nb;
+ 	PyIntObject *io;
+ 	unsigned PY_LONG_LONG val;
+ 
+ 	if (op && PyInt_Check(op))
+ 		return PyInt_AS_LONG((PyIntObject*) op);
+ 	if (op && PyLong_Check(op))
+ 		return PyLong_AsUnsignedLongLongMask(op);
+ 
+ 	if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
+ 	    nb->nb_int == NULL) {
+ 		PyErr_SetString(PyExc_TypeError, "an integer is required");
+ 		return -1;
+ 	}
+ 
+ 	io = (PyIntObject*) (*nb->nb_int) (op);
+ 	if (io == NULL)
+ 		return -1;
+ 	if (!PyInt_Check(io)) {
+ 		if (PyLong_Check(io)) {
+ 			val = PyLong_AsUnsignedLongLongMask((PyObject *)io);
+ 			Py_DECREF(io);
+ 			if (PyErr_Occurred())
+ 				return -1;
+ 			return val;
+ 		}
+ 		else
+ 		{
+ 			Py_DECREF(io);
+ 			PyErr_SetString(PyExc_TypeError,
+ 					"nb_int should return int object");
+ 			return -1;
+ 		}
+ 	}
+ 
+ 	val = PyInt_AS_LONG(io);
+ 	Py_DECREF(io);
+ 
+ 	return val;
+ }
+ #endif
  
  PyObject *