Fix DeprecationWarnings on __complex__ returning subclass

Merged Yannick Jadoul requested to merge topic/py3.7/py3.7-bpo-29894 into branch/py3.7

The changes introduced by 91a34a0db5d2 do not result the failing tests (see, as the warnings are raised at the wrong moment (when calling complex() on an instance of a subclass of complex, rather than when __complex__ returns an instance of a subclass).

Cfr. and

I'm not sure if accessing w_z.realval and w_z.imagval for these subclass instances is correct, though? But while looking into that, I bumped into this (related?) discrepancy between CPython and PyPy:

class complex3(complex):
    def __new__(self, value=0j):
        return complex.__new__(self, 2*value)
    def real(self):
        return 0
    def imag(self):
        return 1


>>> complex3(1j)
>>> complex(complex3(1j))


>>>> complex3(1j)
>>>> complex(complex3(1j))

This seems to be because CPython directly accesses the internal C representation (((PyComplexObject*)r)->cval), while PyPy goes for the real and imag fields (space.float(space.getattr(w_complex, space.newtext("real")))).

Merge request reports