summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1997-01-03 17:14:46 (GMT)
committerGuido van Rossum <guido@python.org>1997-01-03 17:14:46 (GMT)
commit53756b1097805e6a7910a1788d6eb7cb190864ba (patch)
treeb641682e2e8347bd7437f5d1ae47ac1e93813990 /Objects
parent461a1c172f6e7f053da03ee39a9b762cb173ce68 (diff)
downloadcpython-53756b1097805e6a7910a1788d6eb7cb190864ba.zip
cpython-53756b1097805e6a7910a1788d6eb7cb190864ba.tar.gz
cpython-53756b1097805e6a7910a1788d6eb7cb190864ba.tar.bz2
Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Diffstat (limited to 'Objects')
-rw-r--r--Objects/longobject.c56
1 files changed, 56 insertions, 0 deletions
diff --git a/Objects/longobject.c b/Objects/longobject.c
index f98e517..36f2a26 100644
--- a/Objects/longobject.c
+++ b/Objects/longobject.c
@@ -109,6 +109,27 @@ newlongobject(ival)
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 */
object *
@@ -181,6 +202,41 @@ getlongvalue(vv)
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. */
double