Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions graalpython/com.oracle.graal.python.test/src/tests/test_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,20 @@ class SubComplex(complex):
assert result == 2 + 3j
assert type(result) is complex


def test_create_checks_arguments_before_converting():
class RaisingReal:
def __float__(self):
raise KeyError

class RaisingImag:
def __float__(self):
raise RuntimeError

assert_raises(TypeError, complex, {}, RaisingImag())
assert_raises(TypeError, complex, RaisingReal(), {})
assert_raises(KeyError, complex, RaisingReal(), RaisingImag())

class ComplexTest(unittest.TestCase):

def assertAlmostEqual(self, a, b):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -618,21 +618,17 @@ Object complexFromComplexObject(VirtualFrame frame, Object cls, Object one, Obje
@Cached.Shared("tryComplex") @Cached TryComplexSpecialMethodNode tryComplexNode,
@Cached.Shared @Cached PRaiseNode raiseNode) {
PComplex oneValue = getComplexNumberFromObject(frame, one, inliningTarget, isComplexType, tryComplexNode);
if (canBeDoubleNode.execute(inliningTarget, two)) {
double twoValue = asDoubleNode.execute(frame, inliningTarget, two);
if (oneValue == null) {
if (one == PNone.NO_VALUE) {
return createComplexNode.execute(inliningTarget, cls, 0, twoValue);
} else if (canBeDoubleNode.execute(inliningTarget, one)) {
return createComplexNode.execute(inliningTarget, cls, asDoubleNode.execute(frame, inliningTarget, one), twoValue);
} else {
throw raiseFirstArgError(one, inliningTarget, raiseNode);
}
}
return createComplexNode.execute(inliningTarget, cls, oneValue.getReal(), oneValue.getImag() + twoValue);
} else {
if (oneValue == null && one != PNone.NO_VALUE && !canBeDoubleNode.execute(inliningTarget, one)) {
throw raiseFirstArgError(one, inliningTarget, raiseNode);
}
if (!canBeDoubleNode.execute(inliningTarget, two)) {
throw raiseSecondArgError(two, inliningTarget, raiseNode);
}
if (oneValue == null) {
double oneDouble = one == PNone.NO_VALUE ? 0 : asDoubleNode.execute(frame, inliningTarget, one);
return createComplexNode.execute(inliningTarget, cls, oneDouble, asDoubleNode.execute(frame, inliningTarget, two));
}
return createComplexNode.execute(inliningTarget, cls, oneValue.getReal(), oneValue.getImag() + asDoubleNode.execute(frame, inliningTarget, two));
}

@Specialization
Expand Down