| 1 |
""" |
| 2 |
Primitives. |
| 3 |
""" |
| 4 |
|
| 5 |
import sys |
| 6 |
|
| 7 |
from rpython.rlib.rarithmetic import r_uint, r_ulonglong, intmask |
| 8 |
from rpython.rlib import jit, rutf8 |
| 9 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 10 |
from rpython.rtyper.tool import rfficache |
| 11 |
|
| 12 |
from pypy.interpreter.error import oefmt |
| 13 |
from pypy.module._cffi_backend import cdataobj, misc, wchar_helper |
| 14 |
from pypy.module._cffi_backend.ctypeobj import W_CType |
| 15 |
|
| 16 |
|
| 17 |
class W_CTypePrimitive(W_CType): |
| 18 |
_attrs_ = ['align'] |
| 19 |
_immutable_fields_ = ['align'] |
| 20 |
kind = "primitive" |
| 21 |
|
| 22 |
def __init__(self, space, size, name, name_position, align): |
| 23 |
W_CType.__init__(self, space, size, name, name_position) |
| 24 |
self.align = align |
| 25 |
|
| 26 |
def extra_repr(self, cdata): |
| 27 |
w_ob = self.convert_to_object(cdata) |
| 28 |
return self.space.text_w(self.space.repr(w_ob)) |
| 29 |
|
| 30 |
def _alignof(self): |
| 31 |
return self.align |
| 32 |
|
| 33 |
def cast_str(self, w_ob): |
| 34 |
space = self.space |
| 35 |
s = space.bytes_w(w_ob) |
| 36 |
if len(s) != 1: |
| 37 |
raise oefmt(space.w_TypeError, |
| 38 |
"cannot cast string of length %d to ctype '%s'", |
| 39 |
len(s), self.name) |
| 40 |
return ord(s[0]) |
| 41 |
|
| 42 |
def cast_unicode(self, w_ob): |
| 43 |
space = self.space |
| 44 |
w_u = space.convert_arg_to_w_unicode(w_ob) |
| 45 |
if w_u._len() != 1: |
| 46 |
raise oefmt(space.w_TypeError, |
| 47 |
"cannot cast unicode string of length %d to ctype '%s'", |
| 48 |
w_u._len(), self.name) |
| 49 |
return rutf8.codepoint_at_pos(w_u._utf8, 0) |
| 50 |
|
| 51 |
def cast(self, w_ob): |
| 52 |
from pypy.module._cffi_backend import ctypeptr, wrapper |
| 53 |
space = self.space |
| 54 |
if (isinstance(w_ob, cdataobj.W_CData) and |
| 55 |
isinstance(w_ob.ctype, ctypeptr.W_CTypePtrOrArray)): |
| 56 |
ptr = w_ob.unsafe_escaping_ptr() |
| 57 |
value = rffi.cast(lltype.Signed, ptr) |
| 58 |
value = self._cast_result(value) |
| 59 |
elif space.isinstance_w(w_ob, space.w_bytes): |
| 60 |
value = self.cast_str(w_ob) |
| 61 |
value = self._cast_result(value) |
| 62 |
elif space.isinstance_w(w_ob, space.w_unicode): |
| 63 |
value = self.cast_unicode(w_ob) |
| 64 |
value = self._cast_result(value) |
| 65 |
elif (isinstance(w_ob, wrapper.W_FunctionWrapper) |
| 66 |
and w_ob.directfnptr): |
| 67 |
value = rffi.cast(lltype.Signed, w_ob.directfnptr) |
| 68 |
value = self._cast_result(value) |
| 69 |
else: |
| 70 |
value = self._cast_generic(w_ob) |
| 71 |
w_cdata = cdataobj.W_CDataMem(space, self) |
| 72 |
self.write_raw_integer_data(w_cdata, value) |
| 73 |
return w_cdata |
| 74 |
|
| 75 |
def _cast_result(self, intvalue): |
| 76 |
return r_ulonglong(intvalue) |
| 77 |
|
| 78 |
def _cast_generic(self, w_ob): |
| 79 |
return misc.as_unsigned_long_long(self.space, w_ob, strict=False) |
| 80 |
|
| 81 |
def _overflow(self, w_ob): |
| 82 |
space = self.space |
| 83 |
s = space.text_w(space.str(w_ob)) |
| 84 |
raise oefmt(space.w_OverflowError, |
| 85 |
"integer %s does not fit '%s'", s, self.name) |
| 86 |
|
| 87 |
def string(self, cdataobj, maxlen): |
| 88 |
if self.size == 1: |
| 89 |
with cdataobj as ptr: |
| 90 |
s = ptr[0] |
| 91 |
return self.space.newbytes(s) |
| 92 |
return W_CType.string(self, cdataobj, maxlen) |
| 93 |
|
| 94 |
def unpack_ptr(self, w_ctypeptr, ptr, length): |
| 95 |
result = self.unpack_list_of_int_items(ptr, length) |
| 96 |
if result is not None: |
| 97 |
return self.space.newlist_int(result) |
| 98 |
return W_CType.unpack_ptr(self, w_ctypeptr, ptr, length) |
| 99 |
|
| 100 |
def nonzero(self, cdata): |
| 101 |
if self.size <= rffi.sizeof(lltype.Signed): |
| 102 |
value = misc.read_raw_long_data(cdata, self.size) |
| 103 |
return value != 0 |
| 104 |
else: |
| 105 |
return self._nonzero_longlong(cdata) |
| 106 |
|
| 107 |
def _nonzero_longlong(self, cdata): |
| 108 |
|
| 109 |
value = misc.read_raw_signed_data(cdata, self.size) |
| 110 |
return bool(value) |
| 111 |
|
| 112 |
|
| 113 |
class W_CTypePrimitiveCharOrUniChar(W_CTypePrimitive): |
| 114 |
_attrs_ = [] |
| 115 |
is_primitive_integer = True |
| 116 |
|
| 117 |
def get_vararg_type(self): |
| 118 |
from pypy.module._cffi_backend import newtype |
| 119 |
return newtype.new_primitive_type(self.space, "int") |
| 120 |
|
| 121 |
def write_raw_integer_data(self, w_cdata, value): |
| 122 |
w_cdata.write_raw_unsigned_data(value) |
| 123 |
|
| 124 |
|
| 125 |
class W_CTypePrimitiveChar(W_CTypePrimitiveCharOrUniChar): |
| 126 |
_attrs_ = [] |
| 127 |
|
| 128 |
def cast_to_int(self, cdata): |
| 129 |
return self.space.newint(ord(cdata[0])) |
| 130 |
|
| 131 |
def convert_to_object(self, cdata): |
| 132 |
return self.space.newbytes(cdata[0]) |
| 133 |
|
| 134 |
def _convert_to_char(self, w_ob): |
| 135 |
space = self.space |
| 136 |
if space.isinstance_w(w_ob, space.w_bytes): |
| 137 |
s = space.bytes_w(w_ob) |
| 138 |
if len(s) == 1: |
| 139 |
return s[0] |
| 140 |
if (isinstance(w_ob, cdataobj.W_CData) and |
| 141 |
isinstance(w_ob.ctype, W_CTypePrimitiveChar)): |
| 142 |
with w_ob as ptr: |
| 143 |
return ptr[0] |
| 144 |
raise self._convert_error("string of length 1", w_ob) |
| 145 |
|
| 146 |
def convert_from_object(self, cdata, w_ob): |
| 147 |
value = self._convert_to_char(w_ob) |
| 148 |
cdata[0] = value |
| 149 |
|
| 150 |
def unpack_ptr(self, w_ctypeptr, ptr, length): |
| 151 |
s = rffi.charpsize2str(ptr, length) |
| 152 |
return self.space.newbytes(s) |
| 153 |
|
| 154 |
|
| 155 |
class W_CTypePrimitiveUniChar(W_CTypePrimitiveCharOrUniChar): |
| 156 |
_attrs_ = ['is_signed_wchar'] |
| 157 |
_immutable_fields_ = ['is_signed_wchar'] |
| 158 |
|
| 159 |
_wchar_is_signed = rfficache.signof_c_type('wchar_t') |
| 160 |
|
| 161 |
def __init__(self, space, size, name, name_position, align): |
| 162 |
W_CTypePrimitiveCharOrUniChar.__init__(self, space, size, name, |
| 163 |
name_position, align) |
| 164 |
self.is_signed_wchar = self._wchar_is_signed and (name == "wchar_t") |
| 165 |
|
| 166 |
|
| 167 |
def cast_to_int(self, cdata): |
| 168 |
if self.is_signed_wchar: |
| 169 |
value = misc.read_raw_long_data(cdata, self.size) |
| 170 |
return self.space.newint(value) |
| 171 |
else: |
| 172 |
value = misc.read_raw_ulong_data(cdata, self.size) |
| 173 |
if self.size < rffi.sizeof(lltype.Signed): |
| 174 |
return self.space.newint(intmask(value)) |
| 175 |
else: |
| 176 |
return self.space.newint(value) |
| 177 |
|
| 178 |
def convert_to_object(self, cdata): |
| 179 |
value = misc.read_raw_ulong_data(cdata, self.size) |
| 180 |
try: |
| 181 |
utf8 = rutf8.unichr_as_utf8(value, allow_surrogates=True) |
| 182 |
except rutf8.OutOfRange: |
| 183 |
if self.is_signed_wchar: |
| 184 |
s = hex(intmask(value)) |
| 185 |
else: |
| 186 |
s = hex(value) |
| 187 |
raise oefmt(self.space.w_ValueError, |
| 188 |
"%s out of range for conversion to unicode: %s", |
| 189 |
self.name, s) |
| 190 |
return self.space.newutf8(utf8, 1) |
| 191 |
|
| 192 |
def string(self, cdataobj, maxlen): |
| 193 |
with cdataobj as ptr: |
| 194 |
w_res = self.convert_to_object(ptr) |
| 195 |
return w_res |
| 196 |
|
| 197 |
def _convert_to_charN_t(self, w_ob): |
| 198 |
|
| 199 |
space = self.space |
| 200 |
if space.isinstance_w(w_ob, space.w_unicode): |
| 201 |
w_u = space.convert_arg_to_w_unicode(w_ob) |
| 202 |
if w_u._len() != 1: |
| 203 |
raise self._convert_error("single character", w_ob) |
| 204 |
ordinal = rutf8.codepoint_at_pos(w_u._utf8, 0) |
| 205 |
if self.size == 2 and ordinal > 0xFFFF: |
| 206 |
raise self._convert_error("single character <= 0xFFFF", w_ob) |
| 207 |
return r_uint(ordinal) |
| 208 |
elif (isinstance(w_ob, cdataobj.W_CData) and |
| 209 |
isinstance(w_ob.ctype, W_CTypePrimitiveUniChar) and |
| 210 |
w_ob.ctype.size == self.size): |
| 211 |
with w_ob as ptr: |
| 212 |
return misc.read_raw_ulong_data(ptr, self.size) |
| 213 |
raise self._convert_error("unicode string of length 1", w_ob) |
| 214 |
|
| 215 |
def convert_from_object(self, cdata, w_ob): |
| 216 |
ordinal = self._convert_to_charN_t(w_ob) |
| 217 |
misc.write_raw_unsigned_data(cdata, ordinal, self.size) |
| 218 |
|
| 219 |
def unpack_ptr(self, w_ctypeptr, ptr, length): |
| 220 |
if self.size == 2: |
| 221 |
utf8, lgt = wchar_helper.utf8_from_char16(ptr, length) |
| 222 |
else: |
| 223 |
try: |
| 224 |
utf8, lgt = wchar_helper.utf8_from_char32(ptr, length) |
| 225 |
except wchar_helper.OutOfRange as e: |
| 226 |
raise oefmt(self.space.w_ValueError, |
| 227 |
"%s out of range for conversion to unicode: %s", |
| 228 |
self.name, hex(e.ordinal)) |
| 229 |
assert lgt >= 0 |
| 230 |
return self.space.newutf8(utf8, lgt) |
| 231 |
|
| 232 |
|
| 233 |
class W_CTypePrimitiveSigned(W_CTypePrimitive): |
| 234 |
_attrs_ = ['value_fits_long', 'value_smaller_than_long'] |
| 235 |
_immutable_fields_ = ['value_fits_long', 'value_smaller_than_long'] |
| 236 |
is_primitive_integer = True |
| 237 |
|
| 238 |
def __init__(self, *args): |
| 239 |
W_CTypePrimitive.__init__(self, *args) |
| 240 |
self.value_fits_long = self.size <= rffi.sizeof(lltype.Signed) |
| 241 |
self.value_smaller_than_long = self.size < rffi.sizeof(lltype.Signed) |
| 242 |
|
| 243 |
def cast_to_int(self, cdata): |
| 244 |
return self.convert_to_object(cdata) |
| 245 |
|
| 246 |
def convert_to_object(self, cdata): |
| 247 |
if self.value_fits_long: |
| 248 |
value = misc.read_raw_long_data(cdata, self.size) |
| 249 |
return self.space.newint(value) |
| 250 |
else: |
| 251 |
return self._convert_to_object_longlong(cdata) |
| 252 |
|
| 253 |
def _convert_to_object_longlong(self, cdata): |
| 254 |
|
| 255 |
value = misc.read_raw_signed_data(cdata, self.size) |
| 256 |
return self.space.newint(value) |
| 257 |
|
| 258 |
def convert_from_object(self, cdata, w_ob): |
| 259 |
if self.value_fits_long: |
| 260 |
value = misc.as_long(self.space, w_ob) |
| 261 |
if self.value_smaller_than_long: |
| 262 |
if value != misc.signext(value, self.size): |
| 263 |
self._overflow(w_ob) |
| 264 |
misc.write_raw_signed_data(cdata, value, self.size) |
| 265 |
else: |
| 266 |
self._convert_from_object_longlong(cdata, w_ob) |
| 267 |
|
| 268 |
def _convert_from_object_longlong(self, cdata, w_ob): |
| 269 |
|
| 270 |
value = misc.as_long_long(self.space, w_ob) |
| 271 |
misc.write_raw_signed_data(cdata, value, self.size) |
| 272 |
|
| 273 |
def get_vararg_type(self): |
| 274 |
if self.size < rffi.sizeof(rffi.INT): |
| 275 |
from pypy.module._cffi_backend import newtype |
| 276 |
return newtype.new_primitive_type(self.space, "int") |
| 277 |
return self |
| 278 |
|
| 279 |
def write_raw_integer_data(self, w_cdata, value): |
| 280 |
w_cdata.write_raw_signed_data(value) |
| 281 |
|
| 282 |
def unpack_list_of_int_items(self, ptr, length): |
| 283 |
if self.size == rffi.sizeof(rffi.SIGNED): |
| 284 |
from rpython.rlib.rrawarray import populate_list_from_raw_array |
| 285 |
res = [] |
| 286 |
buf = rffi.cast(rffi.SIGNEDP, ptr) |
| 287 |
populate_list_from_raw_array(res, buf, length) |
| 288 |
return res |
| 289 |
elif self.value_smaller_than_long: |
| 290 |
res = [0] * length |
| 291 |
misc.unpack_list_from_raw_array(res, ptr, self.size) |
| 292 |
return res |
| 293 |
return None |
| 294 |
|
| 295 |
def pack_list_of_items(self, cdata, w_ob, expected_length): |
| 296 |
int_list = self.space.listview_int(w_ob) |
| 297 |
if (int_list is not None and |
| 298 |
self._within_bounds(len(int_list), expected_length)): |
| 299 |
if self.size == rffi.sizeof(rffi.SIGNED): |
| 300 |
from rpython.rlib.rrawarray import copy_list_to_raw_array |
| 301 |
cdata = rffi.cast(rffi.SIGNEDP, cdata) |
| 302 |
copy_list_to_raw_array(int_list, cdata) |
| 303 |
else: |
| 304 |
overflowed = misc.pack_list_to_raw_array_bounds_signed( |
| 305 |
int_list, cdata, self.size) |
| 306 |
if overflowed != 0: |
| 307 |
self._overflow(self.space.newint(overflowed)) |
| 308 |
return True |
| 309 |
return W_CTypePrimitive.pack_list_of_items(self, cdata, w_ob, |
| 310 |
expected_length) |
| 311 |
|
| 312 |
|
| 313 |
class W_CTypePrimitiveUnsigned(W_CTypePrimitive): |
| 314 |
_attrs_ = ['value_fits_long', 'value_fits_ulong', 'vrangemax'] |
| 315 |
_immutable_fields_ = ['value_fits_long', 'value_fits_ulong', 'vrangemax'] |
| 316 |
is_primitive_integer = True |
| 317 |
|
| 318 |
def __init__(self, *args): |
| 319 |
W_CTypePrimitive.__init__(self, *args) |
| 320 |
self.value_fits_long = self.size < rffi.sizeof(lltype.Signed) |
| 321 |
self.value_fits_ulong = self.size <= rffi.sizeof(lltype.Unsigned) |
| 322 |
if self.value_fits_long: |
| 323 |
self.vrangemax = self._compute_vrange_max() |
| 324 |
else: |
| 325 |
self.vrangemax = r_uint(sys.maxint) |
| 326 |
|
| 327 |
def _compute_vrange_max(self): |
| 328 |
sh = self.size * 8 |
| 329 |
return (r_uint(1) << sh) - 1 |
| 330 |
|
| 331 |
def cast_to_int(self, cdata): |
| 332 |
return self.convert_to_object(cdata) |
| 333 |
|
| 334 |
def convert_from_object(self, cdata, w_ob): |
| 335 |
if self.value_fits_ulong: |
| 336 |
value = misc.as_unsigned_long(self.space, w_ob, strict=True) |
| 337 |
if self.value_fits_long: |
| 338 |
if value > self.vrangemax: |
| 339 |
self._overflow(w_ob) |
| 340 |
misc.write_raw_unsigned_data(cdata, value, self.size) |
| 341 |
else: |
| 342 |
self._convert_from_object_longlong(cdata, w_ob) |
| 343 |
|
| 344 |
def _convert_from_object_longlong(self, cdata, w_ob): |
| 345 |
|
| 346 |
value = misc.as_unsigned_long_long(self.space, w_ob, strict=True) |
| 347 |
misc.write_raw_unsigned_data(cdata, value, self.size) |
| 348 |
|
| 349 |
def convert_to_object(self, cdata): |
| 350 |
if self.value_fits_ulong: |
| 351 |
value = misc.read_raw_ulong_data(cdata, self.size) |
| 352 |
if self.value_fits_long: |
| 353 |
return self.space.newint(intmask(value)) |
| 354 |
else: |
| 355 |
return self.space.newint(value) |
| 356 |
else: |
| 357 |
return self._convert_to_object_longlong(cdata) |
| 358 |
|
| 359 |
def _convert_to_object_longlong(self, cdata): |
| 360 |
|
| 361 |
value = misc.read_raw_unsigned_data(cdata, self.size) |
| 362 |
return self.space.newint(value) |
| 363 |
|
| 364 |
def get_vararg_type(self): |
| 365 |
if self.size < rffi.sizeof(rffi.INT): |
| 366 |
from pypy.module._cffi_backend import newtype |
| 367 |
return newtype.new_primitive_type(self.space, "int") |
| 368 |
return self |
| 369 |
|
| 370 |
def write_raw_integer_data(self, w_cdata, value): |
| 371 |
w_cdata.write_raw_unsigned_data(value) |
| 372 |
|
| 373 |
def unpack_list_of_int_items(self, ptr, length): |
| 374 |
if self.value_fits_long: |
| 375 |
res = [0] * length |
| 376 |
misc.unpack_unsigned_list_from_raw_array(res, ptr, self.size) |
| 377 |
return res |
| 378 |
return None |
| 379 |
|
| 380 |
def pack_list_of_items(self, cdata, w_ob, expected_length): |
| 381 |
int_list = self.space.listview_int(w_ob) |
| 382 |
if (int_list is not None and |
| 383 |
self._within_bounds(len(int_list), expected_length)): |
| 384 |
overflowed = misc.pack_list_to_raw_array_bounds_unsigned( |
| 385 |
int_list, cdata, self.size, self.vrangemax) |
| 386 |
if overflowed != 0: |
| 387 |
self._overflow(self.space.newint(overflowed)) |
| 388 |
return True |
| 389 |
return W_CTypePrimitive.pack_list_of_items(self, cdata, w_ob, |
| 390 |
expected_length) |
| 391 |
|
| 392 |
|
| 393 |
class W_CTypePrimitiveBool(W_CTypePrimitiveUnsigned): |
| 394 |
_attrs_ = [] |
| 395 |
|
| 396 |
def _compute_vrange_max(self): |
| 397 |
return r_uint(1) |
| 398 |
|
| 399 |
def _cast_result(self, intvalue): |
| 400 |
return r_ulonglong(intvalue != 0) |
| 401 |
|
| 402 |
def _cast_generic(self, w_ob): |
| 403 |
return misc.object_as_bool(self.space, w_ob) |
| 404 |
|
| 405 |
def string(self, cdataobj, maxlen): |
| 406 |
|
| 407 |
return W_CType.string(self, cdataobj, maxlen) |
| 408 |
|
| 409 |
def _read_bool_0_or_1(self, cdata): |
| 410 |
"""Read one byte, check it is 0 or 1, but return it as an integer""" |
| 411 |
value = ord(cdata[0]) |
| 412 |
if value >= 2: |
| 413 |
raise oefmt(self.space.w_ValueError, |
| 414 |
"got a _Bool of value %d, expected 0 or 1", |
| 415 |
value) |
| 416 |
return value |
| 417 |
|
| 418 |
def convert_to_object(self, cdata): |
| 419 |
value = self._read_bool_0_or_1(cdata) |
| 420 |
return self.space.newbool(value != 0) |
| 421 |
|
| 422 |
def cast_to_int(self, cdata): |
| 423 |
value = self._read_bool_0_or_1(cdata) |
| 424 |
return self.space.newint(value) |
| 425 |
|
| 426 |
def unpack_list_of_int_items(self, ptr, length): |
| 427 |
return None |
| 428 |
|
| 429 |
|
| 430 |
class W_CTypePrimitiveFloat(W_CTypePrimitive): |
| 431 |
_attrs_ = [] |
| 432 |
|
| 433 |
def cast(self, w_ob): |
| 434 |
space = self.space |
| 435 |
if isinstance(w_ob, cdataobj.W_CData): |
| 436 |
if not isinstance(w_ob.ctype, W_CTypePrimitive): |
| 437 |
raise oefmt(space.w_TypeError, |
| 438 |
"cannot cast ctype '%s' to ctype '%s'", |
| 439 |
w_ob.ctype.name, self.name) |
| 440 |
w_ob = w_ob.convert_to_object() |
| 441 |
|
| 442 |
if space.isinstance_w(w_ob, space.w_bytes): |
| 443 |
value = self.cast_str(w_ob) |
| 444 |
elif space.isinstance_w(w_ob, space.w_unicode): |
| 445 |
value = self.cast_unicode(w_ob) |
| 446 |
else: |
| 447 |
value = space.float_w(w_ob) |
| 448 |
w_cdata = cdataobj.W_CDataMem(space, self) |
| 449 |
if not isinstance(self, W_CTypePrimitiveLongDouble): |
| 450 |
w_cdata.write_raw_float_data(value) |
| 451 |
else: |
| 452 |
with w_cdata as ptr: |
| 453 |
self._to_longdouble_and_write(value, ptr) |
| 454 |
return w_cdata |
| 455 |
|
| 456 |
def cast_to_int(self, cdata): |
| 457 |
w_value = self.float(cdata) |
| 458 |
return self.space.int(w_value) |
| 459 |
|
| 460 |
def float(self, cdata): |
| 461 |
return self.convert_to_object(cdata) |
| 462 |
|
| 463 |
def convert_to_object(self, cdata): |
| 464 |
value = misc.read_raw_float_data(cdata, self.size) |
| 465 |
return self.space.newfloat(value) |
| 466 |
|
| 467 |
def convert_from_object(self, cdata, w_ob): |
| 468 |
space = self.space |
| 469 |
value = space.float_w(space.float(w_ob)) |
| 470 |
misc.write_raw_float_data(cdata, value, self.size) |
| 471 |
|
| 472 |
def unpack_list_of_float_items(self, ptr, length): |
| 473 |
if self.size == rffi.sizeof(rffi.DOUBLE): |
| 474 |
from rpython.rlib.rrawarray import populate_list_from_raw_array |
| 475 |
res = [] |
| 476 |
buf = rffi.cast(rffi.DOUBLEP, ptr) |
| 477 |
populate_list_from_raw_array(res, buf, length) |
| 478 |
return res |
| 479 |
elif self.size == rffi.sizeof(rffi.FLOAT): |
| 480 |
res = [0.0] * length |
| 481 |
misc.unpack_cfloat_list_from_raw_array(res, ptr) |
| 482 |
return res |
| 483 |
return None |
| 484 |
|
| 485 |
def pack_list_of_items(self, cdata, w_ob, expected_length): |
| 486 |
float_list = self.space.listview_float(w_ob) |
| 487 |
if (float_list is not None and |
| 488 |
self._within_bounds(len(float_list), expected_length)): |
| 489 |
if self.size == rffi.sizeof(rffi.DOUBLE): |
| 490 |
from rpython.rlib.rrawarray import copy_list_to_raw_array |
| 491 |
cdata = rffi.cast(rffi.DOUBLEP, cdata) |
| 492 |
copy_list_to_raw_array(float_list, cdata) |
| 493 |
return True |
| 494 |
elif self.size == rffi.sizeof(rffi.FLOAT): |
| 495 |
misc.pack_float_list_to_raw_array(float_list, cdata, |
| 496 |
rffi.FLOAT, rffi.FLOATP) |
| 497 |
return True |
| 498 |
return W_CTypePrimitive.pack_list_of_items(self, cdata, w_ob, |
| 499 |
expected_length) |
| 500 |
|
| 501 |
def unpack_ptr(self, w_ctypeptr, ptr, length): |
| 502 |
result = self.unpack_list_of_float_items(ptr, length) |
| 503 |
if result is not None: |
| 504 |
return self.space.newlist_float(result) |
| 505 |
return W_CType.unpack_ptr(self, w_ctypeptr, ptr, length) |
| 506 |
|
| 507 |
def nonzero(self, cdata): |
| 508 |
return misc.is_nonnull_float(cdata, self.size) |
| 509 |
|
| 510 |
|
| 511 |
class W_CTypePrimitiveLongDouble(W_CTypePrimitiveFloat): |
| 512 |
_attrs_ = [] |
| 513 |
is_indirect_arg_for_call_python = True |
| 514 |
|
| 515 |
@jit.dont_look_inside |
| 516 |
def extra_repr(self, cdata): |
| 517 |
lvalue = misc.read_raw_longdouble_data(cdata) |
| 518 |
return misc.longdouble2str(lvalue) |
| 519 |
|
| 520 |
def cast(self, w_ob): |
| 521 |
if (isinstance(w_ob, cdataobj.W_CData) and |
| 522 |
isinstance(w_ob.ctype, W_CTypePrimitiveLongDouble)): |
| 523 |
with w_ob as ptr: |
| 524 |
w_cdata = self.convert_to_object(ptr) |
| 525 |
return w_cdata |
| 526 |
else: |
| 527 |
return W_CTypePrimitiveFloat.cast(self, w_ob) |
| 528 |
|
| 529 |
@jit.dont_look_inside |
| 530 |
def _to_longdouble_and_write(self, value, cdata): |
| 531 |
lvalue = rffi.cast(rffi.LONGDOUBLE, value) |
| 532 |
misc.write_raw_longdouble_data(cdata, lvalue) |
| 533 |
|
| 534 |
@jit.dont_look_inside |
| 535 |
def _read_from_longdouble(self, cdata): |
| 536 |
lvalue = misc.read_raw_longdouble_data(cdata) |
| 537 |
value = rffi.cast(lltype.Float, lvalue) |
| 538 |
return value |
| 539 |
|
| 540 |
@jit.dont_look_inside |
| 541 |
def _copy_longdouble(self, cdatasrc, cdatadst): |
| 542 |
lvalue = misc.read_raw_longdouble_data(cdatasrc) |
| 543 |
misc.write_raw_longdouble_data(cdatadst, lvalue) |
| 544 |
|
| 545 |
def float(self, cdata): |
| 546 |
value = self._read_from_longdouble(cdata) |
| 547 |
return self.space.newfloat(value) |
| 548 |
|
| 549 |
def convert_to_object(self, cdata): |
| 550 |
w_cdata = cdataobj.W_CDataMem(self.space, self) |
| 551 |
with w_cdata as ptr: |
| 552 |
self._copy_longdouble(cdata, ptr) |
| 553 |
return w_cdata |
| 554 |
|
| 555 |
def convert_from_object(self, cdata, w_ob): |
| 556 |
space = self.space |
| 557 |
if (isinstance(w_ob, cdataobj.W_CData) and |
| 558 |
isinstance(w_ob.ctype, W_CTypePrimitiveLongDouble)): |
| 559 |
with w_ob as ptr: |
| 560 |
self._copy_longdouble(ptr, cdata) |
| 561 |
else: |
| 562 |
value = space.float_w(space.float(w_ob)) |
| 563 |
self._to_longdouble_and_write(value, cdata) |
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
def pack_list_of_items(self, cdata, w_ob, expected_length): |
| 570 |
float_list = self.space.listview_float(w_ob) |
| 571 |
if (float_list is not None and |
| 572 |
self._within_bounds(len(float_list), expected_length)): |
| 573 |
misc.pack_float_list_to_raw_array(float_list, cdata, |
| 574 |
rffi.LONGDOUBLE, rffi.LONGDOUBLEP) |
| 575 |
return True |
| 576 |
return W_CTypePrimitive.pack_list_of_items(self, cdata, w_ob, |
| 577 |
expected_length) |
| 578 |
|
| 579 |
@jit.dont_look_inside |
| 580 |
def nonzero(self, cdata): |
| 581 |
return misc.is_nonnull_longdouble(cdata) |
| 582 |
|
| 583 |
|
| 584 |
class W_CTypePrimitiveComplex(W_CTypePrimitive): |
| 585 |
_attrs_ = [] |
| 586 |
|
| 587 |
def cast(self, w_ob): |
| 588 |
space = self.space |
| 589 |
if isinstance(w_ob, cdataobj.W_CData): |
| 590 |
if not isinstance(w_ob.ctype, W_CTypePrimitive): |
| 591 |
raise oefmt(space.w_TypeError, |
| 592 |
"cannot cast ctype '%s' to ctype '%s'", |
| 593 |
w_ob.ctype.name, self.name) |
| 594 |
w_ob = w_ob.convert_to_object() |
| 595 |
|
| 596 |
imag = 0.0 |
| 597 |
if space.isinstance_w(w_ob, space.w_bytes): |
| 598 |
real = self.cast_str(w_ob) |
| 599 |
elif space.isinstance_w(w_ob, space.w_unicode): |
| 600 |
real = self.cast_unicode(w_ob) |
| 601 |
else: |
| 602 |
real, imag = space.unpackcomplex(w_ob) |
| 603 |
w_cdata = cdataobj.W_CDataMem(space, self) |
| 604 |
w_cdata.write_raw_complex_data(real, imag) |
| 605 |
return w_cdata |
| 606 |
|
| 607 |
def complex(self, cdata): |
| 608 |
return self.convert_to_object(cdata) |
| 609 |
|
| 610 |
def convert_to_object(self, cdata): |
| 611 |
halfsize = self.size >> 1 |
| 612 |
cdata2 = rffi.ptradd(cdata, halfsize) |
| 613 |
real = misc.read_raw_float_data(cdata, halfsize) |
| 614 |
imag = misc.read_raw_float_data(cdata2, halfsize) |
| 615 |
return self.space.newcomplex(real, imag) |
| 616 |
|
| 617 |
def convert_from_object(self, cdata, w_ob): |
| 618 |
space = self.space |
| 619 |
real, imag = space.unpackcomplex(w_ob) |
| 620 |
halfsize = self.size >> 1 |
| 621 |
cdata2 = rffi.ptradd(cdata, halfsize) |
| 622 |
misc.write_raw_float_data(cdata, real, halfsize) |
| 623 |
misc.write_raw_float_data(cdata2, imag, halfsize) |
| 624 |
|
| 625 |
def nonzero(self, cdata): |
| 626 |
halfsize = self.size >> 1 |
| 627 |
cdata2 = rffi.ptradd(cdata, halfsize) |
| 628 |
return (misc.is_nonnull_float(cdata, halfsize) | |
| 629 |
misc.is_nonnull_float(cdata2, halfsize)) |