Skip to content

Commit e8b322b

Browse files
committed
Check both complex() arguments before converting either
complexFromComplexObject, the specialization taken when imag is neither a complex nor a string, called imag's __float__ before it looked at real, so an exception from that conversion replaced the first-argument TypeError and complex(Real(), Imag()) converted imag first. CPython's complex_new_impl checks the type of real, then of imag, and only then converts real followed by imag. The specialization now does the same; the (Object, long/double/PInt) specializations already check real first. Signed-off-by: Jeongseop Lim <jeongseop_lim@korea.ac.kr>
1 parent 7e7182b commit e8b322b

2 files changed

Lines changed: 23 additions & 13 deletions

File tree

‎graalpython/com.oracle.graal.python.test/src/tests/test_complex.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,20 @@ class SubComplex(complex):
304304
assert result == 2 + 3j
305305
assert type(result) is complex
306306

307+
308+
def test_create_checks_arguments_before_converting():
309+
class RaisingReal:
310+
def __float__(self):
311+
raise KeyError
312+
313+
class RaisingImag:
314+
def __float__(self):
315+
raise RuntimeError
316+
317+
assert_raises(TypeError, complex, {}, RaisingImag())
318+
assert_raises(TypeError, complex, RaisingReal(), {})
319+
assert_raises(KeyError, complex, RaisingReal(), RaisingImag())
320+
307321
class ComplexTest(unittest.TestCase):
308322

309323
def assertAlmostEqual(self, a, b):

‎graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/complex/ComplexBuiltins.java‎

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -618,21 +618,17 @@ Object complexFromComplexObject(VirtualFrame frame, Object cls, Object one, Obje
618618
@Cached.Shared("tryComplex") @Cached TryComplexSpecialMethodNode tryComplexNode,
619619
@Cached.Shared @Cached PRaiseNode raiseNode) {
620620
PComplex oneValue = getComplexNumberFromObject(frame, one, inliningTarget, isComplexType, tryComplexNode);
621-
if (canBeDoubleNode.execute(inliningTarget, two)) {
622-
double twoValue = asDoubleNode.execute(frame, inliningTarget, two);
623-
if (oneValue == null) {
624-
if (one == PNone.NO_VALUE) {
625-
return createComplexNode.execute(inliningTarget, cls, 0, twoValue);
626-
} else if (canBeDoubleNode.execute(inliningTarget, one)) {
627-
return createComplexNode.execute(inliningTarget, cls, asDoubleNode.execute(frame, inliningTarget, one), twoValue);
628-
} else {
629-
throw raiseFirstArgError(one, inliningTarget, raiseNode);
630-
}
631-
}
632-
return createComplexNode.execute(inliningTarget, cls, oneValue.getReal(), oneValue.getImag() + twoValue);
633-
} else {
621+
if (oneValue == null && one != PNone.NO_VALUE && !canBeDoubleNode.execute(inliningTarget, one)) {
622+
throw raiseFirstArgError(one, inliningTarget, raiseNode);
623+
}
624+
if (!canBeDoubleNode.execute(inliningTarget, two)) {
634625
throw raiseSecondArgError(two, inliningTarget, raiseNode);
635626
}
627+
if (oneValue == null) {
628+
double oneDouble = one == PNone.NO_VALUE ? 0 : asDoubleNode.execute(frame, inliningTarget, one);
629+
return createComplexNode.execute(inliningTarget, cls, oneDouble, asDoubleNode.execute(frame, inliningTarget, two));
630+
}
631+
return createComplexNode.execute(inliningTarget, cls, oneValue.getReal(), oneValue.getImag() + asDoubleNode.execute(frame, inliningTarget, two));
636632
}
637633

638634
@Specialization

0 commit comments

Comments
 (0)