Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().

This commit is contained in:
Guido van Rossum 1997-01-03 17:14:46 +00:00
parent 461a1c172f
commit 53756b1097
2 changed files with 58 additions and 0 deletions

View File

@ -44,8 +44,10 @@ extern DL_IMPORT(PyTypeObject) PyLong_Type;
#define PyLong_Check(op) ((op)->ob_type == &PyLong_Type) #define PyLong_Check(op) ((op)->ob_type == &PyLong_Type)
extern PyObject *PyLong_FromLong Py_PROTO((long)); extern PyObject *PyLong_FromLong Py_PROTO((long));
extern PyObject *PyLong_FromUnsignedLong Py_PROTO((unsigned long));
extern PyObject *PyLong_FromDouble Py_PROTO((double)); extern PyObject *PyLong_FromDouble Py_PROTO((double));
extern long PyLong_AsLong Py_PROTO((PyObject *)); extern long PyLong_AsLong Py_PROTO((PyObject *));
extern unsigned long PyLong_AsUnsignedLong Py_PROTO((PyObject *));
extern double PyLong_AsDouble Py_PROTO((PyObject *)); extern double PyLong_AsDouble Py_PROTO((PyObject *));
PyObject *PyLong_FromString Py_PROTO((char *, char **, int)); PyObject *PyLong_FromString Py_PROTO((char *, char **, int));

View File

@ -109,6 +109,27 @@ newlongobject(ival)
return (object *)v; return (object *)v;
} }
/* Create a new long int object from a C unsigned long int */
object *
PyLong_FromUnsignedLong(ival)
unsigned long ival;
{
/* Assume a C long fits in at most 5 'digits' */
/* Works on both 32- and 64-bit machines */
longobject *v = alloclongobject(5);
if (v != NULL) {
unsigned long t = ival;
int i;
for (i = 0; i < 5; i++) {
v->ob_digit[i] = t & MASK;
t >>= SHIFT;
}
v = long_normalize(v);
}
return (object *)v;
}
/* Create a new long int object from a C double */ /* Create a new long int object from a C double */
object * object *
@ -181,6 +202,41 @@ getlongvalue(vv)
return x * sign; return x * sign;
} }
/* Get a C long int from a long int object.
Returns -1 and sets an error condition if overflow occurs. */
unsigned long
PyLong_AsUnsignedLong(vv)
object *vv;
{
register longobject *v;
unsigned long x, prev;
int i;
if (vv == NULL || !is_longobject(vv)) {
err_badcall();
return (unsigned long) -1;
}
v = (longobject *)vv;
i = v->ob_size;
x = 0;
if (i < 0) {
err_setstr(OverflowError,
"can't convert negative value to unsigned long");
return (unsigned long) -1;
}
while (--i >= 0) {
prev = x;
x = (x << SHIFT) + v->ob_digit[i];
if ((x >> SHIFT) != prev) {
err_setstr(OverflowError,
"long int too long to convert");
return (unsigned long) -1;
}
}
return x;
}
/* Get a C double from a long int object. No overflow check. */ /* Get a C double from a long int object. No overflow check. */
double double