summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorTim Peters <tim.peters@gmail.com>2001-09-12 19:12:49 (GMT)
committerTim Peters <tim.peters@gmail.com>2001-09-12 19:12:49 (GMT)
commit2400fa4ad115e8ebe75c61c3bb96bd7cf2364dd4 (patch)
treeb7b324cfd94a932ccfb9d533ce1df33c57413e3d /Objects
parent1140cb2b9e74ce302c35e84ac04adc0b11e2db00 (diff)
downloadcpython-2400fa4ad115e8ebe75c61c3bb96bd7cf2364dd4.zip
cpython-2400fa4ad115e8ebe75c61c3bb96bd7cf2364dd4.tar.gz
cpython-2400fa4ad115e8ebe75c61c3bb96bd7cf2364dd4.tar.bz2
Again perhaps the end of [#460020] bug or feature: unicode() and subclasses.
Inhibited complex unary plus optimization when applied to a complex subtype. Added PyComplex_CheckExact macro. Some comments and minor code fiddling.
Diffstat (limited to 'Objects')
-rw-r--r--Objects/complexobject.c30
-rw-r--r--Objects/floatobject.c2
2 files changed, 20 insertions, 12 deletions
diff --git a/Objects/complexobject.c b/Objects/complexobject.c
index 7404993..a8419e3 100644
--- a/Objects/complexobject.c
+++ b/Objects/complexobject.c
@@ -489,8 +489,12 @@ complex_neg(PyComplexObject *v)
static PyObject *
complex_pos(PyComplexObject *v)
{
- Py_INCREF(v);
- return (PyObject *)v;
+ if (PyComplex_CheckExact(v)) {
+ Py_INCREF(v);
+ return (PyObject *)v;
+ }
+ else
+ return PyComplex_FromCComplex(v->cval);
}
static PyObject *
@@ -792,11 +796,12 @@ complex_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return NULL;
if (PyString_Check(r) || PyUnicode_Check(r))
return complex_subtype_from_string(type, r);
- if ((nbr = r->ob_type->tp_as_number) == NULL ||
- nbr->nb_float == NULL ||
- (i != NULL &&
- ((nbi = i->ob_type->tp_as_number) == NULL ||
- nbi->nb_float == NULL))) {
+
+ nbr = r->ob_type->tp_as_number;
+ if (i != NULL)
+ nbi = i->ob_type->tp_as_number;
+ if (nbr == NULL || nbr->nb_float == NULL ||
+ ((i != NULL) && (nbi == NULL || nbi->nb_float == NULL))) {
PyErr_SetString(PyExc_TypeError,
"complex() arg can't be converted to complex");
return NULL;
@@ -826,6 +831,9 @@ complex_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
}
}
if (PyComplex_Check(r)) {
+ /* Note that if r is of a complex subtype, we're only
+ retaining its real & imag parts here, and the return
+ value is (properly) of the builtin complex type. */
cr = ((PyComplexObject*)r)->cval;
if (own_r) {
Py_DECREF(r);
@@ -868,10 +876,10 @@ complex_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
}
static char complex_doc[] =
-"complex(real[, imag]) -> complex number\n\
-\n\
-Create a complex number from a real part and an optional imaginary part.\n\
-This is equivalent to (real + imag*1j) where imag defaults to 0.";
+"complex(real[, imag]) -> complex number\n"
+"\n"
+"Create a complex number from a real part and an optional imaginary part.\n"
+"This is equivalent to (real + imag*1j) where imag defaults to 0.";
static PyNumberMethods complex_as_number = {
(binaryfunc)complex_add, /* nb_add */
diff --git a/Objects/floatobject.c b/Objects/floatobject.c
index 880eb0e..b9a5e1b 100644
--- a/Objects/floatobject.c
+++ b/Objects/floatobject.c
@@ -659,7 +659,7 @@ float_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
tmp = float_new(&PyFloat_Type, args, kwds);
if (tmp == NULL)
return NULL;
- assert(PyFloat_Check(tmp));
+ assert(PyFloat_CheckExact(tmp));
new = type->tp_alloc(type, 0);
if (new == NULL)
return NULL;