summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2001-09-19 01:25:16 (GMT)
committerGuido van Rossum <guido@python.org>2001-09-19 01:25:16 (GMT)
commit1952e388ca63b8eaa090bef966dd538f78ece208 (patch)
tree31afd318ac859dfbe337d2cf6e11fc6c10bcb45f
parentd5d8e4a43632d9d4ec9f6fb23f7405c4f0678617 (diff)
downloadcpython-1952e388ca63b8eaa090bef966dd538f78ece208.zip
cpython-1952e388ca63b8eaa090bef966dd538f78ece208.tar.gz
cpython-1952e388ca63b8eaa090bef966dd538f78ece208.tar.bz2
Add additional coercion support for "self subtypes" to int, long,
float (compare the recent checkin to complex). Added tests for these.
-rw-r--r--Lib/test/test_descr.py28
-rw-r--r--Objects/floatobject.c5
-rw-r--r--Objects/intobject.c13
-rw-r--r--Objects/longobject.c5
4 files changed, 50 insertions, 1 deletions
diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py
index a57471b..d9c166b 100644
--- a/Lib/test/test_descr.py
+++ b/Lib/test/test_descr.py
@@ -1924,6 +1924,33 @@ def rich_comparisons():
verify(eval("x %s c[y]" % op) == eval("x %s y" % op),
"x=%d, y=%d" % (x, y))
+def coercions():
+ if verbose: print "Testing coercions..."
+ class I(int): pass
+ coerce(I(0), 0)
+ coerce(0, I(0))
+ class L(long): pass
+ coerce(L(0), 0)
+ coerce(L(0), 0L)
+ coerce(0, L(0))
+ coerce(0L, L(0))
+ class F(float): pass
+ coerce(F(0), 0)
+ coerce(F(0), 0L)
+ coerce(F(0), 0.)
+ coerce(0, F(0))
+ coerce(0L, F(0))
+ coerce(0., F(0))
+ class C(complex): pass
+ coerce(C(0), 0)
+ coerce(C(0), 0L)
+ coerce(C(0), 0.)
+ coerce(C(0), 0j)
+ coerce(0, C(0))
+ coerce(0L, C(0))
+ coerce(0., C(0))
+ coerce(0j, C(0))
+
def all():
lists()
@@ -1964,6 +1991,7 @@ def all():
str_subclass_as_dict_key()
classic_comparisons()
rich_comparisons()
+ coercions()
all()
diff --git a/Objects/floatobject.c b/Objects/floatobject.c
index b9a5e1b..2de5535 100644
--- a/Objects/floatobject.c
+++ b/Objects/floatobject.c
@@ -590,6 +590,11 @@ float_coerce(PyObject **pv, PyObject **pw)
Py_INCREF(*pv);
return 0;
}
+ else if (PyFloat_Check(*pw)) {
+ Py_INCREF(*pv);
+ Py_INCREF(*pw);
+ return 0;
+ }
return 1; /* Can't do it */
}
diff --git a/Objects/intobject.c b/Objects/intobject.c
index 16e4336..bb5ad16 100644
--- a/Objects/intobject.c
+++ b/Objects/intobject.c
@@ -782,6 +782,17 @@ int_or(PyIntObject *v, PyIntObject *w)
return PyInt_FromLong(a | b);
}
+static int
+int_coerce(PyObject **pv, PyObject **pw)
+{
+ if (PyInt_Check(*pw)) {
+ Py_INCREF(*pv);
+ Py_INCREF(*pw);
+ return 0;
+ }
+ return 1; /* Can't do it */
+}
+
static PyObject *
int_int(PyIntObject *v)
{
@@ -904,7 +915,7 @@ static PyNumberMethods int_as_number = {
(binaryfunc)int_and, /*nb_and*/
(binaryfunc)int_xor, /*nb_xor*/
(binaryfunc)int_or, /*nb_or*/
- 0, /*nb_coerce*/
+ int_coerce, /*nb_coerce*/
(unaryfunc)int_int, /*nb_int*/
(unaryfunc)int_long, /*nb_long*/
(unaryfunc)int_float, /*nb_float*/
diff --git a/Objects/longobject.c b/Objects/longobject.c
index 1e01bf0..8f7d9e4 100644
--- a/Objects/longobject.c
+++ b/Objects/longobject.c
@@ -2134,6 +2134,11 @@ long_coerce(PyObject **pv, PyObject **pw)
Py_INCREF(*pv);
return 0;
}
+ else if (PyLong_Check(*pw)) {
+ Py_INCREF(*pv);
+ Py_INCREF(*pw);
+ return 0;
+ }
return 1; /* Can't do it */
}