| 1 |
from rpython.rlib.objectmodel import we_are_translated |
| 2 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 3 |
from rpython.rlib.rbigint import rbigint, InvalidSignednessError |
| 4 |
from rpython.rlib.rarithmetic import maxint, widen |
| 5 |
from pypy.module.cpyext.api import ( |
| 6 |
cpython_api, PyObject, build_type_checkers_flags, Py_ssize_t, |
| 7 |
CONST_STRING, ADDR, CANNOT_FAIL, INTP_real, cts) |
| 8 |
from pypy.interpreter.error import OperationError, oefmt |
| 9 |
from pypy.interpreter.unicodehelper import wcharpsize2utf8 |
| 10 |
from pypy.module.cpyext.pyerrors import PyErr_BadInternalCall |
| 11 |
|
| 12 |
PyLong_Check, PyLong_CheckExact = build_type_checkers_flags("Long") |
| 13 |
|
| 14 |
@cpython_api([rffi.LONG], PyObject) |
| 15 |
def PyLong_FromLong(space, val): |
| 16 |
"""Return a new PyLongObject object from v, or NULL on failure.""" |
| 17 |
return space.newlong(widen(val)) |
| 18 |
|
| 19 |
@cpython_api([Py_ssize_t], PyObject) |
| 20 |
def PyLong_FromSsize_t(space, val): |
| 21 |
"""Return a new PyLongObject object from a C Py_ssize_t, or |
| 22 |
NULL on failure. |
| 23 |
""" |
| 24 |
return space.newlong(val) |
| 25 |
|
| 26 |
@cpython_api([rffi.SIZE_T], PyObject) |
| 27 |
def PyLong_FromSize_t(space, val): |
| 28 |
"""Return a new PyLongObject object from a C size_t, or NULL on |
| 29 |
failure. |
| 30 |
""" |
| 31 |
return space.newlong_from_rarith_int(rffi.cast(lltype.Unsigned, val)) |
| 32 |
|
| 33 |
@cpython_api([rffi.LONGLONG], PyObject) |
| 34 |
def PyLong_FromLongLong(space, val): |
| 35 |
"""Return a new PyLongObject object from a C long long, or NULL |
| 36 |
on failure.""" |
| 37 |
return space.newlong_from_rarith_int(val) |
| 38 |
|
| 39 |
@cts.decl("PyObject *PyLong_FromUnsignedLong(unsigned long val)") |
| 40 |
def PyLong_FromUnsignedLong(space, val): |
| 41 |
"""Return a new PyLongObject object from a C unsigned long, or |
| 42 |
NULL on failure.""" |
| 43 |
return space.newlong_from_rarith_int(val) |
| 44 |
|
| 45 |
@cts.decl("PyObject *PyLong_FromUnsignedLongLong(unsigned long long val)") |
| 46 |
def PyLong_FromUnsignedLongLong(space, val): |
| 47 |
"""Return a new PyLongObject object from a C unsigned long long, |
| 48 |
or NULL on failure.""" |
| 49 |
return space.newlong_from_rarith_int(val) |
| 50 |
|
| 51 |
ULONG_MASK = (2 ** (8 * rffi.sizeof(rffi.ULONG)) -1) |
| 52 |
ULONG_MAX = (2 ** (8 * rffi.sizeof(rffi.ULONG)) -1) |
| 53 |
LONG_MAX = (2 ** (8 * rffi.sizeof(rffi.ULONG) - 1) -1) |
| 54 |
LONG_MIN = (-2 ** (8 * rffi.sizeof(rffi.ULONG) - 1)) |
| 55 |
INT_MAX = (2 ** (8 * rffi.sizeof(rffi.UINT) - 1) -1) |
| 56 |
INT_MIN = (-2 ** (8 * rffi.sizeof(rffi.UINT) - 1)) |
| 57 |
need_to_check = maxint > ULONG_MAX |
| 58 |
|
| 59 |
@cpython_api([PyObject], rffi.ULONG, error=-1) |
| 60 |
def PyLong_AsUnsignedLong(space, w_long): |
| 61 |
""" |
| 62 |
Return a C unsigned long representation of the contents of pylong. |
| 63 |
If pylong is greater than ULONG_MAX, an OverflowError is |
| 64 |
raised.""" |
| 65 |
try: |
| 66 |
val = space.uint_w(space.index(w_long)) |
| 67 |
except OperationError as e: |
| 68 |
if e.match(space, space.w_ValueError): |
| 69 |
e.w_type = space.w_OverflowError |
| 70 |
if (e.match(space, space.w_OverflowError) and |
| 71 |
space.isinstance_w(w_long, space.w_int)): |
| 72 |
raise oefmt(space.w_OverflowError, |
| 73 |
"Python int too large to convert to C unsigned long") |
| 74 |
raise e |
| 75 |
if need_to_check and val > ULONG_MAX: |
| 76 |
|
| 77 |
|
| 78 |
raise oefmt(space.w_OverflowError, |
| 79 |
"Python int too large to convert to C unsigned long") |
| 80 |
return rffi.cast(rffi.ULONG, val) |
| 81 |
|
| 82 |
@cpython_api([PyObject], rffi.ULONG, error=-1) |
| 83 |
def PyLong_AsUnsignedLongMask(space, w_long): |
| 84 |
"""Return a C unsigned long from a Python long integer, without checking |
| 85 |
for overflow. |
| 86 |
""" |
| 87 |
num = space.bigint_w(space.index(w_long)) |
| 88 |
val = num.uintmask() |
| 89 |
if need_to_check and not we_are_translated(): |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
val &= ULONG_MASK |
| 95 |
return rffi.cast(rffi.ULONG, val) |
| 96 |
|
| 97 |
@cpython_api([PyObject], rffi.LONG, error=-1) |
| 98 |
def PyLong_AsLong(space, w_long): |
| 99 |
""" |
| 100 |
Get a C long int from an int object or any object that has an __index__ |
| 101 |
method. Return -1 and set an error if overflow occurs. |
| 102 |
""" |
| 103 |
try: |
| 104 |
val = space.int_w(space.index(w_long)) |
| 105 |
except OperationError as e: |
| 106 |
if e.match(space, space.w_ValueError): |
| 107 |
e.w_type = space.w_OverflowError |
| 108 |
if (e.match(space, space.w_OverflowError) and |
| 109 |
space.isinstance_w(w_long, space.w_int)): |
| 110 |
raise oefmt(space.w_OverflowError, |
| 111 |
"Python int too large to convert to C long") |
| 112 |
raise e |
| 113 |
if need_to_check and (val > LONG_MAX or val < LONG_MIN): |
| 114 |
|
| 115 |
|
| 116 |
raise oefmt(space.w_OverflowError, |
| 117 |
"Python int too large to convert to C long") |
| 118 |
return rffi.cast(rffi.LONG, val) |
| 119 |
|
| 120 |
@cpython_api([PyObject], rffi.INT, error=-1) |
| 121 |
def _PyLong_AsInt(space, w_long): |
| 122 |
""" |
| 123 |
Get a C int from an int object or any object that has an __index__ |
| 124 |
method. Return -1 and set an error if overflow occurs. |
| 125 |
""" |
| 126 |
try: |
| 127 |
val = space.int_w(space.index(w_long)) |
| 128 |
except OperationError as e: |
| 129 |
if e.match(space, space.w_ValueError): |
| 130 |
e.w_type = space.w_OverflowError |
| 131 |
if (e.match(space, space.w_OverflowError) and |
| 132 |
space.isinstance_w(w_long, space.w_int)): |
| 133 |
raise oefmt(space.w_OverflowError, |
| 134 |
"Python int too large to convert to C int") |
| 135 |
raise e |
| 136 |
if val > INT_MAX or val < INT_MIN: |
| 137 |
raise oefmt(space.w_OverflowError, |
| 138 |
"Python int too large to convert to C int") |
| 139 |
return rffi.cast(rffi.INT, val) |
| 140 |
|
| 141 |
@cpython_api([PyObject], Py_ssize_t, error=-1) |
| 142 |
def PyLong_AsSsize_t(space, w_long): |
| 143 |
"""Return a C Py_ssize_t representation of the contents of pylong. If |
| 144 |
pylong is greater than PY_SSIZE_T_MAX, an OverflowError is raised |
| 145 |
and -1 will be returned. |
| 146 |
""" |
| 147 |
return space.int_w(space.index(w_long)) |
| 148 |
|
| 149 |
@cpython_api([PyObject], rffi.SIZE_T, error=-1) |
| 150 |
def PyLong_AsSize_t(space, w_long): |
| 151 |
"""Return a C size_t representation of of pylong. pylong must be |
| 152 |
an instance of PyLongObject. |
| 153 |
|
| 154 |
Raise OverflowError if the value of pylong is out of range for a |
| 155 |
size_t.""" |
| 156 |
return space.uint_w(space.index(w_long)) |
| 157 |
|
| 158 |
@cts.decl("long long PyLong_AsLongLong(PyObject *val)", error=-1) |
| 159 |
def PyLong_AsLongLong(space, w_long): |
| 160 |
""" |
| 161 |
Return a C unsigned long representation of the contents of pylong. |
| 162 |
If pylong is greater than ULONG_MAX, an OverflowError is |
| 163 |
raised.""" |
| 164 |
return cts.cast("long long", space.r_longlong_w(space.index(w_long))) |
| 165 |
|
| 166 |
@cts.decl("unsigned long long PyLong_AsUnsignedLongLong(PyObject *val)", error=-1) |
| 167 |
def PyLong_AsUnsignedLongLong(space, w_long): |
| 168 |
""" |
| 169 |
Return a C unsigned long representation of the contents of pylong. |
| 170 |
If pylong is greater than ULONG_MAX, an OverflowError is |
| 171 |
raised.""" |
| 172 |
if not w_long: |
| 173 |
return PyErr_BadInternalCall(space) |
| 174 |
try: |
| 175 |
return rffi.cast(rffi.ULONGLONG, space.r_ulonglong_w(space.index(w_long))) |
| 176 |
except OperationError as e: |
| 177 |
if e.match(space, space.w_ValueError): |
| 178 |
if not w_long: |
| 179 |
raise |
| 180 |
e.w_type = space.w_OverflowError |
| 181 |
raise |
| 182 |
|
| 183 |
@cts.decl("unsigned long long PyLong_AsUnsignedLongLongMask(PyObject *val)", error=-1) |
| 184 |
def PyLong_AsUnsignedLongLongMask(space, w_long): |
| 185 |
"""Will first attempt to cast the object to a PyIntObject or |
| 186 |
PyLongObject, if it is not already one, and then return its value as |
| 187 |
unsigned long long, without checking for overflow. |
| 188 |
""" |
| 189 |
if not w_long: |
| 190 |
return PyErr_BadInternalCall(space) |
| 191 |
num = space.bigint_w(space.index(w_long)) |
| 192 |
return num.ulonglongmask() |
| 193 |
|
| 194 |
@cpython_api([PyObject, INTP_real], rffi.LONG, error=-1) |
| 195 |
def PyLong_AsLongAndOverflow(space, w_long, overflow_ptr): |
| 196 |
"""Get a C long int from an int object or any object that has an __index__ |
| 197 |
method. |
| 198 |
|
| 199 |
On overflow, return -1 and set *overflow to 1 or -1 depending on the sign of |
| 200 |
the result. Otherwise *overflow is 0. |
| 201 |
|
| 202 |
For other errors (e.g., TypeError), return -1 and set an error condition. |
| 203 |
In this case *overflow will be 0. |
| 204 |
""" |
| 205 |
|
| 206 |
overflow_ptr[0] = rffi.cast(rffi.INT_real, 0) |
| 207 |
try: |
| 208 |
val = space.int_w(w_long) |
| 209 |
if not need_to_check or (val >= LONG_MIN and val <= LONG_MAX): |
| 210 |
|
| 211 |
|
| 212 |
return rffi.cast(rffi.LONG, val) |
| 213 |
except OperationError as e: |
| 214 |
if not e.match(space, space.w_OverflowError): |
| 215 |
raise |
| 216 |
if space.is_true(space.gt(w_long, space.newint(0))): |
| 217 |
overflow_ptr[0] = rffi.cast(rffi.INT_real, 1) |
| 218 |
else: |
| 219 |
overflow_ptr[0] = rffi.cast(rffi.INT_real, -1) |
| 220 |
return -1 |
| 221 |
|
| 222 |
@cts.decl("long long PyLong_AsLongLongAndOverflow(PyObject *val, int*)", error=-1) |
| 223 |
def PyLong_AsLongLongAndOverflow(space, w_long, overflow_ptr): |
| 224 |
""" |
| 225 |
Return a C long long representation of the contents of pylong. If pylong is |
| 226 |
greater than PY_LLONG_MAX or less than PY_LLONG_MIN, set *overflow to 1 or |
| 227 |
-1, respectively, and return -1; otherwise, set *overflow to 0. If any |
| 228 |
other exception occurs (for example a TypeError or MemoryError), then -1 |
| 229 |
will be returned and *overflow will be 0.""" |
| 230 |
overflow_ptr[0] = rffi.cast(rffi.INT_real, 0) |
| 231 |
try: |
| 232 |
return rffi.cast(rffi.LONGLONG, space.r_longlong_w(w_long)) |
| 233 |
except OperationError as e: |
| 234 |
if not e.match(space, space.w_OverflowError): |
| 235 |
raise |
| 236 |
if space.is_true(space.gt(w_long, space.newint(0))): |
| 237 |
overflow_ptr[0] = rffi.cast(rffi.INT_real, 1) |
| 238 |
else: |
| 239 |
overflow_ptr[0] = rffi.cast(rffi.INT_real, -1) |
| 240 |
return -1 |
| 241 |
|
| 242 |
@cpython_api([lltype.Float], PyObject) |
| 243 |
def PyLong_FromDouble(space, val): |
| 244 |
"""Return a new PyLongObject object from v, or NULL on failure.""" |
| 245 |
return space.long(space.newfloat(val)) |
| 246 |
|
| 247 |
@cpython_api([PyObject], lltype.Float, error=-1.0) |
| 248 |
def PyLong_AsDouble(space, w_long): |
| 249 |
"""Return a C double representation of the contents of pylong. If |
| 250 |
pylong cannot be approximately represented as a double, an |
| 251 |
OverflowError exception is raised and -1.0 will be returned.""" |
| 252 |
return space.float_w(space.float(w_long)) |
| 253 |
|
| 254 |
@cpython_api([CONST_STRING, rffi.CCHARPP, rffi.INT_real], PyObject) |
| 255 |
def PyLong_FromString(space, str, pend, base): |
| 256 |
"""Return a new PyLongObject based on the string value in str, which is |
| 257 |
interpreted according to the radix in base. If pend is non-NULL, |
| 258 |
*pend will point to the first character in str which follows the |
| 259 |
representation of the number. If base is 0, the radix will be determined |
| 260 |
based on the leading characters of str: if str starts with '0x' or |
| 261 |
'0X', radix 16 will be used; if str starts with '0', radix 8 will be |
| 262 |
used; otherwise radix 10 will be used. If base is not 0, it must be |
| 263 |
between 2 and 36, inclusive. Leading spaces are ignored. If there are |
| 264 |
no digits, ValueError will be raised.""" |
| 265 |
s = rffi.charp2str(str) |
| 266 |
w_str = space.newtext(s) |
| 267 |
w_base = space.newint(rffi.cast(lltype.Signed, base)) |
| 268 |
if pend: |
| 269 |
pend[0] = rffi.ptradd(str, len(s)) |
| 270 |
return space.call_function(space.w_long, w_str, w_base) |
| 271 |
|
| 272 |
@cpython_api([rffi.CWCHARP, Py_ssize_t, rffi.INT_real], PyObject) |
| 273 |
def PyLong_FromUnicode(space, u, length, base): |
| 274 |
"""Convert a sequence of Unicode digits to a Python long integer value. |
| 275 |
The first parameter, u, points to the first character of the Unicode |
| 276 |
string, length gives the number of characters, and base is the radix |
| 277 |
for the conversion. The radix must be in the range [2, 36]; if it is |
| 278 |
out of range, ValueError will be raised.""" |
| 279 |
if length < 0: |
| 280 |
length = 0 |
| 281 |
w_value = space.newutf8(wcharpsize2utf8(space, u, length), length) |
| 282 |
return PyLong_FromUnicodeObject(space, w_value, base) |
| 283 |
|
| 284 |
@cpython_api([PyObject, rffi.INT_real], PyObject) |
| 285 |
def PyLong_FromUnicodeObject(space, w_value, base): |
| 286 |
w_base = space.newint(rffi.cast(lltype.Signed, base)) |
| 287 |
return space.call_function(space.w_long, w_value, w_base) |
| 288 |
|
| 289 |
@cpython_api([rffi.VOIDP], PyObject) |
| 290 |
def PyLong_FromVoidPtr(space, p): |
| 291 |
"""Create a Python integer or long integer from the pointer p. The pointer value |
| 292 |
can be retrieved from the resulting value using PyLong_AsVoidPtr(). |
| 293 |
|
| 294 |
If the integer is larger than LONG_MAX, a positive long integer is returned.""" |
| 295 |
value = rffi.cast(ADDR, p) |
| 296 |
if value < 0: |
| 297 |
return space.newlong_from_rarith_int(rffi.cast(lltype.Unsigned, p)) |
| 298 |
return space.newint(value) |
| 299 |
|
| 300 |
@cpython_api([PyObject], rffi.VOIDP, error=lltype.nullptr(rffi.VOIDP.TO)) |
| 301 |
def PyLong_AsVoidPtr(space, w_long): |
| 302 |
"""Convert a Python integer or long integer pylong to a C void pointer. |
| 303 |
If pylong cannot be converted, an OverflowError will be raised. This |
| 304 |
is only assured to produce a usable void pointer for values created |
| 305 |
with PyLong_FromVoidPtr(). |
| 306 |
For values outside 0..LONG_MAX, both signed and unsigned integers are accepted.""" |
| 307 |
return rffi.cast(rffi.VOIDP, space.uint_w(w_long)) |
| 308 |
|
| 309 |
@cpython_api([PyObject], rffi.SIZE_T, error=-1) |
| 310 |
def _PyLong_NumBits(space, w_long): |
| 311 |
return space.uint_w(space.call_method(w_long, "bit_length")) |
| 312 |
|
| 313 |
@cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL) |
| 314 |
def _PyLong_Sign(space, w_long): |
| 315 |
bigint = space.bigint_w(w_long) |
| 316 |
return bigint.get_sign() |
| 317 |
|
| 318 |
CONST_UCHARP = lltype.Ptr(lltype.Array(rffi.UCHAR, hints={'nolength': True, |
| 319 |
'render_as_const': True})) |
| 320 |
@cpython_api([CONST_UCHARP, rffi.SIZE_T, rffi.INT_real, rffi.INT_real], PyObject) |
| 321 |
def _PyLong_FromByteArray(space, bytes, n, little_endian, signed): |
| 322 |
little_endian = rffi.cast(lltype.Signed, little_endian) |
| 323 |
signed = rffi.cast(lltype.Signed, signed) |
| 324 |
s = rffi.charpsize2str(rffi.cast(rffi.CCHARP, bytes), |
| 325 |
rffi.cast(lltype.Signed, n)) |
| 326 |
if little_endian: |
| 327 |
byteorder = 'little' |
| 328 |
else: |
| 329 |
byteorder = 'big' |
| 330 |
result = rbigint.frombytes(s, byteorder, signed != 0) |
| 331 |
return space.newlong_from_rbigint(result) |
| 332 |
|
| 333 |
@cpython_api([PyObject, rffi.UCHARP, rffi.SIZE_T, |
| 334 |
rffi.INT_real, rffi.INT_real], rffi.INT_real, error=-1) |
| 335 |
def _PyLong_AsByteArrayO(space, w_v, bytes, n, little_endian, is_signed): |
| 336 |
n = rffi.cast(lltype.Signed, n) |
| 337 |
little_endian = rffi.cast(lltype.Signed, little_endian) |
| 338 |
signed = rffi.cast(lltype.Signed, is_signed) != 0 |
| 339 |
byteorder = 'little' if little_endian else 'big' |
| 340 |
bigint = space.bigint_w(w_v) |
| 341 |
try: |
| 342 |
digits = bigint.tobytes(n, byteorder, signed) |
| 343 |
except InvalidSignednessError: |
| 344 |
|
| 345 |
|
| 346 |
raise oefmt(space.w_OverflowError, |
| 347 |
"can't convert negative long to unsigned") |
| 348 |
except OverflowError: |
| 349 |
raise oefmt(space.w_OverflowError, |
| 350 |
"long too big to convert") |
| 351 |
assert len(digits) == n |
| 352 |
for i in range(n): |
| 353 |
bytes[i] = rffi.cast(rffi.UCHAR, digits[i]) |
| 354 |
return 0 |