def to_native_value(self, obj, typ): if isinstance(typ, types.Object) or typ == types.pyobject: return obj elif typ == types.boolean: istrue = self.object_istrue(obj) zero = Constant.null(istrue.type) return self.builder.icmp(lc.ICMP_NE, istrue, zero) elif typ in types.unsigned_domain: longobj = self.number_long(obj) ullval = self.long_as_ulonglong(longobj) self.decref(longobj) return self.builder.trunc(ullval, self.context.get_argument_type(typ)) elif typ in types.signed_domain: longobj = self.number_long(obj) llval = self.long_as_longlong(longobj) self.decref(longobj) return self.builder.trunc(llval, self.context.get_argument_type(typ)) elif typ == types.float32: fobj = self.number_float(obj) fval = self.float_as_double(fobj) self.decref(fobj) return self.builder.fptrunc(fval, self.context.get_argument_type(typ)) elif typ == types.float64: fobj = self.number_float(obj) fval = self.float_as_double(fobj) self.decref(fobj) return fval elif typ in (types.complex128, types.complex64): cplxcls = self.context.make_complex(types.complex128) cplx = cplxcls(self.context, self.builder) pcplx = cplx._getpointer() ok = self.complex_adaptor(obj, pcplx) failed = cgutils.is_false(self.builder, ok) with cgutils.if_unlikely(self.builder, failed): self.builder.ret(self.get_null_object()) if typ == types.complex64: c64cls = self.context.make_complex(typ) c64 = c64cls(self.context, self.builder) freal = self.context.cast(self.builder, cplx.real, types.float64, types.float32) fimag = self.context.cast(self.builder, cplx.imag, types.float64, types.float32) c64.real = freal c64.imag = fimag return c64._getvalue() else: return cplx._getvalue() elif isinstance(typ, types.NPDatetime): val = self.extract_np_datetime(obj) return val elif isinstance(typ, types.NPTimedelta): val = self.extract_np_timedelta(obj) return val elif isinstance(typ, types.Array): return self.to_native_array(typ, obj) elif isinstance(typ, types.Optional): isnone = self.builder.icmp(lc.ICMP_EQ, obj, self.borrow_none()) with cgutils.ifelse(self.builder, isnone) as (then, orelse): with then: noneval = self.context.make_optional_none(self.builder, typ.type) ret = cgutils.alloca_once(self.builder, noneval.type) self.builder.store(noneval, ret) with orelse: val = self.to_native_value(obj, typ.type) just = self.context.make_optional_value(self.builder, typ.type, val) self.builder.store(just, ret) return ret raise NotImplementedError(typ)
def to_native_value(self, obj, typ): if isinstance(typ, types.Object) or typ == types.pyobject: return obj elif typ == types.boolean: istrue = self.object_istrue(obj) zero = Constant.null(istrue.type) return self.builder.icmp(lc.ICMP_NE, istrue, zero) elif typ in types.unsigned_domain: longobj = self.number_long(obj) ullval = self.long_as_ulonglong(longobj) self.decref(longobj) return self.builder.trunc(ullval, self.context.get_argument_type(typ)) elif typ in types.signed_domain: longobj = self.number_long(obj) llval = self.long_as_longlong(longobj) self.decref(longobj) return self.builder.trunc(llval, self.context.get_argument_type(typ)) elif typ == types.float32: fobj = self.number_float(obj) fval = self.float_as_double(fobj) self.decref(fobj) return self.builder.fptrunc(fval, self.context.get_argument_type(typ)) elif typ == types.float64: fobj = self.number_float(obj) fval = self.float_as_double(fobj) self.decref(fobj) return fval elif typ in (types.complex128, types.complex64): cplxcls = self.context.make_complex(types.complex128) cplx = cplxcls(self.context, self.builder) pcplx = cplx._getpointer() ok = self.complex_adaptor(obj, pcplx) failed = cgutils.is_false(self.builder, ok) with cgutils.if_unlikely(self.builder, failed): self.builder.ret(self.get_null_object()) if typ == types.complex64: c64cls = self.context.make_complex(typ) c64 = c64cls(self.context, self.builder) freal = self.context.cast(self.builder, cplx.real, types.float64, types.float32) fimag = self.context.cast(self.builder, cplx.imag, types.float64, types.float32) c64.real = freal c64.imag = fimag return c64._getvalue() else: return cplx._getvalue() elif isinstance(typ, types.Array): return self.to_native_array(typ, obj) raise NotImplementedError(typ)
def to_native_value(self, obj, typ): builder = self.builder def c_api_error(): return cgutils.is_not_null(builder, self.err_occurred()) if isinstance(typ, types.Object) or typ == types.pyobject: return NativeValue(obj) elif typ == types.boolean: istrue = self.object_istrue(obj) zero = Constant.null(istrue.type) val = builder.icmp(lc.ICMP_NE, istrue, zero) return NativeValue(val, is_error=c_api_error()) elif isinstance(typ, types.Integer): val = self.to_native_int(obj, typ) return NativeValue(val, is_error=c_api_error()) elif typ == types.float32: fobj = self.number_float(obj) fval = self.float_as_double(fobj) self.decref(fobj) val = builder.fptrunc(fval, self.context.get_argument_type(typ)) return NativeValue(val, is_error=c_api_error()) elif typ == types.float64: fobj = self.number_float(obj) val = self.float_as_double(fobj) self.decref(fobj) return NativeValue(val, is_error=c_api_error()) elif typ in (types.complex128, types.complex64): cplxcls = self.context.make_complex(types.complex128) cplx = cplxcls(self.context, builder) pcplx = cplx._getpointer() ok = self.complex_adaptor(obj, pcplx) failed = cgutils.is_false(builder, ok) with cgutils.if_unlikely(builder, failed): self.err_set_string("PyExc_TypeError", "conversion to %s failed" % (typ,)) if typ == types.complex64: c64cls = self.context.make_complex(typ) c64 = c64cls(self.context, builder) freal = self.context.cast(builder, cplx.real, types.float64, types.float32) fimag = self.context.cast(builder, cplx.imag, types.float64, types.float32) c64.real = freal c64.imag = fimag return NativeValue(c64._getvalue(), is_error=failed) else: return NativeValue(cplx._getvalue(), is_error=failed) elif isinstance(typ, types.NPDatetime): val = self.extract_np_datetime(obj) return NativeValue(val, is_error=c_api_error()) elif isinstance(typ, types.NPTimedelta): val = self.extract_np_timedelta(obj) return NativeValue(val, is_error=c_api_error()) elif isinstance(typ, types.Record): buf = self.alloca_buffer() ptr = self.extract_record_data(obj, buf) is_error = cgutils.is_null(self.builder, ptr) ltyp = self.context.get_value_type(typ) val = builder.bitcast(ptr, ltyp) def cleanup(): self.release_buffer(buf) return NativeValue(val, cleanup=cleanup, is_error=is_error) elif isinstance(typ, types.Array): val, failed = self.to_native_array(obj, typ) return NativeValue(val, is_error=failed) elif isinstance(typ, types.Buffer): return self.to_native_buffer(obj, typ) elif isinstance(typ, types.Optional): return self.to_native_optional(obj, typ) elif isinstance(typ, (types.Tuple, types.UniTuple)): return self.to_native_tuple(obj, typ) elif isinstance(typ, types.Generator): return self.to_native_generator(obj, typ) elif isinstance(typ, types.ExternalFunctionPointer): if typ.get_pointer is not None: # Call get_pointer() on the object to get the raw pointer value ptrty = self.context.get_function_pointer_type(typ) ret = cgutils.alloca_once_value(builder, Constant.null(ptrty), name='fnptr') ser = self.serialize_object(typ.get_pointer) get_pointer = self.unserialize(ser) with cgutils.if_likely(builder, cgutils.is_not_null(builder, get_pointer)): intobj = self.call_function_objargs(get_pointer, (obj,)) self.decref(get_pointer) with cgutils.if_likely(builder, cgutils.is_not_null(builder, intobj)): ptr = self.long_as_voidptr(intobj) self.decref(intobj) builder.store(builder.bitcast(ptr, ptrty), ret) return NativeValue(builder.load(ret), is_error=c_api_error()) raise NotImplementedError("cannot convert %s to native value" % (typ,))
def to_native_value(self, obj, typ): if isinstance(typ, types.Object) or typ == types.pyobject: return obj elif typ == types.boolean: istrue = self.object_istrue(obj) zero = Constant.null(istrue.type) return self.builder.icmp(lc.ICMP_NE, istrue, zero) elif typ in types.unsigned_domain: longobj = self.number_long(obj) ullval = self.long_as_ulonglong(longobj) self.decref(longobj) return self.builder.trunc(ullval, self.context.get_argument_type(typ)) elif typ in types.signed_domain: longobj = self.number_long(obj) llval = self.long_as_longlong(longobj) self.decref(longobj) return self.builder.trunc(llval, self.context.get_argument_type(typ)) elif typ == types.float32: fobj = self.number_float(obj) fval = self.float_as_double(fobj) self.decref(fobj) return self.builder.fptrunc(fval, self.context.get_argument_type(typ)) elif typ == types.float64: fobj = self.number_float(obj) fval = self.float_as_double(fobj) self.decref(fobj) return fval elif typ in (types.complex128, types.complex64): cplxcls = self.context.make_complex(types.complex128) cplx = cplxcls(self.context, self.builder) pcplx = cplx._getpointer() ok = self.complex_adaptor(obj, pcplx) failed = cgutils.is_false(self.builder, ok) with cgutils.if_unlikely(self.builder, failed): self.builder.ret(self.get_null_object()) if typ == types.complex64: c64cls = self.context.make_complex(typ) c64 = c64cls(self.context, self.builder) freal = self.context.cast(self.builder, cplx.real, types.float64, types.float32) fimag = self.context.cast(self.builder, cplx.imag, types.float64, types.float32) c64.real = freal c64.imag = fimag return c64._getvalue() else: return cplx._getvalue() elif isinstance(typ, types.NPDatetime): val = self.extract_np_datetime(obj) return val elif isinstance(typ, types.NPTimedelta): val = self.extract_np_timedelta(obj) return val elif isinstance(typ, types.Array): return self.to_native_array(typ, obj) elif isinstance(typ, types.Optional): isnone = self.builder.icmp(lc.ICMP_EQ, obj, self.borrow_none()) with cgutils.ifelse(self.builder, isnone) as (then, orelse): with then: noneval = self.context.make_optional_none( self.builder, typ.type) ret = cgutils.alloca_once(self.builder, noneval.type) self.builder.store(noneval, ret) with orelse: val = self.to_native_value(obj, typ.type) just = self.context.make_optional_value( self.builder, typ.type, val) self.builder.store(just, ret) return ret raise NotImplementedError(typ)