'Numpy uses cast semantics that are different from standard Python
(for example, it does allow casting from complex to float).
This method acts as a patch to context.cast so that it allows
complex to real/int casts.'

def cast(self, val, fromty, toty):

    if (isinstance(fromty, types.Complex) and (not isinstance(toty, types.Complex))):
        newty = fromty.underlying_float
        attr = self.context.get_getattr(fromty, 'real')
        val = attr(self.context, self.builder, fromty, val, 'real')
        fromty = newty
    return self.context.cast(self.builder, val, fromty, toty)
