blob: 3317106f1880c4913fcca36b8b4f4b660fcc2c23 [file] [log] [blame]
Guido van Rossum96783941997-05-20 18:21:34 +00001
Guido van Rossumf9fca921996-01-12 00:47:05 +00002/* Complex object implementation */
3
4/* Borrows heavily from floatobject.c */
5
Guido van Rossum96783941997-05-20 18:21:34 +00006/* Submitted by Jim Hugunin */
7
Guido van Rossumc0b618a1997-05-02 03:12:38 +00008#include "Python.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +00009#include "structmember.h"
Guido van Rossumf9fca921996-01-12 00:47:05 +000010
Guido van Rossum65ce6de2002-06-13 17:07:07 +000011#ifndef WITHOUT_COMPLEX
12
Tim Peters70695122001-03-11 08:37:29 +000013/* Precisions used by repr() and str(), respectively.
14
15 The repr() precision (17 significant decimal digits) is the minimal number
16 that is guaranteed to have enough precision so that if the number is read
17 back in the exact same binary value is recreated. This is true for IEEE
18 floating point by design, and also happens to work for all other modern
19 hardware.
20
21 The str() precision is chosen so that in most cases, the rounding noise
22 created by various operations is suppressed, while giving plenty of
23 precision for practical use.
24*/
25
26#define PREC_REPR 17
27#define PREC_STR 12
Guido van Rossumf9fca921996-01-12 00:47:05 +000028
29/* elementary operations on complex numbers */
30
Guido van Rossum9e720e31996-07-21 02:31:35 +000031static Py_complex c_1 = {1., 0.};
Guido van Rossumf9fca921996-01-12 00:47:05 +000032
Tim Peters0f336042001-03-18 08:21:57 +000033Py_complex
34c_sum(Py_complex a, Py_complex b)
Guido van Rossumf9fca921996-01-12 00:47:05 +000035{
Guido van Rossum9e720e31996-07-21 02:31:35 +000036 Py_complex r;
Guido van Rossumf9fca921996-01-12 00:47:05 +000037 r.real = a.real + b.real;
38 r.imag = a.imag + b.imag;
39 return r;
40}
41
Tim Peters0f336042001-03-18 08:21:57 +000042Py_complex
43c_diff(Py_complex a, Py_complex b)
Guido van Rossumf9fca921996-01-12 00:47:05 +000044{
Guido van Rossum9e720e31996-07-21 02:31:35 +000045 Py_complex r;
Guido van Rossumf9fca921996-01-12 00:47:05 +000046 r.real = a.real - b.real;
47 r.imag = a.imag - b.imag;
48 return r;
49}
50
Tim Peters0f336042001-03-18 08:21:57 +000051Py_complex
52c_neg(Py_complex a)
Guido van Rossumf9fca921996-01-12 00:47:05 +000053{
Guido van Rossum9e720e31996-07-21 02:31:35 +000054 Py_complex r;
Guido van Rossumf9fca921996-01-12 00:47:05 +000055 r.real = -a.real;
56 r.imag = -a.imag;
57 return r;
58}
59
Tim Peters0f336042001-03-18 08:21:57 +000060Py_complex
61c_prod(Py_complex a, Py_complex b)
Guido van Rossumf9fca921996-01-12 00:47:05 +000062{
Guido van Rossum9e720e31996-07-21 02:31:35 +000063 Py_complex r;
Guido van Rossumf9fca921996-01-12 00:47:05 +000064 r.real = a.real*b.real - a.imag*b.imag;
65 r.imag = a.real*b.imag + a.imag*b.real;
66 return r;
67}
68
Tim Peters0f336042001-03-18 08:21:57 +000069Py_complex
70c_quot(Py_complex a, Py_complex b)
Guido van Rossumf9fca921996-01-12 00:47:05 +000071{
Tim Peters0f336042001-03-18 08:21:57 +000072 /******************************************************************
73 This was the original algorithm. It's grossly prone to spurious
74 overflow and underflow errors. It also merrily divides by 0 despite
75 checking for that(!). The code still serves a doc purpose here, as
76 the algorithm following is a simple by-cases transformation of this
77 one:
78
Guido van Rossum9e720e31996-07-21 02:31:35 +000079 Py_complex r;
Guido van Rossumf9fca921996-01-12 00:47:05 +000080 double d = b.real*b.real + b.imag*b.imag;
81 if (d == 0.)
Guido van Rossum96783941997-05-20 18:21:34 +000082 errno = EDOM;
Guido van Rossumf9fca921996-01-12 00:47:05 +000083 r.real = (a.real*b.real + a.imag*b.imag)/d;
84 r.imag = (a.imag*b.real - a.real*b.imag)/d;
85 return r;
Tim Peters0f336042001-03-18 08:21:57 +000086 ******************************************************************/
87
88 /* This algorithm is better, and is pretty obvious: first divide the
89 * numerators and denominator by whichever of {b.real, b.imag} has
90 * larger magnitude. The earliest reference I found was to CACM
91 * Algorithm 116 (Complex Division, Robert L. Smith, Stanford
92 * University). As usual, though, we're still ignoring all IEEE
93 * endcases.
94 */
95 Py_complex r; /* the result */
96 const double abs_breal = b.real < 0 ? -b.real : b.real;
97 const double abs_bimag = b.imag < 0 ? -b.imag : b.imag;
98
99 if (abs_breal >= abs_bimag) {
100 /* divide tops and bottom by b.real */
101 if (abs_breal == 0.0) {
102 errno = EDOM;
103 r.real = r.imag = 0.0;
104 }
105 else {
106 const double ratio = b.imag / b.real;
107 const double denom = b.real + b.imag * ratio;
108 r.real = (a.real + a.imag * ratio) / denom;
109 r.imag = (a.imag - a.real * ratio) / denom;
110 }
111 }
112 else {
113 /* divide tops and bottom by b.imag */
114 const double ratio = b.real / b.imag;
115 const double denom = b.real * ratio + b.imag;
116 assert(b.imag != 0.0);
117 r.real = (a.real * ratio + a.imag) / denom;
118 r.imag = (a.imag * ratio - a.real) / denom;
119 }
120 return r;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000121}
122
Tim Peters0f336042001-03-18 08:21:57 +0000123Py_complex
124c_pow(Py_complex a, Py_complex b)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000125{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000126 Py_complex r;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000127 double vabs,len,at,phase;
128 if (b.real == 0. && b.imag == 0.) {
129 r.real = 1.;
130 r.imag = 0.;
131 }
132 else if (a.real == 0. && a.imag == 0.) {
133 if (b.imag != 0. || b.real < 0.)
Tim Petersbab22be2002-03-22 02:48:46 +0000134 errno = EDOM;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000135 r.real = 0.;
136 r.imag = 0.;
137 }
138 else {
139 vabs = hypot(a.real,a.imag);
140 len = pow(vabs,b.real);
Martin v. Löwis387c5472001-09-06 08:16:17 +0000141 at = atan2(a.imag, a.real);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000142 phase = at*b.real;
143 if (b.imag != 0.0) {
144 len /= exp(at*b.imag);
145 phase += b.imag*log(vabs);
146 }
147 r.real = len*cos(phase);
148 r.imag = len*sin(phase);
149 }
150 return r;
151}
152
Tim Peters0f336042001-03-18 08:21:57 +0000153static Py_complex
154c_powu(Py_complex x, long n)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000155{
Guido van Rossum926518b1996-08-19 19:30:45 +0000156 Py_complex r, p;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000157 long mask = 1;
Guido van Rossum926518b1996-08-19 19:30:45 +0000158 r = c_1;
159 p = x;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000160 while (mask > 0 && n >= mask) {
161 if (n & mask)
162 r = c_prod(r,p);
163 mask <<= 1;
164 p = c_prod(p,p);
165 }
166 return r;
167}
168
Tim Peters0f336042001-03-18 08:21:57 +0000169static Py_complex
170c_powi(Py_complex x, long n)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000171{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000172 Py_complex cn;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000173
174 if (n > 100 || n < -100) {
175 cn.real = (double) n;
176 cn.imag = 0.;
177 return c_pow(x,cn);
178 }
179 else if (n > 0)
180 return c_powu(x,n);
181 else
182 return c_quot(c_1,c_powu(x,-n));
183
184}
185
Christian Heimes6f341092008-04-18 23:13:07 +0000186double
187c_abs(Py_complex z)
188{
189 /* sets errno = ERANGE on overflow; otherwise errno = 0 */
190 double result;
191
192 if (!Py_IS_FINITE(z.real) || !Py_IS_FINITE(z.imag)) {
193 /* C99 rules: if either the real or the imaginary part is an
194 infinity, return infinity, even if the other part is a
195 NaN. */
196 if (Py_IS_INFINITY(z.real)) {
197 result = fabs(z.real);
198 errno = 0;
199 return result;
200 }
201 if (Py_IS_INFINITY(z.imag)) {
202 result = fabs(z.imag);
203 errno = 0;
204 return result;
205 }
206 /* either the real or imaginary part is a NaN,
207 and neither is infinite. Result should be NaN. */
208 return Py_NAN;
209 }
210 result = hypot(z.real, z.imag);
211 if (!Py_IS_FINITE(result))
212 errno = ERANGE;
213 else
214 errno = 0;
215 return result;
216}
217
Tim Peters6d6c1a32001-08-02 04:15:00 +0000218static PyObject *
219complex_subtype_from_c_complex(PyTypeObject *type, Py_complex cval)
220{
221 PyObject *op;
222
Georg Brandl6b50c632006-06-01 08:27:32 +0000223 op = type->tp_alloc(type, 0);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000224 if (op != NULL)
225 ((PyComplexObject *)op)->cval = cval;
226 return op;
227}
228
Guido van Rossumf9fca921996-01-12 00:47:05 +0000229PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000230PyComplex_FromCComplex(Py_complex cval)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000231{
Guido van Rossumb18618d2000-05-03 23:44:39 +0000232 register PyComplexObject *op;
233
Guido van Rossume3a8e7e2002-08-19 19:26:42 +0000234 /* Inline PyObject_New */
Guido van Rossumb18618d2000-05-03 23:44:39 +0000235 op = (PyComplexObject *) PyObject_MALLOC(sizeof(PyComplexObject));
Guido van Rossumf9fca921996-01-12 00:47:05 +0000236 if (op == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000237 return PyErr_NoMemory();
Guido van Rossumb18618d2000-05-03 23:44:39 +0000238 PyObject_INIT(op, &PyComplex_Type);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000239 op->cval = cval;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000240 return (PyObject *) op;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000241}
242
Tim Peters6d6c1a32001-08-02 04:15:00 +0000243static PyObject *
244complex_subtype_from_doubles(PyTypeObject *type, double real, double imag)
245{
246 Py_complex c;
247 c.real = real;
248 c.imag = imag;
249 return complex_subtype_from_c_complex(type, c);
250}
251
Guido van Rossumf9fca921996-01-12 00:47:05 +0000252PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000253PyComplex_FromDoubles(double real, double imag)
Guido van Rossum926518b1996-08-19 19:30:45 +0000254{
255 Py_complex c;
256 c.real = real;
257 c.imag = imag;
258 return PyComplex_FromCComplex(c);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000259}
260
261double
Fred Drake4288c802000-07-09 04:36:04 +0000262PyComplex_RealAsDouble(PyObject *op)
Guido van Rossum926518b1996-08-19 19:30:45 +0000263{
264 if (PyComplex_Check(op)) {
265 return ((PyComplexObject *)op)->cval.real;
Fred Drake4288c802000-07-09 04:36:04 +0000266 }
267 else {
Guido van Rossum926518b1996-08-19 19:30:45 +0000268 return PyFloat_AsDouble(op);
269 }
Guido van Rossumf9fca921996-01-12 00:47:05 +0000270}
271
272double
Fred Drake4288c802000-07-09 04:36:04 +0000273PyComplex_ImagAsDouble(PyObject *op)
Guido van Rossum926518b1996-08-19 19:30:45 +0000274{
275 if (PyComplex_Check(op)) {
276 return ((PyComplexObject *)op)->cval.imag;
Fred Drake4288c802000-07-09 04:36:04 +0000277 }
278 else {
Guido van Rossum926518b1996-08-19 19:30:45 +0000279 return 0.0;
280 }
Guido van Rossumf9fca921996-01-12 00:47:05 +0000281}
282
Benjamin Peterson36943662010-01-04 01:00:47 +0000283static PyObject *
284try_complex_special_method(PyObject *op) {
285 PyObject *f;
286 static PyObject *complexstr;
287
288 if (complexstr == NULL) {
289 complexstr = PyString_InternFromString("__complex__");
290 if (complexstr == NULL)
291 return NULL;
292 }
293 if (PyInstance_Check(op)) {
294 f = PyObject_GetAttr(op, complexstr);
295 if (f == NULL) {
296 if (PyErr_ExceptionMatches(PyExc_AttributeError))
297 PyErr_Clear();
298 else
299 return NULL;
300 }
301 }
302 else {
303 f = _PyObject_LookupSpecial(op, "__complex__", &complexstr);
304 if (f == NULL && PyErr_Occurred())
305 return NULL;
306 }
307 if (f != NULL) {
308 PyObject *res = PyObject_CallFunctionObjArgs(f, NULL);
309 Py_DECREF(f);
310 return res;
311 }
312 return NULL;
313}
314
Guido van Rossum9e720e31996-07-21 02:31:35 +0000315Py_complex
Fred Drake4288c802000-07-09 04:36:04 +0000316PyComplex_AsCComplex(PyObject *op)
Guido van Rossum926518b1996-08-19 19:30:45 +0000317{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000318 Py_complex cv;
Georg Brandl2b869942007-03-17 16:08:45 +0000319 PyObject *newop = NULL;
Georg Brandl2b869942007-03-17 16:08:45 +0000320
321 assert(op);
322 /* If op is already of type PyComplex_Type, return its value */
Guido van Rossumcf3d1081996-01-12 01:21:14 +0000323 if (PyComplex_Check(op)) {
324 return ((PyComplexObject *)op)->cval;
Fred Drake4288c802000-07-09 04:36:04 +0000325 }
Georg Brandl2b869942007-03-17 16:08:45 +0000326 /* If not, use op's __complex__ method, if it exists */
Christian Heimesd7e1b2b2008-01-28 02:07:53 +0000327
Georg Brandl2b869942007-03-17 16:08:45 +0000328 /* return -1 on failure */
329 cv.real = -1.;
330 cv.imag = 0.;
Christian Heimesd7e1b2b2008-01-28 02:07:53 +0000331
Benjamin Peterson36943662010-01-04 01:00:47 +0000332 newop = try_complex_special_method(op);
Georg Brandl2b869942007-03-17 16:08:45 +0000333
Georg Brandl2b869942007-03-17 16:08:45 +0000334 if (newop) {
335 if (!PyComplex_Check(newop)) {
336 PyErr_SetString(PyExc_TypeError,
337 "__complex__ should return a complex object");
338 Py_DECREF(newop);
339 return cv;
340 }
341 cv = ((PyComplexObject *)newop)->cval;
342 Py_DECREF(newop);
343 return cv;
344 }
Benjamin Peterson36943662010-01-04 01:00:47 +0000345 else if (PyErr_Occurred()) {
346 return cv;
347 }
Georg Brandl2b869942007-03-17 16:08:45 +0000348 /* If neither of the above works, interpret op as a float giving the
349 real part of the result, and fill in the imaginary part as 0. */
Fred Drake4288c802000-07-09 04:36:04 +0000350 else {
Georg Brandl2b869942007-03-17 16:08:45 +0000351 /* PyFloat_AsDouble will return -1 on failure */
Guido van Rossumcf3d1081996-01-12 01:21:14 +0000352 cv.real = PyFloat_AsDouble(op);
Guido van Rossumcf3d1081996-01-12 01:21:14 +0000353 return cv;
Tim Peters70695122001-03-11 08:37:29 +0000354 }
Guido van Rossumcf3d1081996-01-12 01:21:14 +0000355}
356
Guido van Rossumf9fca921996-01-12 00:47:05 +0000357static void
Fred Drake4288c802000-07-09 04:36:04 +0000358complex_dealloc(PyObject *op)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000359{
Guido van Rossum9475a232001-10-05 20:51:39 +0000360 op->ob_type->tp_free(op);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000361}
362
363
Mark Dickinson95bc9802009-04-24 12:46:53 +0000364static PyObject *
Eric Smitha985a3a2009-05-05 18:26:08 +0000365complex_format(PyComplexObject *v, int precision, char format_code)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000366{
Mark Dickinson95bc9802009-04-24 12:46:53 +0000367 PyObject *result = NULL;
368 Py_ssize_t len;
369
370 /* If these are non-NULL, they'll need to be freed. */
371 char *pre = NULL;
372 char *im = NULL;
373 char *buf = NULL;
374
375 /* These do not need to be freed. re is either an alias
376 for pre or a pointer to a constant. lead and tail
377 are pointers to constants. */
378 char *re = NULL;
379 char *lead = "";
380 char *tail = "";
381
382 if (v->cval.real == 0. && copysign(1.0, v->cval.real)==1.0) {
383 re = "";
384 im = PyOS_double_to_string(v->cval.imag, format_code,
Eric Smitha985a3a2009-05-05 18:26:08 +0000385 precision, 0, NULL);
Mark Dickinson95bc9802009-04-24 12:46:53 +0000386 if (!im) {
387 PyErr_NoMemory();
388 goto done;
Christian Heimes2f0da532008-02-15 06:57:08 +0000389 }
Martin v. Löwis737ea822004-06-08 18:52:54 +0000390 } else {
Georg Brandlc404ff22005-09-16 06:42:26 +0000391 /* Format imaginary part with sign, real part without */
Mark Dickinson95bc9802009-04-24 12:46:53 +0000392 pre = PyOS_double_to_string(v->cval.real, format_code,
Eric Smitha985a3a2009-05-05 18:26:08 +0000393 precision, 0, NULL);
Mark Dickinson95bc9802009-04-24 12:46:53 +0000394 if (!pre) {
395 PyErr_NoMemory();
396 goto done;
Christian Heimes2f0da532008-02-15 06:57:08 +0000397 }
Mark Dickinson95bc9802009-04-24 12:46:53 +0000398 re = pre;
399
400 im = PyOS_double_to_string(v->cval.imag, format_code,
Eric Smitha985a3a2009-05-05 18:26:08 +0000401 precision, Py_DTSF_SIGN, NULL);
Mark Dickinson95bc9802009-04-24 12:46:53 +0000402 if (!im) {
403 PyErr_NoMemory();
404 goto done;
Christian Heimes2f0da532008-02-15 06:57:08 +0000405 }
Mark Dickinson95bc9802009-04-24 12:46:53 +0000406 lead = "(";
407 tail = ")";
Martin v. Löwis737ea822004-06-08 18:52:54 +0000408 }
Mark Dickinson95bc9802009-04-24 12:46:53 +0000409 /* Alloc the final buffer. Add one for the "j" in the format string,
410 and one for the trailing zero. */
411 len = strlen(lead) + strlen(re) + strlen(im) + strlen(tail) + 2;
412 buf = PyMem_Malloc(len);
413 if (!buf) {
414 PyErr_NoMemory();
415 goto done;
416 }
417 PyOS_snprintf(buf, len, "%s%s%sj%s", lead, re, im, tail);
418 result = PyString_FromString(buf);
419 done:
420 PyMem_Free(im);
421 PyMem_Free(pre);
422 PyMem_Free(buf);
423
424 return result;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000425}
426
427static int
Fred Drake4288c802000-07-09 04:36:04 +0000428complex_print(PyComplexObject *v, FILE *fp, int flags)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000429{
Mark Dickinson95bc9802009-04-24 12:46:53 +0000430 PyObject *formatv;
431 char *buf;
Eric Smitha985a3a2009-05-05 18:26:08 +0000432 if (flags & Py_PRINT_RAW)
433 formatv = complex_format(v, PyFloat_STR_PRECISION, 'g');
434 else
435 formatv = complex_format(v, 0, 'r');
Mark Dickinson95bc9802009-04-24 12:46:53 +0000436 if (formatv == NULL)
437 return -1;
438 buf = PyString_AS_STRING(formatv);
Brett Cannon01531592007-09-17 03:28:34 +0000439 Py_BEGIN_ALLOW_THREADS
Guido van Rossumf9fca921996-01-12 00:47:05 +0000440 fputs(buf, fp);
Brett Cannon01531592007-09-17 03:28:34 +0000441 Py_END_ALLOW_THREADS
Mark Dickinson95bc9802009-04-24 12:46:53 +0000442 Py_DECREF(formatv);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000443 return 0;
444}
445
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000446static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000447complex_repr(PyComplexObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000448{
Eric Smitha985a3a2009-05-05 18:26:08 +0000449 return complex_format(v, 0, 'r');
Tim Peters70695122001-03-11 08:37:29 +0000450}
451
452static PyObject *
453complex_str(PyComplexObject *v)
454{
Eric Smitha985a3a2009-05-05 18:26:08 +0000455 return complex_format(v, PyFloat_STR_PRECISION, 'g');
Guido van Rossumf9fca921996-01-12 00:47:05 +0000456}
457
Guido van Rossumf9fca921996-01-12 00:47:05 +0000458static long
Fred Drake4288c802000-07-09 04:36:04 +0000459complex_hash(PyComplexObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000460{
Tim Peters39dce292000-08-15 03:34:48 +0000461 long hashreal, hashimag, combined;
462 hashreal = _Py_HashDouble(v->cval.real);
463 if (hashreal == -1)
464 return -1;
465 hashimag = _Py_HashDouble(v->cval.imag);
466 if (hashimag == -1)
467 return -1;
468 /* Note: if the imaginary part is 0, hashimag is 0 now,
469 * so the following returns hashreal unchanged. This is
470 * important because numbers of different types that
471 * compare equal must have the same hash value, so that
472 * hash(x + 0*j) must equal hash(x).
473 */
474 combined = hashreal + 1000003 * hashimag;
475 if (combined == -1)
476 combined = -2;
477 return combined;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000478}
479
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +0000480/* This macro may return! */
481#define TO_COMPLEX(obj, c) \
482 if (PyComplex_Check(obj)) \
483 c = ((PyComplexObject *)(obj))->cval; \
484 else if (to_complex(&(obj), &(c)) < 0) \
485 return (obj)
486
487static int
488to_complex(PyObject **pobj, Py_complex *pc)
489{
490 PyObject *obj = *pobj;
491
492 pc->real = pc->imag = 0.0;
493 if (PyInt_Check(obj)) {
494 pc->real = PyInt_AS_LONG(obj);
495 return 0;
496 }
497 if (PyLong_Check(obj)) {
498 pc->real = PyLong_AsDouble(obj);
499 if (pc->real == -1.0 && PyErr_Occurred()) {
500 *pobj = NULL;
501 return -1;
502 }
503 return 0;
504 }
505 if (PyFloat_Check(obj)) {
506 pc->real = PyFloat_AsDouble(obj);
507 return 0;
508 }
509 Py_INCREF(Py_NotImplemented);
510 *pobj = Py_NotImplemented;
511 return -1;
512}
513
514
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000515static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000516complex_add(PyComplexObject *v, PyComplexObject *w)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000517{
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000518 Py_complex result;
519 PyFPE_START_PROTECT("complex_add", return 0)
520 result = c_sum(v->cval,w->cval);
Guido van Rossum45b83911997-03-14 04:32:50 +0000521 PyFPE_END_PROTECT(result)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000522 return PyComplex_FromCComplex(result);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000523}
524
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000525static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000526complex_sub(PyComplexObject *v, PyComplexObject *w)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000527{
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000528 Py_complex result;
529 PyFPE_START_PROTECT("complex_sub", return 0)
530 result = c_diff(v->cval,w->cval);
Guido van Rossum45b83911997-03-14 04:32:50 +0000531 PyFPE_END_PROTECT(result)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000532 return PyComplex_FromCComplex(result);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000533}
534
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000535static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000536complex_mul(PyComplexObject *v, PyComplexObject *w)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000537{
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000538 Py_complex result;
539 PyFPE_START_PROTECT("complex_mul", return 0)
540 result = c_prod(v->cval,w->cval);
Guido van Rossum45b83911997-03-14 04:32:50 +0000541 PyFPE_END_PROTECT(result)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000542 return PyComplex_FromCComplex(result);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000543}
544
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000545static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000546complex_div(PyComplexObject *v, PyComplexObject *w)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000547{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000548 Py_complex quot;
Christian Heimes6f341092008-04-18 23:13:07 +0000549
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000550 PyFPE_START_PROTECT("complex_div", return 0)
Guido van Rossum96783941997-05-20 18:21:34 +0000551 errno = 0;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000552 quot = c_quot(v->cval,w->cval);
Guido van Rossum45b83911997-03-14 04:32:50 +0000553 PyFPE_END_PROTECT(quot)
Guido van Rossum96783941997-05-20 18:21:34 +0000554 if (errno == EDOM) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000555 PyErr_SetString(PyExc_ZeroDivisionError, "complex division");
Guido van Rossumf9fca921996-01-12 00:47:05 +0000556 return NULL;
557 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000558 return PyComplex_FromCComplex(quot);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000559}
560
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000561static PyObject *
Guido van Rossum393661d2001-08-31 17:40:15 +0000562complex_classic_div(PyComplexObject *v, PyComplexObject *w)
563{
564 Py_complex quot;
565
Guido van Rossum1832de42001-09-04 03:51:09 +0000566 if (Py_DivisionWarningFlag >= 2 &&
Guido van Rossum393661d2001-08-31 17:40:15 +0000567 PyErr_Warn(PyExc_DeprecationWarning,
568 "classic complex division") < 0)
569 return NULL;
570
571 PyFPE_START_PROTECT("complex_classic_div", return 0)
572 errno = 0;
573 quot = c_quot(v->cval,w->cval);
574 PyFPE_END_PROTECT(quot)
575 if (errno == EDOM) {
576 PyErr_SetString(PyExc_ZeroDivisionError, "complex division");
577 return NULL;
578 }
579 return PyComplex_FromCComplex(quot);
580}
581
582static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000583complex_remainder(PyComplexObject *v, PyComplexObject *w)
Guido van Rossumee09fc11996-09-11 13:55:55 +0000584{
Georg Brandl96f21842008-01-19 10:18:07 +0000585 Py_complex div, mod;
Guido van Rossum69cf3c72002-04-15 12:39:12 +0000586
587 if (PyErr_Warn(PyExc_DeprecationWarning,
588 "complex divmod(), // and % are deprecated") < 0)
589 return NULL;
590
Guido van Rossum96783941997-05-20 18:21:34 +0000591 errno = 0;
Guido van Rossum3be12e91996-09-12 20:56:18 +0000592 div = c_quot(v->cval,w->cval); /* The raw divisor value. */
Guido van Rossum96783941997-05-20 18:21:34 +0000593 if (errno == EDOM) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000594 PyErr_SetString(PyExc_ZeroDivisionError, "complex remainder");
Guido van Rossum3be12e91996-09-12 20:56:18 +0000595 return NULL;
596 }
597 div.real = floor(div.real); /* Use the floor of the real part. */
598 div.imag = 0.0;
599 mod = c_diff(v->cval, c_prod(w->cval, div));
600
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000601 return PyComplex_FromCComplex(mod);
Guido van Rossumee09fc11996-09-11 13:55:55 +0000602}
603
Guido van Rossumee09fc11996-09-11 13:55:55 +0000604
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000605static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000606complex_divmod(PyComplexObject *v, PyComplexObject *w)
Guido van Rossum3be12e91996-09-12 20:56:18 +0000607{
Georg Brandl96f21842008-01-19 10:18:07 +0000608 Py_complex div, mod;
Guido van Rossum3be12e91996-09-12 20:56:18 +0000609 PyObject *d, *m, *z;
Guido van Rossum9ec4c782002-04-15 01:41:56 +0000610
611 if (PyErr_Warn(PyExc_DeprecationWarning,
Guido van Rossum69cf3c72002-04-15 12:39:12 +0000612 "complex divmod(), // and % are deprecated") < 0)
Guido van Rossum9ec4c782002-04-15 01:41:56 +0000613 return NULL;
614
Guido van Rossum96783941997-05-20 18:21:34 +0000615 errno = 0;
Guido van Rossum3be12e91996-09-12 20:56:18 +0000616 div = c_quot(v->cval,w->cval); /* The raw divisor value. */
Guido van Rossum96783941997-05-20 18:21:34 +0000617 if (errno == EDOM) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000618 PyErr_SetString(PyExc_ZeroDivisionError, "complex divmod()");
Guido van Rossum3be12e91996-09-12 20:56:18 +0000619 return NULL;
620 }
621 div.real = floor(div.real); /* Use the floor of the real part. */
622 div.imag = 0.0;
623 mod = c_diff(v->cval, c_prod(w->cval, div));
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000624 d = PyComplex_FromCComplex(div);
625 m = PyComplex_FromCComplex(mod);
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000626 z = PyTuple_Pack(2, d, m);
Guido van Rossum3be12e91996-09-12 20:56:18 +0000627 Py_XDECREF(d);
628 Py_XDECREF(m);
629 return z;
630}
Guido van Rossumf9fca921996-01-12 00:47:05 +0000631
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000632static PyObject *
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +0000633complex_pow(PyObject *v, PyObject *w, PyObject *z)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000634{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000635 Py_complex p;
636 Py_complex exponent;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000637 long int_exponent;
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +0000638 Py_complex a, b;
Georg Brandl96f21842008-01-19 10:18:07 +0000639 TO_COMPLEX(v, a);
640 TO_COMPLEX(w, b);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000641
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +0000642 if (z!=Py_None) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000643 PyErr_SetString(PyExc_ValueError, "complex modulo");
Guido van Rossumf9fca921996-01-12 00:47:05 +0000644 return NULL;
645 }
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000646 PyFPE_START_PROTECT("complex_pow", return 0)
Guido van Rossum96783941997-05-20 18:21:34 +0000647 errno = 0;
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +0000648 exponent = b;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000649 int_exponent = (long)exponent.real;
650 if (exponent.imag == 0. && exponent.real == int_exponent)
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +0000651 p = c_powi(a,int_exponent);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000652 else
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +0000653 p = c_pow(a,exponent);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000654
Guido van Rossum45b83911997-03-14 04:32:50 +0000655 PyFPE_END_PROTECT(p)
Tim Petersbab22be2002-03-22 02:48:46 +0000656 Py_ADJUST_ERANGE2(p.real, p.imag);
657 if (errno == EDOM) {
658 PyErr_SetString(PyExc_ZeroDivisionError,
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000659 "0.0 to a negative or complex power");
Guido van Rossumf9fca921996-01-12 00:47:05 +0000660 return NULL;
661 }
Tim Petersbab22be2002-03-22 02:48:46 +0000662 else if (errno == ERANGE) {
663 PyErr_SetString(PyExc_OverflowError,
Neal Norwitz0593de32007-03-09 05:59:01 +0000664 "complex exponentiation");
Tim Petersbab22be2002-03-22 02:48:46 +0000665 return NULL;
666 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000667 return PyComplex_FromCComplex(p);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000668}
669
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000670static PyObject *
Guido van Rossum4668b002001-08-08 05:00:18 +0000671complex_int_div(PyComplexObject *v, PyComplexObject *w)
672{
673 PyObject *t, *r;
674
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +0000675 if (PyErr_Warn(PyExc_DeprecationWarning,
676 "complex divmod(), // and % are deprecated") < 0)
677 return NULL;
678
Guido van Rossum4668b002001-08-08 05:00:18 +0000679 t = complex_divmod(v, w);
680 if (t != NULL) {
681 r = PyTuple_GET_ITEM(t, 0);
682 Py_INCREF(r);
683 Py_DECREF(t);
684 return r;
685 }
686 return NULL;
687}
688
689static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000690complex_neg(PyComplexObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000691{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000692 Py_complex neg;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000693 neg.real = -v->cval.real;
694 neg.imag = -v->cval.imag;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000695 return PyComplex_FromCComplex(neg);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000696}
697
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000698static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000699complex_pos(PyComplexObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000700{
Tim Peters2400fa42001-09-12 19:12:49 +0000701 if (PyComplex_CheckExact(v)) {
702 Py_INCREF(v);
703 return (PyObject *)v;
704 }
705 else
706 return PyComplex_FromCComplex(v->cval);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000707}
708
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000709static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000710complex_abs(PyComplexObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000711{
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000712 double result;
Christian Heimes6f341092008-04-18 23:13:07 +0000713
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000714 PyFPE_START_PROTECT("complex_abs", return 0)
Christian Heimes6f341092008-04-18 23:13:07 +0000715 result = c_abs(v->cval);
Guido van Rossum45b83911997-03-14 04:32:50 +0000716 PyFPE_END_PROTECT(result)
Christian Heimes6f341092008-04-18 23:13:07 +0000717
718 if (errno == ERANGE) {
719 PyErr_SetString(PyExc_OverflowError,
720 "absolute value too large");
721 return NULL;
722 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000723 return PyFloat_FromDouble(result);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000724}
725
726static int
Fred Drake4288c802000-07-09 04:36:04 +0000727complex_nonzero(PyComplexObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000728{
Guido van Rossum3bbef601999-01-25 19:42:19 +0000729 return v->cval.real != 0.0 || v->cval.imag != 0.0;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000730}
731
732static int
Fred Drake4288c802000-07-09 04:36:04 +0000733complex_coerce(PyObject **pv, PyObject **pw)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000734{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000735 Py_complex cval;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000736 cval.imag = 0.;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000737 if (PyInt_Check(*pw)) {
738 cval.real = (double)PyInt_AsLong(*pw);
739 *pw = PyComplex_FromCComplex(cval);
740 Py_INCREF(*pv);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000741 return 0;
742 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000743 else if (PyLong_Check(*pw)) {
744 cval.real = PyLong_AsDouble(*pw);
Tim Peters9fffa3e2001-09-04 05:14:19 +0000745 if (cval.real == -1.0 && PyErr_Occurred())
746 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000747 *pw = PyComplex_FromCComplex(cval);
748 Py_INCREF(*pv);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000749 return 0;
750 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000751 else if (PyFloat_Check(*pw)) {
752 cval.real = PyFloat_AsDouble(*pw);
753 *pw = PyComplex_FromCComplex(cval);
754 Py_INCREF(*pv);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000755 return 0;
756 }
Guido van Rossum63805962001-09-19 01:13:10 +0000757 else if (PyComplex_Check(*pw)) {
758 Py_INCREF(*pv);
759 Py_INCREF(*pw);
760 return 0;
761 }
Guido van Rossumf9fca921996-01-12 00:47:05 +0000762 return 1; /* Can't do it */
763}
764
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000765static PyObject *
Guido van Rossumbe4cbb12001-01-18 01:12:39 +0000766complex_richcompare(PyObject *v, PyObject *w, int op)
767{
768 int c;
769 Py_complex i, j;
770 PyObject *res;
771
Guido van Rossumbe4cbb12001-01-18 01:12:39 +0000772 c = PyNumber_CoerceEx(&v, &w);
773 if (c < 0)
774 return NULL;
775 if (c > 0) {
776 Py_INCREF(Py_NotImplemented);
777 return Py_NotImplemented;
778 }
Guido van Rossum2ed6bf82001-09-27 20:30:07 +0000779 /* Make sure both arguments are complex. */
780 if (!(PyComplex_Check(v) && PyComplex_Check(w))) {
Guido van Rossumbe4cbb12001-01-18 01:12:39 +0000781 Py_DECREF(v);
782 Py_DECREF(w);
783 Py_INCREF(Py_NotImplemented);
784 return Py_NotImplemented;
785 }
786
787 i = ((PyComplexObject *)v)->cval;
788 j = ((PyComplexObject *)w)->cval;
789 Py_DECREF(v);
790 Py_DECREF(w);
791
Guido van Rossum22056422001-09-24 17:52:04 +0000792 if (op != Py_EQ && op != Py_NE) {
793 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger5d01aa42004-12-19 20:45:20 +0000794 "no ordering relation is defined for complex numbers");
Guido van Rossum22056422001-09-24 17:52:04 +0000795 return NULL;
796 }
797
Guido van Rossumbe4cbb12001-01-18 01:12:39 +0000798 if ((i.real == j.real && i.imag == j.imag) == (op == Py_EQ))
799 res = Py_True;
800 else
801 res = Py_False;
802
803 Py_INCREF(res);
804 return res;
805}
806
807static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000808complex_int(PyObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000809{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000810 PyErr_SetString(PyExc_TypeError,
Mark Dickinson50626db2009-05-17 10:38:30 +0000811 "can't convert complex to int");
Guido van Rossumd4ab3cd1996-09-11 22:54:37 +0000812 return NULL;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000813}
814
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000815static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000816complex_long(PyObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000817{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000818 PyErr_SetString(PyExc_TypeError,
Mark Dickinson50626db2009-05-17 10:38:30 +0000819 "can't convert complex to long");
Guido van Rossumd4ab3cd1996-09-11 22:54:37 +0000820 return NULL;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000821}
822
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000823static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000824complex_float(PyObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000825{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000826 PyErr_SetString(PyExc_TypeError,
Mark Dickinson50626db2009-05-17 10:38:30 +0000827 "can't convert complex to float");
Guido van Rossumd4ab3cd1996-09-11 22:54:37 +0000828 return NULL;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000829}
830
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000831static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000832complex_conjugate(PyObject *self)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000833{
Guido van Rossum926518b1996-08-19 19:30:45 +0000834 Py_complex c;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000835 c = ((PyComplexObject *)self)->cval;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000836 c.imag = -c.imag;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000837 return PyComplex_FromCComplex(c);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000838}
839
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +0000840PyDoc_STRVAR(complex_conjugate_doc,
841"complex.conjugate() -> complex\n"
842"\n"
843"Returns the complex conjugate of its argument. (3-4j).conjugate() == 3+4j.");
844
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000845static PyObject *
846complex_getnewargs(PyComplexObject *v)
847{
Alexandre Vassalotti80af6da2008-06-04 20:41:44 +0000848 Py_complex c = v->cval;
849 return Py_BuildValue("(dd)", c.real, c.imag);
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000850}
851
Eric Smith9139cc62009-04-30 00:58:58 +0000852PyDoc_STRVAR(complex__format__doc,
853"complex.__format__() -> str\n"
854"\n"
855"Converts to a string according to format_spec.");
856
857static PyObject *
858complex__format__(PyObject* self, PyObject* args)
859{
860 PyObject *format_spec;
861
862 if (!PyArg_ParseTuple(args, "O:__format__", &format_spec))
863 return NULL;
864 if (PyBytes_Check(format_spec))
865 return _PyComplex_FormatAdvanced(self,
866 PyBytes_AS_STRING(format_spec),
867 PyBytes_GET_SIZE(format_spec));
868 if (PyUnicode_Check(format_spec)) {
869 /* Convert format_spec to a str */
870 PyObject *result;
871 PyObject *str_spec = PyObject_Str(format_spec);
872
873 if (str_spec == NULL)
874 return NULL;
875
876 result = _PyComplex_FormatAdvanced(self,
877 PyBytes_AS_STRING(str_spec),
878 PyBytes_GET_SIZE(str_spec));
879
880 Py_DECREF(str_spec);
881 return result;
882 }
883 PyErr_SetString(PyExc_TypeError, "__format__ requires str or unicode");
884 return NULL;
885}
886
Christian Heimes6f341092008-04-18 23:13:07 +0000887#if 0
888static PyObject *
889complex_is_finite(PyObject *self)
890{
891 Py_complex c;
892 c = ((PyComplexObject *)self)->cval;
893 return PyBool_FromLong((long)(Py_IS_FINITE(c.real) &&
894 Py_IS_FINITE(c.imag)));
895}
896
897PyDoc_STRVAR(complex_is_finite_doc,
898"complex.is_finite() -> bool\n"
899"\n"
900"Returns True if the real and the imaginary part is finite.");
901#endif
902
Guido van Rossumf9fca921996-01-12 00:47:05 +0000903static PyMethodDef complex_methods[] = {
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +0000904 {"conjugate", (PyCFunction)complex_conjugate, METH_NOARGS,
905 complex_conjugate_doc},
Christian Heimes6f341092008-04-18 23:13:07 +0000906#if 0
907 {"is_finite", (PyCFunction)complex_is_finite, METH_NOARGS,
908 complex_is_finite_doc},
909#endif
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000910 {"__getnewargs__", (PyCFunction)complex_getnewargs, METH_NOARGS},
Eric Smith9139cc62009-04-30 00:58:58 +0000911 {"__format__", (PyCFunction)complex__format__,
912 METH_VARARGS, complex__format__doc},
Guido van Rossumf9fca921996-01-12 00:47:05 +0000913 {NULL, NULL} /* sentinel */
914};
915
Guido van Rossum6f799372001-09-20 20:46:19 +0000916static PyMemberDef complex_members[] = {
Guido van Rossumfa2e4c22002-02-08 21:26:07 +0000917 {"real", T_DOUBLE, offsetof(PyComplexObject, cval.real), READONLY,
Guido van Rossum6f799372001-09-20 20:46:19 +0000918 "the real part of a complex number"},
Guido van Rossumfa2e4c22002-02-08 21:26:07 +0000919 {"imag", T_DOUBLE, offsetof(PyComplexObject, cval.imag), READONLY,
Guido van Rossum6f799372001-09-20 20:46:19 +0000920 "the imaginary part of a complex number"},
Tim Peters6d6c1a32001-08-02 04:15:00 +0000921 {0},
922};
Guido van Rossumf9fca921996-01-12 00:47:05 +0000923
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000924static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000925complex_subtype_from_string(PyTypeObject *type, PyObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000926{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000927 const char *s, *start;
928 char *end;
929 double x=0.0, y=0.0, z;
Mark Dickinson95bc9802009-04-24 12:46:53 +0000930 int got_bracket=0;
Guido van Rossum70e36882001-10-25 18:07:22 +0000931#ifdef Py_USING_UNICODE
Mark Dickinsonc04c7c52009-10-26 22:28:14 +0000932 char *s_buffer = NULL;
Guido van Rossum70e36882001-10-25 18:07:22 +0000933#endif
Martin v. Löwis18e16552006-02-15 17:27:45 +0000934 Py_ssize_t len;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000935
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000936 if (PyString_Check(v)) {
937 s = PyString_AS_STRING(v);
938 len = PyString_GET_SIZE(v);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000939 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000940#ifdef Py_USING_UNICODE
Tim Peters6d6c1a32001-08-02 04:15:00 +0000941 else if (PyUnicode_Check(v)) {
Mark Dickinsonc04c7c52009-10-26 22:28:14 +0000942 s_buffer = (char *)PyMem_MALLOC(PyUnicode_GET_SIZE(v)+1);
943 if (s_buffer == NULL)
944 return PyErr_NoMemory();
Tim Peters6d6c1a32001-08-02 04:15:00 +0000945 if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v),
946 PyUnicode_GET_SIZE(v),
947 s_buffer,
948 NULL))
Mark Dickinsonc04c7c52009-10-26 22:28:14 +0000949 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000950 s = s_buffer;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000951 len = strlen(s);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000952 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000953#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +0000954 else if (PyObject_AsCharBuffer(v, &s, &len)) {
955 PyErr_SetString(PyExc_TypeError,
956 "complex() arg is not a string");
957 return NULL;
958 }
959
960 /* position on first nonblank */
961 start = s;
Mark Dickinson777e4ff2009-05-03 20:59:48 +0000962 while (Py_ISSPACE(*s))
Tim Peters6d6c1a32001-08-02 04:15:00 +0000963 s++;
Mark Dickinson95bc9802009-04-24 12:46:53 +0000964 if (*s == '(') {
Collin Wintere38051d2007-03-09 20:33:07 +0000965 /* Skip over possible bracket from repr(). */
966 got_bracket = 1;
967 s++;
Mark Dickinson777e4ff2009-05-03 20:59:48 +0000968 while (Py_ISSPACE(*s))
Collin Wintere38051d2007-03-09 20:33:07 +0000969 s++;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000970 }
971
Mark Dickinson95bc9802009-04-24 12:46:53 +0000972 /* a valid complex string usually takes one of the three forms:
Tim Peters6d6c1a32001-08-02 04:15:00 +0000973
Mark Dickinson95bc9802009-04-24 12:46:53 +0000974 <float> - real part only
975 <float>j - imaginary part only
976 <float><signed-float>j - real and imaginary parts
Tim Peters6d6c1a32001-08-02 04:15:00 +0000977
Mark Dickinson95bc9802009-04-24 12:46:53 +0000978 where <float> represents any numeric string that's accepted by the
979 float constructor (including 'nan', 'inf', 'infinity', etc.), and
980 <signed-float> is any string of the form <float> whose first
981 character is '+' or '-'.
Tim Peters6d6c1a32001-08-02 04:15:00 +0000982
Mark Dickinson95bc9802009-04-24 12:46:53 +0000983 For backwards compatibility, the extra forms
984
985 <float><sign>j
986 <sign>j
987 j
988
989 are also accepted, though support for these forms may be removed from
990 a future version of Python.
991 */
992
993 /* first look for forms starting with <float> */
Mark Dickinsonc04c7c52009-10-26 22:28:14 +0000994 z = PyOS_string_to_double(s, &end, NULL);
995 if (z == -1.0 && PyErr_Occurred()) {
996 if (PyErr_ExceptionMatches(PyExc_ValueError))
997 PyErr_Clear();
998 else
999 goto error;
1000 }
Mark Dickinson95bc9802009-04-24 12:46:53 +00001001 if (end != s) {
1002 /* all 4 forms starting with <float> land here */
1003 s = end;
1004 if (*s == '+' || *s == '-') {
1005 /* <float><signed-float>j | <float><sign>j */
1006 x = z;
Mark Dickinsonc04c7c52009-10-26 22:28:14 +00001007 y = PyOS_string_to_double(s, &end, NULL);
1008 if (y == -1.0 && PyErr_Occurred()) {
1009 if (PyErr_ExceptionMatches(PyExc_ValueError))
1010 PyErr_Clear();
1011 else
1012 goto error;
1013 }
Mark Dickinson95bc9802009-04-24 12:46:53 +00001014 if (end != s)
1015 /* <float><signed-float>j */
1016 s = end;
1017 else {
1018 /* <float><sign>j */
1019 y = *s == '+' ? 1.0 : -1.0;
Collin Wintere38051d2007-03-09 20:33:07 +00001020 s++;
Mark Dickinson95bc9802009-04-24 12:46:53 +00001021 }
1022 if (!(*s == 'j' || *s == 'J'))
1023 goto parse_error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001024 s++;
Mark Dickinson95bc9802009-04-24 12:46:53 +00001025 }
1026 else if (*s == 'j' || *s == 'J') {
1027 /* <float>j */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001028 s++;
Mark Dickinson95bc9802009-04-24 12:46:53 +00001029 y = z;
1030 }
1031 else
1032 /* <float> */
1033 x = z;
1034 }
1035 else {
1036 /* not starting with <float>; must be <sign>j or j */
1037 if (*s == '+' || *s == '-') {
1038 /* <sign>j */
1039 y = *s == '+' ? 1.0 : -1.0;
1040 s++;
1041 }
1042 else
1043 /* j */
1044 y = 1.0;
1045 if (!(*s == 'j' || *s == 'J'))
1046 goto parse_error;
1047 s++;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001048 }
1049
Mark Dickinson95bc9802009-04-24 12:46:53 +00001050 /* trailing whitespace and closing bracket */
Mark Dickinson777e4ff2009-05-03 20:59:48 +00001051 while (Py_ISSPACE(*s))
Mark Dickinson95bc9802009-04-24 12:46:53 +00001052 s++;
1053 if (got_bracket) {
1054 /* if there was an opening parenthesis, then the corresponding
1055 closing parenthesis should be right here */
1056 if (*s != ')')
1057 goto parse_error;
1058 s++;
Mark Dickinson777e4ff2009-05-03 20:59:48 +00001059 while (Py_ISSPACE(*s))
Mark Dickinson95bc9802009-04-24 12:46:53 +00001060 s++;
1061 }
1062
1063 /* we should now be at the end of the string */
1064 if (s-start != len)
1065 goto parse_error;
1066
Mark Dickinsonc04c7c52009-10-26 22:28:14 +00001067
1068#ifdef Py_USING_UNICODE
1069 if (s_buffer)
1070 PyMem_FREE(s_buffer);
1071#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00001072 return complex_subtype_from_doubles(type, x, y);
Mark Dickinson95bc9802009-04-24 12:46:53 +00001073
1074 parse_error:
1075 PyErr_SetString(PyExc_ValueError,
1076 "complex() arg is a malformed string");
Mark Dickinsonc04c7c52009-10-26 22:28:14 +00001077 error:
1078#ifdef Py_USING_UNICODE
1079 if (s_buffer)
1080 PyMem_FREE(s_buffer);
1081#endif
Mark Dickinson95bc9802009-04-24 12:46:53 +00001082 return NULL;
Guido van Rossumf9fca921996-01-12 00:47:05 +00001083}
1084
Tim Peters6d6c1a32001-08-02 04:15:00 +00001085static PyObject *
1086complex_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1087{
Benjamin Peterson36943662010-01-04 01:00:47 +00001088 PyObject *r, *i, *tmp;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001089 PyNumberMethods *nbr, *nbi = NULL;
1090 Py_complex cr, ci;
1091 int own_r = 0;
Guido van Rossum715ec182007-11-27 22:38:36 +00001092 int cr_is_complex = 0;
1093 int ci_is_complex = 0;
Martin v. Löwis15e62742006-02-27 16:46:16 +00001094 static char *kwlist[] = {"real", "imag", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +00001095
1096 r = Py_False;
1097 i = NULL;
1098 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO:complex", kwlist,
1099 &r, &i))
1100 return NULL;
Raymond Hettinger604cd6a2002-08-29 14:22:51 +00001101
Georg Brandl8f032cb2007-03-13 07:57:51 +00001102 /* Special-case for a single argument when type(arg) is complex. */
Guido van Rossum4eadfa22003-03-02 13:51:47 +00001103 if (PyComplex_CheckExact(r) && i == NULL &&
1104 type == &PyComplex_Type) {
Raymond Hettinger604cd6a2002-08-29 14:22:51 +00001105 /* Note that we can't know whether it's safe to return
1106 a complex *subclass* instance as-is, hence the restriction
Georg Brandl8f032cb2007-03-13 07:57:51 +00001107 to exact complexes here. If either the input or the
1108 output is a complex subclass, it will be handled below
1109 as a non-orthogonal vector. */
Raymond Hettinger604cd6a2002-08-29 14:22:51 +00001110 Py_INCREF(r);
1111 return r;
1112 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001113 if (PyString_Check(r) || PyUnicode_Check(r)) {
Fred Drake526c7a02001-12-13 19:52:22 +00001114 if (i != NULL) {
1115 PyErr_SetString(PyExc_TypeError,
1116 "complex() can't take second arg"
1117 " if first is a string");
1118 return NULL;
Georg Brandl96f21842008-01-19 10:18:07 +00001119 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001120 return complex_subtype_from_string(type, r);
Fred Drake526c7a02001-12-13 19:52:22 +00001121 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001122 if (i != NULL && (PyString_Check(i) || PyUnicode_Check(i))) {
Fred Drake526c7a02001-12-13 19:52:22 +00001123 PyErr_SetString(PyExc_TypeError,
1124 "complex() second arg can't be a string");
1125 return NULL;
1126 }
Tim Peters2400fa42001-09-12 19:12:49 +00001127
Benjamin Peterson36943662010-01-04 01:00:47 +00001128 tmp = try_complex_special_method(r);
1129 if (tmp) {
1130 r = tmp;
Raymond Hettinger478d47a2002-06-06 15:45:38 +00001131 own_r = 1;
1132 }
Benjamin Peterson36943662010-01-04 01:00:47 +00001133 else if (PyErr_Occurred()) {
1134 return NULL;
1135 }
1136
Tim Peters2400fa42001-09-12 19:12:49 +00001137 nbr = r->ob_type->tp_as_number;
1138 if (i != NULL)
1139 nbi = i->ob_type->tp_as_number;
1140 if (nbr == NULL || nbr->nb_float == NULL ||
1141 ((i != NULL) && (nbi == NULL || nbi->nb_float == NULL))) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001142 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger478d47a2002-06-06 15:45:38 +00001143 "complex() argument must be a string or a number");
Tim Peters465fa3d2003-08-15 01:16:37 +00001144 if (own_r) {
1145 Py_DECREF(r);
1146 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001147 return NULL;
1148 }
Georg Brandl8f032cb2007-03-13 07:57:51 +00001149
1150 /* If we get this far, then the "real" and "imag" parts should
1151 both be treated as numbers, and the constructor should return a
1152 complex number equal to (real + imag*1j).
1153
1154 Note that we do NOT assume the input to already be in canonical
1155 form; the "real" and "imag" parts might themselves be complex
1156 numbers, which slightly complicates the code below. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001157 if (PyComplex_Check(r)) {
Tim Peters2400fa42001-09-12 19:12:49 +00001158 /* Note that if r is of a complex subtype, we're only
1159 retaining its real & imag parts here, and the return
1160 value is (properly) of the builtin complex type. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001161 cr = ((PyComplexObject*)r)->cval;
Guido van Rossum715ec182007-11-27 22:38:36 +00001162 cr_is_complex = 1;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001163 if (own_r) {
1164 Py_DECREF(r);
1165 }
1166 }
1167 else {
Georg Brandl8f032cb2007-03-13 07:57:51 +00001168 /* The "real" part really is entirely real, and contributes
1169 nothing in the imaginary direction.
1170 Just treat it as a double. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001171 tmp = PyNumber_Float(r);
1172 if (own_r) {
Georg Brandl8f032cb2007-03-13 07:57:51 +00001173 /* r was a newly created complex number, rather
1174 than the original "real" argument. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001175 Py_DECREF(r);
1176 }
1177 if (tmp == NULL)
1178 return NULL;
1179 if (!PyFloat_Check(tmp)) {
1180 PyErr_SetString(PyExc_TypeError,
1181 "float(r) didn't return a float");
1182 Py_DECREF(tmp);
1183 return NULL;
1184 }
1185 cr.real = PyFloat_AsDouble(tmp);
Georg Brandl96f21842008-01-19 10:18:07 +00001186 cr.imag = 0.0; /* Shut up compiler warning */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001187 Py_DECREF(tmp);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001188 }
1189 if (i == NULL) {
1190 ci.real = 0.0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001191 }
Guido van Rossum715ec182007-11-27 22:38:36 +00001192 else if (PyComplex_Check(i)) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001193 ci = ((PyComplexObject*)i)->cval;
Guido van Rossum715ec182007-11-27 22:38:36 +00001194 ci_is_complex = 1;
1195 } else {
Georg Brandl8f032cb2007-03-13 07:57:51 +00001196 /* The "imag" part really is entirely imaginary, and
1197 contributes nothing in the real direction.
1198 Just treat it as a double. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001199 tmp = (*nbi->nb_float)(i);
1200 if (tmp == NULL)
1201 return NULL;
1202 ci.real = PyFloat_AsDouble(tmp);
1203 Py_DECREF(tmp);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001204 }
Georg Brandl8f032cb2007-03-13 07:57:51 +00001205 /* If the input was in canonical form, then the "real" and "imag"
Guido van Rossum715ec182007-11-27 22:38:36 +00001206 parts are real numbers, so that ci.imag and cr.imag are zero.
Georg Brandl8f032cb2007-03-13 07:57:51 +00001207 We need this correction in case they were not real numbers. */
Guido van Rossum715ec182007-11-27 22:38:36 +00001208
1209 if (ci_is_complex) {
1210 cr.real -= ci.imag;
1211 }
1212 if (cr_is_complex) {
1213 ci.real += cr.imag;
1214 }
1215 return complex_subtype_from_doubles(type, cr.real, ci.real);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001216}
1217
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001218PyDoc_STRVAR(complex_doc,
Tim Peters2400fa42001-09-12 19:12:49 +00001219"complex(real[, imag]) -> complex number\n"
1220"\n"
1221"Create a complex number from a real part and an optional imaginary part.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001222"This is equivalent to (real + imag*1j) where imag defaults to 0.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001223
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001224static PyNumberMethods complex_as_number = {
Guido van Rossumbe4cbb12001-01-18 01:12:39 +00001225 (binaryfunc)complex_add, /* nb_add */
1226 (binaryfunc)complex_sub, /* nb_subtract */
1227 (binaryfunc)complex_mul, /* nb_multiply */
Guido van Rossum393661d2001-08-31 17:40:15 +00001228 (binaryfunc)complex_classic_div, /* nb_divide */
Guido van Rossumbe4cbb12001-01-18 01:12:39 +00001229 (binaryfunc)complex_remainder, /* nb_remainder */
1230 (binaryfunc)complex_divmod, /* nb_divmod */
1231 (ternaryfunc)complex_pow, /* nb_power */
1232 (unaryfunc)complex_neg, /* nb_negative */
1233 (unaryfunc)complex_pos, /* nb_positive */
1234 (unaryfunc)complex_abs, /* nb_absolute */
1235 (inquiry)complex_nonzero, /* nb_nonzero */
1236 0, /* nb_invert */
1237 0, /* nb_lshift */
1238 0, /* nb_rshift */
1239 0, /* nb_and */
1240 0, /* nb_xor */
1241 0, /* nb_or */
Georg Brandl347b3002006-03-30 11:57:00 +00001242 complex_coerce, /* nb_coerce */
1243 complex_int, /* nb_int */
1244 complex_long, /* nb_long */
1245 complex_float, /* nb_float */
Guido van Rossumbe4cbb12001-01-18 01:12:39 +00001246 0, /* nb_oct */
1247 0, /* nb_hex */
Guido van Rossum4668b002001-08-08 05:00:18 +00001248 0, /* nb_inplace_add */
1249 0, /* nb_inplace_subtract */
1250 0, /* nb_inplace_multiply*/
1251 0, /* nb_inplace_divide */
1252 0, /* nb_inplace_remainder */
1253 0, /* nb_inplace_power */
1254 0, /* nb_inplace_lshift */
1255 0, /* nb_inplace_rshift */
1256 0, /* nb_inplace_and */
1257 0, /* nb_inplace_xor */
1258 0, /* nb_inplace_or */
1259 (binaryfunc)complex_int_div, /* nb_floor_divide */
1260 (binaryfunc)complex_div, /* nb_true_divide */
1261 0, /* nb_inplace_floor_divide */
1262 0, /* nb_inplace_true_divide */
Guido van Rossumf9fca921996-01-12 00:47:05 +00001263};
1264
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001265PyTypeObject PyComplex_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00001266 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossumf9fca921996-01-12 00:47:05 +00001267 "complex",
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001268 sizeof(PyComplexObject),
Guido van Rossumf9fca921996-01-12 00:47:05 +00001269 0,
Georg Brandl347b3002006-03-30 11:57:00 +00001270 complex_dealloc, /* tp_dealloc */
Guido van Rossumbe4cbb12001-01-18 01:12:39 +00001271 (printfunc)complex_print, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001272 0, /* tp_getattr */
Guido van Rossumbe4cbb12001-01-18 01:12:39 +00001273 0, /* tp_setattr */
1274 0, /* tp_compare */
1275 (reprfunc)complex_repr, /* tp_repr */
1276 &complex_as_number, /* tp_as_number */
1277 0, /* tp_as_sequence */
1278 0, /* tp_as_mapping */
1279 (hashfunc)complex_hash, /* tp_hash */
1280 0, /* tp_call */
Tim Peters70695122001-03-11 08:37:29 +00001281 (reprfunc)complex_str, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001282 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossumbe4cbb12001-01-18 01:12:39 +00001283 0, /* tp_setattro */
1284 0, /* tp_as_buffer */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001285 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
1286 complex_doc, /* tp_doc */
Guido van Rossumbe4cbb12001-01-18 01:12:39 +00001287 0, /* tp_traverse */
1288 0, /* tp_clear */
1289 complex_richcompare, /* tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001290 0, /* tp_weaklistoffset */
1291 0, /* tp_iter */
1292 0, /* tp_iternext */
1293 complex_methods, /* tp_methods */
1294 complex_members, /* tp_members */
1295 0, /* tp_getset */
1296 0, /* tp_base */
1297 0, /* tp_dict */
1298 0, /* tp_descr_get */
1299 0, /* tp_descr_set */
1300 0, /* tp_dictoffset */
1301 0, /* tp_init */
Georg Brandl6b50c632006-06-01 08:27:32 +00001302 PyType_GenericAlloc, /* tp_alloc */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001303 complex_new, /* tp_new */
Neil Schemenaueraa769ae2002-04-12 02:44:10 +00001304 PyObject_Del, /* tp_free */
Guido van Rossumf9fca921996-01-12 00:47:05 +00001305};
1306
1307#endif