Kaydet (Commit) c52a36f6 authored tarafından Raymond Hettinger's avatar Raymond Hettinger

Backport 2.63 and 2.60:

Call me anal, but there was a particular phrase that was speading to
comments everywhere that bugged me: /* Foo is inlined */ instead of
/* Inline Foo */.  Somehow the "is inlined" phrase always confused me
for half a second (thinking, "No it isn't" until I added the missing
"here").  The new phrase is hopefully unambiguous.

Close SF bug 563740. complex() now finds __complex__() in new style classes.
Made conversion failure error messages consistent between types.
Added related unittests.
üst ad565a34
...@@ -199,7 +199,7 @@ PyComplex_FromCComplex(Py_complex cval) ...@@ -199,7 +199,7 @@ PyComplex_FromCComplex(Py_complex cval)
{ {
register PyComplexObject *op; register PyComplexObject *op;
/* PyObject_New is inlined */ /* Inline PyObject_New */
op = (PyComplexObject *) PyObject_MALLOC(sizeof(PyComplexObject)); op = (PyComplexObject *) PyObject_MALLOC(sizeof(PyComplexObject));
if (op == NULL) if (op == NULL)
return PyErr_NoMemory(); return PyErr_NoMemory();
...@@ -811,10 +811,11 @@ complex_subtype_from_string(PyTypeObject *type, PyObject *v) ...@@ -811,10 +811,11 @@ complex_subtype_from_string(PyTypeObject *type, PyObject *v)
static PyObject * static PyObject *
complex_new(PyTypeObject *type, PyObject *args, PyObject *kwds) complex_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{ {
PyObject *r, *i, *tmp; PyObject *r, *i, *tmp, *f;
PyNumberMethods *nbr, *nbi = NULL; PyNumberMethods *nbr, *nbi = NULL;
Py_complex cr, ci; Py_complex cr, ci;
int own_r = 0; int own_r = 0;
static PyObject *complexstr;
static char *kwlist[] = {"real", "imag", 0}; static char *kwlist[] = {"real", "imag", 0};
r = Py_False; r = Py_False;
...@@ -837,39 +838,35 @@ complex_new(PyTypeObject *type, PyObject *args, PyObject *kwds) ...@@ -837,39 +838,35 @@ complex_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return NULL; return NULL;
} }
/* XXX Hack to support classes with __complex__ method */
if (complexstr == NULL) {
complexstr = PyString_InternFromString("__complex__");
if (complexstr == NULL)
return NULL;
}
f = PyObject_GetAttr(r, complexstr);
if (f == NULL)
PyErr_Clear();
else {
PyObject *args = Py_BuildValue("()");
if (args == NULL)
return NULL;
r = PyEval_CallObject(f, args);
Py_DECREF(args);
Py_DECREF(f);
if (r == NULL)
return NULL;
own_r = 1;
}
nbr = r->ob_type->tp_as_number; nbr = r->ob_type->tp_as_number;
if (i != NULL) if (i != NULL)
nbi = i->ob_type->tp_as_number; nbi = i->ob_type->tp_as_number;
if (nbr == NULL || nbr->nb_float == NULL || if (nbr == NULL || nbr->nb_float == NULL ||
((i != NULL) && (nbi == NULL || nbi->nb_float == NULL))) { ((i != NULL) && (nbi == NULL || nbi->nb_float == NULL))) {
PyErr_SetString(PyExc_TypeError, PyErr_SetString(PyExc_TypeError,
"complex() arg can't be converted to complex"); "complex() argument must be a string or a number");
return NULL; return NULL;
} }
/* XXX Hack to support classes with __complex__ method */
if (PyInstance_Check(r)) {
static PyObject *complexstr;
PyObject *f;
if (complexstr == NULL) {
complexstr = PyString_InternFromString("__complex__");
if (complexstr == NULL)
return NULL;
}
f = PyObject_GetAttr(r, complexstr);
if (f == NULL)
PyErr_Clear();
else {
PyObject *args = Py_BuildValue("()");
if (args == NULL)
return NULL;
r = PyEval_CallObject(f, args);
Py_DECREF(args);
Py_DECREF(f);
if (r == NULL)
return NULL;
own_r = 1;
}
}
if (PyComplex_Check(r)) { if (PyComplex_Check(r)) {
/* Note that if r is of a complex subtype, we're only /* Note that if r is of a complex subtype, we're only
retaining its real & imag parts here, and the return retaining its real & imag parts here, and the return
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment