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 http://buildbot.pypy.org/summary/longrepr?testname=unmodified&builder=pypy-c-jit-linux-x86-64&build=6808&mod=lib-python.3.test.test_complex), 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. https://bugs.python.org/issue29894 and https://github.com/python/cpython/pull/798

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)
    @property
    def real(self):
        return 0
    @property
    def imag(self):
        return 1

CPython:

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

PyPy:

>>>> complex3(1j)
2j
>>>> complex(complex3(1j))
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