@@ -905,7 +905,7 @@ PyNumber_Check(PyObject *o)
905905 if (o == NULL )
906906 return 0 ;
907907 PyNumberMethods * nb = Py_TYPE (o )-> tp_as_number ;
908- return nb && (nb -> nb_index || nb -> nb_int || nb -> nb_float || PyComplex_Check ( o ) );
908+ return nb && (nb -> nb_index || nb -> nb_int || nb -> nb_float || nb -> nb_complex );
909909}
910910
911911/* Binary operators */
@@ -1659,44 +1659,61 @@ PyNumber_Complex(PyObject *o)
16591659 return Py_NewRef (o );
16601660 }
16611661
1662- PyObject * f = _PyObject_LookupSpecial (o , & _Py_ID (__complex__ ));
1663- if (f ) {
1664- PyObject * res = _PyObject_CallNoArgs (f );
1665- Py_DECREF (f );
1666- if (!res || PyComplex_CheckExact (res )) {
1667- return res ;
1668- }
1669- if (!PyComplex_Check (res )) {
1670- PyErr_Format (PyExc_TypeError ,
1671- "%.50s.__complex__ returned non-complex (type %.50s)" ,
1672- Py_TYPE (o )-> tp_name , Py_TYPE (res )-> tp_name );
1673- Py_DECREF (res );
1674- return NULL ;
1675- }
1676- /* Issue #26983: warn if 'res' not of exact type complex. */
1677- if (PyErr_WarnFormat (PyExc_DeprecationWarning , 1 ,
1678- "%.50s.__complex__ returned non-complex (type %.50s). "
1679- "The ability to return an instance of a strict subclass of complex "
1680- "is deprecated, and may be removed in a future version of Python." ,
1681- Py_TYPE (o )-> tp_name , Py_TYPE (res )-> tp_name )) {
1682- Py_DECREF (res );
1683- return NULL ;
1662+ PyNumberMethods * m = Py_TYPE (o )-> tp_as_number ;
1663+ PyObject * res = NULL ;
1664+
1665+ if (m && m -> nb_complex ) {
1666+ res = m -> nb_complex (o );
1667+ goto complex_slot ;
1668+ }
1669+ else {
1670+ PyObject * f = _PyObject_LookupSpecial (o , & _Py_ID (__complex__ ));
1671+ if (f ) {
1672+ res = _PyObject_CallNoArgs (f );
1673+ Py_DECREF (f );
1674+ if (PyErr_WarnEx (PyExc_DeprecationWarning ,
1675+ "Use nb_complex slot to implement __complex__" , 1 )) {
1676+ Py_XDECREF (res );
1677+ return NULL ;
1678+ }
1679+ goto complex_slot ;
16841680 }
1685- Py_complex cv = ((PyComplexObject * )res )-> cval ;
1686- Py_DECREF (res );
1687- return PyComplex_FromCComplex (cv );
16881681 }
1689-
1690- PyNumberMethods * m = Py_TYPE (o )-> tp_as_number ;
1682+ m = Py_TYPE (o )-> tp_as_number ;
16911683 if (m && (m -> nb_float || m -> nb_index )) {
16921684 double real = PyFloat_AsDouble (o );
16931685 if (real != -1 || !PyErr_Occurred ()) {
16941686 return PyComplex_FromDoubles (real , 0.0 );
16951687 }
16961688 return NULL ;
16971689 }
1698-
16991690 return PyComplex_FromString (o );
1691+ complex_slot :
1692+ if (!res ) {
1693+ return NULL ;
1694+ }
1695+ if (PyComplex_CheckExact (res )) {
1696+ return res ;
1697+ }
1698+ if (!PyComplex_Check (res )) {
1699+ PyErr_Format (PyExc_TypeError ,
1700+ "%.50s.__complex__ returned non-complex (type %.50s)" ,
1701+ Py_TYPE (o )-> tp_name , Py_TYPE (res )-> tp_name );
1702+ Py_DECREF (res );
1703+ return NULL ;
1704+ }
1705+ /* Issue #26983: warn if 'res' not of exact type complex. */
1706+ if (PyErr_WarnFormat (PyExc_DeprecationWarning , 1 ,
1707+ "%.50s.__complex__ returned non-complex (type %.50s). "
1708+ "The ability to return an instance of a strict subclass of complex "
1709+ "is deprecated, and may be removed in a future version of Python." ,
1710+ Py_TYPE (o )-> tp_name , Py_TYPE (res )-> tp_name )) {
1711+ Py_DECREF (res );
1712+ return NULL ;
1713+ }
1714+ Py_complex cv = ((PyComplexObject * )res )-> cval ;
1715+ Py_DECREF (res );
1716+ return PyComplex_FromCComplex (cv );
17001717}
17011718
17021719PyObject *
0 commit comments