Skip to content

Commit 70099a1

Browse files
committed
Removed static function complex_format, moved it into complex_repr. Modified tests to check both str and repr, which are the same for complex.
1 parent 7b1bee4 commit 70099a1

2 files changed

Lines changed: 28 additions & 26 deletions

File tree

‎Lib/test/test_complex.py‎

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -381,29 +381,33 @@ def test_abs(self):
381381
for num in nums:
382382
self.assertAlmostEqual((num.real**2 + num.imag**2) ** 0.5, abs(num))
383383

384-
def test_repr(self):
385-
self.assertEqual(repr(1+6j), '(1+6j)')
386-
self.assertEqual(repr(1-6j), '(1-6j)')
384+
def test_repr_str(self):
385+
def test(v, expected, test_fn=self.assertEqual):
386+
test_fn(repr(v), expected)
387+
test_fn(str(v), expected)
387388

388-
self.assertNotEqual(repr(-(1+0j)), '(-1+-0j)')
389+
test(1+6j, '(1+6j)')
390+
test(1-6j, '(1-6j)')
391+
392+
test(-(1+0j), '(-1+-0j)', test_fn=self.assertNotEqual)
393+
394+
test(complex(1., INF), "(1+infj)")
395+
test(complex(1., -INF), "(1-infj)")
396+
test(complex(INF, 1), "(inf+1j)")
397+
test(complex(-INF, INF), "(-inf+infj)")
398+
test(complex(NAN, 1), "(nan+1j)")
399+
test(complex(1, NAN), "(1+nanj)")
400+
test(complex(NAN, NAN), "(nan+nanj)")
401+
402+
test(complex(0, INF), "infj")
403+
test(complex(0, -INF), "-infj")
404+
test(complex(0, NAN), "nanj")
389405

390406
self.assertEqual(1-6j,complex(repr(1-6j)))
391407
self.assertEqual(1+6j,complex(repr(1+6j)))
392408
self.assertEqual(-6j,complex(repr(-6j)))
393409
self.assertEqual(6j,complex(repr(6j)))
394410

395-
self.assertEqual(repr(complex(1., INF)), "(1+infj)")
396-
self.assertEqual(repr(complex(1., -INF)), "(1-infj)")
397-
self.assertEqual(repr(complex(INF, 1)), "(inf+1j)")
398-
self.assertEqual(repr(complex(-INF, INF)), "(-inf+infj)")
399-
self.assertEqual(repr(complex(NAN, 1)), "(nan+1j)")
400-
self.assertEqual(repr(complex(1, NAN)), "(1+nanj)")
401-
self.assertEqual(repr(complex(NAN, NAN)), "(nan+nanj)")
402-
403-
self.assertEqual(repr(complex(0, INF)), "infj")
404-
self.assertEqual(repr(complex(0, -INF)), "-infj")
405-
self.assertEqual(repr(complex(0, NAN)), "nanj")
406-
407411
def test_neg(self):
408412
self.assertEqual(-(1+6j), -1-6j)
409413

‎Objects/complexobject.c‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -324,10 +324,11 @@ complex_dealloc(PyObject *op)
324324
op->ob_type->tp_free(op);
325325
}
326326

327-
328327
static PyObject *
329-
complex_format(PyComplexObject *v, int precision, char format_code)
328+
complex_repr(PyComplexObject *v)
330329
{
330+
int precision = 0;
331+
char format_code = 'r';
331332
PyObject *result = NULL;
332333
Py_ssize_t len;
333334

@@ -344,6 +345,8 @@ complex_format(PyComplexObject *v, int precision, char format_code)
344345
char *tail = "";
345346

346347
if (v->cval.real == 0. && copysign(1.0, v->cval.real)==1.0) {
348+
/* Real part is +0: just output the imaginary part and do not
349+
include parens. */
347350
re = "";
348351
im = PyOS_double_to_string(v->cval.imag, format_code,
349352
precision, 0, NULL);
@@ -352,7 +355,8 @@ complex_format(PyComplexObject *v, int precision, char format_code)
352355
goto done;
353356
}
354357
} else {
355-
/* Format imaginary part with sign, real part without */
358+
/* Format imaginary part with sign, real part without. Include
359+
parens in the result. */
356360
pre = PyOS_double_to_string(v->cval.real, format_code,
357361
precision, 0, NULL);
358362
if (!pre) {
@@ -371,7 +375,7 @@ complex_format(PyComplexObject *v, int precision, char format_code)
371375
tail = ")";
372376
}
373377
/* Alloc the final buffer. Add one for the "j" in the format string,
374-
and one for the trailing zero. */
378+
and one for the trailing zero byte. */
375379
len = strlen(lead) + strlen(re) + strlen(im) + strlen(tail) + 2;
376380
buf = PyMem_Malloc(len);
377381
if (!buf) {
@@ -388,12 +392,6 @@ complex_format(PyComplexObject *v, int precision, char format_code)
388392
return result;
389393
}
390394

391-
static PyObject *
392-
complex_repr(PyComplexObject *v)
393-
{
394-
return complex_format(v, 0, 'r');
395-
}
396-
397395
static Py_hash_t
398396
complex_hash(PyComplexObject *v)
399397
{

0 commit comments

Comments
 (0)