| 1 |
// Mypyc C API |
| 2 |
|
| 3 |
#ifndef CPY_CPY_H |
| 4 |
#define CPY_CPY_H |
| 5 |
|
| 6 |
#include <Python.h> |
| 7 |
#include <stdbool.h> |
| 8 |
#include <frameobject.h> |
| 9 |
#include <structmember.h> |
| 10 |
#include <assert.h> |
| 11 |
#include <stdint.h> |
| 12 |
#include "pythonsupport.h" |
| 13 |
#include "mypyc_util.h" |
| 14 |
|
| 15 |
#ifdef __cplusplus |
| 16 |
extern "C" { |
| 17 |
#endif |
| 18 |
#if 0 |
| 19 |
} // why isn't emacs smart enough to not indent this |
| 20 |
#endif |
| 21 |
|
| 22 |
#define CPYTHON_LARGE_INT_ERRMSG "Python int too large to convert to C ssize_t" |
| 23 |
|
| 24 |
|
| 25 |
// Naming conventions: |
| 26 |
// |
| 27 |
// Tagged: tagged int |
| 28 |
// Long: tagged long int (pointer) |
| 29 |
// Short: tagged short int (unboxed) |
| 30 |
// Ssize_t: A Py_ssize_t, which ought to be the same width as pointers |
| 31 |
// Object: CPython object (PyObject *) |
| 32 |
|
| 33 |
|
| 34 |
// Tuple type definitions needed for API functions |
| 35 |
|
| 36 |
|
| 37 |
#ifndef MYPYC_DECLARED_tuple_T3OOO |
| 38 |
#define MYPYC_DECLARED_tuple_T3OOO |
| 39 |
typedef struct tuple_T3OOO { |
| 40 |
PyObject *f0; |
| 41 |
PyObject *f1; |
| 42 |
PyObject *f2; |
| 43 |
} tuple_T3OOO; |
| 44 |
#endif |
| 45 |
|
| 46 |
// Our return tuple wrapper for dictionary iteration helper. |
| 47 |
#ifndef MYPYC_DECLARED_tuple_T3CIO |
| 48 |
#define MYPYC_DECLARED_tuple_T3CIO |
| 49 |
typedef struct tuple_T3CIO { |
| 50 |
char f0; // Should continue? |
| 51 |
CPyTagged f1; // Last dict offset |
| 52 |
PyObject *f2; // Next dictionary key or value |
| 53 |
} tuple_T3CIO; |
| 54 |
#endif |
| 55 |
|
| 56 |
// Same as above but for both key and value. |
| 57 |
#ifndef MYPYC_DECLARED_tuple_T4CIOO |
| 58 |
#define MYPYC_DECLARED_tuple_T4CIOO |
| 59 |
typedef struct tuple_T4CIOO { |
| 60 |
char f0; // Should continue? |
| 61 |
CPyTagged f1; // Last dict offset |
| 62 |
PyObject *f2; // Next dictionary key |
| 63 |
PyObject *f3; // Next dictionary value |
| 64 |
} tuple_T4CIOO; |
| 65 |
#endif |
| 66 |
|
| 67 |
// System-wide empty tuple constant |
| 68 |
extern PyObject * __mypyc_empty_tuple__; |
| 69 |
|
| 70 |
static inline PyObject *CPyTuple_LoadEmptyTupleConstant(void) { |
| 71 |
#if !CPY_3_12_FEATURES |
| 72 |
Py_INCREF(__mypyc_empty_tuple__); |
| 73 |
#endif |
| 74 |
return __mypyc_empty_tuple__; |
| 75 |
} |
| 76 |
|
| 77 |
// Native object operations |
| 78 |
|
| 79 |
|
| 80 |
// Search backwards through the trait part of a vtable (which sits *before* |
| 81 |
// the start of the vtable proper) looking for the subvtable describing a trait |
| 82 |
// implementation. We don't do any bounds checking so we'd better be pretty sure |
| 83 |
// we know that it is there. |
| 84 |
static inline CPyVTableItem *CPy_FindTraitVtable(PyTypeObject *trait, CPyVTableItem *vtable) { |
| 85 |
int i; |
| 86 |
for (i = -3; ; i -= 3) { |
| 87 |
if ((PyTypeObject *)vtable[i] == trait) { |
| 88 |
return (CPyVTableItem *)vtable[i + 1]; |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
// Use the same logic for offset table. |
| 94 |
static inline size_t CPy_FindAttrOffset(PyTypeObject *trait, CPyVTableItem *vtable, size_t index) { |
| 95 |
int i; |
| 96 |
for (i = -3; ; i -= 3) { |
| 97 |
if ((PyTypeObject *)vtable[i] == trait) { |
| 98 |
return ((size_t *)vtable[i + 2])[index]; |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
// Get attribute value using vtable (may return an undefined value) |
| 104 |
#define CPY_GET_ATTR(obj, type, vtable_index, object_type, attr_type) \ |
| 105 |
((attr_type (*)(object_type *))((object_type *)obj)->vtable[vtable_index])((object_type *)obj) |
| 106 |
|
| 107 |
#define CPY_GET_ATTR_TRAIT(obj, trait, vtable_index, object_type, attr_type) \ |
| 108 |
((attr_type (*)(object_type *))(CPy_FindTraitVtable(trait, ((object_type *)obj)->vtable))[vtable_index])((object_type *)obj) |
| 109 |
|
| 110 |
// Set attribute value using vtable |
| 111 |
#define CPY_SET_ATTR(obj, type, vtable_index, value, object_type, attr_type) \ |
| 112 |
((bool (*)(object_type *, attr_type))((object_type *)obj)->vtable[vtable_index])( \ |
| 113 |
(object_type *)obj, value) |
| 114 |
|
| 115 |
#define CPY_SET_ATTR_TRAIT(obj, trait, vtable_index, value, object_type, attr_type) \ |
| 116 |
((bool (*)(object_type *, attr_type))(CPy_FindTraitVtable(trait, ((object_type *)obj)->vtable))[vtable_index])( \ |
| 117 |
(object_type *)obj, value) |
| 118 |
|
| 119 |
#define CPY_GET_METHOD(obj, type, vtable_index, object_type, method_type) \ |
| 120 |
((method_type)(((object_type *)obj)->vtable[vtable_index])) |
| 121 |
|
| 122 |
#define CPY_GET_METHOD_TRAIT(obj, trait, vtable_index, object_type, method_type) \ |
| 123 |
((method_type)(CPy_FindTraitVtable(trait, ((object_type *)obj)->vtable)[vtable_index])) |
| 124 |
|
| 125 |
|
| 126 |
// Int operations |
| 127 |
|
| 128 |
|
| 129 |
CPyTagged CPyTagged_FromSsize_t(Py_ssize_t value); |
| 130 |
CPyTagged CPyTagged_FromVoidPtr(void *ptr); |
| 131 |
CPyTagged CPyTagged_FromInt64(int64_t value); |
| 132 |
PyObject *CPyTagged_AsObject(CPyTagged x); |
| 133 |
PyObject *CPyTagged_StealAsObject(CPyTagged x); |
| 134 |
Py_ssize_t CPyTagged_AsSsize_t(CPyTagged x); |
| 135 |
void CPyTagged_IncRef(CPyTagged x); |
| 136 |
void CPyTagged_DecRef(CPyTagged x); |
| 137 |
void CPyTagged_XDecRef(CPyTagged x); |
| 138 |
|
| 139 |
bool CPyTagged_IsEq_(CPyTagged left, CPyTagged right); |
| 140 |
bool CPyTagged_IsLt_(CPyTagged left, CPyTagged right); |
| 141 |
CPyTagged CPyTagged_Negate_(CPyTagged num); |
| 142 |
CPyTagged CPyTagged_Invert_(CPyTagged num); |
| 143 |
CPyTagged CPyTagged_Add_(CPyTagged left, CPyTagged right); |
| 144 |
CPyTagged CPyTagged_Subtract_(CPyTagged left, CPyTagged right); |
| 145 |
CPyTagged CPyTagged_Multiply_(CPyTagged left, CPyTagged right); |
| 146 |
CPyTagged CPyTagged_FloorDivide_(CPyTagged left, CPyTagged right); |
| 147 |
CPyTagged CPyTagged_Remainder_(CPyTagged left, CPyTagged right); |
| 148 |
CPyTagged CPyTagged_BitwiseLongOp_(CPyTagged a, CPyTagged b, char op); |
| 149 |
CPyTagged CPyTagged_Rshift_(CPyTagged left, CPyTagged right); |
| 150 |
CPyTagged CPyTagged_Lshift_(CPyTagged left, CPyTagged right); |
| 151 |
CPyTagged CPyTagged_BitLength(CPyTagged self); |
| 152 |
PyObject *CPyTagged_ToBytes(CPyTagged self, Py_ssize_t length, PyObject *byteorder, int signed_flag); |
| 153 |
PyObject *CPyTagged_ToBigEndianBytes(CPyTagged self, Py_ssize_t length, int signed_flag); |
| 154 |
PyObject *CPyTagged_ToLittleEndianBytes(CPyTagged self, Py_ssize_t length, int signed_flag); |
| 155 |
|
| 156 |
PyObject *CPyTagged_Str(CPyTagged n); |
| 157 |
PyObject *CPyTagged_AsciiBytes(CPyTagged n); |
| 158 |
CPyTagged CPyTagged_FromFloat(double f); |
| 159 |
PyObject *CPyLong_FromStrWithBase(PyObject *o, CPyTagged base); |
| 160 |
PyObject *CPyLong_FromStr(PyObject *o); |
| 161 |
PyObject *CPyBool_Str(bool b); |
| 162 |
int64_t CPyLong_AsInt64_(PyObject *o); |
| 163 |
int64_t CPyInt64_Divide(int64_t x, int64_t y); |
| 164 |
int64_t CPyInt64_Remainder(int64_t x, int64_t y); |
| 165 |
int32_t CPyLong_AsInt32_(PyObject *o); |
| 166 |
int32_t CPyInt32_Divide(int32_t x, int32_t y); |
| 167 |
int32_t CPyInt32_Remainder(int32_t x, int32_t y); |
| 168 |
void CPyInt32_Overflow(void); |
| 169 |
int16_t CPyLong_AsInt16_(PyObject *o); |
| 170 |
int16_t CPyInt16_Divide(int16_t x, int16_t y); |
| 171 |
int16_t CPyInt16_Remainder(int16_t x, int16_t y); |
| 172 |
void CPyInt16_Overflow(void); |
| 173 |
uint8_t CPyLong_AsUInt8_(PyObject *o); |
| 174 |
void CPyUInt8_Overflow(void); |
| 175 |
double CPyTagged_TrueDivide(CPyTagged x, CPyTagged y); |
| 176 |
|
| 177 |
static inline int CPyTagged_CheckLong(CPyTagged x) { |
| 178 |
return x & CPY_INT_TAG; |
| 179 |
} |
| 180 |
|
| 181 |
static inline int CPyTagged_CheckShort(CPyTagged x) { |
| 182 |
return !CPyTagged_CheckLong(x); |
| 183 |
} |
| 184 |
|
| 185 |
static inline void CPyTagged_INCREF(CPyTagged x) { |
| 186 |
if (unlikely(CPyTagged_CheckLong(x))) { |
| 187 |
CPyTagged_IncRef(x); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
static inline void CPyTagged_DECREF(CPyTagged x) { |
| 192 |
if (unlikely(CPyTagged_CheckLong(x))) { |
| 193 |
CPyTagged_DecRef(x); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
static inline void CPyTagged_XDECREF(CPyTagged x) { |
| 198 |
if (unlikely(CPyTagged_CheckLong(x))) { |
| 199 |
CPyTagged_XDecRef(x); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
static inline Py_ssize_t CPyTagged_ShortAsSsize_t(CPyTagged x) { |
| 204 |
// NOTE: Assume that we sign extend. |
| 205 |
return (Py_ssize_t)x >> 1; |
| 206 |
} |
| 207 |
|
| 208 |
static inline PyObject *CPyTagged_LongAsObject(CPyTagged x) { |
| 209 |
// NOTE: Assume target is not a short int. |
| 210 |
return (PyObject *)(x & ~CPY_INT_TAG); |
| 211 |
} |
| 212 |
|
| 213 |
static inline CPyTagged CPyTagged_FromObject(PyObject *object) { |
| 214 |
int overflow; |
| 215 |
// The overflow check knows about CPyTagged's width |
| 216 |
Py_ssize_t value = CPyLong_AsSsize_tAndOverflow(object, &overflow); |
| 217 |
if (unlikely(overflow != 0)) { |
| 218 |
Py_INCREF(object); |
| 219 |
return ((CPyTagged)object) | CPY_INT_TAG; |
| 220 |
} else { |
| 221 |
return value << 1; |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
static inline CPyTagged CPyTagged_StealFromObject(PyObject *object) { |
| 226 |
int overflow; |
| 227 |
// The overflow check knows about CPyTagged's width |
| 228 |
Py_ssize_t value = CPyLong_AsSsize_tAndOverflow(object, &overflow); |
| 229 |
if (unlikely(overflow != 0)) { |
| 230 |
return ((CPyTagged)object) | CPY_INT_TAG; |
| 231 |
} else { |
| 232 |
Py_DECREF(object); |
| 233 |
return value << 1; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
static inline CPyTagged CPyTagged_BorrowFromObject(PyObject *object) { |
| 238 |
int overflow; |
| 239 |
// The overflow check knows about CPyTagged's width |
| 240 |
Py_ssize_t value = CPyLong_AsSsize_tAndOverflow(object, &overflow); |
| 241 |
if (unlikely(overflow != 0)) { |
| 242 |
return ((CPyTagged)object) | CPY_INT_TAG; |
| 243 |
} else { |
| 244 |
return value << 1; |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
static inline bool CPyTagged_TooBig(Py_ssize_t value) { |
| 249 |
// Micro-optimized for the common case where it fits. |
| 250 |
return (size_t)value > CPY_TAGGED_MAX |
| 251 |
&& (value >= 0 || value < CPY_TAGGED_MIN); |
| 252 |
} |
| 253 |
|
| 254 |
static inline bool CPyTagged_TooBigInt64(int64_t value) { |
| 255 |
// Micro-optimized for the common case where it fits. |
| 256 |
return (uint64_t)value > CPY_TAGGED_MAX |
| 257 |
&& (value >= 0 || value < CPY_TAGGED_MIN); |
| 258 |
} |
| 259 |
|
| 260 |
static inline bool CPyTagged_IsAddOverflow(CPyTagged sum, CPyTagged left, CPyTagged right) { |
| 261 |
// This check was copied from some of my old code I believe that it works :-) |
| 262 |
return (Py_ssize_t)(sum ^ left) < 0 && (Py_ssize_t)(sum ^ right) < 0; |
| 263 |
} |
| 264 |
|
| 265 |
static inline bool CPyTagged_IsSubtractOverflow(CPyTagged diff, CPyTagged left, CPyTagged right) { |
| 266 |
// This check was copied from some of my old code I believe that it works :-) |
| 267 |
return (Py_ssize_t)(diff ^ left) < 0 && (Py_ssize_t)(diff ^ right) >= 0; |
| 268 |
} |
| 269 |
|
| 270 |
static inline bool CPyTagged_IsMultiplyOverflow(CPyTagged left, CPyTagged right) { |
| 271 |
// This is conservative -- return false only in a small number of all non-overflow cases |
| 272 |
return left >= (1U << (CPY_INT_BITS/2 - 1)) || right >= (1U << (CPY_INT_BITS/2 - 1)); |
| 273 |
} |
| 274 |
|
| 275 |
static inline bool CPyTagged_MaybeFloorDivideFault(CPyTagged left, CPyTagged right) { |
| 276 |
return right == 0 || left == -((size_t)1 << (CPY_INT_BITS-1)); |
| 277 |
} |
| 278 |
|
| 279 |
static inline bool CPyTagged_MaybeRemainderFault(CPyTagged left, CPyTagged right) { |
| 280 |
// Division/modulus can fault when dividing INT_MIN by -1, but we |
| 281 |
// do our mods on still-tagged integers with the low-bit clear, so |
| 282 |
// -1 is actually represented as -2 and can't overflow. |
| 283 |
// Mod by 0 can still fault though. |
| 284 |
return right == 0; |
| 285 |
} |
| 286 |
|
| 287 |
static inline bool CPyTagged_IsEq(CPyTagged left, CPyTagged right) { |
| 288 |
if (CPyTagged_CheckShort(left)) { |
| 289 |
return left == right; |
| 290 |
} else { |
| 291 |
return CPyTagged_IsEq_(left, right); |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
static inline bool CPyTagged_IsNe(CPyTagged left, CPyTagged right) { |
| 296 |
if (CPyTagged_CheckShort(left)) { |
| 297 |
return left != right; |
| 298 |
} else { |
| 299 |
return !CPyTagged_IsEq_(left, right); |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
static inline bool CPyTagged_IsLt(CPyTagged left, CPyTagged right) { |
| 304 |
if (CPyTagged_CheckShort(left) && CPyTagged_CheckShort(right)) { |
| 305 |
return (Py_ssize_t)left < (Py_ssize_t)right; |
| 306 |
} else { |
| 307 |
return CPyTagged_IsLt_(left, right); |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
static inline bool CPyTagged_IsGe(CPyTagged left, CPyTagged right) { |
| 312 |
if (CPyTagged_CheckShort(left) && CPyTagged_CheckShort(right)) { |
| 313 |
return (Py_ssize_t)left >= (Py_ssize_t)right; |
| 314 |
} else { |
| 315 |
return !CPyTagged_IsLt_(left, right); |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
static inline bool CPyTagged_IsGt(CPyTagged left, CPyTagged right) { |
| 320 |
if (CPyTagged_CheckShort(left) && CPyTagged_CheckShort(right)) { |
| 321 |
return (Py_ssize_t)left > (Py_ssize_t)right; |
| 322 |
} else { |
| 323 |
return CPyTagged_IsLt_(right, left); |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
static inline bool CPyTagged_IsLe(CPyTagged left, CPyTagged right) { |
| 328 |
if (CPyTagged_CheckShort(left) && CPyTagged_CheckShort(right)) { |
| 329 |
return (Py_ssize_t)left <= (Py_ssize_t)right; |
| 330 |
} else { |
| 331 |
return !CPyTagged_IsLt_(right, left); |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
static inline int64_t CPyLong_AsInt64(PyObject *o) { |
| 336 |
if (likely(PyLong_Check(o))) { |
| 337 |
PyLongObject *lobj = (PyLongObject *)o; |
| 338 |
#if CPY_3_12_FEATURES |
| 339 |
if (likely(PyUnstable_Long_IsCompact(lobj))) { |
| 340 |
return PyUnstable_Long_CompactValue(lobj); |
| 341 |
} |
| 342 |
#else |
| 343 |
Py_ssize_t size = Py_SIZE(lobj); |
| 344 |
if (likely(size == 1)) { |
| 345 |
// Fast path |
| 346 |
return CPY_LONG_DIGIT(lobj, 0); |
| 347 |
} else if (likely(size == 0)) { |
| 348 |
return 0; |
| 349 |
} |
| 350 |
#endif |
| 351 |
} |
| 352 |
// Slow path |
| 353 |
return CPyLong_AsInt64_(o); |
| 354 |
} |
| 355 |
|
| 356 |
static inline int32_t CPyLong_AsInt32(PyObject *o) { |
| 357 |
if (likely(PyLong_Check(o))) { |
| 358 |
#if CPY_3_12_FEATURES |
| 359 |
PyLongObject *lobj = (PyLongObject *)o; |
| 360 |
size_t tag = CPY_LONG_TAG(lobj); |
| 361 |
if (likely(tag == (1 << CPY_NON_SIZE_BITS))) { |
| 362 |
// Fast path |
| 363 |
return CPY_LONG_DIGIT(lobj, 0); |
| 364 |
} else if (likely(tag == CPY_SIGN_ZERO)) { |
| 365 |
return 0; |
| 366 |
} |
| 367 |
#else |
| 368 |
PyLongObject *lobj = (PyLongObject *)o; |
| 369 |
Py_ssize_t size = lobj->ob_base.ob_size; |
| 370 |
if (likely(size == 1)) { |
| 371 |
// Fast path |
| 372 |
return CPY_LONG_DIGIT(lobj, 0); |
| 373 |
} else if (likely(size == 0)) { |
| 374 |
return 0; |
| 375 |
} |
| 376 |
#endif |
| 377 |
} |
| 378 |
// Slow path |
| 379 |
return CPyLong_AsInt32_(o); |
| 380 |
} |
| 381 |
|
| 382 |
static inline int16_t CPyLong_AsInt16(PyObject *o) { |
| 383 |
if (likely(PyLong_Check(o))) { |
| 384 |
#if CPY_3_12_FEATURES |
| 385 |
PyLongObject *lobj = (PyLongObject *)o; |
| 386 |
size_t tag = CPY_LONG_TAG(lobj); |
| 387 |
if (likely(tag == (1 << CPY_NON_SIZE_BITS))) { |
| 388 |
// Fast path |
| 389 |
digit x = CPY_LONG_DIGIT(lobj, 0); |
| 390 |
if (x < 0x8000) |
| 391 |
return x; |
| 392 |
} else if (likely(tag == CPY_SIGN_ZERO)) { |
| 393 |
return 0; |
| 394 |
} |
| 395 |
#else |
| 396 |
PyLongObject *lobj = (PyLongObject *)o; |
| 397 |
Py_ssize_t size = lobj->ob_base.ob_size; |
| 398 |
if (likely(size == 1)) { |
| 399 |
// Fast path |
| 400 |
digit x = lobj->ob_digit[0]; |
| 401 |
if (x < 0x8000) |
| 402 |
return x; |
| 403 |
} else if (likely(size == 0)) { |
| 404 |
return 0; |
| 405 |
} |
| 406 |
#endif |
| 407 |
} |
| 408 |
// Slow path |
| 409 |
return CPyLong_AsInt16_(o); |
| 410 |
} |
| 411 |
|
| 412 |
static inline uint8_t CPyLong_AsUInt8(PyObject *o) { |
| 413 |
if (likely(PyLong_Check(o))) { |
| 414 |
#if CPY_3_12_FEATURES |
| 415 |
PyLongObject *lobj = (PyLongObject *)o; |
| 416 |
size_t tag = CPY_LONG_TAG(lobj); |
| 417 |
if (likely(tag == (1 << CPY_NON_SIZE_BITS))) { |
| 418 |
// Fast path |
| 419 |
digit x = CPY_LONG_DIGIT(lobj, 0); |
| 420 |
if (x < 256) |
| 421 |
return x; |
| 422 |
} else if (likely(tag == CPY_SIGN_ZERO)) { |
| 423 |
return 0; |
| 424 |
} |
| 425 |
#else |
| 426 |
PyLongObject *lobj = (PyLongObject *)o; |
| 427 |
Py_ssize_t size = lobj->ob_base.ob_size; |
| 428 |
if (likely(size == 1)) { |
| 429 |
// Fast path |
| 430 |
digit x = lobj->ob_digit[0]; |
| 431 |
if (x < 256) |
| 432 |
return x; |
| 433 |
} else if (likely(size == 0)) { |
| 434 |
return 0; |
| 435 |
} |
| 436 |
#endif |
| 437 |
} |
| 438 |
// Slow path |
| 439 |
return CPyLong_AsUInt8_(o); |
| 440 |
} |
| 441 |
|
| 442 |
static inline CPyTagged CPyTagged_Negate(CPyTagged num) { |
| 443 |
if (likely(CPyTagged_CheckShort(num) |
| 444 |
&& num != (CPyTagged) ((Py_ssize_t)1 << (CPY_INT_BITS - 1)))) { |
| 445 |
// The only possibility of an overflow error happening when negating a short is if we |
| 446 |
// attempt to negate the most negative number. |
| 447 |
return -num; |
| 448 |
} |
| 449 |
return CPyTagged_Negate_(num); |
| 450 |
} |
| 451 |
|
| 452 |
static inline CPyTagged CPyTagged_Add(CPyTagged left, CPyTagged right) { |
| 453 |
// TODO: Use clang/gcc extension __builtin_saddll_overflow instead. |
| 454 |
if (likely(CPyTagged_CheckShort(left) && CPyTagged_CheckShort(right))) { |
| 455 |
CPyTagged sum = left + right; |
| 456 |
if (likely(!CPyTagged_IsAddOverflow(sum, left, right))) { |
| 457 |
return sum; |
| 458 |
} |
| 459 |
} |
| 460 |
return CPyTagged_Add_(left, right); |
| 461 |
} |
| 462 |
|
| 463 |
static inline CPyTagged CPyTagged_Subtract(CPyTagged left, CPyTagged right) { |
| 464 |
// TODO: Use clang/gcc extension __builtin_saddll_overflow instead. |
| 465 |
if (likely(CPyTagged_CheckShort(left) && CPyTagged_CheckShort(right))) { |
| 466 |
CPyTagged diff = left - right; |
| 467 |
if (likely(!CPyTagged_IsSubtractOverflow(diff, left, right))) { |
| 468 |
return diff; |
| 469 |
} |
| 470 |
} |
| 471 |
return CPyTagged_Subtract_(left, right); |
| 472 |
} |
| 473 |
|
| 474 |
static inline CPyTagged CPyTagged_Multiply(CPyTagged left, CPyTagged right) { |
| 475 |
// TODO: Consider using some clang/gcc extension to check for overflow |
| 476 |
if (CPyTagged_CheckShort(left) && CPyTagged_CheckShort(right)) { |
| 477 |
if (!CPyTagged_IsMultiplyOverflow(left, right)) { |
| 478 |
return left * CPyTagged_ShortAsSsize_t(right); |
| 479 |
} |
| 480 |
} |
| 481 |
return CPyTagged_Multiply_(left, right); |
| 482 |
} |
| 483 |
|
| 484 |
static inline CPyTagged CPyTagged_FloorDivide(CPyTagged left, CPyTagged right) { |
| 485 |
if (CPyTagged_CheckShort(left) |
| 486 |
&& CPyTagged_CheckShort(right) |
| 487 |
&& !CPyTagged_MaybeFloorDivideFault(left, right)) { |
| 488 |
Py_ssize_t result = CPyTagged_ShortAsSsize_t(left) / CPyTagged_ShortAsSsize_t(right); |
| 489 |
if (((Py_ssize_t)left < 0) != (((Py_ssize_t)right) < 0)) { |
| 490 |
if (result * right != left) { |
| 491 |
// Round down |
| 492 |
result--; |
| 493 |
} |
| 494 |
} |
| 495 |
return result << 1; |
| 496 |
} |
| 497 |
return CPyTagged_FloorDivide_(left, right); |
| 498 |
} |
| 499 |
|
| 500 |
static inline CPyTagged CPyTagged_Remainder(CPyTagged left, CPyTagged right) { |
| 501 |
if (CPyTagged_CheckShort(left) && CPyTagged_CheckShort(right) |
| 502 |
&& !CPyTagged_MaybeRemainderFault(left, right)) { |
| 503 |
Py_ssize_t result = (Py_ssize_t)left % (Py_ssize_t)right; |
| 504 |
if (((Py_ssize_t)right < 0) != ((Py_ssize_t)left < 0) && result != 0) { |
| 505 |
result += right; |
| 506 |
} |
| 507 |
return result; |
| 508 |
} |
| 509 |
return CPyTagged_Remainder_(left, right); |
| 510 |
} |
| 511 |
|
| 512 |
// Bitwise '~' |
| 513 |
static inline CPyTagged CPyTagged_Invert(CPyTagged num) { |
| 514 |
if (likely(CPyTagged_CheckShort(num) && num != CPY_TAGGED_ABS_MIN)) { |
| 515 |
return ~num & ~CPY_INT_TAG; |
| 516 |
} |
| 517 |
return CPyTagged_Invert_(num); |
| 518 |
} |
| 519 |
|
| 520 |
// Bitwise '&' |
| 521 |
static inline CPyTagged CPyTagged_And(CPyTagged left, CPyTagged right) { |
| 522 |
if (likely(CPyTagged_CheckShort(left) && CPyTagged_CheckShort(right))) { |
| 523 |
return left & right; |
| 524 |
} |
| 525 |
return CPyTagged_BitwiseLongOp_(left, right, '&'); |
| 526 |
} |
| 527 |
|
| 528 |
// Bitwise '|' |
| 529 |
static inline CPyTagged CPyTagged_Or(CPyTagged left, CPyTagged right) { |
| 530 |
if (likely(CPyTagged_CheckShort(left) && CPyTagged_CheckShort(right))) { |
| 531 |
return left | right; |
| 532 |
} |
| 533 |
return CPyTagged_BitwiseLongOp_(left, right, '|'); |
| 534 |
} |
| 535 |
|
| 536 |
// Bitwise '^' |
| 537 |
static inline CPyTagged CPyTagged_Xor(CPyTagged left, CPyTagged right) { |
| 538 |
if (likely(CPyTagged_CheckShort(left) && CPyTagged_CheckShort(right))) { |
| 539 |
return left ^ right; |
| 540 |
} |
| 541 |
return CPyTagged_BitwiseLongOp_(left, right, '^'); |
| 542 |
} |
| 543 |
|
| 544 |
// Bitwise '>>' |
| 545 |
static inline CPyTagged CPyTagged_Rshift(CPyTagged left, CPyTagged right) { |
| 546 |
if (likely(CPyTagged_CheckShort(left) |
| 547 |
&& CPyTagged_CheckShort(right) |
| 548 |
&& (Py_ssize_t)right >= 0)) { |
| 549 |
CPyTagged count = CPyTagged_ShortAsSsize_t(right); |
| 550 |
if (unlikely(count >= CPY_INT_BITS)) { |
| 551 |
if ((Py_ssize_t)left >= 0) { |
| 552 |
return 0; |
| 553 |
} else { |
| 554 |
return CPyTagged_ShortFromInt(-1); |
| 555 |
} |
| 556 |
} |
| 557 |
return ((Py_ssize_t)left >> count) & ~CPY_INT_TAG; |
| 558 |
} |
| 559 |
return CPyTagged_Rshift_(left, right); |
| 560 |
} |
| 561 |
|
| 562 |
static inline bool IsShortLshiftOverflow(Py_ssize_t short_int, Py_ssize_t shift) { |
| 563 |
return ((Py_ssize_t)(short_int << shift) >> shift) != short_int; |
| 564 |
} |
| 565 |
|
| 566 |
// Bitwise '<<' |
| 567 |
static inline CPyTagged CPyTagged_Lshift(CPyTagged left, CPyTagged right) { |
| 568 |
if (likely(CPyTagged_CheckShort(left) |
| 569 |
&& CPyTagged_CheckShort(right) |
| 570 |
&& (Py_ssize_t)right >= 0 |
| 571 |
&& right < CPY_INT_BITS * 2)) { |
| 572 |
CPyTagged shift = CPyTagged_ShortAsSsize_t(right); |
| 573 |
if (!IsShortLshiftOverflow(left, shift)) |
| 574 |
// Short integers, no overflow |
| 575 |
return left << shift; |
| 576 |
} |
| 577 |
return CPyTagged_Lshift_(left, right); |
| 578 |
} |
| 579 |
|
| 580 |
|
| 581 |
// Float operations |
| 582 |
|
| 583 |
|
| 584 |
double CPyFloat_FloorDivide(double x, double y); |
| 585 |
double CPyFloat_Pow(double x, double y); |
| 586 |
double CPyFloat_Sin(double x); |
| 587 |
double CPyFloat_Cos(double x); |
| 588 |
double CPyFloat_Tan(double x); |
| 589 |
double CPyFloat_Sqrt(double x); |
| 590 |
double CPyFloat_Exp(double x); |
| 591 |
double CPyFloat_Log(double x); |
| 592 |
CPyTagged CPyFloat_Floor(double x); |
| 593 |
CPyTagged CPyFloat_Ceil(double x); |
| 594 |
double CPyFloat_FromTagged(CPyTagged x); |
| 595 |
bool CPyFloat_IsInf(double x); |
| 596 |
bool CPyFloat_IsNaN(double x); |
| 597 |
|
| 598 |
|
| 599 |
// Generic operations (that work with arbitrary types) |
| 600 |
|
| 601 |
|
| 602 |
/* We use intentionally non-inlined decrefs in rarely executed code |
| 603 |
* paths since it pretty substantially speeds up compile time. We have |
| 604 |
* our own copies both to avoid the null check in Py_DecRef and to avoid |
| 605 |
* making an indirect PIC call. */ |
| 606 |
CPy_NOINLINE |
| 607 |
static void CPy_DecRef(PyObject *p) { |
| 608 |
CPy_DECREF(p); |
| 609 |
} |
| 610 |
|
| 611 |
CPy_NOINLINE |
| 612 |
static void CPy_XDecRef(void *p) { |
| 613 |
CPy_XDECREF((PyObject *)p); |
| 614 |
} |
| 615 |
|
| 616 |
static inline CPyTagged CPyObject_Size(PyObject *obj) { |
| 617 |
Py_ssize_t s = PyObject_Size(obj); |
| 618 |
if (s < 0) { |
| 619 |
return CPY_INT_TAG; |
| 620 |
} else { |
| 621 |
// Technically __len__ could return a really big number, so we |
| 622 |
// should allow this to produce a boxed int. In practice it |
| 623 |
// shouldn't ever if the data structure actually contains all |
| 624 |
// the elements, but... |
| 625 |
return CPyTagged_FromSsize_t(s); |
| 626 |
} |
| 627 |
} |
| 628 |
|
| 629 |
#ifdef MYPYC_LOG_GETATTR |
| 630 |
static void CPy_LogGetAttr(const char *method, PyObject *obj, PyObject *attr) { |
| 631 |
PyObject *module = PyImport_ImportModule("getattr_hook"); |
| 632 |
if (module) { |
| 633 |
PyObject *res = PyObject_CallMethodObjArgs(module, method, obj, attr, NULL); |
| 634 |
Py_XDECREF(res); |
| 635 |
Py_DECREF(module); |
| 636 |
} |
| 637 |
PyErr_Clear(); |
| 638 |
} |
| 639 |
#else |
| 640 |
#define CPy_LogGetAttr(method, obj, attr) (void)0 |
| 641 |
#endif |
| 642 |
|
| 643 |
// Intercept a method call and log it. This needs to be a macro |
| 644 |
// because there is no API that accepts va_args for making a |
| 645 |
// call. Worse, it needs to use the comma operator to return the right |
| 646 |
// value. |
| 647 |
#define CPyObject_CallMethodObjArgs(obj, attr, ...) \ |
| 648 |
(CPy_LogGetAttr("log_method", (obj), (attr)), \ |
| 649 |
PyObject_CallMethodObjArgs((obj), (attr), __VA_ARGS__)) |
| 650 |
|
| 651 |
// This one is a macro for consistency with the above, I guess. |
| 652 |
#define CPyObject_GetAttr(obj, attr) \ |
| 653 |
(CPy_LogGetAttr("log", (obj), (attr)), \ |
| 654 |
PyObject_GetAttr((obj), (attr))) |
| 655 |
|
| 656 |
CPyTagged CPyObject_Hash(PyObject *o); |
| 657 |
PyObject *CPyObject_GetAttr3(PyObject *v, PyObject *name, PyObject *defl); |
| 658 |
PyObject *CPyIter_Next(PyObject *iter); |
| 659 |
PyObject *CPyNumber_Power(PyObject *base, PyObject *index); |
| 660 |
PyObject *CPyNumber_InPlacePower(PyObject *base, PyObject *index); |
| 661 |
PyObject *CPyObject_GetSlice(PyObject *obj, CPyTagged start, CPyTagged end); |
| 662 |
|
| 663 |
|
| 664 |
// List operations |
| 665 |
|
| 666 |
|
| 667 |
#ifndef Py_GIL_DISABLED |
| 668 |
|
| 669 |
PyObject *CPyList_GetItem(PyObject *list, CPyTagged index); |
| 670 |
PyObject *CPyList_GetItemShort(PyObject *list, CPyTagged index); |
| 671 |
PyObject *CPyList_GetItemInt64(PyObject *list, int64_t index); |
| 672 |
bool CPyList_SetItem(PyObject *list, CPyTagged index, PyObject *value); |
| 673 |
bool CPyList_SetItemInt64(PyObject *list, int64_t index, PyObject *value); |
| 674 |
|
| 675 |
#else |
| 676 |
|
| 677 |
PyObject *CPyList_GetItem_(PyObject *list, CPyTagged index); |
| 678 |
bool CPyList_SetItem_(PyObject *list, CPyTagged index, PyObject *value); |
| 679 |
|
| 680 |
static inline PyObject *CPyList_GetItem(PyObject *list, CPyTagged index) { |
| 681 |
if (likely(CPyTagged_CheckShort(index) && !CPyTagged_IsNegative(index))) { |
| 682 |
// Inlined fast path |
| 683 |
Py_ssize_t n = CPyTagged_ShortAsSsize_t(index); |
| 684 |
return PyList_GetItemRef(list, n); |
| 685 |
} else { |
| 686 |
return CPyList_GetItem_(list, index); |
| 687 |
} |
| 688 |
} |
| 689 |
|
| 690 |
static inline PyObject *CPyList_GetItemShort(PyObject *list, CPyTagged index) { |
| 691 |
Py_ssize_t n = CPyTagged_ShortAsSsize_t(index); |
| 692 |
if (n < 0) { |
| 693 |
n += PyList_GET_SIZE(list); |
| 694 |
} |
| 695 |
return PyList_GetItemRef(list, n); |
| 696 |
} |
| 697 |
|
| 698 |
static inline PyObject *CPyList_GetItemInt64(PyObject *list, int64_t index) { |
| 699 |
if (index < 0) { |
| 700 |
index += PyList_GET_SIZE(list); |
| 701 |
} |
| 702 |
return PyList_GetItemRef(list, index); |
| 703 |
} |
| 704 |
|
| 705 |
static inline bool CPyList_SetItem(PyObject *list, CPyTagged index, PyObject *value) { |
| 706 |
if (likely(CPyTagged_CheckShort(index) && !CPyTagged_IsNegative(index))) { |
| 707 |
// Inlined fast path |
| 708 |
Py_ssize_t n = CPyTagged_ShortAsSsize_t(index); |
| 709 |
return PyList_SetItem(list, n, value) >= 0; |
| 710 |
} else { |
| 711 |
return CPyList_SetItem_(list, index, value); |
| 712 |
} |
| 713 |
} |
| 714 |
|
| 715 |
static inline bool CPyList_SetItemInt64(PyObject *list, int64_t index, PyObject *value) { |
| 716 |
if (index < 0) { |
| 717 |
index += PyList_GET_SIZE(list); |
| 718 |
} |
| 719 |
return PyList_SetItem(list, index, value) >= 0; |
| 720 |
} |
| 721 |
|
| 722 |
#endif |
| 723 |
|
| 724 |
PyObject *CPyList_GetItemBorrow(PyObject *list, CPyTagged index); |
| 725 |
PyObject *CPyList_GetItemShortBorrow(PyObject *list, CPyTagged index); |
| 726 |
PyObject *CPyList_GetItemInt64Borrow(PyObject *list, int64_t index); |
| 727 |
void CPyList_SetItemUnsafe(PyObject *list, Py_ssize_t index, PyObject *value); |
| 728 |
PyObject *CPyList_Build(Py_ssize_t len, ...); |
| 729 |
PyObject *CPyList_PopLast(PyObject *obj); |
| 730 |
PyObject *CPyList_Pop(PyObject *obj, CPyTagged index); |
| 731 |
CPyTagged CPyList_Count(PyObject *obj, PyObject *value); |
| 732 |
int CPyList_Insert(PyObject *list, CPyTagged index, PyObject *value); |
| 733 |
PyObject *CPyList_Extend(PyObject *o1, PyObject *o2); |
| 734 |
int CPyList_Remove(PyObject *list, PyObject *obj); |
| 735 |
CPyTagged CPyList_Index(PyObject *list, PyObject *obj); |
| 736 |
PyObject *CPySequence_Sort(PyObject *seq); |
| 737 |
PyObject *CPySequence_Multiply(PyObject *seq, CPyTagged t_size); |
| 738 |
PyObject *CPySequence_RMultiply(CPyTagged t_size, PyObject *seq); |
| 739 |
PyObject *CPySequence_InPlaceMultiply(PyObject *seq, CPyTagged t_size); |
| 740 |
PyObject *CPyList_GetSlice(PyObject *obj, CPyTagged start, CPyTagged end); |
| 741 |
char CPyList_Clear(PyObject *list); |
| 742 |
PyObject *CPyList_Copy(PyObject *list); |
| 743 |
int CPySequence_Check(PyObject *obj); |
| 744 |
|
| 745 |
|
| 746 |
// Dict operations |
| 747 |
|
| 748 |
|
| 749 |
PyObject *CPyDict_GetItem(PyObject *dict, PyObject *key); |
| 750 |
int CPyDict_SetItem(PyObject *dict, PyObject *key, PyObject *value); |
| 751 |
PyObject *CPyDict_Get(PyObject *dict, PyObject *key, PyObject *fallback); |
| 752 |
PyObject *CPyDict_GetWithNone(PyObject *dict, PyObject *key); |
| 753 |
PyObject *CPyDict_SetDefault(PyObject *dict, PyObject *key, PyObject *value); |
| 754 |
PyObject *CPyDict_SetDefaultWithNone(PyObject *dict, PyObject *key); |
| 755 |
PyObject *CPyDict_SetDefaultWithEmptyDatatype(PyObject *dict, PyObject *key, int data_type); |
| 756 |
PyObject *CPyDict_Build(Py_ssize_t size, ...); |
| 757 |
int CPyDict_Update(PyObject *dict, PyObject *stuff); |
| 758 |
int CPyDict_UpdateInDisplay(PyObject *dict, PyObject *stuff); |
| 759 |
int CPyDict_UpdateFromAny(PyObject *dict, PyObject *stuff); |
| 760 |
PyObject *CPyDict_FromAny(PyObject *obj); |
| 761 |
PyObject *CPyDict_KeysView(PyObject *dict); |
| 762 |
PyObject *CPyDict_ValuesView(PyObject *dict); |
| 763 |
PyObject *CPyDict_ItemsView(PyObject *dict); |
| 764 |
PyObject *CPyDict_Keys(PyObject *dict); |
| 765 |
PyObject *CPyDict_Values(PyObject *dict); |
| 766 |
PyObject *CPyDict_Items(PyObject *dict); |
| 767 |
char CPyDict_Clear(PyObject *dict); |
| 768 |
PyObject *CPyDict_Copy(PyObject *dict); |
| 769 |
PyObject *CPyDict_GetKeysIter(PyObject *dict); |
| 770 |
PyObject *CPyDict_GetItemsIter(PyObject *dict); |
| 771 |
PyObject *CPyDict_GetValuesIter(PyObject *dict); |
| 772 |
tuple_T3CIO CPyDict_NextKey(PyObject *dict_or_iter, CPyTagged offset); |
| 773 |
tuple_T3CIO CPyDict_NextValue(PyObject *dict_or_iter, CPyTagged offset); |
| 774 |
tuple_T4CIOO CPyDict_NextItem(PyObject *dict_or_iter, CPyTagged offset); |
| 775 |
int CPyMapping_Check(PyObject *obj); |
| 776 |
|
| 777 |
// Check that dictionary didn't change size during iteration. |
| 778 |
static inline char CPyDict_CheckSize(PyObject *dict, Py_ssize_t size) { |
| 779 |
if (!PyDict_CheckExact(dict)) { |
| 780 |
// Dict subclasses will be checked by Python runtime. |
| 781 |
return 1; |
| 782 |
} |
| 783 |
Py_ssize_t dict_size = PyDict_Size(dict); |
| 784 |
if (size != dict_size) { |
| 785 |
PyErr_SetString(PyExc_RuntimeError, "dictionary changed size during iteration"); |
| 786 |
return 0; |
| 787 |
} |
| 788 |
return 1; |
| 789 |
} |
| 790 |
|
| 791 |
|
| 792 |
// Str operations |
| 793 |
|
| 794 |
// Macros for strip type. These values are copied from CPython. |
| 795 |
#define LEFTSTRIP 0 |
| 796 |
#define RIGHTSTRIP 1 |
| 797 |
#define BOTHSTRIP 2 |
| 798 |
|
| 799 |
char CPyStr_Equal(PyObject *str1, PyObject *str2); |
| 800 |
char CPyStr_EqualLiteral(PyObject *str, PyObject *literal_str, Py_ssize_t literal_length); |
| 801 |
PyObject *CPyStr_Build(Py_ssize_t len, ...); |
| 802 |
PyObject *CPyStr_GetItem(PyObject *str, CPyTagged index); |
| 803 |
PyObject *CPyStr_GetItemUnsafe(PyObject *str, Py_ssize_t index); |
| 804 |
CPyTagged CPyStr_Find(PyObject *str, PyObject *substr, CPyTagged start, int direction); |
| 805 |
CPyTagged CPyStr_FindWithEnd(PyObject *str, PyObject *substr, CPyTagged start, CPyTagged end, int direction); |
| 806 |
PyObject *CPyStr_Split(PyObject *str, PyObject *sep, CPyTagged max_split); |
| 807 |
PyObject *CPyStr_RSplit(PyObject *str, PyObject *sep, CPyTagged max_split); |
| 808 |
PyObject *_CPyStr_Strip(PyObject *self, int strip_type, PyObject *sep); |
| 809 |
static inline PyObject *CPyStr_Strip(PyObject *self, PyObject *sep) { |
| 810 |
return _CPyStr_Strip(self, BOTHSTRIP, sep); |
| 811 |
} |
| 812 |
static inline PyObject *CPyStr_LStrip(PyObject *self, PyObject *sep) { |
| 813 |
return _CPyStr_Strip(self, LEFTSTRIP, sep); |
| 814 |
} |
| 815 |
static inline PyObject *CPyStr_RStrip(PyObject *self, PyObject *sep) { |
| 816 |
return _CPyStr_Strip(self, RIGHTSTRIP, sep); |
| 817 |
} |
| 818 |
PyObject *CPyStr_Replace(PyObject *str, PyObject *old_substr, PyObject *new_substr, CPyTagged max_replace); |
| 819 |
PyObject *CPyStr_Append(PyObject *o1, PyObject *o2); |
| 820 |
PyObject *CPyStr_GetSlice(PyObject *obj, CPyTagged start, CPyTagged end); |
| 821 |
int CPyStr_Startswith(PyObject *self, PyObject *subobj); |
| 822 |
int CPyStr_Endswith(PyObject *self, PyObject *subobj); |
| 823 |
PyObject *CPyStr_Removeprefix(PyObject *self, PyObject *prefix); |
| 824 |
PyObject *CPyStr_Removesuffix(PyObject *self, PyObject *suffix); |
| 825 |
bool CPyStr_IsTrue(PyObject *obj); |
| 826 |
Py_ssize_t CPyStr_Size_size_t(PyObject *str); |
| 827 |
PyObject *CPy_Decode(PyObject *obj, PyObject *encoding, PyObject *errors); |
| 828 |
PyObject *CPy_DecodeUTF8(PyObject *bytes); |
| 829 |
PyObject *CPy_DecodeASCII(PyObject *bytes); |
| 830 |
PyObject *CPy_DecodeLatin1(PyObject *bytes); |
| 831 |
PyObject *CPy_Encode(PyObject *obj, PyObject *encoding, PyObject *errors); |
| 832 |
Py_ssize_t CPyStr_Count(PyObject *unicode, PyObject *substring, CPyTagged start); |
| 833 |
Py_ssize_t CPyStr_CountFull(PyObject *unicode, PyObject *substring, CPyTagged start, CPyTagged end); |
| 834 |
CPyTagged CPyStr_Ord(PyObject *obj); |
| 835 |
PyObject *CPyStr_Multiply(PyObject *str, CPyTagged count); |
| 836 |
PyObject *CPyStr_Lower(PyObject *str); |
| 837 |
PyObject *CPyStr_Upper(PyObject *str); |
| 838 |
bool CPyStr_IsSpace(PyObject *str); |
| 839 |
bool CPyStr_IsAlnum(PyObject *str); |
| 840 |
bool CPyStr_IsDigit(PyObject *str); |
| 841 |
|
| 842 |
// Bytes operations |
| 843 |
|
| 844 |
|
| 845 |
PyObject *CPyBytes_Build(Py_ssize_t len, ...); |
| 846 |
PyObject *CPyBytes_GetSlice(PyObject *obj, CPyTagged start, CPyTagged end); |
| 847 |
CPyTagged CPyBytes_GetItem(PyObject *o, CPyTagged index); |
| 848 |
PyObject *CPyBytes_Concat(PyObject *a, PyObject *b); |
| 849 |
PyObject *CPyBytes_Join(PyObject *sep, PyObject *iter); |
| 850 |
CPyTagged CPyBytes_Ord(PyObject *obj); |
| 851 |
PyObject *CPyBytes_Multiply(PyObject *bytes, CPyTagged count); |
| 852 |
int CPyBytes_Startswith(PyObject *self, PyObject *subobj); |
| 853 |
int CPyBytes_Endswith(PyObject *self, PyObject *subobj); |
| 854 |
int CPyBytes_Compare(PyObject *left, PyObject *right); |
| 855 |
|
| 856 |
|
| 857 |
// Set operations |
| 858 |
|
| 859 |
|
| 860 |
bool CPySet_Remove(PyObject *set, PyObject *key); |
| 861 |
|
| 862 |
|
| 863 |
// Tuple operations |
| 864 |
|
| 865 |
|
| 866 |
PyObject *CPySequenceTuple_GetItem(PyObject *tuple, CPyTagged index); |
| 867 |
PyObject *CPySequenceTuple_GetSlice(PyObject *obj, CPyTagged start, CPyTagged end); |
| 868 |
PyObject *CPySequenceTuple_GetItemUnsafe(PyObject *tuple, Py_ssize_t index); |
| 869 |
void CPySequenceTuple_SetItemUnsafe(PyObject *tuple, Py_ssize_t index, PyObject *value); |
| 870 |
|
| 871 |
|
| 872 |
// Exception operations |
| 873 |
|
| 874 |
|
| 875 |
// mypyc is not very good at dealing with refcount management of |
| 876 |
// pointers that might be NULL. As a workaround for this, the |
| 877 |
// exception APIs that might want to return NULL pointers instead |
| 878 |
// return properly refcounted pointers to this dummy object. |
| 879 |
struct ExcDummyStruct { PyObject_HEAD }; |
| 880 |
extern struct ExcDummyStruct _CPy_ExcDummyStruct; |
| 881 |
extern PyObject *_CPy_ExcDummy; |
| 882 |
|
| 883 |
static inline void _CPy_ToDummy(PyObject **p) { |
| 884 |
if (*p == NULL) { |
| 885 |
Py_INCREF(_CPy_ExcDummy); |
| 886 |
*p = _CPy_ExcDummy; |
| 887 |
} |
| 888 |
} |
| 889 |
|
| 890 |
static inline PyObject *_CPy_FromDummy(PyObject *p) { |
| 891 |
if (p == _CPy_ExcDummy) return NULL; |
| 892 |
Py_INCREF(p); |
| 893 |
return p; |
| 894 |
} |
| 895 |
|
| 896 |
static int CPy_NoErrOccurred(void) { |
| 897 |
return PyErr_Occurred() == NULL; |
| 898 |
} |
| 899 |
|
| 900 |
static inline bool CPy_KeepPropagating(void) { |
| 901 |
return 0; |
| 902 |
} |
| 903 |
// We want to avoid the public PyErr_GetExcInfo API for these because |
| 904 |
// it requires a bunch of spurious refcount traffic on the parts of |
| 905 |
// the triple we don't care about. |
| 906 |
#define CPy_ExcState() PyThreadState_GET()->exc_info |
| 907 |
|
| 908 |
void CPy_Raise(PyObject *exc); |
| 909 |
void CPy_Reraise(void); |
| 910 |
void CPyErr_SetObjectAndTraceback(PyObject *type, PyObject *value, PyObject *traceback); |
| 911 |
tuple_T3OOO CPy_CatchError(void); |
| 912 |
void CPy_RestoreExcInfo(tuple_T3OOO info); |
| 913 |
bool CPy_ExceptionMatches(PyObject *type); |
| 914 |
PyObject *CPy_GetExcValue(void); |
| 915 |
tuple_T3OOO CPy_GetExcInfo(void); |
| 916 |
void _CPy_GetExcInfo(PyObject **p_type, PyObject **p_value, PyObject **p_traceback); |
| 917 |
void CPyError_OutOfMemory(void); |
| 918 |
void CPy_TypeError(const char *expected, PyObject *value); |
| 919 |
void CPy_AddTraceback(const char *filename, const char *funcname, int line, PyObject *globals); |
| 920 |
void CPy_TypeErrorTraceback(const char *filename, const char *funcname, int line, |
| 921 |
PyObject *globals, const char *expected, PyObject *value); |
| 922 |
void CPy_AttributeError(const char *filename, const char *funcname, const char *classname, |
| 923 |
const char *attrname, int line, PyObject *globals); |
| 924 |
|
| 925 |
|
| 926 |
// Misc operations |
| 927 |
|
| 928 |
#define CPy_TRASHCAN_BEGIN(op, dealloc) Py_TRASHCAN_BEGIN(op, dealloc) |
| 929 |
#define CPy_TRASHCAN_END(op) Py_TRASHCAN_END |
| 930 |
|
| 931 |
// Tweaked version of _PyArg_Parser in CPython |
| 932 |
typedef struct CPyArg_Parser { |
| 933 |
const char *format; |
| 934 |
const char * const *keywords; |
| 935 |
const char *fname; |
| 936 |
const char *custom_msg; |
| 937 |
int pos; /* number of positional-only arguments */ |
| 938 |
int min; /* minimal number of arguments */ |
| 939 |
int max; /* maximal number of positional arguments */ |
| 940 |
int has_required_kws; /* are there any keyword-only arguments? */ |
| 941 |
int required_kwonly_start; |
| 942 |
int varargs; /* does the function accept *args or **kwargs? */ |
| 943 |
PyObject *kwtuple; /* tuple of keyword parameter names */ |
| 944 |
struct CPyArg_Parser *next; |
| 945 |
} CPyArg_Parser; |
| 946 |
|
| 947 |
// mypy lets ints silently coerce to floats, so a mypyc runtime float |
| 948 |
// might be an int also |
| 949 |
static inline bool CPyFloat_Check(PyObject *o) { |
| 950 |
return PyFloat_Check(o) || PyLong_Check(o); |
| 951 |
} |
| 952 |
|
| 953 |
// TODO: find an unified way to avoid inline functions in non-C back ends that can not |
| 954 |
// use inline functions |
| 955 |
static inline bool CPy_TypeCheck(PyObject *o, PyObject *type) { |
| 956 |
return PyObject_TypeCheck(o, (PyTypeObject *)type); |
| 957 |
} |
| 958 |
|
| 959 |
static inline PyObject *CPy_TYPE(PyObject *obj) { |
| 960 |
PyObject *result = (PyObject *)Py_TYPE(obj); |
| 961 |
Py_INCREF(result); |
| 962 |
return result; |
| 963 |
} |
| 964 |
|
| 965 |
PyObject *CPy_CalculateMetaclass(PyObject *type, PyObject *o); |
| 966 |
PyObject *CPy_GetCoro(PyObject *obj); |
| 967 |
PyObject *CPyIter_Send(PyObject *iter, PyObject *val); |
| 968 |
int CPy_YieldFromErrorHandle(PyObject *iter, PyObject **outp); |
| 969 |
PyObject *CPy_FetchStopIterationValue(void); |
| 970 |
PyObject *CPyType_FromTemplate(PyObject *template_, |
| 971 |
PyObject *orig_bases, |
| 972 |
PyObject *modname); |
| 973 |
PyObject *CPyType_FromTemplateWrapper(PyObject *template_, |
| 974 |
PyObject *orig_bases, |
| 975 |
PyObject *modname); |
| 976 |
bool CPy_InitSubclass(PyObject *type); |
| 977 |
int CPyDataclass_SleightOfHand(PyObject *dataclass_dec, PyObject *tp, |
| 978 |
PyObject *dict, PyObject *annotations, |
| 979 |
PyObject *dataclass_type); |
| 980 |
PyObject *CPyPickle_SetState(PyObject *obj, PyObject *state); |
| 981 |
PyObject *CPyPickle_GetState(PyObject *obj); |
| 982 |
CPyTagged CPyTagged_Id(PyObject *o); |
| 983 |
void CPyDebug_Print(const char *msg); |
| 984 |
void CPyDebug_PrintObject(PyObject *obj); |
| 985 |
void CPy_Init(void); |
| 986 |
int CPyArg_ParseTupleAndKeywords(PyObject *, PyObject *, |
| 987 |
const char *, const char *, const char * const *, ...); |
| 988 |
int CPyArg_ParseStackAndKeywords(PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames, |
| 989 |
CPyArg_Parser *parser, ...); |
| 990 |
int CPyArg_ParseStackAndKeywordsNoArgs(PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames, |
| 991 |
CPyArg_Parser *parser, ...); |
| 992 |
int CPyArg_ParseStackAndKeywordsOneArg(PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames, |
| 993 |
CPyArg_Parser *parser, ...); |
| 994 |
int CPyArg_ParseStackAndKeywordsSimple(PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames, |
| 995 |
CPyArg_Parser *parser, ...); |
| 996 |
|
| 997 |
int CPySequence_CheckUnpackCount(PyObject *sequence, Py_ssize_t expected); |
| 998 |
int CPyStatics_Initialize(PyObject **statics, |
| 999 |
const char * const *strings, |
| 1000 |
const char * const *bytestrings, |
| 1001 |
const char * const *ints, |
| 1002 |
const double *floats, |
| 1003 |
const double *complex_numbers, |
| 1004 |
const int *tuples, |
| 1005 |
const int *frozensets); |
| 1006 |
PyObject *CPy_Super(PyObject *builtins, PyObject *self); |
| 1007 |
PyObject *CPy_CallReverseOpMethod(PyObject *left, PyObject *right, const char *op, |
| 1008 |
PyObject *method); |
| 1009 |
|
| 1010 |
bool CPyImport_ImportMany(PyObject *modules, CPyModule **statics[], PyObject *globals, |
| 1011 |
PyObject *tb_path, PyObject *tb_function, Py_ssize_t *tb_lines); |
| 1012 |
PyObject *CPyImport_ImportFromMany(PyObject *mod_id, PyObject *names, PyObject *as_names, |
| 1013 |
PyObject *globals); |
| 1014 |
PyObject *CPyImport_GetNativeAttrs(PyObject *mod_id, PyObject *names, PyObject *as_names, |
| 1015 |
PyObject *globals); |
| 1016 |
PyObject *CPyImport_ImportNative(PyObject *module_name, |
| 1017 |
PyObject *(*init_only_fn)(void), |
| 1018 |
int (*exec_fn)(PyObject *), |
| 1019 |
CPyModule **module_static, |
| 1020 |
PyObject *shared_lib_file, PyObject *ext_suffix, |
| 1021 |
Py_ssize_t is_package); |
| 1022 |
int CPyImport_SetDunderAttrs(PyObject *module, PyObject *module_name, PyObject *shared_lib_file, |
| 1023 |
PyObject *ext_suffix, Py_ssize_t is_package); |
| 1024 |
|
| 1025 |
PyObject *CPySingledispatch_RegisterFunction(PyObject *singledispatch_func, PyObject *cls, |
| 1026 |
PyObject *func); |
| 1027 |
|
| 1028 |
PyObject *CPy_GetAIter(PyObject *obj); |
| 1029 |
PyObject *CPy_GetANext(PyObject *aiter); |
| 1030 |
void CPy_SetTypeAliasTypeComputeFunction(PyObject *alias, PyObject *compute_value); |
| 1031 |
void CPyTrace_LogEvent(const char *location, const char *line, const char *op, const char *details); |
| 1032 |
|
| 1033 |
static inline PyObject *CPyObject_GenericGetAttr(PyObject *self, PyObject *name) { |
| 1034 |
return _PyObject_GenericGetAttrWithDict(self, name, NULL, 1); |
| 1035 |
} |
| 1036 |
static inline int CPyObject_GenericSetAttr(PyObject *self, PyObject *name, PyObject *value) { |
| 1037 |
return _PyObject_GenericSetAttrWithDict(self, name, value, NULL); |
| 1038 |
} |
| 1039 |
|
| 1040 |
PyObject *CPy_SetupObject(PyObject *type); |
| 1041 |
|
| 1042 |
typedef struct { |
| 1043 |
PyCMethodObject func; |
| 1044 |
|
| 1045 |
PyObject *func_name; |
| 1046 |
PyObject *func_code; |
| 1047 |
} CPyFunction; |
| 1048 |
|
| 1049 |
PyObject* CPyFunction_New(PyObject *module, const char *filename, const char *funcname, |
| 1050 |
PyCFunction func, int func_flags, const char *func_doc, |
| 1051 |
int first_line, int code_flags, bool has_self_arg); |
| 1052 |
PyObject* CPyFunction_get_name(PyObject *op, void *context); |
| 1053 |
int CPyFunction_set_name(PyObject *op, PyObject *value, void *context); |
| 1054 |
PyObject* CPyFunction_get_code(PyObject *op, void *context); |
| 1055 |
PyObject* CPyFunction_get_defaults(PyObject *op, void *context); |
| 1056 |
PyObject* CPyFunction_get_kwdefaults(PyObject *op, void *context); |
| 1057 |
PyObject* CPyFunction_get_annotations(PyObject *op, void *context); |
| 1058 |
int CPyFunction_set_annotations(PyObject *op, PyObject *value, void *context); |
| 1059 |
|
| 1060 |
#if CPY_3_11_FEATURES |
| 1061 |
PyObject *CPy_GetName(PyObject *obj); |
| 1062 |
#endif |
| 1063 |
|
| 1064 |
#if CPY_3_14_FEATURES |
| 1065 |
void CPy_SetImmortal(PyObject *obj); |
| 1066 |
#endif |
| 1067 |
|
| 1068 |
#ifdef __cplusplus |
| 1069 |
} |
| 1070 |
#endif |
| 1071 |
|
| 1072 |
#endif // CPY_CPY_H |