8000 * complex_new_impl: L944, L951, L958, L965, L985, L994 · python/cpython@c25563c · GitHub
[go: up one dir, main page]

Skip to content

Commit c25563c

Browse files
committed
* complex_new_impl: L944, L951, L958, L965, L985, L994
1 parent 72b349d commit c25563c

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

Lib/test/test_complex.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,8 @@ def test_conjugate(self):
338338
self.assertClose(complex(5.3, 9.8).conjugate(), 5.3-9.8j)
339339

340340
def test_constructor(self):
341+
from test.test_capi.test_getargs import Complex
342+
341343
class NS:
342344
def __init__(self, value): self.value = value
343345
def __complex__(self): return self.value
@@ -346,6 +348,8 @@ def __complex__(self): return self.value
346348
self.assertRaises(TypeError, complex, {})
347349
self.assertRaises(TypeError, complex, NS(1.5))
348350
self.assertRaises(TypeError, complex, NS(1))
351+
self.assertRaises(TypeError, complex, object())
352+
self.assertRaises(TypeError, complex, Complex(), object())
349353

350354
self.assertAlmostEqual(complex("1+10j"), 1+10j)
351355
self.assertAlmostEqual(complex(10), 10+0j)

Objects/complexobject.c

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -940,9 +940,7 @@ complex_new_impl(PyTypeObject *type, PyObject *r, PyObject *i)
940940
"complex() first argument must be a string or a number, "
941941
"not '%.200s'",
942942
Py_TYPE(r)->tp_name);
943-
if (own_r) {
944-
Py_DECREF(r);
945-
}
943+
assert(!own_r);
946944
return NULL;
947945
}
948946
if (i != NULL) {
@@ -974,20 +972,17 @@ complex_new_impl(PyTypeObject *type, PyObject *r, PyObject *i)
974972
value is (properly) of the builtin complex type. */
975973
cr = ((PyComplexObject*)r)->cval;
976974
cr_is_complex = 1;
977-
if (own_r) {
978-
Py_DECREF(r);
979-
}
975+
assert(own_r);
976+
Py_DECREF(r);
980977
}
981978
else {
982979
/* The "real" part really is entirely real, and contributes
983980
nothing in the imaginary direction.
984981
Just treat it as a double. */
985982
tmp = PyNumber_Float(r);
986-
if (own_r) {
987-
/* r was a newly created complex number, rather
988-
than the original "real" argument. */
989-
Py_DECREF(r);
990-
}
983+
/* r was a newly created complex number, rather
984+
than the original "real" argument. */
985+
assert(!own_r);
991986
if (tmp == NULL)
992987
return NULL;
993988
assert(PyFloat_Check(tmp));

0 commit comments

Comments
 (0)
0