summaryrefslogtreecommitdiffstats
path: root/Objects/intobject.c
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1992-09-12 11:09:23 (GMT)
committerGuido van Rossum <guido@python.org>1992-09-12 11:09:23 (GMT)
commit1899c2e0550fa025080e35bb3ec25aeff0118dc7 (patch)
tree46cf91828dd59c33ef396e1cdb93ce4ada5c8d9e /Objects/intobject.c
parent5c85062e1ce4c7e51daaad1a4eb3f66f6b5a0ea8 (diff)
downloadcpython-1899c2e0550fa025080e35bb3ec25aeff0118dc7.zip
cpython-1899c2e0550fa025080e35bb3ec25aeff0118dc7.tar.gz
cpython-1899c2e0550fa025080e35bb3ec25aeff0118dc7.tar.bz2
Made builtins int(), long(), float(), oct() and hex() more generic.
Diffstat (limited to 'Objects/intobject.c')
-rw-r--r--Objects/intobject.c59
1 files changed, 59 insertions, 0 deletions
diff --git a/Objects/intobject.c b/Objects/intobject.c
index 8ecffc2..e10dab2 100644
--- a/Objects/intobject.c
+++ b/Objects/intobject.c
@@ -421,6 +421,59 @@ int_or(v, w)
return newintobject(a | b);
}
+static object *
+int_int(v)
+ object *v;
+{
+ INCREF(v);
+ return v;
+}
+
+static object *
+int_long(v)
+ object *v;
+{
+ long x = getintvalue(v);
+ return newlongobject(x);
+}
+
+static object *
+int_float(v)
+ object *v;
+{
+ long x = getintvalue(v);
+ return newfloatobject((double)x);
+}
+
+static object *
+int_oct(v)
+ object *v;
+{
+ char buf[20];
+ long x = getintvalue(v);
+ if (x == 0)
+ strcpy(buf, "0");
+ else if (x > 0)
+ sprintf(buf, "0%lo", x);
+ else
+ sprintf(buf, "-0%lo", -x);
+ return newstringobject(buf);
+}
+
+static object *
+int_hex(v)
+ object *v;
+{
+ char buf[20];
+ long x = getintvalue(v);
+ if (x >= 0)
+ sprintf(buf, "0x%lx", x);
+ else
+ sprintf(buf, "-0x%lx", -x);
+ return newstringobject(buf);
+}
+
+
static number_methods int_as_number = {
int_add, /*nb_add*/
int_sub, /*nb_subtract*/
@@ -439,6 +492,12 @@ static number_methods int_as_number = {
int_and, /*nb_and*/
int_xor, /*nb_xor*/
int_or, /*nb_or*/
+ 0, /*nb_coerce*/
+ int_int, /*nb_int*/
+ int_long, /*nb_long*/
+ int_float, /*nb_float*/
+ int_oct, /*nb_oct*/
+ int_hex, /*nb_hex*/
};
typeobject Inttype = {