14
14
#include "pycore_pymath.h" // _Py_ADJUST_ERANGE2()
15
15
16
16
17
+ #define _PyComplexObject_CAST (op ) ((PyComplexObject *)(op))
18
+
17
19
18
20
/*[clinic input]
19
21
class complex "PyComplexObject *" "&PyComplex_Type"
@@ -553,11 +555,12 @@ PyComplex_AsCComplex(PyObject *op)
553
555
}
554
556
555
557
static PyObject *
556
- complex_repr (PyComplexObject * v )
558
+ complex_repr (PyObject * op )
557
559
{
558
560
int precision = 0 ;
559
561
char format_code = 'r' ;
560
562
PyObject * result = NULL ;
563
+ PyComplexObject * v = _PyComplexObject_CAST (op );
561
564
562
565
/* If these are non-NULL, they'll need to be freed. */
563
566
char * pre = NULL ;
@@ -609,13 +612,14 @@ complex_repr(PyComplexObject *v)
609
612
}
610
613
611
614
static Py_hash_t
612
- complex_hash (PyComplexObject * v )
615
+ complex_hash (PyObject * op )
613
616
{
614
617
Py_uhash_t hashreal , hashimag , combined ;
615
- hashreal = (Py_uhash_t )_Py_HashDouble ((PyObject * ) v , v -> cval .real );
618
+ PyComplexObject * v = _PyComplexObject_CAST (op );
619
+ hashreal = (Py_uhash_t )_Py_HashDouble (op , v -> cval .real );
616
620
if (hashreal == (Py_uhash_t )- 1 )
617
621
return -1 ;
618
- hashimag = (Py_uhash_t )_Py_HashDouble (( PyObject * ) v , v -> cval .imag );
622
+ hashimag = (Py_uhash_t )_Py_HashDouble (op , v -> cval .imag );
619
623
if (hashimag == (Py_uhash_t )- 1 )
620
624
return -1 ;
621
625
/* Note: if the imaginary part is 0, hashimag is 0 now,
@@ -753,31 +757,30 @@ complex_pow(PyObject *v, PyObject *w, PyObject *z)
753
757
}
754
758
755
759
static PyObject *
756
- complex_neg (PyComplexObject * v )
760
+ complex_neg (PyObject * op )
757
761
{
762
+ PyComplexObject * v = _PyComplexObject_CAST (op );
758
763
Py_complex neg ;
759
764
neg .real = - v -> cval .real ;
760
765
neg .imag = - v -> cval .imag ;
761
766
return PyComplex_FromCComplex (neg );
762
767
}
763
768
764
769
static PyObject *
765
- complex_pos (PyComplexObject * v )
770
+ complex_pos (PyObject * op )
766
771
{
772
+ PyComplexObject * v = _PyComplexObject_CAST (op );
767
773
if (PyComplex_CheckExact (v )) {
768
774
return Py_NewRef (v );
769
775
}
770
- else
771
- return PyComplex_FromCComplex (v -> cval );
776
+ return PyComplex_FromCComplex (v -> cval );
772
777
}
773
778
774
779
static PyObject *
775
- complex_abs (PyComplexObject * v )
780
+ complex_abs (PyObject * op )
776
781
{
777
- double result ;
778
-
779
- result = _Py_c_abs (v -> cval );
780
-
782
+ PyComplexObject * v = _PyComplexObject_CAST (op );
783
+ double result = _Py_c_abs (v -> cval );
781
784
if (errno == ERANGE ) {
782
785
PyErr_SetString (PyExc_OverflowError ,
783
786
"absolute value too large" );
@@ -787,8 +790,9 @@ complex_abs(PyComplexObject *v)
787
790
}
788
791
789
792
static int
790
- complex_bool (PyComplexObject * v )
793
+ complex_bool (PyObject * op )
791
794
{
795
+ PyComplexObject * v = _PyComplexObject_CAST (op );
792
796
return v -> cval .real != 0.0 || v -> cval .imag != 0.0 ;
793
797
}
794
798
@@ -1339,16 +1343,16 @@ static PyMemberDef complex_members[] = {
1339
1343
};
1340
1344
1341
1345
static PyNumberMethods complex_as_number = {
1342
- ( binaryfunc ) complex_add , /* nb_add */
1343
- ( binaryfunc ) complex_sub , /* nb_subtract */
1344
- ( binaryfunc ) complex_mul , /* nb_multiply */
1346
+ complex_add , /* nb_add */
1347
+ complex_sub , /* nb_subtract */
1348
+ complex_mul , /* nb_multiply */
1345
1349
0 , /* nb_remainder */
1346
1350
0 , /* nb_divmod */
1347
- ( ternaryfunc ) complex_pow , /* nb_power */
1348
- ( unaryfunc ) complex_neg , /* nb_negative */
1349
- ( unaryfunc ) complex_pos , /* nb_positive */
1350
- ( unaryfunc ) complex_abs , /* nb_absolute */
1351
- ( inquiry ) complex_bool , /* nb_bool */
1351
+ complex_pow , /* nb_power */
1352
+ complex_neg , /* nb_negative */
1353
+ complex_pos , /* nb_positive */
1354
+ complex_abs , /* nb_absolute */
1355
+ complex_bool , /* nb_bool */
1352
1356
0 , /* nb_invert */
1353
1357
0 , /* nb_lshift */
1354
1358
0 , /* nb_rshift */
@@ -1369,7 +1373,7 @@ static PyNumberMethods complex_as_number = {
1369
1373
0 , /* nb_inplace_xor */
1370
1374
0 , /* nb_inplace_or */
1371
1375
0 , /* nb_floor_divide */
1372
- ( binaryfunc ) complex_div , /* nb_true_divide */
1376
+ complex_div , /* nb_true_divide */
1373
1377
0 , /* nb_inplace_floor_divide */
1374
1378
0 , /* nb_inplace_true_divide */
1375
1379
};
@@ -1384,11 +1388,11 @@ PyTypeObject PyComplex_Type = {
1384
1388
0 , /* tp_getattr */
1385
1389
0 , /* tp_setattr */
1386
1390
0 , /* tp_as_async */
1387
- ( reprfunc ) complex_repr , /* tp_repr */
1391
+ complex_repr , /* tp_repr */
1388
1392
& complex_as_number , /* tp_as_number */
1389
1393
0 , /* tp_as_sequence */
1390
1394
0 , /* tp_as_mapping */
1391
- ( hashfunc ) complex_hash , /* tp_hash */
1395
+ complex_hash , /* tp_hash */
1392
1396
0 , /* tp_call */
1393
1397
0 , /* tp_str */
1394
1398
PyObject_GenericGetAttr , /* tp_getattro */
0 commit comments