| 1 |
from __future__ import with_statement |
| 2 |
import sys |
| 3 |
|
| 4 |
from pypy.interpreter.error import OperationError, oefmt |
| 5 |
from pypy.module._rawffi.interp_rawffi import wrap_dlopenerror |
| 6 |
|
| 7 |
from rpython.rlib import jit |
| 8 |
from rpython.rlib.objectmodel import specialize, we_are_translated |
| 9 |
from rpython.rlib.rarithmetic import r_uint, r_ulonglong |
| 10 |
from rpython.rlib.unroll import unrolling_iterable |
| 11 |
from rpython.rlib.rdynload import dlopen, DLOpenError, DLLHANDLE |
| 12 |
from rpython.rlib.nonconst import NonConstant |
| 13 |
from rpython.rtyper.lltypesystem import lltype, llmemory, rffi |
| 14 |
from rpython.translator.tool.cbuild import ExternalCompilationInfo |
| 15 |
|
| 16 |
_WIN32 = sys.platform == 'win32' |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
_prim_signed_types = unrolling_iterable([ |
| 22 |
(rffi.SIGNEDCHAR, rffi.SIGNEDCHARP), |
| 23 |
(rffi.SHORT, rffi.SHORTP), |
| 24 |
(rffi.INT, rffi.INTP), |
| 25 |
(rffi.LONG, rffi.LONGP), |
| 26 |
(rffi.LONGLONG, rffi.LONGLONGP)]) |
| 27 |
|
| 28 |
_prim_unsigned_types = unrolling_iterable([ |
| 29 |
(rffi.UCHAR, rffi.UCHARP), |
| 30 |
(rffi.USHORT, rffi.USHORTP), |
| 31 |
(rffi.UINT, rffi.UINTP), |
| 32 |
(rffi.ULONG, rffi.ULONGP), |
| 33 |
(rffi.ULONGLONG, rffi.ULONGLONGP)]) |
| 34 |
|
| 35 |
_prim_float_types = unrolling_iterable([ |
| 36 |
(rffi.FLOAT, rffi.FLOATP), |
| 37 |
(rffi.DOUBLE, rffi.DOUBLEP)]) |
| 38 |
|
| 39 |
def read_raw_signed_data(target, size): |
| 40 |
for TP, TPP in _prim_signed_types: |
| 41 |
if size == rffi.sizeof(TP): |
| 42 |
return rffi.cast(lltype.SignedLongLong, rffi.cast(TPP, target)[0]) |
| 43 |
raise NotImplementedError("bad integer size") |
| 44 |
|
| 45 |
def read_raw_long_data(target, size): |
| 46 |
for TP, TPP in _prim_signed_types: |
| 47 |
if size == rffi.sizeof(TP): |
| 48 |
assert rffi.sizeof(TP) <= rffi.sizeof(lltype.Signed) |
| 49 |
return rffi.cast(lltype.Signed, rffi.cast(TPP, target)[0]) |
| 50 |
raise NotImplementedError("bad integer size") |
| 51 |
|
| 52 |
def read_raw_unsigned_data(target, size): |
| 53 |
for TP, TPP in _prim_unsigned_types: |
| 54 |
if size == rffi.sizeof(TP): |
| 55 |
return rffi.cast(lltype.UnsignedLongLong, rffi.cast(TPP, target)[0]) |
| 56 |
raise NotImplementedError("bad integer size") |
| 57 |
|
| 58 |
def read_raw_ulong_data(target, size): |
| 59 |
for TP, TPP in _prim_unsigned_types: |
| 60 |
if size == rffi.sizeof(TP): |
| 61 |
assert rffi.sizeof(TP) <= rffi.sizeof(lltype.Unsigned) |
| 62 |
return rffi.cast(lltype.Unsigned, rffi.cast(TPP, target)[0]) |
| 63 |
raise NotImplementedError("bad integer size") |
| 64 |
|
| 65 |
@specialize.arg(0) |
| 66 |
def _read_raw_float_data_tp(TPP, target): |
| 67 |
|
| 68 |
return rffi.cast(lltype.Float, rffi.cast(TPP, target)[0]) |
| 69 |
|
| 70 |
def read_raw_float_data(target, size): |
| 71 |
for TP, TPP in _prim_float_types: |
| 72 |
if size == rffi.sizeof(TP): |
| 73 |
return _read_raw_float_data_tp(TPP, target) |
| 74 |
raise NotImplementedError("bad float size") |
| 75 |
|
| 76 |
def read_raw_longdouble_data(target): |
| 77 |
return rffi.cast(rffi.LONGDOUBLEP, target)[0] |
| 78 |
|
| 79 |
@specialize.argtype(1) |
| 80 |
def write_raw_unsigned_data(target, source, size): |
| 81 |
for TP, TPP in _prim_unsigned_types: |
| 82 |
if size == rffi.sizeof(TP): |
| 83 |
rffi.cast(TPP, target)[0] = rffi.cast(TP, source) |
| 84 |
return |
| 85 |
raise NotImplementedError("bad integer size") |
| 86 |
|
| 87 |
@specialize.argtype(1) |
| 88 |
def write_raw_signed_data(target, source, size): |
| 89 |
for TP, TPP in _prim_signed_types: |
| 90 |
if size == rffi.sizeof(TP): |
| 91 |
rffi.cast(TPP, target)[0] = rffi.cast(TP, source) |
| 92 |
return |
| 93 |
raise NotImplementedError("bad integer size") |
| 94 |
|
| 95 |
|
| 96 |
@specialize.arg(0, 1) |
| 97 |
def _write_raw_float_data_tp(TP, TPP, target, source): |
| 98 |
|
| 99 |
rffi.cast(TPP, target)[0] = rffi.cast(TP, source) |
| 100 |
|
| 101 |
def write_raw_float_data(target, source, size): |
| 102 |
for TP, TPP in _prim_float_types: |
| 103 |
if size == rffi.sizeof(TP): |
| 104 |
_write_raw_float_data_tp(TP, TPP, target, source) |
| 105 |
return |
| 106 |
raise NotImplementedError("bad float size") |
| 107 |
|
| 108 |
def write_raw_longdouble_data(target, source): |
| 109 |
rffi.cast(rffi.LONGDOUBLEP, target)[0] = source |
| 110 |
|
| 111 |
@jit.dont_look_inside |
| 112 |
def write_string_as_charp(target, string): |
| 113 |
from pypy.module._cffi_backend.ctypefunc import set_mustfree_flag |
| 114 |
buf, llobj, buf_flag = rffi.get_nonmovingbuffer_ll_final_null(string) |
| 115 |
set_mustfree_flag(target, ord(buf_flag)) |
| 116 |
rffi.cast(rffi.CCHARPP, target)[0] = buf |
| 117 |
return llobj |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
sprintf_longdouble = rffi.llexternal( |
| 122 |
"sprintf", [rffi.CCHARP, rffi.CCHARP, rffi.LONGDOUBLE], lltype.Void, |
| 123 |
_nowrapper=True, sandboxsafe=True) |
| 124 |
|
| 125 |
FORMAT_LONGDOUBLE = rffi.str2charp("%LE") |
| 126 |
|
| 127 |
def longdouble2str(lvalue): |
| 128 |
with lltype.scoped_alloc(rffi.CCHARP.TO, 128) as p: |
| 129 |
sprintf_longdouble(p, FORMAT_LONGDOUBLE, lvalue) |
| 130 |
return rffi.charp2str(p) |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
def _is_a_float(space, w_ob): |
| 135 |
from pypy.module._cffi_backend.cdataobj import W_CData |
| 136 |
from pypy.module._cffi_backend.ctypeprim import W_CTypePrimitiveFloat |
| 137 |
if isinstance(w_ob, W_CData): |
| 138 |
return isinstance(w_ob.ctype, W_CTypePrimitiveFloat) |
| 139 |
return space.isinstance_w(w_ob, space.w_float) |
| 140 |
|
| 141 |
def as_long_long(space, w_ob): |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
try: |
| 146 |
return space.int_w(w_ob, allow_conversion=False) |
| 147 |
except OperationError as e: |
| 148 |
if not (e.match(space, space.w_OverflowError) or |
| 149 |
e.match(space, space.w_TypeError)): |
| 150 |
raise |
| 151 |
if _is_a_float(space, w_ob): |
| 152 |
raise |
| 153 |
bigint = space.bigint_w(w_ob, allow_conversion=True) |
| 154 |
try: |
| 155 |
return bigint.tolonglong() |
| 156 |
except OverflowError: |
| 157 |
raise OperationError(space.w_OverflowError, space.newtext(ovf_msg)) |
| 158 |
|
| 159 |
def as_long(space, w_ob): |
| 160 |
|
| 161 |
try: |
| 162 |
return space.int_w(w_ob, allow_conversion=False) |
| 163 |
except OperationError as e: |
| 164 |
if not (e.match(space, space.w_OverflowError) or |
| 165 |
e.match(space, space.w_TypeError)): |
| 166 |
raise |
| 167 |
if _is_a_float(space, w_ob): |
| 168 |
raise |
| 169 |
return space.int_w(w_ob, allow_conversion=True) |
| 170 |
|
| 171 |
def as_unsigned_long_long(space, w_ob, strict): |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
try: |
| 177 |
value = space.int_w(w_ob, allow_conversion=False) |
| 178 |
except OperationError as e: |
| 179 |
if not (e.match(space, space.w_OverflowError) or |
| 180 |
e.match(space, space.w_TypeError)): |
| 181 |
raise |
| 182 |
if strict and _is_a_float(space, w_ob): |
| 183 |
raise |
| 184 |
else: |
| 185 |
if strict and value < 0: |
| 186 |
raise OperationError(space.w_OverflowError, space.newtext(neg_msg)) |
| 187 |
return r_ulonglong(value) |
| 188 |
|
| 189 |
bigint = space.bigint_w(space.int(w_ob), allow_conversion=False) |
| 190 |
if strict: |
| 191 |
try: |
| 192 |
return bigint.toulonglong() |
| 193 |
except ValueError: |
| 194 |
raise OperationError(space.w_OverflowError, space.newtext(neg_msg)) |
| 195 |
except OverflowError: |
| 196 |
raise OperationError(space.w_OverflowError, space.newtext(ovf_msg)) |
| 197 |
else: |
| 198 |
return bigint.ulonglongmask() |
| 199 |
|
| 200 |
def as_unsigned_long(space, w_ob, strict): |
| 201 |
|
| 202 |
try: |
| 203 |
value = space.int_w(w_ob, allow_conversion=False) |
| 204 |
except OperationError as e: |
| 205 |
if not (e.match(space, space.w_OverflowError) or |
| 206 |
e.match(space, space.w_TypeError)): |
| 207 |
raise |
| 208 |
if strict and _is_a_float(space, w_ob): |
| 209 |
raise |
| 210 |
else: |
| 211 |
if strict and value < 0: |
| 212 |
raise OperationError(space.w_OverflowError, space.newtext(neg_msg)) |
| 213 |
if not we_are_translated(): |
| 214 |
if isinstance(value, NonConstant): |
| 215 |
return r_uint(0) |
| 216 |
return r_uint(value) |
| 217 |
|
| 218 |
bigint = space.bigint_w(space.int(w_ob), allow_conversion=False) |
| 219 |
if strict: |
| 220 |
try: |
| 221 |
return bigint.touint() |
| 222 |
except ValueError: |
| 223 |
raise OperationError(space.w_OverflowError, space.newtext(neg_msg)) |
| 224 |
except OverflowError: |
| 225 |
raise OperationError(space.w_OverflowError, space.newtext(ovf_msg)) |
| 226 |
else: |
| 227 |
return bigint.uintmask() |
| 228 |
|
| 229 |
neg_msg = "can't convert negative number to unsigned" |
| 230 |
ovf_msg = "long too big to convert" |
| 231 |
|
| 232 |
def signext(value, size): |
| 233 |
|
| 234 |
|
| 235 |
if size == rffi.sizeof(rffi.SIGNEDCHAR): |
| 236 |
return rffi.cast(lltype.Signed, rffi.cast(rffi.SIGNEDCHAR, value)) |
| 237 |
elif size == rffi.sizeof(rffi.SHORT): |
| 238 |
return rffi.cast(lltype.Signed, rffi.cast(rffi.SHORT, value)) |
| 239 |
elif size == rffi.sizeof(rffi.INT): |
| 240 |
return rffi.cast(lltype.Signed, rffi.cast(rffi.INT, value)) |
| 241 |
else: |
| 242 |
raise AssertionError("unsupported size") |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
class _NotStandardObject(Exception): |
| 247 |
pass |
| 248 |
|
| 249 |
def _standard_object_as_bool(space, w_ob): |
| 250 |
if space.isinstance_w(w_ob, space.w_int): |
| 251 |
try: |
| 252 |
return space.int_w(w_ob) != 0 |
| 253 |
except OperationError as e: |
| 254 |
if not e.match(space, space.w_OverflowError): |
| 255 |
raise |
| 256 |
return space.bigint_w(w_ob).tobool() |
| 257 |
if space.isinstance_w(w_ob, space.w_float): |
| 258 |
return space.float_w(w_ob) != 0.0 |
| 259 |
raise _NotStandardObject |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
eci = ExternalCompilationInfo(post_include_bits=[""" |
| 264 |
#define pypy__is_nonnull_longdouble(x) ((x) != 0.0) |
| 265 |
"""]) |
| 266 |
_is_nonnull_longdouble = rffi.llexternal( |
| 267 |
"pypy__is_nonnull_longdouble", [rffi.LONGDOUBLE], lltype.Bool, |
| 268 |
compilation_info=eci, _nowrapper=True, elidable_function=True, |
| 269 |
sandboxsafe=True) |
| 270 |
|
| 271 |
|
| 272 |
@jit.dont_look_inside |
| 273 |
def is_nonnull_longdouble(cdata): |
| 274 |
return _is_nonnull_longdouble(read_raw_longdouble_data(cdata)) |
| 275 |
def is_nonnull_float(cdata, size): |
| 276 |
return read_raw_float_data(cdata, size) != 0.0 |
| 277 |
|
| 278 |
def object_as_bool(space, w_ob): |
| 279 |
|
| 280 |
|
| 281 |
try: |
| 282 |
return _standard_object_as_bool(space, w_ob) |
| 283 |
except _NotStandardObject: |
| 284 |
pass |
| 285 |
|
| 286 |
from pypy.module._cffi_backend.cdataobj import W_CData |
| 287 |
from pypy.module._cffi_backend.ctypeprim import W_CTypePrimitiveFloat |
| 288 |
from pypy.module._cffi_backend.ctypeprim import W_CTypePrimitiveLongDouble |
| 289 |
is_cdata = isinstance(w_ob, W_CData) |
| 290 |
if is_cdata and isinstance(w_ob.ctype, W_CTypePrimitiveFloat): |
| 291 |
with w_ob as ptr: |
| 292 |
if isinstance(w_ob.ctype, W_CTypePrimitiveLongDouble): |
| 293 |
result = is_nonnull_longdouble(ptr) |
| 294 |
else: |
| 295 |
result = is_nonnull_float(ptr, w_ob.ctype.size) |
| 296 |
return result |
| 297 |
|
| 298 |
if not is_cdata and space.lookup(w_ob, '__float__') is not None: |
| 299 |
w_io = space.float(w_ob) |
| 300 |
else: |
| 301 |
w_io = space.int(w_ob) |
| 302 |
try: |
| 303 |
return _standard_object_as_bool(space, w_io) |
| 304 |
except _NotStandardObject: |
| 305 |
raise oefmt(space.w_TypeError, "integer/float expected") |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
@specialize.arg(0) |
| 310 |
def _raw_memcopy_tp(TPP, source, dest): |
| 311 |
|
| 312 |
rffi.cast(TPP, dest)[0] = rffi.cast(TPP, source)[0] |
| 313 |
|
| 314 |
def _raw_memcopy(source, dest, size): |
| 315 |
if jit.isconstant(size): |
| 316 |
|
| 317 |
|
| 318 |
for TP, TPP in _prim_unsigned_types: |
| 319 |
if size == rffi.sizeof(TP): |
| 320 |
_raw_memcopy_tp(TPP, source, dest) |
| 321 |
return |
| 322 |
_raw_memcopy_opaque(source, dest, size) |
| 323 |
|
| 324 |
@jit.dont_look_inside |
| 325 |
def _raw_memcopy_opaque(source, dest, size): |
| 326 |
|
| 327 |
|
| 328 |
zero = llmemory.itemoffsetof(rffi.CCHARP.TO, 0) |
| 329 |
llmemory.raw_memcopy( |
| 330 |
llmemory.cast_ptr_to_adr(source) + zero, |
| 331 |
llmemory.cast_ptr_to_adr(dest) + zero, |
| 332 |
size * llmemory.sizeof(lltype.Char)) |
| 333 |
|
| 334 |
@specialize.arg(0, 1) |
| 335 |
def _raw_memclear_tp(TP, TPP, dest): |
| 336 |
|
| 337 |
rffi.cast(TPP, dest)[0] = rffi.cast(TP, 0) |
| 338 |
|
| 339 |
def _raw_memclear(dest, size): |
| 340 |
|
| 341 |
for TP, TPP in _prim_unsigned_types: |
| 342 |
if size == rffi.sizeof(TP): |
| 343 |
_raw_memclear_tp(TP, TPP, dest) |
| 344 |
return |
| 345 |
raise NotImplementedError("bad clear size") |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
def pack_list_to_raw_array_bounds_signed(int_list, target, size): |
| 350 |
for TP, TPP in _prim_signed_types: |
| 351 |
if size == rffi.sizeof(TP): |
| 352 |
ptr = rffi.cast(TPP, target) |
| 353 |
for i in range(len(int_list)): |
| 354 |
x = int_list[i] |
| 355 |
y = rffi.cast(TP, x) |
| 356 |
if x != rffi.cast(lltype.Signed, y): |
| 357 |
return x |
| 358 |
ptr[i] = y |
| 359 |
return 0 |
| 360 |
raise NotImplementedError("bad integer size") |
| 361 |
|
| 362 |
def pack_list_to_raw_array_bounds_unsigned(int_list, target, size, vrangemax): |
| 363 |
for TP, TPP in _prim_signed_types: |
| 364 |
if size == rffi.sizeof(TP): |
| 365 |
ptr = rffi.cast(TPP, target) |
| 366 |
for i in range(len(int_list)): |
| 367 |
x = int_list[i] |
| 368 |
if r_uint(x) > vrangemax: |
| 369 |
return x |
| 370 |
ptr[i] = rffi.cast(TP, x) |
| 371 |
return 0 |
| 372 |
raise NotImplementedError("bad integer size") |
| 373 |
|
| 374 |
@specialize.arg(2) |
| 375 |
def pack_float_list_to_raw_array(float_list, target, TP, TPP): |
| 376 |
target = rffi.cast(TPP, target) |
| 377 |
for i in range(len(float_list)): |
| 378 |
x = float_list[i] |
| 379 |
target[i] = rffi.cast(TP, x) |
| 380 |
|
| 381 |
def unpack_list_from_raw_array(int_list, source, size): |
| 382 |
for TP, TPP in _prim_signed_types: |
| 383 |
if size == rffi.sizeof(TP): |
| 384 |
ptr = rffi.cast(TPP, source) |
| 385 |
for i in range(len(int_list)): |
| 386 |
int_list[i] = rffi.cast(lltype.Signed, ptr[i]) |
| 387 |
return |
| 388 |
raise NotImplementedError("bad integer size") |
| 389 |
|
| 390 |
def unpack_unsigned_list_from_raw_array(int_list, source, size): |
| 391 |
for TP, TPP in _prim_unsigned_types: |
| 392 |
if size == rffi.sizeof(TP): |
| 393 |
ptr = rffi.cast(TPP, source) |
| 394 |
for i in range(len(int_list)): |
| 395 |
int_list[i] = rffi.cast(lltype.Signed, ptr[i]) |
| 396 |
return |
| 397 |
raise NotImplementedError("bad integer size") |
| 398 |
|
| 399 |
def unpack_cfloat_list_from_raw_array(float_list, source): |
| 400 |
ptr = rffi.cast(rffi.FLOATP, source) |
| 401 |
for i in range(len(float_list)): |
| 402 |
float_list[i] = rffi.cast(lltype.Float, ptr[i]) |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
def dlopen_w(space, w_filename, flags): |
| 407 |
from pypy.module._cffi_backend.cdataobj import W_CData |
| 408 |
from pypy.module._cffi_backend import ctypeptr |
| 409 |
|
| 410 |
autoclose = True |
| 411 |
if isinstance(w_filename, W_CData): |
| 412 |
|
| 413 |
w_ctype = w_filename.ctype |
| 414 |
if (not isinstance(w_ctype, ctypeptr.W_CTypePointer) or |
| 415 |
not w_ctype.is_void_ptr): |
| 416 |
raise oefmt(space.w_TypeError, |
| 417 |
"dlopen() takes a file name or 'void *' handle, not '%s'", |
| 418 |
w_ctype.name) |
| 419 |
handle = w_filename.unsafe_escaping_ptr() |
| 420 |
if not handle: |
| 421 |
raise oefmt(space.w_RuntimeError, "cannot call dlopen(NULL)") |
| 422 |
fname = w_ctype.extra_repr(handle) |
| 423 |
handle = rffi.cast(DLLHANDLE, handle) |
| 424 |
autoclose = False |
| 425 |
|
| 426 |
elif _WIN32 and space.isinstance_w(w_filename, space.w_unicode): |
| 427 |
fname = space.text_w(w_filename) |
| 428 |
utf8_name = space.utf8_w(w_filename) |
| 429 |
try: |
| 430 |
handle = dlopen(utf8_name, flags) |
| 431 |
except DLOpenError as e: |
| 432 |
raise wrap_dlopenerror(space, e, fname) |
| 433 |
else: |
| 434 |
if space.is_none(w_filename): |
| 435 |
_fname = None |
| 436 |
else: |
| 437 |
_fname = space.fsencode_w(w_filename) |
| 438 |
if _fname is None: |
| 439 |
fname = "<None>" |
| 440 |
else: |
| 441 |
fname = _fname |
| 442 |
try: |
| 443 |
handle = dlopen(_fname, flags) |
| 444 |
except DLOpenError as e: |
| 445 |
raise wrap_dlopenerror(space, e, fname) |
| 446 |
return fname, handle, autoclose |