| 1 |
// Always enable assertions |
| 2 |
#undef NDEBUG |
| 3 |
|
| 4 |
#include "pythoncapi_compat.h" |
| 5 |
|
| 6 |
#ifdef NDEBUG |
| 7 |
# error "assertions must be enabled" |
| 8 |
#endif |
| 9 |
|
| 10 |
#ifdef Py_LIMITED_API |
| 11 |
# error "Py_LIMITED_API is not supported" |
| 12 |
#endif |
| 13 |
|
| 14 |
#if PY_VERSION_HEX >= 0x03000000 |
| 15 |
# define PYTHON3 1 |
| 16 |
#endif |
| 17 |
|
| 18 |
#if defined(__cplusplus) && __cplusplus >= 201402 |
| 19 |
# define MODULE_NAME test_pythoncapi_compat_cpp14ext |
| 20 |
#elif defined(__cplusplus) && __cplusplus >= 201103 |
| 21 |
# define MODULE_NAME test_pythoncapi_compat_cpp11ext |
| 22 |
#elif defined(__cplusplus) && !defined(_MSC_VER) |
| 23 |
# define MODULE_NAME test_pythoncapi_compat_cpp03ext |
| 24 |
#elif defined(__cplusplus) |
| 25 |
# define MODULE_NAME test_pythoncapi_compat_cppext |
| 26 |
#else |
| 27 |
# define MODULE_NAME test_pythoncapi_compat_cext |
| 28 |
#endif |
| 29 |
|
| 30 |
#define _STR(NAME) #NAME |
| 31 |
#define STR(NAME) _STR(NAME) |
| 32 |
#define _CONCAT(a, b) a ## b |
| 33 |
#define CONCAT(a, b) _CONCAT(a, b) |
| 34 |
|
| 35 |
#define MODULE_NAME_STR STR(MODULE_NAME) |
| 36 |
|
| 37 |
// Ignore reference count checks on PyPy |
| 38 |
#ifndef PYPY_VERSION |
| 39 |
# define CHECK_REFCNT |
| 40 |
#endif |
| 41 |
|
| 42 |
// CPython 3.12 beta 1 implements immortal objects (PEP 683) |
| 43 |
#if 0x030C00B1 <= PY_VERSION_HEX && !defined(PYPY_VERSION) |
| 44 |
// Don't check reference count of Python 3.12 immortal objects (ex: bool |
| 45 |
// and str types) |
| 46 |
# define IMMORTAL_OBJS |
| 47 |
#endif |
| 48 |
|
| 49 |
#ifdef CHECK_REFCNT |
| 50 |
# define ASSERT_REFCNT(expr) assert(expr) |
| 51 |
#else |
| 52 |
# define ASSERT_REFCNT(expr) |
| 53 |
#endif |
| 54 |
|
| 55 |
// Marker to check that pointer value was set |
| 56 |
static const char uninitialized[] = "uninitialized"; |
| 57 |
#define UNINITIALIZED_OBJ ((PyObject *)uninitialized) |
| 58 |
#define UNINITIALIZED_INT 0x83ff979 |
| 59 |
|
| 60 |
|
| 61 |
static PyObject* |
| 62 |
create_string(const char *str) |
| 63 |
{ |
| 64 |
PyObject *obj; |
| 65 |
#ifdef PYTHON3 |
| 66 |
obj = PyUnicode_FromString(str); |
| 67 |
#else |
| 68 |
obj = PyString_FromString(str); |
| 69 |
#endif |
| 70 |
assert(obj != _Py_NULL); |
| 71 |
return obj; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
static PyObject * |
| 76 |
test_object(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored)) |
| 77 |
{ |
| 78 |
PyObject *obj = PyList_New(0); |
| 79 |
if (obj == _Py_NULL) { |
| 80 |
return _Py_NULL; |
| 81 |
} |
| 82 |
Py_ssize_t refcnt = Py_REFCNT(obj); |
| 83 |
|
| 84 |
// Py_NewRef() |
| 85 |
PyObject *ref = Py_NewRef(obj); |
| 86 |
assert(ref == obj); |
| 87 |
assert(Py_REFCNT(obj) == (refcnt + 1)); |
| 88 |
Py_DECREF(ref); |
| 89 |
|
| 90 |
// Py_XNewRef() |
| 91 |
PyObject *xref = Py_XNewRef(obj); |
| 92 |
assert(xref == obj); |
| 93 |
assert(Py_REFCNT(obj) == (refcnt + 1)); |
| 94 |
Py_DECREF(xref); |
| 95 |
|
| 96 |
assert(Py_XNewRef(_Py_NULL) == _Py_NULL); |
| 97 |
|
| 98 |
// Py_SETREF() |
| 99 |
PyObject *setref = Py_NewRef(obj); |
| 100 |
PyObject *none = Py_None; |
| 101 |
assert(Py_REFCNT(obj) == (refcnt + 1)); |
| 102 |
|
| 103 |
Py_SETREF(setref, none); |
| 104 |
assert(setref == none); |
| 105 |
assert(Py_REFCNT(obj) == refcnt); |
| 106 |
Py_INCREF(setref); |
| 107 |
|
| 108 |
Py_SETREF(setref, _Py_NULL); |
| 109 |
assert(setref == _Py_NULL); |
| 110 |
|
| 111 |
// Py_XSETREF() |
| 112 |
PyObject *xsetref = _Py_NULL; |
| 113 |
|
| 114 |
Py_INCREF(obj); |
| 115 |
assert(Py_REFCNT(obj) == (refcnt + 1)); |
| 116 |
Py_XSETREF(xsetref, obj); |
| 117 |
assert(xsetref == obj); |
| 118 |
|
| 119 |
Py_XSETREF(xsetref, _Py_NULL); |
| 120 |
assert(Py_REFCNT(obj) == refcnt); |
| 121 |
assert(xsetref == _Py_NULL); |
| 122 |
|
| 123 |
// Py_SET_REFCNT |
| 124 |
Py_SET_REFCNT(obj, Py_REFCNT(obj)); |
| 125 |
// Py_SET_TYPE |
| 126 |
Py_SET_TYPE(obj, Py_TYPE(obj)); |
| 127 |
// Py_SET_SIZE |
| 128 |
Py_SET_SIZE(obj, Py_SIZE(obj)); |
| 129 |
// Py_IS_TYPE() |
| 130 |
int is_type = Py_IS_TYPE(obj, Py_TYPE(obj)); |
| 131 |
assert(is_type); |
| 132 |
|
| 133 |
assert(Py_REFCNT(obj) == refcnt); |
| 134 |
Py_DECREF(obj); |
| 135 |
Py_RETURN_NONE; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
static PyObject * |
| 140 |
test_py_is(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored)) |
| 141 |
{ |
| 142 |
PyObject *o_none = Py_None; |
| 143 |
PyObject *o_true = Py_True; |
| 144 |
PyObject *o_false = Py_False; |
| 145 |
PyObject *obj = PyList_New(0); |
| 146 |
if (obj == _Py_NULL) { |
| 147 |
return _Py_NULL; |
| 148 |
} |
| 149 |
|
| 150 |
/* test Py_Is() */ |
| 151 |
assert(Py_Is(obj, obj)); |
| 152 |
assert(!Py_Is(obj, o_none)); |
| 153 |
|
| 154 |
/* test Py_IsNone() */ |
| 155 |
assert(Py_IsNone(o_none)); |
| 156 |
assert(!Py_IsNone(obj)); |
| 157 |
|
| 158 |
/* test Py_IsTrue() */ |
| 159 |
assert(Py_IsTrue(o_true)); |
| 160 |
assert(!Py_IsTrue(o_false)); |
| 161 |
assert(!Py_IsTrue(obj)); |
| 162 |
|
| 163 |
/* testPy_IsFalse() */ |
| 164 |
assert(Py_IsFalse(o_false)); |
| 165 |
assert(!Py_IsFalse(o_true)); |
| 166 |
assert(!Py_IsFalse(obj)); |
| 167 |
|
| 168 |
Py_DECREF(obj); |
| 169 |
Py_RETURN_NONE; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
#ifndef PYPY_VERSION |
| 174 |
static void |
| 175 |
test_frame_getvar(PyFrameObject *frame) |
| 176 |
{ |
| 177 |
// Make the assumption that test_frame_getvar() is only called by |
| 178 |
// _run_tests() of test_pythoncapi_compat.py and so that the "name" |
| 179 |
// variable exists. |
| 180 |
|
| 181 |
// test PyFrame_GetVar() and PyFrame_GetVarString() |
| 182 |
PyObject *attr = PyUnicode_FromString("name"); |
| 183 |
assert(attr != _Py_NULL); |
| 184 |
PyObject *name1 = PyFrame_GetVar(frame, attr); |
| 185 |
Py_DECREF(attr); |
| 186 |
assert(name1 != _Py_NULL); |
| 187 |
Py_DECREF(name1); |
| 188 |
|
| 189 |
PyObject *name2 = PyFrame_GetVarString(frame, "name"); |
| 190 |
assert(name2 != _Py_NULL); |
| 191 |
Py_DECREF(name2); |
| 192 |
|
| 193 |
// test PyFrame_GetVar() and PyFrame_GetVarString() NameError |
| 194 |
PyObject *attr3 = PyUnicode_FromString("dontexist"); |
| 195 |
assert(attr3 != _Py_NULL); |
| 196 |
PyObject *name3 = PyFrame_GetVar(frame, attr3); |
| 197 |
Py_DECREF(attr3); |
| 198 |
assert(name3 == _Py_NULL); |
| 199 |
assert(PyErr_ExceptionMatches(PyExc_NameError)); |
| 200 |
PyErr_Clear(); |
| 201 |
|
| 202 |
PyObject *name4 = PyFrame_GetVarString(frame, "dontexist"); |
| 203 |
assert(name4 == _Py_NULL); |
| 204 |
assert(PyErr_ExceptionMatches(PyExc_NameError)); |
| 205 |
PyErr_Clear(); |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
static PyObject * |
| 210 |
test_frame(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored)) |
| 211 |
{ |
| 212 |
PyThreadState *tstate = PyThreadState_Get(); |
| 213 |
|
| 214 |
// test PyThreadState_GetFrame() |
| 215 |
PyFrameObject *frame = PyThreadState_GetFrame(tstate); |
| 216 |
if (frame == _Py_NULL) { |
| 217 |
PyErr_SetString(PyExc_AssertionError, "PyThreadState_GetFrame failed"); |
| 218 |
return _Py_NULL; |
| 219 |
} |
| 220 |
|
| 221 |
// test _PyThreadState_GetFrameBorrow() |
| 222 |
Py_ssize_t frame_refcnt = Py_REFCNT(frame); |
| 223 |
PyFrameObject *frame2 = _PyThreadState_GetFrameBorrow(tstate); |
| 224 |
assert(frame2 == frame); |
| 225 |
assert(Py_REFCNT(frame) == frame_refcnt); |
| 226 |
|
| 227 |
// test PyFrame_GetCode() |
| 228 |
PyCodeObject *code = PyFrame_GetCode(frame); |
| 229 |
assert(code != _Py_NULL); |
| 230 |
assert(PyCode_Check(code)); |
| 231 |
|
| 232 |
// test _PyFrame_GetCodeBorrow() |
| 233 |
Py_ssize_t code_refcnt = Py_REFCNT(code); |
| 234 |
PyCodeObject *code2 = _PyFrame_GetCodeBorrow(frame); |
| 235 |
assert(code2 == code); |
| 236 |
assert(Py_REFCNT(code) == code_refcnt); |
| 237 |
Py_DECREF(code); |
| 238 |
|
| 239 |
// PyFrame_GetBack() |
| 240 |
PyFrameObject* back = PyFrame_GetBack(frame); |
| 241 |
if (back != _Py_NULL) { |
| 242 |
assert(PyFrame_Check(back)); |
| 243 |
} |
| 244 |
|
| 245 |
// test _PyFrame_GetBackBorrow() |
| 246 |
if (back != _Py_NULL) { |
| 247 |
Py_ssize_t back_refcnt = Py_REFCNT(back); |
| 248 |
PyFrameObject *back2 = _PyFrame_GetBackBorrow(frame); |
| 249 |
assert(back2 == back); |
| 250 |
assert(Py_REFCNT(back) == back_refcnt); |
| 251 |
} |
| 252 |
else { |
| 253 |
PyFrameObject *back2 = _PyFrame_GetBackBorrow(frame); |
| 254 |
assert(back2 == back); |
| 255 |
} |
| 256 |
Py_XDECREF(back); |
| 257 |
|
| 258 |
// test PyFrame_GetLocals() |
| 259 |
PyObject *locals = PyFrame_GetLocals(frame); |
| 260 |
assert(locals != _Py_NULL); |
| 261 |
// Python 3.13 creates a local proxy |
| 262 |
#if PY_VERSION_HEX < 0x030D0000 |
| 263 |
assert(PyDict_Check(locals)); |
| 264 |
#endif |
| 265 |
|
| 266 |
// test PyFrame_GetGlobals() |
| 267 |
PyObject *globals = PyFrame_GetGlobals(frame); |
| 268 |
assert(globals != _Py_NULL); |
| 269 |
assert(PyDict_Check(globals)); |
| 270 |
|
| 271 |
// test PyFrame_GetBuiltins() |
| 272 |
PyObject *builtins = PyFrame_GetBuiltins(frame); |
| 273 |
assert(builtins != _Py_NULL); |
| 274 |
assert(PyDict_Check(builtins)); |
| 275 |
|
| 276 |
assert(locals != globals); |
| 277 |
assert(globals != builtins); |
| 278 |
assert(builtins != locals); |
| 279 |
|
| 280 |
Py_DECREF(locals); |
| 281 |
Py_DECREF(globals); |
| 282 |
Py_DECREF(builtins); |
| 283 |
|
| 284 |
// test PyFrame_GetLasti() |
| 285 |
int lasti = PyFrame_GetLasti(frame); |
| 286 |
assert(lasti >= 0); |
| 287 |
|
| 288 |
// test PyFrame_GetVar() and PyFrame_GetVarString() |
| 289 |
test_frame_getvar(frame); |
| 290 |
|
| 291 |
// done |
| 292 |
Py_DECREF(frame); |
| 293 |
Py_RETURN_NONE; |
| 294 |
} |
| 295 |
#endif |
| 296 |
|
| 297 |
|
| 298 |
static PyObject * |
| 299 |
test_thread_state(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored)) |
| 300 |
{ |
| 301 |
PyThreadState *tstate = PyThreadState_Get(); |
| 302 |
|
| 303 |
// test PyThreadState_GetInterpreter() |
| 304 |
PyInterpreterState *interp = PyThreadState_GetInterpreter(tstate); |
| 305 |
assert(interp != _Py_NULL); |
| 306 |
|
| 307 |
#ifndef PYPY_VERSION |
| 308 |
// test PyThreadState_GetFrame() |
| 309 |
PyFrameObject *frame = PyThreadState_GetFrame(tstate); |
| 310 |
if (frame != _Py_NULL) { |
| 311 |
assert(PyFrame_Check(frame)); |
| 312 |
} |
| 313 |
Py_XDECREF(frame); |
| 314 |
#endif |
| 315 |
|
| 316 |
#if 0x030700A1 <= PY_VERSION_HEX && !defined(PYPY_VERSION) |
| 317 |
uint64_t id = PyThreadState_GetID(tstate); |
| 318 |
assert(id > 0); |
| 319 |
#endif |
| 320 |
|
| 321 |
#ifndef PYPY_VERSION |
| 322 |
// PyThreadState_EnterTracing(), PyThreadState_LeaveTracing() |
| 323 |
PyThreadState_EnterTracing(tstate); |
| 324 |
PyThreadState_LeaveTracing(tstate); |
| 325 |
#endif |
| 326 |
|
| 327 |
#if PY_VERSION_HEX >= 0x03050200 |
| 328 |
// PyThreadState_GetUnchecked() |
| 329 |
assert(PyThreadState_GetUnchecked() == tstate); |
| 330 |
#endif |
| 331 |
|
| 332 |
Py_RETURN_NONE; |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
static PyObject * |
| 337 |
test_interpreter(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored)) |
| 338 |
{ |
| 339 |
// test PyInterpreterState_Get() |
| 340 |
PyInterpreterState *interp = PyInterpreterState_Get(); |
| 341 |
assert(interp != _Py_NULL); |
| 342 |
PyThreadState *tstate = PyThreadState_Get(); |
| 343 |
PyInterpreterState *interp2 = PyThreadState_GetInterpreter(tstate); |
| 344 |
assert(interp == interp2); |
| 345 |
|
| 346 |
#if 0x030300A1 <= PY_VERSION_HEX && (!defined(PYPY_VERSION_NUM) || PYPY_VERSION_NUM >= 0x7030000) |
| 347 |
// test Py_IsFinalizing() |
| 348 |
assert(Py_IsFinalizing() == 0); |
| 349 |
#endif |
| 350 |
|
| 351 |
Py_RETURN_NONE; |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
static PyObject * |
| 356 |
test_calls(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored)) |
| 357 |
{ |
| 358 |
PyObject *func = _Py_CAST(PyObject*, &PyUnicode_Type); |
| 359 |
|
| 360 |
// test PyObject_CallNoArgs(): str() returns '' |
| 361 |
PyObject *res = PyObject_CallNoArgs(func); |
| 362 |
if (res == _Py_NULL) { |
| 363 |
return _Py_NULL; |
| 364 |
} |
| 365 |
assert(PyUnicode_Check(res)); |
| 366 |
Py_DECREF(res); |
| 367 |
|
| 368 |
// test PyObject_CallOneArg(): str(1) returns '1' |
| 369 |
PyObject *arg = PyLong_FromLong(1); |
| 370 |
if (arg == _Py_NULL) { |
| 371 |
return _Py_NULL; |
| 372 |
} |
| 373 |
res = PyObject_CallOneArg(func, arg); |
| 374 |
Py_DECREF(arg); |
| 375 |
if (res == _Py_NULL) { |
| 376 |
return _Py_NULL; |
| 377 |
} |
| 378 |
assert(PyUnicode_Check(res)); |
| 379 |
Py_DECREF(res); |
| 380 |
|
| 381 |
Py_RETURN_NONE; |
| 382 |
} |
| 383 |
|
| 384 |
|
| 385 |
static PyObject * |
| 386 |
test_gc(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored)) |
| 387 |
{ |
| 388 |
PyObject *tuple = PyTuple_New(1); |
| 389 |
Py_INCREF(Py_None); |
| 390 |
PyTuple_SET_ITEM(tuple, 0, Py_None); |
| 391 |
|
| 392 |
#if !defined(PYPY_VERSION) |
| 393 |
// test PyObject_GC_IsTracked() |
| 394 |
int tracked = PyObject_GC_IsTracked(tuple); |
| 395 |
assert(tracked); |
| 396 |
#endif |
| 397 |
|
| 398 |
#if PY_VERSION_HEX >= 0x030400F0 && !defined(PYPY_VERSION) |
| 399 |
// test PyObject_GC_IsFinalized() |
| 400 |
int finalized = PyObject_GC_IsFinalized(tuple); |
| 401 |
assert(!finalized); |
| 402 |
#endif |
| 403 |
|
| 404 |
Py_DECREF(tuple); |
| 405 |
Py_RETURN_NONE; |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
static int |
| 410 |
check_module_attr(PyObject *module, const char *name, PyObject *expected) |
| 411 |
{ |
| 412 |
PyObject *attr = PyObject_GetAttrString(module, name); |
| 413 |
if (attr == _Py_NULL) { |
| 414 |
return -1; |
| 415 |
} |
| 416 |
assert(attr == expected); |
| 417 |
Py_DECREF(attr); |
| 418 |
|
| 419 |
if (PyObject_DelAttrString(module, name) < 0) { |
| 420 |
return -1; |
| 421 |
} |
| 422 |
return 0; |
| 423 |
} |
| 424 |
|
| 425 |
|
| 426 |
// test PyModule_AddType() |
| 427 |
static int |
| 428 |
test_module_add_type(PyObject *module) |
| 429 |
{ |
| 430 |
PyTypeObject *type = &PyUnicode_Type; |
| 431 |
#ifdef PYTHON3 |
| 432 |
const char *type_name = "str"; |
| 433 |
#else |
| 434 |
const char *type_name = "unicode"; |
| 435 |
#endif |
| 436 |
#ifdef CHECK_REFCNT |
| 437 |
Py_ssize_t refcnt = Py_REFCNT(type); |
| 438 |
#endif |
| 439 |
|
| 440 |
if (PyModule_AddType(module, type) < 0) { |
| 441 |
return -1; |
| 442 |
} |
| 443 |
#ifndef IMMORTAL_OBJS |
| 444 |
ASSERT_REFCNT(Py_REFCNT(type) == refcnt + 1); |
| 445 |
#endif |
| 446 |
|
| 447 |
if (check_module_attr(module, type_name, _Py_CAST(PyObject*, type)) < 0) { |
| 448 |
return -1; |
| 449 |
} |
| 450 |
ASSERT_REFCNT(Py_REFCNT(type) == refcnt); |
| 451 |
return 0; |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
// test PyModule_AddObjectRef() |
| 456 |
static int |
| 457 |
test_module_addobjectref(PyObject *module) |
| 458 |
{ |
| 459 |
const char *name = "test_module_addobjectref"; |
| 460 |
PyObject *obj = PyUnicode_FromString(name); |
| 461 |
assert(obj != _Py_NULL); |
| 462 |
#ifdef CHECK_REFCNT |
| 463 |
Py_ssize_t refcnt = Py_REFCNT(obj); |
| 464 |
#endif |
| 465 |
|
| 466 |
if (PyModule_AddObjectRef(module, name, obj) < 0) { |
| 467 |
ASSERT_REFCNT(Py_REFCNT(obj) == refcnt); |
| 468 |
Py_DECREF(obj); |
| 469 |
return -1; |
| 470 |
} |
| 471 |
ASSERT_REFCNT(Py_REFCNT(obj) == refcnt + 1); |
| 472 |
|
| 473 |
if (check_module_attr(module, name, obj) < 0) { |
| 474 |
Py_DECREF(obj); |
| 475 |
return -1; |
| 476 |
} |
| 477 |
ASSERT_REFCNT(Py_REFCNT(obj) == refcnt); |
| 478 |
|
| 479 |
// PyModule_AddObjectRef() with value=NULL must not crash |
| 480 |
assert(!PyErr_Occurred()); |
| 481 |
int res = PyModule_AddObjectRef(module, name, _Py_NULL); |
| 482 |
assert(res < 0); |
| 483 |
assert(PyErr_ExceptionMatches(PyExc_SystemError)); |
| 484 |
PyErr_Clear(); |
| 485 |
|
| 486 |
Py_DECREF(obj); |
| 487 |
return 0; |
| 488 |
} |
| 489 |
|
| 490 |
|
| 491 |
// test PyModule_Add() |
| 492 |
static int |
| 493 |
test_module_add(PyObject *module) |
| 494 |
{ |
| 495 |
const char *name = "test_module_add"; |
| 496 |
PyObject *obj = PyUnicode_FromString(name); |
| 497 |
assert(obj != _Py_NULL); |
| 498 |
#ifdef CHECK_REFCNT |
| 499 |
Py_ssize_t refcnt = Py_REFCNT(obj); |
| 500 |
#endif |
| 501 |
|
| 502 |
if (PyModule_Add(module, name, Py_NewRef(obj)) < 0) { |
| 503 |
ASSERT_REFCNT(Py_REFCNT(obj) == refcnt); |
| 504 |
Py_DECREF(obj); |
| 505 |
return -1; |
| 506 |
} |
| 507 |
ASSERT_REFCNT(Py_REFCNT(obj) == refcnt + 1); |
| 508 |
|
| 509 |
if (check_module_attr(module, name, obj) < 0) { |
| 510 |
Py_DECREF(obj); |
| 511 |
return -1; |
| 512 |
} |
| 513 |
ASSERT_REFCNT(Py_REFCNT(obj) == refcnt); |
| 514 |
|
| 515 |
// PyModule_Add() with value=NULL must not crash |
| 516 |
assert(!PyErr_Occurred()); |
| 517 |
int res = PyModule_Add(module, name, _Py_NULL); |
| 518 |
assert(res < 0); |
| 519 |
assert(PyErr_ExceptionMatches(PyExc_SystemError)); |
| 520 |
PyErr_Clear(); |
| 521 |
|
| 522 |
Py_DECREF(obj); |
| 523 |
return 0; |
| 524 |
} |
| 525 |
|
| 526 |
|
| 527 |
static PyObject * |
| 528 |
test_module(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored)) |
| 529 |
{ |
| 530 |
PyObject *module = PyImport_ImportModule("sys"); |
| 531 |
if (module == _Py_NULL) { |
| 532 |
return _Py_NULL; |
| 533 |
} |
| 534 |
assert(PyModule_Check(module)); |
| 535 |
|
| 536 |
if (test_module_add_type(module) < 0) { |
| 537 |
goto error; |
| 538 |
} |
| 539 |
if (test_module_addobjectref(module) < 0) { |
| 540 |
goto error; |
| 541 |
} |
| 542 |
if (test_module_add(module) < 0) { |
| 543 |
goto error; |
| 544 |
} |
| 545 |
|
| 546 |
Py_DECREF(module); |
| 547 |
Py_RETURN_NONE; |
| 548 |
|
| 549 |
error: |
| 550 |
Py_DECREF(module); |
| 551 |
return _Py_NULL; |
| 552 |
} |
| 553 |
|
| 554 |
|
| 555 |
#if (PY_VERSION_HEX <= 0x030B00A1 || 0x030B00A7 <= PY_VERSION_HEX) && !defined(PYPY_VERSION) |
| 556 |
static PyObject * |
| 557 |
test_float_pack(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored)) |
| 558 |
{ |
| 559 |
const int big_endian = 0; |
| 560 |
const int little_endian = 1; |
| 561 |
char data[8]; |
| 562 |
const double d = 1.5; |
| 563 |
|
| 564 |
#if PY_VERSION_HEX >= 0x030600B1 |
| 565 |
# define HAVE_FLOAT_PACK2 |
| 566 |
#endif |
| 567 |
|
| 568 |
// Test Pack (big endian) |
| 569 |
#ifdef HAVE_FLOAT_PACK2 |
| 570 |
assert(PyFloat_Pack2(d, data, big_endian) == 0); |
| 571 |
assert(memcmp(data, ">\x00", 2) == 0); |
| 572 |
#endif |
| 573 |
|
| 574 |
assert(PyFloat_Pack4(d, data, big_endian) == 0); |
| 575 |
assert(memcmp(data, "?\xc0\x00\x00", 2) == 0); |
| 576 |
|
| 577 |
assert(PyFloat_Pack8(d, data, big_endian) == 0); |
| 578 |
assert(memcmp(data, "?\xf8\x00\x00\x00\x00\x00\x00", 2) == 0); |
| 579 |
|
| 580 |
// Test Pack (little endian) |
| 581 |
#ifdef HAVE_FLOAT_PACK2 |
| 582 |
assert(PyFloat_Pack2(d, data, little_endian) == 0); |
| 583 |
assert(memcmp(data, "\x00>", 2) == 0); |
| 584 |
#endif |
| 585 |
|
| 586 |
assert(PyFloat_Pack4(d, data, little_endian) == 0); |
| 587 |
assert(memcmp(data, "\x00\x00\xc0?", 2) == 0); |
| 588 |
|
| 589 |
assert(PyFloat_Pack8(d, data, little_endian) == 0); |
| 590 |
assert(memcmp(data, "\x00\x00\x00\x00\x00\x00\xf8?", 2) == 0); |
| 591 |
|
| 592 |
// Test Unpack (big endian) |
| 593 |
#ifdef HAVE_FLOAT_PACK2 |
| 594 |
assert(PyFloat_Unpack2(">\x00", big_endian) == d); |
| 595 |
#endif |
| 596 |
assert(PyFloat_Unpack4("?\xc0\x00\x00", big_endian) == d); |
| 597 |
assert(PyFloat_Unpack8("?\xf8\x00\x00\x00\x00\x00\x00", big_endian) == d); |
| 598 |
|
| 599 |
// Test Unpack (little endian) |
| 600 |
#ifdef HAVE_FLOAT_PACK2 |
| 601 |
assert(PyFloat_Unpack2("\x00>", little_endian) == d); |
| 602 |
#endif |
| 603 |
assert(PyFloat_Unpack4("\x00\x00\xc0?", little_endian) == d); |
| 604 |
assert(PyFloat_Unpack8("\x00\x00\x00\x00\x00\x00\xf8?", little_endian) == d); |
| 605 |
|
| 606 |
Py_RETURN_NONE; |
| 607 |
} |
| 608 |
#endif |
| 609 |
|
| 610 |
|
| 611 |
#if !defined(PYPY_VERSION) |
| 612 |
static PyObject * |
| 613 |
test_code(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored)) |
| 614 |
{ |
| 615 |
PyThreadState *tstate = PyThreadState_Get(); |
| 616 |
PyFrameObject *frame = PyThreadState_GetFrame(tstate); |
| 617 |
if (frame == _Py_NULL) { |
| 618 |
PyErr_SetString(PyExc_AssertionError, "PyThreadState_GetFrame failed"); |
| 619 |
return _Py_NULL; |
| 620 |
} |
| 621 |
PyCodeObject *code = PyFrame_GetCode(frame); |
| 622 |
|
| 623 |
// PyCode_GetCode() |
| 624 |
{ |
| 625 |
PyObject *co_code = PyCode_GetCode(code); |
| 626 |
assert(co_code != _Py_NULL); |
| 627 |
assert(PyBytes_Check(co_code)); |
| 628 |
Py_DECREF(co_code); |
| 629 |
} |
| 630 |
|
| 631 |
// PyCode_GetVarnames |
| 632 |
{ |
| 633 |
PyObject *co_varnames = PyCode_GetVarnames(code); |
| 634 |
assert(co_varnames != _Py_NULL); |
| 635 |
assert(PyTuple_CheckExact(co_varnames)); |
| 636 |
assert(PyTuple_GET_SIZE(co_varnames) != 0); |
| 637 |
Py_DECREF(co_varnames); |
| 638 |
} |
| 639 |
|
| 640 |
// PyCode_GetCellvars |
| 641 |
{ |
| 642 |
PyObject *co_cellvars = PyCode_GetCellvars(code); |
| 643 |
assert(co_cellvars != _Py_NULL); |
| 644 |
assert(PyTuple_CheckExact(co_cellvars)); |
| 645 |
assert(PyTuple_GET_SIZE(co_cellvars) == 0); |
| 646 |
Py_DECREF(co_cellvars); |
| 647 |
} |
| 648 |
|
| 649 |
// PyCode_GetFreevars |
| 650 |
{ |
| 651 |
PyObject *co_freevars = PyCode_GetFreevars(code); |
| 652 |
assert(co_freevars != _Py_NULL); |
| 653 |
assert(PyTuple_CheckExact(co_freevars)); |
| 654 |
assert(PyTuple_GET_SIZE(co_freevars) == 0); |
| 655 |
Py_DECREF(co_freevars); |
| 656 |
} |
| 657 |
|
| 658 |
Py_DECREF(code); |
| 659 |
Py_DECREF(frame); |
| 660 |
Py_RETURN_NONE; |
| 661 |
} |
| 662 |
#endif |
| 663 |
|
| 664 |
|
| 665 |
#ifdef __cplusplus |
| 666 |
// Class to test operator casting an object to PyObject* |
| 667 |
class StrongRef |
| 668 |
{ |
| 669 |
public: |
| 670 |
StrongRef(PyObject *obj) : m_obj(obj) { |
| 671 |
Py_INCREF(this->m_obj); |
| 672 |
} |
| 673 |
|
| 674 |
~StrongRef() { |
| 675 |
Py_DECREF(this->m_obj); |
| 676 |
} |
| 677 |
|
| 678 |
// Cast to PyObject*: get a borrowed reference |
| 679 |
inline operator PyObject*() const { return this->m_obj; } |
| 680 |
|
| 681 |
private: |
| 682 |
PyObject *m_obj; // Strong reference |
| 683 |
}; |
| 684 |
#endif |
| 685 |
|
| 686 |
|
| 687 |
static PyObject * |
| 688 |
test_api_casts(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args)) |
| 689 |
{ |
| 690 |
PyObject *obj = Py_BuildValue("(ii)", 1, 2); |
| 691 |
if (obj == _Py_NULL) { |
| 692 |
return _Py_NULL; |
| 693 |
} |
| 694 |
Py_ssize_t refcnt = Py_REFCNT(obj); |
| 695 |
assert(refcnt >= 1); |
| 696 |
|
| 697 |
// gh-92138: For backward compatibility, functions of Python C API accepts |
| 698 |
// "const PyObject*". Check that using it does not emit C++ compiler |
| 699 |
// warnings. |
| 700 |
const PyObject *const_obj = obj; |
| 701 |
Py_INCREF(const_obj); |
| 702 |
Py_DECREF(const_obj); |
| 703 |
PyTypeObject *type = Py_TYPE(const_obj); |
| 704 |
assert(Py_REFCNT(const_obj) == refcnt); |
| 705 |
assert(type == &PyTuple_Type); |
| 706 |
assert(PyTuple_GET_SIZE(const_obj) == 2); |
| 707 |
PyObject *one = PyTuple_GET_ITEM(const_obj, 0); |
| 708 |
assert(PyLong_AsLong(one) == 1); |
| 709 |
|
| 710 |
#ifdef __cplusplus |
| 711 |
// gh-92898: StrongRef doesn't inherit from PyObject but has an operator to |
| 712 |
// cast to PyObject*. |
| 713 |
StrongRef strong_ref(obj); |
| 714 |
assert(Py_TYPE(strong_ref) == &PyTuple_Type); |
| 715 |
assert(Py_REFCNT(strong_ref) == (refcnt + 1)); |
| 716 |
Py_INCREF(strong_ref); |
| 717 |
Py_DECREF(strong_ref); |
| 718 |
#endif |
| 719 |
|
| 720 |
// gh-93442: Pass 0 as NULL for PyObject* |
| 721 |
Py_XINCREF(0); |
| 722 |
Py_XDECREF(0); |
| 723 |
#if _cplusplus >= 201103 |
| 724 |
// Test nullptr passed as PyObject* |
| 725 |
Py_XINCREF(nullptr); |
| 726 |
Py_XDECREF(nullptr); |
| 727 |
#endif |
| 728 |
|
| 729 |
Py_DECREF(obj); |
| 730 |
Py_RETURN_NONE; |
| 731 |
} |
| 732 |
|
| 733 |
|
| 734 |
static PyObject * |
| 735 |
test_import(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args)) |
| 736 |
{ |
| 737 |
PyObject *mod = PyImport_ImportModule("sys"); |
| 738 |
if (mod == _Py_NULL) { |
| 739 |
return _Py_NULL; |
| 740 |
} |
| 741 |
Py_ssize_t refcnt = Py_REFCNT(mod); |
| 742 |
|
| 743 |
// test PyImport_AddModuleRef() |
| 744 |
PyObject *mod2 = PyImport_AddModuleRef("sys"); |
| 745 |
if (mod2 == _Py_NULL) { |
| 746 |
return _Py_NULL; |
| 747 |
} |
| 748 |
assert(PyModule_Check(mod2)); |
| 749 |
assert(Py_REFCNT(mod) == (refcnt + 1)); |
| 750 |
|
| 751 |
Py_DECREF(mod2); |
| 752 |
Py_DECREF(mod); |
| 753 |
|
| 754 |
Py_RETURN_NONE; |
| 755 |
} |
| 756 |
|
| 757 |
|
| 758 |
static void |
| 759 |
gc_collect(void) |
| 760 |
{ |
| 761 |
#if defined(PYPY_VERSION) && PY_VERSION_HEX < 0x030B0000 |
| 762 |
PyObject *mod = PyImport_ImportModule("gc"); |
| 763 |
assert(mod != _Py_NULL); |
| 764 |
|
| 765 |
PyObject *res = PyObject_CallMethod(mod, "collect", _Py_NULL); |
| 766 |
Py_DECREF(mod); |
| 767 |
assert(res != _Py_NULL); |
| 768 |
Py_DECREF(res); |
| 769 |
#else |
| 770 |
PyGC_Collect(); |
| 771 |
#endif |
| 772 |
} |
| 773 |
|
| 774 |
|
| 775 |
static PyObject * |
| 776 |
func_varargs(PyObject *Py_UNUSED(module), PyObject *args, PyObject *kwargs) |
| 777 |
{ |
| 778 |
if (kwargs != _Py_NULL) { |
| 779 |
return PyTuple_Pack(2, args, kwargs); |
| 780 |
} |
| 781 |
else { |
| 782 |
return PyTuple_Pack(1, args); |
| 783 |
} |
| 784 |
} |
| 785 |
|
| 786 |
|
| 787 |
static void |
| 788 |
check_int(PyObject *obj, int value) |
| 789 |
{ |
| 790 |
#ifdef PYTHON3 |
| 791 |
assert(PyLong_Check(obj)); |
| 792 |
assert(PyLong_AsLong(obj) == value); |
| 793 |
#else |
| 794 |
assert(PyInt_Check(obj)); |
| 795 |
assert(PyInt_AsLong(obj) == value); |
| 796 |
#endif |
| 797 |
} |
| 798 |
|
| 799 |
|
| 800 |
static PyObject * |
| 801 |
test_weakref(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args)) |
| 802 |
{ |
| 803 |
// Create a new heap type, create an instance of this type, and delete the |
| 804 |
// type. This object supports weak references. |
| 805 |
PyObject *new_type = PyObject_CallFunction((PyObject*)&PyType_Type, |
| 806 |
"s(){}", "TypeName"); |
| 807 |
if (new_type == _Py_NULL) { |
| 808 |
return _Py_NULL; |
| 809 |
} |
| 810 |
PyObject *obj = PyObject_CallNoArgs(new_type); |
| 811 |
Py_DECREF(new_type); |
| 812 |
if (obj == _Py_NULL) { |
| 813 |
return _Py_NULL; |
| 814 |
} |
| 815 |
Py_ssize_t refcnt = Py_REFCNT(obj); |
| 816 |
|
| 817 |
// create a weak reference |
| 818 |
PyObject *weakref = PyWeakref_NewRef(obj, _Py_NULL); |
| 819 |
if (weakref == _Py_NULL) { |
| 820 |
return _Py_NULL; |
| 821 |
} |
| 822 |
|
| 823 |
// test PyWeakref_GetRef(), reference is alive |
| 824 |
PyObject *ref = UNINITIALIZED_OBJ; |
| 825 |
assert(PyWeakref_GetRef(weakref, &ref) == 1); |
| 826 |
assert(ref == obj); |
| 827 |
assert(Py_REFCNT(obj) == (refcnt + 1)); |
| 828 |
Py_DECREF(ref); |
| 829 |
|
| 830 |
// delete the referenced object: clear the weakref |
| 831 |
Py_DECREF(obj); |
| 832 |
gc_collect(); |
| 833 |
|
| 834 |
// test PyWeakref_GetRef(), reference is dead |
| 835 |
ref = Py_True; |
| 836 |
assert(PyWeakref_GetRef(weakref, &ref) == 0); |
| 837 |
assert(ref == _Py_NULL); |
| 838 |
|
| 839 |
// test PyWeakref_GetRef(), invalid type |
| 840 |
PyObject *invalid_weakref = Py_None; |
| 841 |
assert(!PyErr_Occurred()); |
| 842 |
ref = Py_True; |
| 843 |
assert(PyWeakref_GetRef(invalid_weakref, &ref) == -1); |
| 844 |
assert(PyErr_ExceptionMatches(PyExc_TypeError)); |
| 845 |
assert(ref == _Py_NULL); |
| 846 |
PyErr_Clear(); |
| 847 |
|
| 848 |
#ifndef PYPY_VERSION |
| 849 |
// test PyWeakref_GetRef(NULL) |
| 850 |
ref = Py_True; |
| 851 |
assert(PyWeakref_GetRef(_Py_NULL, &ref) == -1); |
| 852 |
assert(PyErr_ExceptionMatches(PyExc_SystemError)); |
| 853 |
assert(ref == _Py_NULL); |
| 854 |
PyErr_Clear(); |
| 855 |
#endif |
| 856 |
|
| 857 |
Py_DECREF(weakref); |
| 858 |
Py_RETURN_NONE; |
| 859 |
} |
| 860 |
|
| 861 |
|
| 862 |
static void |
| 863 |
test_vectorcall_noargs(PyObject *func_varargs) |
| 864 |
{ |
| 865 |
PyObject *res = PyObject_Vectorcall(func_varargs, _Py_NULL, 0, _Py_NULL); |
| 866 |
assert(res != _Py_NULL); |
| 867 |
|
| 868 |
assert(PyTuple_Check(res)); |
| 869 |
assert(PyTuple_GET_SIZE(res) == 1); |
| 870 |
PyObject *posargs = PyTuple_GET_ITEM(res, 0); |
| 871 |
|
| 872 |
assert(PyTuple_Check(posargs)); |
| 873 |
assert(PyTuple_GET_SIZE(posargs) == 0); |
| 874 |
|
| 875 |
Py_DECREF(res); |
| 876 |
} |
| 877 |
|
| 878 |
|
| 879 |
static void |
| 880 |
test_vectorcall_args(PyObject *func_varargs) |
| 881 |
{ |
| 882 |
PyObject *args_tuple = Py_BuildValue("ii", 1, 2); |
| 883 |
assert(args_tuple != _Py_NULL); |
| 884 |
size_t nargs = (size_t)PyTuple_GET_SIZE(args_tuple); |
| 885 |
PyObject **args = &PyTuple_GET_ITEM(args_tuple, 0); |
| 886 |
|
| 887 |
PyObject *res = PyObject_Vectorcall(func_varargs, args, nargs, _Py_NULL); |
| 888 |
Py_DECREF(args_tuple); |
| 889 |
assert(res != _Py_NULL); |
| 890 |
|
| 891 |
assert(PyTuple_Check(res)); |
| 892 |
assert(PyTuple_GET_SIZE(res) == 1); |
| 893 |
PyObject *posargs = PyTuple_GET_ITEM(res, 0); |
| 894 |
|
| 895 |
assert(PyTuple_Check(posargs)); |
| 896 |
assert(PyTuple_GET_SIZE(posargs) == 2); |
| 897 |
check_int(PyTuple_GET_ITEM(posargs, 0), 1); |
| 898 |
check_int(PyTuple_GET_ITEM(posargs, 1), 2); |
| 899 |
|
| 900 |
Py_DECREF(res); |
| 901 |
} |
| 902 |
|
| 903 |
|
| 904 |
static void |
| 905 |
test_vectorcall_args_offset(PyObject *func_varargs) |
| 906 |
{ |
| 907 |
// args contains 3 values, but only pass 2 last values |
| 908 |
PyObject *args_tuple = Py_BuildValue("iii", 1, 2, 3); |
| 909 |
assert(args_tuple != _Py_NULL); |
| 910 |
size_t nargs = 2 | PY_VECTORCALL_ARGUMENTS_OFFSET; |
| 911 |
PyObject **args = &PyTuple_GET_ITEM(args_tuple, 1); |
| 912 |
PyObject *arg0 = PyTuple_GET_ITEM(args_tuple, 0); |
| 913 |
|
| 914 |
PyObject *res = PyObject_Vectorcall(func_varargs, args, nargs, _Py_NULL); |
| 915 |
assert(PyTuple_GET_ITEM(args_tuple, 0) == arg0); |
| 916 |
Py_DECREF(args_tuple); |
| 917 |
assert(res != _Py_NULL); |
| 918 |
|
| 919 |
assert(PyTuple_Check(res)); |
| 920 |
assert(PyTuple_GET_SIZE(res) == 1); |
| 921 |
PyObject *posargs = PyTuple_GET_ITEM(res, 0); |
| 922 |
|
| 923 |
assert(PyTuple_Check(posargs)); |
| 924 |
assert(PyTuple_GET_SIZE(posargs) == 2); |
| 925 |
check_int(PyTuple_GET_ITEM(posargs, 0), 2); |
| 926 |
check_int(PyTuple_GET_ITEM(posargs, 1), 3); |
| 927 |
|
| 928 |
Py_DECREF(res); |
| 929 |
} |
| 930 |
|
| 931 |
|
| 932 |
static void |
| 933 |
test_vectorcall_args_kwnames(PyObject *func_varargs) |
| 934 |
{ |
| 935 |
PyObject *args_tuple = Py_BuildValue("iiiii", 1, 2, 3, 4, 5); |
| 936 |
assert(args_tuple != _Py_NULL); |
| 937 |
PyObject **args = &PyTuple_GET_ITEM(args_tuple, 0); |
| 938 |
|
| 939 |
#ifdef PYTHON3 |
| 940 |
PyObject *key1 = PyUnicode_FromString("key1"); |
| 941 |
PyObject *key2 = PyUnicode_FromString("key2"); |
| 942 |
#else |
| 943 |
PyObject *key1 = PyString_FromString("key1"); |
| 944 |
PyObject *key2 = PyString_FromString("key2"); |
| 945 |
#endif |
| 946 |
assert(key1 != _Py_NULL); |
| 947 |
assert(key2 != _Py_NULL); |
| 948 |
PyObject *kwnames = PyTuple_Pack(2, key1, key2); |
| 949 |
assert(kwnames != _Py_NULL); |
| 950 |
size_t nargs = (size_t)(PyTuple_GET_SIZE(args_tuple) - PyTuple_GET_SIZE(kwnames)); |
| 951 |
|
| 952 |
PyObject *res = PyObject_Vectorcall(func_varargs, args, nargs, kwnames); |
| 953 |
Py_DECREF(args_tuple); |
| 954 |
Py_DECREF(kwnames); |
| 955 |
assert(res != _Py_NULL); |
| 956 |
|
| 957 |
assert(PyTuple_Check(res)); |
| 958 |
assert(PyTuple_GET_SIZE(res) == 2); |
| 959 |
PyObject *posargs = PyTuple_GET_ITEM(res, 0); |
| 960 |
PyObject *kwargs = PyTuple_GET_ITEM(res, 1); |
| 961 |
|
| 962 |
assert(PyTuple_Check(posargs)); |
| 963 |
assert(PyTuple_GET_SIZE(posargs) == 3); |
| 964 |
check_int(PyTuple_GET_ITEM(posargs, 0), 1); |
| 965 |
check_int(PyTuple_GET_ITEM(posargs, 1), 2); |
| 966 |
check_int(PyTuple_GET_ITEM(posargs, 2), 3); |
| 967 |
|
| 968 |
assert(PyDict_Check(kwargs)); |
| 969 |
assert(PyDict_Size(kwargs) == 2); |
| 970 |
|
| 971 |
Py_ssize_t pos = 0; |
| 972 |
PyObject *key, *value; |
| 973 |
while (PyDict_Next(kwargs, &pos, &key, &value)) { |
| 974 |
#ifdef PYTHON3 |
| 975 |
assert(PyUnicode_Check(key)); |
| 976 |
#else |
| 977 |
assert(PyString_Check(key)); |
| 978 |
#endif |
| 979 |
if (PyObject_RichCompareBool(key, key1, Py_EQ)) { |
| 980 |
check_int(value, 4); |
| 981 |
} |
| 982 |
else { |
| 983 |
assert(PyObject_RichCompareBool(key, key2, Py_EQ)); |
| 984 |
check_int(value, 5); |
| 985 |
} |
| 986 |
} |
| 987 |
|
| 988 |
Py_DECREF(res); |
| 989 |
Py_DECREF(key1); |
| 990 |
Py_DECREF(key2); |
| 991 |
} |
| 992 |
|
| 993 |
|
| 994 |
static PyObject * |
| 995 |
test_vectorcall(PyObject *module, PyObject *Py_UNUSED(args)) |
| 996 |
{ |
| 997 |
#ifndef PYTHON3 |
| 998 |
module = PyImport_ImportModule(MODULE_NAME_STR); |
| 999 |
assert(module != _Py_NULL); |
| 1000 |
#endif |
| 1001 |
PyObject *func_varargs = PyObject_GetAttrString(module, "func_varargs"); |
| 1002 |
#ifndef PYTHON3 |
| 1003 |
Py_DECREF(module); |
| 1004 |
#endif |
| 1005 |
if (func_varargs == _Py_NULL) { |
| 1006 |
return _Py_NULL; |
| 1007 |
} |
| 1008 |
|
| 1009 |
// test PyObject_Vectorcall() |
| 1010 |
test_vectorcall_noargs(func_varargs); |
| 1011 |
test_vectorcall_args(func_varargs); |
| 1012 |
test_vectorcall_args_offset(func_varargs); |
| 1013 |
test_vectorcall_args_kwnames(func_varargs); |
| 1014 |
|
| 1015 |
Py_DECREF(func_varargs); |
| 1016 |
Py_RETURN_NONE; |
| 1017 |
} |
| 1018 |
|
| 1019 |
|
| 1020 |
static PyObject * |
| 1021 |
test_getattr(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args)) |
| 1022 |
{ |
| 1023 |
assert(!PyErr_Occurred()); |
| 1024 |
|
| 1025 |
PyObject *obj = PyImport_ImportModule("sys"); |
| 1026 |
if (obj == _Py_NULL) { |
| 1027 |
return _Py_NULL; |
| 1028 |
} |
| 1029 |
|
| 1030 |
PyObject *attr_name = create_string("version"); |
| 1031 |
PyObject *missing_attr = create_string("nonexistant_attr_name"); |
| 1032 |
|
| 1033 |
// test PyObject_GetOptionalAttr(): attribute exists |
| 1034 |
PyObject *value; |
| 1035 |
value = UNINITIALIZED_OBJ; |
| 1036 |
assert(PyObject_GetOptionalAttr(obj, attr_name, &value) == 1); |
| 1037 |
assert(value != _Py_NULL); |
| 1038 |
Py_DECREF(value); |
| 1039 |
|
| 1040 |
// test PyObject_HasAttrWithError(): attribute exists |
| 1041 |
assert(PyObject_HasAttrWithError(obj, attr_name) == 1); |
| 1042 |
|
| 1043 |
// test PyObject_GetOptionalAttrString(): attribute exists |
| 1044 |
value = UNINITIALIZED_OBJ; |
| 1045 |
assert(PyObject_GetOptionalAttrString(obj, "version", &value) == 1); |
| 1046 |
assert(!PyErr_Occurred()); |
| 1047 |
assert(value != _Py_NULL); |
| 1048 |
Py_DECREF(value); |
| 1049 |
|
| 1050 |
// test PyObject_HasAttrStringWithError(): attribute exists |
| 1051 |
assert(PyObject_HasAttrStringWithError(obj, "version") == 1); |
| 1052 |
assert(!PyErr_Occurred()); |
| 1053 |
|
| 1054 |
// test PyObject_GetOptionalAttr(): attribute doesn't exist |
| 1055 |
value = UNINITIALIZED_OBJ; |
| 1056 |
assert(PyObject_GetOptionalAttr(obj, missing_attr, &value) == 0); |
| 1057 |
assert(!PyErr_Occurred()); |
| 1058 |
assert(value == _Py_NULL); |
| 1059 |
|
| 1060 |
// test PyObject_HasAttrWithError(): attribute doesn't exist |
| 1061 |
assert(PyObject_HasAttrWithError(obj, missing_attr) == 0); |
| 1062 |
assert(!PyErr_Occurred()); |
| 1063 |
|
| 1064 |
// test PyObject_GetOptionalAttrString(): attribute doesn't exist |
| 1065 |
value = UNINITIALIZED_OBJ; |
| 1066 |
assert(PyObject_GetOptionalAttrString(obj, "nonexistant_attr_name", &value) == 0); |
| 1067 |
assert(!PyErr_Occurred()); |
| 1068 |
assert(value == _Py_NULL); |
| 1069 |
|
| 1070 |
// test PyObject_HasAttrStringWithError(): attribute doesn't exist |
| 1071 |
assert(PyObject_HasAttrStringWithError(obj, "nonexistant_attr_name") == 0); |
| 1072 |
assert(!PyErr_Occurred()); |
| 1073 |
|
| 1074 |
Py_DECREF(attr_name); |
| 1075 |
Py_DECREF(missing_attr); |
| 1076 |
Py_DECREF(obj); |
| 1077 |
Py_RETURN_NONE; |
| 1078 |
} |
| 1079 |
|
| 1080 |
|
| 1081 |
static PyObject * |
| 1082 |
test_getitem(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args)) |
| 1083 |
{ |
| 1084 |
assert(!PyErr_Occurred()); |
| 1085 |
|
| 1086 |
PyObject *value = Py_BuildValue("s", "value"); |
| 1087 |
assert(value != _Py_NULL); |
| 1088 |
PyObject *obj = Py_BuildValue("{sO}", "key", value); |
| 1089 |
assert(obj != _Py_NULL); |
| 1090 |
PyObject *present_key, *missing_key; |
| 1091 |
PyObject *item; |
| 1092 |
|
| 1093 |
present_key = create_string("key"); |
| 1094 |
missing_key = create_string("dontexist"); |
| 1095 |
|
| 1096 |
// test PyMapping_GetOptionalItem(): key is present |
| 1097 |
item = UNINITIALIZED_OBJ; |
| 1098 |
assert(PyMapping_GetOptionalItem(obj, present_key, &item) == 1); |
| 1099 |
assert(item == value); |
| 1100 |
Py_DECREF(item); |
| 1101 |
assert(!PyErr_Occurred()); |
| 1102 |
|
| 1103 |
// test PyMapping_HasKeyWithError(): key is present |
| 1104 |
assert(PyMapping_HasKeyWithError(obj, present_key) == 1); |
| 1105 |
assert(!PyErr_Occurred()); |
| 1106 |
|
| 1107 |
// test PyMapping_GetOptionalItemString(): key is present |
| 1108 |
item = UNINITIALIZED_OBJ; |
| 1109 |
assert(PyMapping_GetOptionalItemString(obj, "key", &item) == 1); |
| 1110 |
assert(item == value); |
| 1111 |
Py_DECREF(item); |
| 1112 |
|
| 1113 |
// test PyMapping_HasKeyStringWithError(): key is present |
| 1114 |
assert(PyMapping_HasKeyStringWithError(obj, "key") == 1); |
| 1115 |
assert(!PyErr_Occurred()); |
| 1116 |
|
| 1117 |
// test PyMapping_GetOptionalItem(): missing key |
| 1118 |
item = UNINITIALIZED_OBJ; |
| 1119 |
assert(PyMapping_GetOptionalItem(obj, missing_key, &item) == 0); |
| 1120 |
assert(item == _Py_NULL); |
| 1121 |
assert(!PyErr_Occurred()); |
| 1122 |
|
| 1123 |
// test PyMapping_HasKeyWithError(): missing key |
| 1124 |
assert(PyMapping_HasKeyWithError(obj, missing_key) == 0); |
| 1125 |
assert(!PyErr_Occurred()); |
| 1126 |
|
| 1127 |
// test PyMapping_GetOptionalItemString(): missing key |
| 1128 |
item = UNINITIALIZED_OBJ; |
| 1129 |
assert(PyMapping_GetOptionalItemString(obj, "dontexist", &item) == 0); |
| 1130 |
assert(item == _Py_NULL); |
| 1131 |
|
| 1132 |
// test PyMapping_HasKeyStringWithError(): missing key |
| 1133 |
assert(PyMapping_HasKeyStringWithError(obj, "dontexist") == 0); |
| 1134 |
assert(!PyErr_Occurred()); |
| 1135 |
|
| 1136 |
Py_DECREF(obj); |
| 1137 |
Py_DECREF(value); |
| 1138 |
Py_DECREF(present_key); |
| 1139 |
Py_DECREF(missing_key); |
| 1140 |
Py_RETURN_NONE; |
| 1141 |
} |
| 1142 |
|
| 1143 |
|
| 1144 |
static PyObject * |
| 1145 |
test_dict_api(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args)) |
| 1146 |
{ |
| 1147 |
assert(!PyErr_Occurred()); |
| 1148 |
|
| 1149 |
PyObject *dict = NULL, *key = NULL, *missing_key = NULL, *value = NULL; |
| 1150 |
PyObject *invalid_key = NULL; |
| 1151 |
PyObject *invalid_dict = NULL; |
| 1152 |
PyObject *get_value = NULL; |
| 1153 |
int res; |
| 1154 |
|
| 1155 |
// test PyDict_New() |
| 1156 |
dict = PyDict_New(); |
| 1157 |
if (dict == NULL) { |
| 1158 |
goto error; |
| 1159 |
} |
| 1160 |
|
| 1161 |
key = PyUnicode_FromString("key"); |
| 1162 |
if (key == NULL) { |
| 1163 |
goto error; |
| 1164 |
} |
| 1165 |
invalid_dict = key; // borrowed reference |
| 1166 |
|
| 1167 |
missing_key = PyUnicode_FromString("missing_key"); |
| 1168 |
if (missing_key == NULL) { |
| 1169 |
goto error; |
| 1170 |
} |
| 1171 |
|
| 1172 |
invalid_key = PyList_New(0); // not hashable key |
| 1173 |
if (invalid_key == NULL) { |
| 1174 |
goto error; |
| 1175 |
} |
| 1176 |
|
| 1177 |
value = PyUnicode_FromString("value"); |
| 1178 |
if (value == NULL) { |
| 1179 |
goto error; |
| 1180 |
} |
| 1181 |
|
| 1182 |
res = PyDict_SetItemString(dict, "key", value); |
| 1183 |
if (res < 0) { |
| 1184 |
goto error; |
| 1185 |
} |
| 1186 |
assert(res == 0); |
| 1187 |
|
| 1188 |
// test PyDict_Contains() |
| 1189 |
assert(PyDict_Contains(dict, key) == 1); |
| 1190 |
assert(PyDict_Contains(dict, missing_key) == 0); |
| 1191 |
|
| 1192 |
// test PyDict_ContainsString() |
| 1193 |
assert(PyDict_ContainsString(dict, "key") == 1); |
| 1194 |
assert(PyDict_ContainsString(dict, "missing_key") == 0); |
| 1195 |
assert(PyDict_ContainsString(dict, "\xff") == -1); |
| 1196 |
assert(PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)); |
| 1197 |
PyErr_Clear(); |
| 1198 |
|
| 1199 |
// test PyDict_GetItemRef(), key is present |
| 1200 |
get_value = UNINITIALIZED_OBJ; |
| 1201 |
assert(PyDict_GetItemRef(dict, key, &get_value) == 1); |
| 1202 |
assert(get_value == value); |
| 1203 |
Py_DECREF(get_value); |
| 1204 |
|
| 1205 |
// test PyDict_GetItemStringRef(), key is present |
| 1206 |
get_value = UNINITIALIZED_OBJ; |
| 1207 |
assert(PyDict_GetItemStringRef(dict, "key", &get_value) == 1); |
| 1208 |
assert(get_value == value); |
| 1209 |
Py_DECREF(get_value); |
| 1210 |
|
| 1211 |
// test PyDict_GetItemRef(), missing key |
| 1212 |
get_value = UNINITIALIZED_OBJ; |
| 1213 |
assert(PyDict_GetItemRef(dict, missing_key, &get_value) == 0); |
| 1214 |
assert(!PyErr_Occurred()); |
| 1215 |
assert(get_value == NULL); |
| 1216 |
|
| 1217 |
// test PyDict_GetItemStringRef(), missing key |
| 1218 |
get_value = UNINITIALIZED_OBJ; |
| 1219 |
assert(PyDict_GetItemStringRef(dict, "missing_key", &get_value) == 0); |
| 1220 |
assert(!PyErr_Occurred()); |
| 1221 |
assert(get_value == NULL); |
| 1222 |
|
| 1223 |
// test PyDict_GetItemRef(), invalid dict |
| 1224 |
get_value = UNINITIALIZED_OBJ; |
| 1225 |
assert(PyDict_GetItemRef(invalid_dict, key, &get_value) == -1); |
| 1226 |
assert(PyErr_ExceptionMatches(PyExc_SystemError)); |
| 1227 |
PyErr_Clear(); |
| 1228 |
assert(get_value == NULL); |
| 1229 |
|
| 1230 |
// test PyDict_GetItemStringRef(), invalid dict |
| 1231 |
get_value = UNINITIALIZED_OBJ; |
| 1232 |
assert(PyDict_GetItemStringRef(invalid_dict, "key", &get_value) == -1); |
| 1233 |
assert(PyErr_ExceptionMatches(PyExc_SystemError)); |
| 1234 |
PyErr_Clear(); |
| 1235 |
assert(get_value == NULL); |
| 1236 |
|
| 1237 |
// test PyDict_GetItemRef(), invalid key |
| 1238 |
get_value = UNINITIALIZED_OBJ; |
| 1239 |
assert(PyDict_GetItemRef(dict, invalid_key, &get_value) == -1); |
| 1240 |
assert(PyErr_ExceptionMatches(PyExc_TypeError)); |
| 1241 |
PyErr_Clear(); |
| 1242 |
assert(get_value == NULL); |
| 1243 |
|
| 1244 |
Py_DECREF(dict); |
| 1245 |
Py_DECREF(key); |
| 1246 |
Py_DECREF(missing_key); |
| 1247 |
Py_DECREF(value); |
| 1248 |
Py_DECREF(invalid_key); |
| 1249 |
|
| 1250 |
Py_RETURN_NONE; |
| 1251 |
|
| 1252 |
error: |
| 1253 |
Py_XDECREF(dict); |
| 1254 |
Py_XDECREF(key); |
| 1255 |
Py_XDECREF(missing_key); |
| 1256 |
Py_XDECREF(value); |
| 1257 |
Py_XDECREF(invalid_key); |
| 1258 |
return NULL; |
| 1259 |
} |
| 1260 |
|
| 1261 |
|
| 1262 |
static PyObject * |
| 1263 |
test_dict_pop(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args)) |
| 1264 |
{ |
| 1265 |
PyObject *dict = PyDict_New(); |
| 1266 |
if (dict == NULL) { |
| 1267 |
return NULL; |
| 1268 |
} |
| 1269 |
|
| 1270 |
PyObject *key = PyUnicode_FromString("key"); |
| 1271 |
assert(key != NULL); |
| 1272 |
PyObject *value = PyUnicode_FromString("abc"); |
| 1273 |
assert(value != NULL); |
| 1274 |
|
| 1275 |
// test PyDict_Pop(), get the removed value, key is present |
| 1276 |
assert(PyDict_SetItem(dict, key, value) == 0); |
| 1277 |
PyObject *removed = UNINITIALIZED_OBJ; |
| 1278 |
assert(PyDict_Pop(dict, key, &removed) == 1); |
| 1279 |
assert(removed == value); |
| 1280 |
Py_DECREF(removed); |
| 1281 |
|
| 1282 |
// test PyDict_Pop(), ignore the removed value, key is present |
| 1283 |
assert(PyDict_SetItem(dict, key, value) == 0); |
| 1284 |
assert(PyDict_Pop(dict, key, NULL) == 1); |
| 1285 |
|
| 1286 |
// test PyDict_Pop(), key is missing |
| 1287 |
removed = UNINITIALIZED_OBJ; |
| 1288 |
assert(PyDict_Pop(dict, key, &removed) == 0); |
| 1289 |
assert(removed == NULL); |
| 1290 |
assert(PyDict_Pop(dict, key, NULL) == 0); |
| 1291 |
|
| 1292 |
// test PyDict_PopString(), get the removed value, key is present |
| 1293 |
assert(PyDict_SetItem(dict, key, value) == 0); |
| 1294 |
removed = UNINITIALIZED_OBJ; |
| 1295 |
assert(PyDict_PopString(dict, "key", &removed) == 1); |
| 1296 |
assert(removed == value); |
| 1297 |
Py_DECREF(removed); |
| 1298 |
|
| 1299 |
// test PyDict_PopString(), ignore the removed value, key is present |
| 1300 |
assert(PyDict_SetItem(dict, key, value) == 0); |
| 1301 |
assert(PyDict_PopString(dict, "key", NULL) == 1); |
| 1302 |
|
| 1303 |
// test PyDict_PopString(), key is missing |
| 1304 |
removed = UNINITIALIZED_OBJ; |
| 1305 |
assert(PyDict_PopString(dict, "key", &removed) == 0); |
| 1306 |
assert(removed == NULL); |
| 1307 |
assert(PyDict_PopString(dict, "key", NULL) == 0); |
| 1308 |
|
| 1309 |
// dict error |
| 1310 |
removed = UNINITIALIZED_OBJ; |
| 1311 |
assert(PyDict_Pop(key, key, &removed) == -1); |
| 1312 |
assert(removed == NULL); |
| 1313 |
assert(PyErr_ExceptionMatches(PyExc_SystemError)); |
| 1314 |
PyErr_Clear(); |
| 1315 |
|
| 1316 |
assert(PyDict_Pop(key, key, NULL) == -1); |
| 1317 |
assert(PyErr_ExceptionMatches(PyExc_SystemError)); |
| 1318 |
PyErr_Clear(); |
| 1319 |
|
| 1320 |
removed = UNINITIALIZED_OBJ; |
| 1321 |
assert(PyDict_PopString(key, "key", &removed) == -1); |
| 1322 |
assert(removed == NULL); |
| 1323 |
assert(PyErr_ExceptionMatches(PyExc_SystemError)); |
| 1324 |
PyErr_Clear(); |
| 1325 |
|
| 1326 |
assert(PyDict_PopString(key, "key", NULL) == -1); |
| 1327 |
assert(PyErr_ExceptionMatches(PyExc_SystemError)); |
| 1328 |
PyErr_Clear(); |
| 1329 |
|
| 1330 |
// exit |
| 1331 |
Py_DECREF(dict); |
| 1332 |
Py_DECREF(key); |
| 1333 |
Py_DECREF(value); |
| 1334 |
Py_RETURN_NONE; |
| 1335 |
} |
| 1336 |
|
| 1337 |
|
| 1338 |
static PyObject * |
| 1339 |
test_dict_setdefault(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args)) |
| 1340 |
{ |
| 1341 |
PyObject *dict = PyDict_New(); |
| 1342 |
if (dict == NULL) { |
| 1343 |
return NULL; |
| 1344 |
} |
| 1345 |
PyObject *key = PyUnicode_FromString("key"); |
| 1346 |
assert(key != NULL); |
| 1347 |
PyObject *value = PyUnicode_FromString("abc"); |
| 1348 |
assert(value != NULL); |
| 1349 |
PyObject *invalid_key = PyList_New(0); // not hashable key |
| 1350 |
assert(invalid_key != NULL); |
| 1351 |
|
| 1352 |
// insert item |
| 1353 |
PyObject *result = UNINITIALIZED_OBJ; |
| 1354 |
assert(PyDict_SetDefaultRef(dict, key, value, &result) == 0); |
| 1355 |
assert(result == value); |
| 1356 |
Py_DECREF(result); |
| 1357 |
|
| 1358 |
// item already present |
| 1359 |
result = UNINITIALIZED_OBJ; |
| 1360 |
assert(PyDict_SetDefaultRef(dict, key, value, &result) == 1); |
| 1361 |
assert(result == value); |
| 1362 |
Py_DECREF(result); |
| 1363 |
|
| 1364 |
// error: invalid key |
| 1365 |
assert(!PyErr_Occurred()); |
| 1366 |
result = UNINITIALIZED_OBJ; |
| 1367 |
assert(PyDict_SetDefaultRef(dict, invalid_key, value, &result) == -1); |
| 1368 |
assert(result == NULL); |
| 1369 |
assert(PyErr_Occurred()); |
| 1370 |
PyErr_Clear(); |
| 1371 |
|
| 1372 |
// insert item with NULL result |
| 1373 |
assert(PyDict_Pop(dict, key, NULL) == 1); |
| 1374 |
assert(PyDict_SetDefaultRef(dict, key, value, NULL) == 0); |
| 1375 |
|
| 1376 |
// item already present with NULL result |
| 1377 |
assert(PyDict_SetDefaultRef(dict, key, value, NULL) == 1); |
| 1378 |
|
| 1379 |
// error: invalid key with NULL result |
| 1380 |
assert(!PyErr_Occurred()); |
| 1381 |
assert(PyDict_SetDefaultRef(dict, invalid_key, value, NULL) == -1); |
| 1382 |
assert(PyErr_Occurred()); |
| 1383 |
PyErr_Clear(); |
| 1384 |
|
| 1385 |
// exit |
| 1386 |
Py_DECREF(dict); |
| 1387 |
Py_DECREF(key); |
| 1388 |
Py_DECREF(value); |
| 1389 |
Py_DECREF(invalid_key); |
| 1390 |
Py_RETURN_NONE; |
| 1391 |
} |
| 1392 |
|
| 1393 |
|
| 1394 |
static PyObject * |
| 1395 |
test_long_api(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args)) |
| 1396 |
{ |
| 1397 |
// test PyLong_AsInt() |
| 1398 |
assert(!PyErr_Occurred()); |
| 1399 |
PyObject *obj = PyLong_FromLong(123); |
| 1400 |
if (obj == NULL) { |
| 1401 |
return NULL; |
| 1402 |
} |
| 1403 |
int value = PyLong_AsInt(obj); |
| 1404 |
assert(value == 123); |
| 1405 |
assert(!PyErr_Occurred()); |
| 1406 |
Py_DECREF(obj); |
| 1407 |
|
| 1408 |
// test PyLong_AsInt() with overflow |
| 1409 |
PyObject *obj2 = PyLong_FromLongLong((long long)INT_MAX + 1); |
| 1410 |
if (obj2 == NULL) { |
| 1411 |
return NULL; |
| 1412 |
} |
| 1413 |
value = PyLong_AsInt(obj2); |
| 1414 |
assert(value == -1); |
| 1415 |
assert(PyErr_ExceptionMatches(PyExc_OverflowError)); |
| 1416 |
PyErr_Clear(); |
| 1417 |
Py_DECREF(obj2); |
| 1418 |
|
| 1419 |
// test PyLong_GetSign() |
| 1420 |
int sign = UNINITIALIZED_INT; |
| 1421 |
assert(PyLong_GetSign(obj, &sign) == 0); |
| 1422 |
assert(sign == 1); |
| 1423 |
|
| 1424 |
// test PyLong_IsPositive(), PyLong_IsNegative() and PyLong_IsZero() |
| 1425 |
assert(PyLong_IsPositive(obj) == 1); |
| 1426 |
assert(PyLong_IsNegative(obj) == 0); |
| 1427 |
assert(PyLong_IsZero(obj) == 0); |
| 1428 |
|
| 1429 |
#if defined(PYTHON3) && !defined(PYPY_VERSION) |
| 1430 |
// test import/export API |
| 1431 |
digit *digits; |
| 1432 |
PyLongWriter *writer; |
| 1433 |
static PyLongExport long_export; |
| 1434 |
|
| 1435 |
writer = PyLongWriter_Create(1, 1, (void **)&digits); |
| 1436 |
if (writer == NULL) { |
| 1437 |
return NULL; |
| 1438 |
} |
| 1439 |
PyLongWriter_Discard(writer); |
| 1440 |
|
| 1441 |
writer = PyLongWriter_Create(1, 1, (void **)&digits); |
| 1442 |
if (writer == NULL) { |
| 1443 |
return NULL; |
| 1444 |
} |
| 1445 |
digits[0] = 123; |
| 1446 |
obj = PyLongWriter_Finish(writer); |
| 1447 |
if (obj == NULL) { |
| 1448 |
return NULL; |
| 1449 |
} |
| 1450 |
|
| 1451 |
check_int(obj, -123); |
| 1452 |
if (PyLong_Export(obj, &long_export) < 0) { |
| 1453 |
return NULL; |
| 1454 |
} |
| 1455 |
assert(long_export.value == -123); |
| 1456 |
assert(long_export.digits == NULL); |
| 1457 |
PyLong_FreeExport(&long_export); |
| 1458 |
Py_DECREF(obj); |
| 1459 |
|
| 1460 |
writer = PyLongWriter_Create(0, 5, (void **)&digits); |
| 1461 |
if (writer == NULL) { |
| 1462 |
return NULL; |
| 1463 |
} |
| 1464 |
digits[0] = 1; |
| 1465 |
digits[1] = 0; |
| 1466 |
digits[2] = 0; |
| 1467 |
digits[3] = 0; |
| 1468 |
digits[4] = 1; |
| 1469 |
obj = PyLongWriter_Finish(writer); |
| 1470 |
if (obj == NULL) { |
| 1471 |
return NULL; |
| 1472 |
} |
| 1473 |
|
| 1474 |
if (PyLong_Export(obj, &long_export) < 0) { |
| 1475 |
return NULL; |
| 1476 |
} |
| 1477 |
assert(long_export.value == 0); |
| 1478 |
digits = (digit*)long_export.digits; |
| 1479 |
assert(digits[0] == 1); |
| 1480 |
assert(digits[1] == 0); |
| 1481 |
assert(digits[2] == 0); |
| 1482 |
assert(digits[3] == 0); |
| 1483 |
assert(digits[4] == 1); |
| 1484 |
PyLong_FreeExport(&long_export); |
| 1485 |
Py_DECREF(obj); |
| 1486 |
|
| 1487 |
const PyLongLayout *layout = PyLong_GetNativeLayout(); |
| 1488 |
assert(layout->digits_order == -1); |
| 1489 |
assert(layout->digit_size == sizeof(digit)); |
| 1490 |
#endif // defined(PYTHON3) && !defined(PYPY_VERSION) |
| 1491 |
|
| 1492 |
Py_RETURN_NONE; |
| 1493 |
} |
| 1494 |
|
| 1495 |
|
| 1496 |
// --- HeapCTypeWithManagedDict -------------------------------------------- |
| 1497 |
|
| 1498 |
// Py_TPFLAGS_MANAGED_DICT was added to Python 3.11.0a3 but is not implemented on PyPy |
| 1499 |
#if PY_VERSION_HEX >= 0x030B00A3 && ! defined(PYPY_VERSION) |
| 1500 |
# define TEST_MANAGED_DICT |
| 1501 |
|
| 1502 |
typedef struct { |
| 1503 |
PyObject_HEAD |
| 1504 |
} HeapCTypeObject; |
| 1505 |
|
| 1506 |
static int |
| 1507 |
heapmanaged_traverse(PyObject *self, visitproc visit, void *arg) |
| 1508 |
{ |
| 1509 |
Py_VISIT(Py_TYPE(self)); |
| 1510 |
// Test PyObject_VisitManagedDict() |
| 1511 |
return PyObject_VisitManagedDict(self, visit, arg); |
| 1512 |
} |
| 1513 |
|
| 1514 |
static int |
| 1515 |
heapmanaged_clear(PyObject *self) |
| 1516 |
{ |
| 1517 |
// Test PyObject_ClearManagedDict() |
| 1518 |
PyObject_ClearManagedDict(self); |
| 1519 |
return 0; |
| 1520 |
} |
| 1521 |
|
| 1522 |
static void |
| 1523 |
heapmanaged_dealloc(HeapCTypeObject *self) |
| 1524 |
{ |
| 1525 |
PyTypeObject *tp = Py_TYPE(self); |
| 1526 |
PyObject_ClearManagedDict((PyObject *)self); |
| 1527 |
PyObject_GC_UnTrack(self); |
| 1528 |
PyObject_GC_Del(self); |
| 1529 |
Py_DECREF(tp); |
| 1530 |
} |
| 1531 |
|
| 1532 |
static PyType_Slot HeapCTypeWithManagedDict_slots[] = { |
| 1533 |
{Py_tp_traverse, _Py_CAST(void*, heapmanaged_traverse)}, |
| 1534 |
{Py_tp_clear, _Py_CAST(void*, heapmanaged_clear)}, |
| 1535 |
{Py_tp_dealloc, _Py_CAST(void*, heapmanaged_dealloc)}, |
| 1536 |
{0, 0}, |
| 1537 |
}; |
| 1538 |
|
| 1539 |
static PyType_Spec HeapCTypeWithManagedDict_spec = { |
| 1540 |
"test_pythoncapi_compat.HeapCTypeWithManagedDict", |
| 1541 |
sizeof(PyObject), |
| 1542 |
0, |
| 1543 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_MANAGED_DICT, |
| 1544 |
HeapCTypeWithManagedDict_slots |
| 1545 |
}; |
| 1546 |
|
| 1547 |
static PyObject * |
| 1548 |
test_managed_dict(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args)) |
| 1549 |
{ |
| 1550 |
// Test PyObject_VisitManagedDict() and PyObject_ClearManagedDict() |
| 1551 |
PyObject *type = PyType_FromSpec(&HeapCTypeWithManagedDict_spec); |
| 1552 |
if (type == NULL) { |
| 1553 |
return NULL; |
| 1554 |
} |
| 1555 |
|
| 1556 |
PyObject *obj = PyObject_CallNoArgs(type); |
| 1557 |
if (obj == NULL) { |
| 1558 |
Py_DECREF(type); |
| 1559 |
return NULL; |
| 1560 |
} |
| 1561 |
|
| 1562 |
// call heapmanaged_traverse() |
| 1563 |
PyGC_Collect(); |
| 1564 |
|
| 1565 |
// call heapmanaged_clear() |
| 1566 |
Py_DECREF(obj); |
| 1567 |
PyGC_Collect(); |
| 1568 |
|
| 1569 |
Py_DECREF(type); |
| 1570 |
// Just in case! |
| 1571 |
PyGC_Collect(); |
| 1572 |
|
| 1573 |
Py_RETURN_NONE; |
| 1574 |
} |
| 1575 |
#endif // PY_VERSION_HEX >= 0x030B00A3 |
| 1576 |
|
| 1577 |
|
| 1578 |
static PyObject * |
| 1579 |
test_unicode(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args)) |
| 1580 |
{ |
| 1581 |
PyObject *abc = PyUnicode_FromString("abc"); |
| 1582 |
if (abc == NULL) { |
| 1583 |
return NULL; |
| 1584 |
} |
| 1585 |
|
| 1586 |
PyObject *abc0def = PyUnicode_FromStringAndSize("abc\0def", 7); |
| 1587 |
if (abc0def == NULL) { |
| 1588 |
Py_DECREF(abc); |
| 1589 |
return NULL; |
| 1590 |
} |
| 1591 |
|
| 1592 |
// PyUnicode_EqualToUTF8() and PyUnicode_EqualToUTF8AndSize() can be called |
| 1593 |
// with an exception raised and they must not clear the current exception. |
| 1594 |
PyErr_NoMemory(); |
| 1595 |
|
| 1596 |
assert(PyUnicode_EqualToUTF8AndSize(abc, "abc", 3) == 1); |
| 1597 |
assert(PyUnicode_EqualToUTF8AndSize(abc, "Python", 6) == 0); |
| 1598 |
assert(PyUnicode_EqualToUTF8AndSize(abc0def, "abc\0def", 7) == 1); |
| 1599 |
|
| 1600 |
assert(PyUnicode_EqualToUTF8(abc, "abc") == 1); |
| 1601 |
assert(PyUnicode_EqualToUTF8(abc, "Python") == 0); |
| 1602 |
assert(PyUnicode_EqualToUTF8(abc0def, "abc\0def") == 0); |
| 1603 |
|
| 1604 |
assert(PyErr_ExceptionMatches(PyExc_MemoryError)); |
| 1605 |
PyErr_Clear(); |
| 1606 |
|
| 1607 |
// Test PyUnicode_Equal() |
| 1608 |
assert(PyUnicode_Equal(abc, abc) == 1); |
| 1609 |
assert(PyUnicode_Equal(abc, abc0def) == 0); |
| 1610 |
assert(PyUnicode_Equal(abc, Py_True) == -1); |
| 1611 |
assert(PyErr_ExceptionMatches(PyExc_TypeError)); |
| 1612 |
PyErr_Clear(); |
| 1613 |
|
| 1614 |
Py_DECREF(abc); |
| 1615 |
Py_DECREF(abc0def); |
| 1616 |
Py_RETURN_NONE; |
| 1617 |
} |
| 1618 |
|
| 1619 |
|
| 1620 |
static PyObject * |
| 1621 |
test_list(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args)) |
| 1622 |
{ |
| 1623 |
PyObject *list = PyList_New(0); |
| 1624 |
if (list == NULL) { |
| 1625 |
return NULL; |
| 1626 |
} |
| 1627 |
|
| 1628 |
// test PyList_Extend() |
| 1629 |
{ |
| 1630 |
PyObject *abc = PyUnicode_FromString("abc"); |
| 1631 |
if (abc == NULL) { |
| 1632 |
Py_DECREF(list); |
| 1633 |
return NULL; |
| 1634 |
} |
| 1635 |
|
| 1636 |
assert(PyList_Extend(list, abc) == 0); |
| 1637 |
Py_DECREF(abc); |
| 1638 |
assert(PyList_GET_SIZE(list) == 3); |
| 1639 |
} |
| 1640 |
|
| 1641 |
// test PyList_GetItemRef() |
| 1642 |
PyObject *item = PyList_GetItemRef(list, 1); |
| 1643 |
assert(item != NULL); |
| 1644 |
assert(item == PyList_GetItem(list, 1)); |
| 1645 |
Py_DECREF(item); |
| 1646 |
|
| 1647 |
// test PyList_Clear() |
| 1648 |
assert(PyList_Clear(list) == 0); |
| 1649 |
assert(PyList_GET_SIZE(list) == 0); |
| 1650 |
|
| 1651 |
Py_DECREF(list); |
| 1652 |
Py_RETURN_NONE; |
| 1653 |
} |
| 1654 |
|
| 1655 |
|
| 1656 |
static PyObject * |
| 1657 |
test_hash(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args)) |
| 1658 |
{ |
| 1659 |
void *ptr0 = NULL; |
| 1660 |
assert(Py_HashPointer(ptr0) == 0); |
| 1661 |
|
| 1662 |
#ifndef PYPY_VERSION |
| 1663 |
#if SIZEOF_VOID_P == 8 |
| 1664 |
void *ptr1 = (void*)(uintptr_t)0xABCDEF1234567890; |
| 1665 |
assert(Py_HashPointer(ptr1) == (uintptr_t)0x0ABCDEF123456789); |
| 1666 |
#else |
| 1667 |
void *ptr1 = (void*)(uintptr_t)0xDEADCAFE; |
| 1668 |
assert(Py_HashPointer(ptr1) == (uintptr_t)0xEDEADCAF); |
| 1669 |
#endif |
| 1670 |
#else |
| 1671 |
// PyPy |
| 1672 |
#if SIZEOF_VOID_P == 8 |
| 1673 |
void *ptr1 = (void*)(uintptr_t)0xABCDEF1234567890; |
| 1674 |
#else |
| 1675 |
void *ptr1 = (void*)(uintptr_t)0xDEADCAFE; |
| 1676 |
#endif |
| 1677 |
assert(Py_HashPointer(ptr1) == (Py_hash_t)ptr1); |
| 1678 |
#endif |
| 1679 |
|
| 1680 |
#if ((!defined(PYPY_VERSION) && PY_VERSION_HEX >= 0x030400B1) \ |
| 1681 |
|| (defined(PYPY_VERSION) && PY_VERSION_HEX >= 0x03070000 \ |
| 1682 |
&& PYPY_VERSION_NUM >= 0x07030800)) |
| 1683 |
// Just check that constants are available |
| 1684 |
size_t bits = PyHASH_BITS; |
| 1685 |
assert(bits >= 8); |
| 1686 |
size_t mod = PyHASH_MODULUS; |
| 1687 |
assert(mod >= 7); |
| 1688 |
size_t inf = PyHASH_INF; |
| 1689 |
assert(inf != 0); |
| 1690 |
size_t imag = PyHASH_IMAG; |
| 1691 |
assert(imag != 0); |
| 1692 |
#endif |
| 1693 |
|
| 1694 |
// Test Py_HashBuffer() |
| 1695 |
{ |
| 1696 |
PyObject *abc = PyBytes_FromString("abc"); |
| 1697 |
if (abc == NULL) { |
| 1698 |
return NULL; |
| 1699 |
} |
| 1700 |
Py_hash_t hash = Py_HashBuffer(PyBytes_AS_STRING(abc), |
| 1701 |
PyBytes_GET_SIZE(abc)); |
| 1702 |
Py_hash_t hash2 = PyObject_Hash(abc); |
| 1703 |
assert(hash == hash2); |
| 1704 |
|
| 1705 |
Py_DECREF(abc); |
| 1706 |
} |
| 1707 |
|
| 1708 |
Py_RETURN_NONE; |
| 1709 |
} |
| 1710 |
|
| 1711 |
|
| 1712 |
#if PY_VERSION_HEX >= 0x03050000 |
| 1713 |
#define TEST_PYTIME |
| 1714 |
|
| 1715 |
static PyObject * |
| 1716 |
test_time(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args)) |
| 1717 |
{ |
| 1718 |
PyTime_t t; |
| 1719 |
#define UNINITIALIZED_TIME ((PyTime_t)-483884113929936179) |
| 1720 |
|
| 1721 |
t = UNINITIALIZED_TIME; |
| 1722 |
assert(PyTime_Time(&t) == 0); |
| 1723 |
assert(t != UNINITIALIZED_TIME); |
| 1724 |
|
| 1725 |
t = UNINITIALIZED_TIME; |
| 1726 |
assert(PyTime_Monotonic(&t) == 0); |
| 1727 |
assert(t != UNINITIALIZED_TIME); |
| 1728 |
|
| 1729 |
// Test multiple times since an implementation uses a cache |
| 1730 |
for (int i=0; i < 5; i++) { |
| 1731 |
t = UNINITIALIZED_TIME; |
| 1732 |
assert(PyTime_PerfCounter(&t) == 0); |
| 1733 |
assert(t != UNINITIALIZED_TIME); |
| 1734 |
} |
| 1735 |
|
| 1736 |
assert(PyTime_AsSecondsDouble(1) == 1e-9); |
| 1737 |
assert(PyTime_AsSecondsDouble(1500 * 1000 * 1000) == 1.5); |
| 1738 |
assert(PyTime_AsSecondsDouble(-500 * 1000 * 1000) == -0.5); |
| 1739 |
|
| 1740 |
Py_RETURN_NONE; |
| 1741 |
} |
| 1742 |
#endif |
| 1743 |
|
| 1744 |
|
| 1745 |
static void |
| 1746 |
check_get_constant(PyObject* (*get_constant)(unsigned int), int borrowed) |
| 1747 |
{ |
| 1748 |
#define CLEAR(var) if (!borrowed) { Py_DECREF(var); } |
| 1749 |
|
| 1750 |
PyObject *obj, *expected; |
| 1751 |
|
| 1752 |
// Py_CONSTANT_NONE |
| 1753 |
obj = get_constant(Py_CONSTANT_NONE); |
| 1754 |
assert(obj == Py_None); |
| 1755 |
CLEAR(obj); |
| 1756 |
|
| 1757 |
// Py_CONSTANT_FALSE |
| 1758 |
obj = get_constant(Py_CONSTANT_FALSE); |
| 1759 |
assert(obj == Py_False); |
| 1760 |
CLEAR(obj); |
| 1761 |
|
| 1762 |
// Py_CONSTANT_TRUE |
| 1763 |
obj = get_constant(Py_CONSTANT_TRUE); |
| 1764 |
assert(obj == Py_True); |
| 1765 |
CLEAR(obj); |
| 1766 |
|
| 1767 |
// Py_CONSTANT_ELLIPSIS |
| 1768 |
obj = get_constant(Py_CONSTANT_ELLIPSIS); |
| 1769 |
assert(obj == Py_Ellipsis); |
| 1770 |
CLEAR(obj); |
| 1771 |
|
| 1772 |
// Py_CONSTANT_NOT_IMPLEMENTED |
| 1773 |
obj = get_constant(Py_CONSTANT_NOT_IMPLEMENTED); |
| 1774 |
assert(obj == Py_NotImplemented); |
| 1775 |
CLEAR(obj); |
| 1776 |
|
| 1777 |
// Py_CONSTANT_ZERO |
| 1778 |
obj = get_constant(Py_CONSTANT_ZERO); |
| 1779 |
expected = PyLong_FromLong(0); |
| 1780 |
assert(expected != NULL); |
| 1781 |
assert(Py_TYPE(obj) == &PyLong_Type); |
| 1782 |
assert(PyObject_RichCompareBool(obj, expected, Py_EQ) == 1); |
| 1783 |
CLEAR(obj); |
| 1784 |
Py_DECREF(expected); |
| 1785 |
|
| 1786 |
// Py_CONSTANT_ONE |
| 1787 |
obj = get_constant(Py_CONSTANT_ONE); |
| 1788 |
expected = PyLong_FromLong(1); |
| 1789 |
assert(expected != NULL); |
| 1790 |
assert(Py_TYPE(obj) == &PyLong_Type); |
| 1791 |
assert(PyObject_RichCompareBool(obj, expected, Py_EQ) == 1); |
| 1792 |
CLEAR(obj); |
| 1793 |
Py_DECREF(expected); |
| 1794 |
|
| 1795 |
// Py_CONSTANT_EMPTY_STR |
| 1796 |
obj = get_constant(Py_CONSTANT_EMPTY_STR); |
| 1797 |
assert(Py_TYPE(obj) == &PyUnicode_Type); |
| 1798 |
#if PY_VERSION_HEX >= 0x03030000 |
| 1799 |
assert(PyUnicode_GetLength(obj) == 0); |
| 1800 |
#else |
| 1801 |
assert(PyUnicode_GetSize(obj) == 0); |
| 1802 |
#endif |
| 1803 |
CLEAR(obj); |
| 1804 |
|
| 1805 |
// Py_CONSTANT_EMPTY_BYTES |
| 1806 |
obj = get_constant(Py_CONSTANT_EMPTY_BYTES); |
| 1807 |
assert(Py_TYPE(obj) == &PyBytes_Type); |
| 1808 |
assert(PyBytes_Size(obj) == 0); |
| 1809 |
CLEAR(obj); |
| 1810 |
|
| 1811 |
// Py_CONSTANT_EMPTY_TUPLE |
| 1812 |
obj = get_constant(Py_CONSTANT_EMPTY_TUPLE); |
| 1813 |
assert(Py_TYPE(obj) == &PyTuple_Type); |
| 1814 |
assert(PyTuple_Size(obj) == 0); |
| 1815 |
CLEAR(obj); |
| 1816 |
|
| 1817 |
#undef CLEAR |
| 1818 |
} |
| 1819 |
|
| 1820 |
|
| 1821 |
static PyObject * |
| 1822 |
test_get_constant(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args)) |
| 1823 |
{ |
| 1824 |
check_get_constant(Py_GetConstant, 0); |
| 1825 |
check_get_constant(Py_GetConstantBorrowed, 1); |
| 1826 |
Py_RETURN_NONE; |
| 1827 |
} |
| 1828 |
|
| 1829 |
|
| 1830 |
#if PY_VERSION_HEX < 0x030E0000 && PY_VERSION_HEX >= 0x03060000 && !defined(PYPY_VERSION) |
| 1831 |
#define TEST_UNICODEWRITER 1 |
| 1832 |
|
| 1833 |
static PyObject * |
| 1834 |
test_unicodewriter(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(args)) |
| 1835 |
{ |
| 1836 |
PyUnicodeWriter *writer = PyUnicodeWriter_Create(0); |
| 1837 |
if (writer == NULL) { |
| 1838 |
return NULL; |
| 1839 |
} |
| 1840 |
int ret; |
| 1841 |
|
| 1842 |
// test PyUnicodeWriter_WriteStr() |
| 1843 |
PyObject *str = PyUnicode_FromString("var"); |
| 1844 |
if (str == NULL) { |
| 1845 |
goto error; |
| 1846 |
} |
| 1847 |
ret = PyUnicodeWriter_WriteStr(writer, str); |
| 1848 |
Py_CLEAR(str); |
| 1849 |
if (ret < 0) { |
| 1850 |
goto error; |
| 1851 |
} |
| 1852 |
|
| 1853 |
// test PyUnicodeWriter_WriteChar() |
| 1854 |
if (PyUnicodeWriter_WriteChar(writer, '=') < 0) { |
| 1855 |
goto error; |
| 1856 |
} |
| 1857 |
|
| 1858 |
// test PyUnicodeWriter_WriteSubstring() |
| 1859 |
str = PyUnicode_FromString("[long]"); |
| 1860 |
if (str == NULL) { |
| 1861 |
goto error; |
| 1862 |
} |
| 1863 |
ret = PyUnicodeWriter_WriteSubstring(writer, str, 1, 5); |
| 1864 |
Py_CLEAR(str); |
| 1865 |
if (ret < 0) { |
| 1866 |
goto error; |
| 1867 |
} |
| 1868 |
|
| 1869 |
// test PyUnicodeWriter_WriteASCII() |
| 1870 |
if (PyUnicodeWriter_WriteASCII(writer, " non-ASCII", -1) < 0) { |
| 1871 |
goto error; |
| 1872 |
} |
| 1873 |
|
| 1874 |
// test PyUnicodeWriter_WriteUTF8() |
| 1875 |
if (PyUnicodeWriter_WriteUTF8(writer, " valu\xC3\xA9", -1) < 0) { |
| 1876 |
goto error; |
| 1877 |
} |
| 1878 |
if (PyUnicodeWriter_WriteChar(writer, ' ') < 0) { |
| 1879 |
goto error; |
| 1880 |
} |
| 1881 |
|
| 1882 |
// test PyUnicodeWriter_WriteRepr() |
| 1883 |
str = PyUnicode_FromString("repr"); |
| 1884 |
if (str == NULL) { |
| 1885 |
goto error; |
| 1886 |
} |
| 1887 |
if (PyUnicodeWriter_WriteRepr(writer, str) < 0) { |
| 1888 |
goto error; |
| 1889 |
} |
| 1890 |
Py_CLEAR(str); |
| 1891 |
|
| 1892 |
{ |
| 1893 |
PyObject *result = PyUnicodeWriter_Finish(writer); |
| 1894 |
if (result == NULL) { |
| 1895 |
return NULL; |
| 1896 |
} |
| 1897 |
assert(PyUnicode_EqualToUTF8(result, "var=long non-ASCII valu\xC3\xA9 'repr'")); |
| 1898 |
Py_DECREF(result); |
| 1899 |
} |
| 1900 |
|
| 1901 |
Py_RETURN_NONE; |
| 1902 |
|
| 1903 |
error: |
| 1904 |
PyUnicodeWriter_Discard(writer); |
| 1905 |
return NULL; |
| 1906 |
} |
| 1907 |
|
| 1908 |
|
| 1909 |
static PyObject * |
| 1910 |
test_unicodewriter_widechar(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(args)) |
| 1911 |
{ |
| 1912 |
PyUnicodeWriter *writer = PyUnicodeWriter_Create(0); |
| 1913 |
if (writer == NULL) { |
| 1914 |
return NULL; |
| 1915 |
} |
| 1916 |
|
| 1917 |
// test PyUnicodeWriter_WriteWideChar() |
| 1918 |
int ret = PyUnicodeWriter_WriteWideChar(writer, L"euro=\u20AC", -1); |
| 1919 |
if (ret < 0) { |
| 1920 |
goto error; |
| 1921 |
} |
| 1922 |
|
| 1923 |
{ |
| 1924 |
PyObject *result = PyUnicodeWriter_Finish(writer); |
| 1925 |
if (result == NULL) { |
| 1926 |
return NULL; |
| 1927 |
} |
| 1928 |
assert(PyUnicode_EqualToUTF8(result, "euro=\xe2\x82\xac")); |
| 1929 |
Py_DECREF(result); |
| 1930 |
} |
| 1931 |
|
| 1932 |
Py_RETURN_NONE; |
| 1933 |
|
| 1934 |
error: |
| 1935 |
PyUnicodeWriter_Discard(writer); |
| 1936 |
return NULL; |
| 1937 |
} |
| 1938 |
|
| 1939 |
|
| 1940 |
static PyObject * |
| 1941 |
test_unicodewriter_format(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(args)) |
| 1942 |
{ |
| 1943 |
PyUnicodeWriter *writer = PyUnicodeWriter_Create(0); |
| 1944 |
if (writer == NULL) { |
| 1945 |
return NULL; |
| 1946 |
} |
| 1947 |
|
| 1948 |
// test PyUnicodeWriter_Format() |
| 1949 |
if (PyUnicodeWriter_Format(writer, "%s %i", "Hello", 123) < 0) { |
| 1950 |
goto error; |
| 1951 |
} |
| 1952 |
|
| 1953 |
// test PyUnicodeWriter_WriteChar() |
| 1954 |
if (PyUnicodeWriter_WriteChar(writer, '.') < 0) { |
| 1955 |
goto error; |
| 1956 |
} |
| 1957 |
|
| 1958 |
{ |
| 1959 |
PyObject *result = PyUnicodeWriter_Finish(writer); |
| 1960 |
if (result == NULL) { |
| 1961 |
return NULL; |
| 1962 |
} |
| 1963 |
assert(PyUnicode_EqualToUTF8(result, "Hello 123.")); |
| 1964 |
Py_DECREF(result); |
| 1965 |
} |
| 1966 |
|
| 1967 |
Py_RETURN_NONE; |
| 1968 |
|
| 1969 |
error: |
| 1970 |
PyUnicodeWriter_Discard(writer); |
| 1971 |
return NULL; |
| 1972 |
} |
| 1973 |
#endif |
| 1974 |
|
| 1975 |
static PyObject * |
| 1976 |
test_uniquely_referenced(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(args)) |
| 1977 |
{ |
| 1978 |
PyObject *obj = Py_BuildValue("(s, s)", "hello", "world"); |
| 1979 |
if (obj == NULL) { |
| 1980 |
return NULL; |
| 1981 |
} |
| 1982 |
|
| 1983 |
assert(PyUnstable_Object_IsUniquelyReferenced(obj)); |
| 1984 |
|
| 1985 |
Py_INCREF(obj); |
| 1986 |
|
| 1987 |
assert(!PyUnstable_Object_IsUniquelyReferenced(obj)); |
| 1988 |
|
| 1989 |
Py_DECREF(obj); |
| 1990 |
Py_DECREF(obj); |
| 1991 |
|
| 1992 |
Py_RETURN_NONE; |
| 1993 |
} |
| 1994 |
|
| 1995 |
static PyObject * |
| 1996 |
test_bytes(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args)) |
| 1997 |
{ |
| 1998 |
// Test PyBytes_Join() |
| 1999 |
PyObject *abc = PyBytes_FromString("a b c"); |
| 2000 |
if (abc == NULL) { |
| 2001 |
return NULL; |
| 2002 |
} |
| 2003 |
PyObject *list = PyObject_CallMethod(abc, "split", NULL); |
| 2004 |
Py_DECREF(abc); |
| 2005 |
if (list == NULL) { |
| 2006 |
return NULL; |
| 2007 |
} |
| 2008 |
PyObject *sep = PyBytes_FromString("-"); |
| 2009 |
if (sep == NULL) { |
| 2010 |
Py_DECREF(list); |
| 2011 |
return NULL; |
| 2012 |
} |
| 2013 |
|
| 2014 |
PyObject *join = PyBytes_Join(sep, list); |
| 2015 |
assert(join != NULL); |
| 2016 |
assert(PyBytes_Check(join)); |
| 2017 |
assert(memcmp(PyBytes_AS_STRING(join), "a-b-c", 5) == 0); |
| 2018 |
Py_DECREF(join); |
| 2019 |
|
| 2020 |
Py_DECREF(list); |
| 2021 |
Py_DECREF(sep); |
| 2022 |
Py_RETURN_NONE; |
| 2023 |
} |
| 2024 |
|
| 2025 |
|
| 2026 |
static PyObject * |
| 2027 |
test_iter(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args)) |
| 2028 |
{ |
| 2029 |
// Test PyIter_NextItem() |
| 2030 |
PyObject *tuple = Py_BuildValue("(i)", 123); |
| 2031 |
if (tuple == NULL) { |
| 2032 |
return NULL; |
| 2033 |
} |
| 2034 |
PyObject *iter = PyObject_GetIter(tuple); |
| 2035 |
Py_DECREF(tuple); |
| 2036 |
if (iter == NULL) { |
| 2037 |
return NULL; |
| 2038 |
} |
| 2039 |
|
| 2040 |
// first item |
| 2041 |
PyObject *item = UNINITIALIZED_OBJ; |
| 2042 |
assert(PyIter_NextItem(iter, &item) == 1); |
| 2043 |
{ |
| 2044 |
PyObject *expected = PyLong_FromLong(123); |
| 2045 |
assert(PyObject_RichCompareBool(item, expected, Py_EQ) == 1); |
| 2046 |
assert(expected != NULL); |
| 2047 |
Py_DECREF(expected); |
| 2048 |
} |
| 2049 |
|
| 2050 |
// StopIteration |
| 2051 |
item = UNINITIALIZED_OBJ; |
| 2052 |
assert(PyIter_NextItem(iter, &item) == 0); |
| 2053 |
assert(item == NULL); |
| 2054 |
assert(!PyErr_Occurred()); |
| 2055 |
|
| 2056 |
// non-iterable object |
| 2057 |
item = UNINITIALIZED_OBJ; |
| 2058 |
assert(PyIter_NextItem(Py_None, &item) == -1); |
| 2059 |
assert(item == NULL); |
| 2060 |
assert(PyErr_ExceptionMatches(PyExc_TypeError)); |
| 2061 |
PyErr_Clear(); |
| 2062 |
|
| 2063 |
Py_DECREF(iter); |
| 2064 |
Py_RETURN_NONE; |
| 2065 |
} |
| 2066 |
|
| 2067 |
|
| 2068 |
static PyObject * |
| 2069 |
test_long_stdint(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args)) |
| 2070 |
{ |
| 2071 |
PyObject *obj; |
| 2072 |
|
| 2073 |
// Test PyLong_FromInt32() and PyLong_AsInt32() |
| 2074 |
obj = PyLong_FromInt32(INT32_C(-0x12345678)); |
| 2075 |
assert(obj != NULL); |
| 2076 |
int32_t i32; |
| 2077 |
assert(PyLong_AsInt32(obj, &i32) == 0); |
| 2078 |
assert(i32 == INT32_C(-0x12345678)); |
| 2079 |
Py_DECREF(obj); |
| 2080 |
|
| 2081 |
// Test PyLong_FromUInt32() and PyLong_AsUInt32() |
| 2082 |
obj = PyLong_FromUInt32(UINT32_C(0xDEADBEEF)); |
| 2083 |
assert(obj != NULL); |
| 2084 |
uint32_t u32; |
| 2085 |
assert(PyLong_AsUInt32(obj, &u32) == 0); |
| 2086 |
assert(u32 == UINT32_C(0xDEADBEEF)); |
| 2087 |
Py_DECREF(obj); |
| 2088 |
|
| 2089 |
// Test PyLong_FromInt64() and PyLong_AsInt64() |
| 2090 |
obj = PyLong_FromInt64(INT64_C(-0x12345678DEADBEEF)); |
| 2091 |
assert(obj != NULL); |
| 2092 |
int64_t i64; |
| 2093 |
assert(PyLong_AsInt64(obj, &i64) == 0); |
| 2094 |
assert(i64 == INT64_C(-0x12345678DEADBEEF)); |
| 2095 |
Py_DECREF(obj); |
| 2096 |
|
| 2097 |
// Test PyLong_FromUInt64() and PyLong_AsUInt64() |
| 2098 |
obj = PyLong_FromUInt64(UINT64_C(0xDEADBEEF12345678)); |
| 2099 |
assert(obj != NULL); |
| 2100 |
uint64_t u64; |
| 2101 |
assert(PyLong_AsUInt64(obj, &u64) == 0); |
| 2102 |
assert(u64 == UINT64_C(0xDEADBEEF12345678)); |
| 2103 |
Py_DECREF(obj); |
| 2104 |
|
| 2105 |
Py_RETURN_NONE; |
| 2106 |
} |
| 2107 |
|
| 2108 |
|
| 2109 |
static PyObject * |
| 2110 |
test_structmember(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args)) |
| 2111 |
{ |
| 2112 |
assert(Py_T_SHORT >= 0); |
| 2113 |
assert(Py_T_INT >= 0); |
| 2114 |
assert(Py_T_LONG >= 0); |
| 2115 |
assert(Py_T_FLOAT >= 0); |
| 2116 |
assert(Py_T_DOUBLE >= 0); |
| 2117 |
assert(Py_T_STRING >= 0); |
| 2118 |
assert(_Py_T_OBJECT >= 0); |
| 2119 |
assert(Py_T_CHAR >= 0); |
| 2120 |
assert(Py_T_BYTE >= 0); |
| 2121 |
assert(Py_T_UBYTE >= 0); |
| 2122 |
assert(Py_T_USHORT >= 0); |
| 2123 |
assert(Py_T_UINT >= 0); |
| 2124 |
assert(Py_T_ULONG >= 0); |
| 2125 |
assert(Py_T_STRING_INPLACE >= 0); |
| 2126 |
assert(Py_T_BOOL >= 0); |
| 2127 |
assert(Py_T_OBJECT_EX >= 0); |
| 2128 |
assert(Py_T_LONGLONG >= 0); |
| 2129 |
assert(Py_T_ULONGLONG >= 0); |
| 2130 |
assert(Py_T_PYSSIZET >= 0); |
| 2131 |
#if PY_VERSION_HEX >= 0x03000000 && !defined(PYPY_VERSION) |
| 2132 |
assert(_Py_T_NONE >= 0); |
| 2133 |
#endif |
| 2134 |
|
| 2135 |
assert(Py_READONLY >= 0); |
| 2136 |
assert(Py_AUDIT_READ >= 0); |
| 2137 |
assert(_Py_WRITE_RESTRICTED >= 0); |
| 2138 |
|
| 2139 |
Py_RETURN_NONE; |
| 2140 |
} |
| 2141 |
|
| 2142 |
|
| 2143 |
static PyObject * |
| 2144 |
test_file(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args)) |
| 2145 |
{ |
| 2146 |
const char *filename = __FILE__; |
| 2147 |
PyObject *path = create_string(filename); |
| 2148 |
|
| 2149 |
FILE *fp = Py_fopen(path, "rb"); |
| 2150 |
Py_DECREF(path); |
| 2151 |
assert(fp != NULL); |
| 2152 |
Py_fclose(fp); |
| 2153 |
|
| 2154 |
Py_RETURN_NONE; |
| 2155 |
} |
| 2156 |
|
| 2157 |
|
| 2158 |
#if 0x03090000 <= PY_VERSION_HEX && !defined(PYPY_VERSION) |
| 2159 |
static PyObject * |
| 2160 |
test_config(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args)) |
| 2161 |
{ |
| 2162 |
// Test PyConfig_Get() |
| 2163 |
PyObject *sys = PyImport_ImportModule("sys"); |
| 2164 |
if (sys == _Py_NULL) { |
| 2165 |
return _Py_NULL; |
| 2166 |
} |
| 2167 |
|
| 2168 |
PyObject *obj = PyConfig_Get("argv"); |
| 2169 |
PyObject *sys_attr = PyObject_GetAttrString(sys, "argv"); |
| 2170 |
assert(obj == sys_attr); |
| 2171 |
Py_DECREF(obj); |
| 2172 |
Py_DECREF(sys_attr); |
| 2173 |
|
| 2174 |
obj = PyConfig_Get("module_search_paths"); |
| 2175 |
sys_attr = PyObject_GetAttrString(sys, "path"); |
| 2176 |
assert(obj == sys_attr); |
| 2177 |
Py_DECREF(obj); |
| 2178 |
Py_DECREF(sys_attr); |
| 2179 |
|
| 2180 |
obj = PyConfig_Get("xoptions"); |
| 2181 |
sys_attr = PyObject_GetAttrString(sys, "_xoptions"); |
| 2182 |
assert(obj == sys_attr); |
| 2183 |
Py_DECREF(obj); |
| 2184 |
Py_DECREF(sys_attr); |
| 2185 |
|
| 2186 |
obj = PyConfig_Get("use_environment"); |
| 2187 |
assert(PyBool_Check(obj)); |
| 2188 |
Py_DECREF(obj); |
| 2189 |
|
| 2190 |
obj = PyConfig_Get("verbose"); |
| 2191 |
assert(PyLong_Check(obj)); |
| 2192 |
Py_DECREF(obj); |
| 2193 |
|
| 2194 |
// Get the last member |
| 2195 |
#if 0x030A0000 <= PY_VERSION_HEX |
| 2196 |
obj = PyConfig_Get("warn_default_encoding"); |
| 2197 |
#else |
| 2198 |
obj = PyConfig_Get("user_site_directory"); |
| 2199 |
#endif |
| 2200 |
assert(PyLong_Check(obj)); |
| 2201 |
Py_DECREF(obj); |
| 2202 |
|
| 2203 |
assert(PyConfig_Get("nonexistent") == NULL); |
| 2204 |
assert(PyErr_ExceptionMatches(PyExc_ValueError)); |
| 2205 |
PyErr_Clear(); |
| 2206 |
|
| 2207 |
// Test PyConfig_GetInt() |
| 2208 |
int value = -3; |
| 2209 |
|
| 2210 |
assert(PyConfig_GetInt("verbose", &value) == 0); |
| 2211 |
assert(value >= 0); |
| 2212 |
|
| 2213 |
assert(PyConfig_GetInt("argv", &value) == -1); |
| 2214 |
assert(PyErr_ExceptionMatches(PyExc_TypeError)); |
| 2215 |
PyErr_Clear(); |
| 2216 |
|
| 2217 |
assert(PyConfig_GetInt("nonexistent", &value) == -1); |
| 2218 |
assert(PyErr_ExceptionMatches(PyExc_ValueError)); |
| 2219 |
PyErr_Clear(); |
| 2220 |
|
| 2221 |
Py_DECREF(sys); |
| 2222 |
Py_RETURN_NONE; |
| 2223 |
} |
| 2224 |
#endif |
| 2225 |
|
| 2226 |
|
| 2227 |
static PyObject * |
| 2228 |
test_sys(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args)) |
| 2229 |
{ |
| 2230 |
const char *stdout_str = "stdout"; |
| 2231 |
PyObject *stdout_obj = create_string(stdout_str); |
| 2232 |
#if PYTHON3 |
| 2233 |
PyObject *sys_stdout = PySys_GetObject(stdout_str); // borrowed ref |
| 2234 |
#else |
| 2235 |
PyObject *sys_stdout = PySys_GetObject((char*)stdout_str); // borrowed ref |
| 2236 |
#endif |
| 2237 |
const char *nonexistent_str = "nonexistent"; |
| 2238 |
PyObject *nonexistent_obj = create_string(nonexistent_str); |
| 2239 |
PyObject *error_obj = PyLong_FromLong(1); |
| 2240 |
PyObject *value; |
| 2241 |
|
| 2242 |
// get sys.stdout |
| 2243 |
value = PySys_GetAttr(stdout_obj); |
| 2244 |
assert(value == sys_stdout); |
| 2245 |
Py_DECREF(value); |
| 2246 |
|
| 2247 |
value = PySys_GetAttrString(stdout_str); |
| 2248 |
assert(value == sys_stdout); |
| 2249 |
Py_DECREF(value); |
| 2250 |
|
| 2251 |
value = UNINITIALIZED_OBJ; |
| 2252 |
assert(PySys_GetOptionalAttr(stdout_obj, &value) == 1); |
| 2253 |
assert(value == sys_stdout); |
| 2254 |
Py_DECREF(value); |
| 2255 |
|
| 2256 |
value = UNINITIALIZED_OBJ; |
| 2257 |
assert(PySys_GetOptionalAttrString(stdout_str, &value) == 1); |
| 2258 |
assert(value == sys_stdout); |
| 2259 |
Py_DECREF(value); |
| 2260 |
|
| 2261 |
// non existent attribute |
| 2262 |
value = PySys_GetAttr(nonexistent_obj); |
| 2263 |
assert(value == NULL); |
| 2264 |
assert(PyErr_ExceptionMatches(PyExc_RuntimeError)); |
| 2265 |
PyErr_Clear(); |
| 2266 |
|
| 2267 |
value = PySys_GetAttrString(nonexistent_str); |
| 2268 |
assert(value == NULL); |
| 2269 |
assert(PyErr_ExceptionMatches(PyExc_RuntimeError)); |
| 2270 |
PyErr_Clear(); |
| 2271 |
|
| 2272 |
value = UNINITIALIZED_OBJ; |
| 2273 |
assert(PySys_GetOptionalAttr(nonexistent_obj, &value) == 0); |
| 2274 |
assert(value == NULL); |
| 2275 |
|
| 2276 |
value = UNINITIALIZED_OBJ; |
| 2277 |
assert(PySys_GetOptionalAttrString(nonexistent_str, &value) == 0); |
| 2278 |
assert(value == NULL); |
| 2279 |
|
| 2280 |
// invalid attribute type |
| 2281 |
value = PySys_GetAttr(error_obj); |
| 2282 |
assert(value == NULL); |
| 2283 |
assert(PyErr_ExceptionMatches(PyExc_TypeError)); |
| 2284 |
PyErr_Clear(); |
| 2285 |
|
| 2286 |
value = UNINITIALIZED_OBJ; |
| 2287 |
assert(PySys_GetOptionalAttr(error_obj, &value) == -1); |
| 2288 |
assert(value == NULL); |
| 2289 |
assert(PyErr_ExceptionMatches(PyExc_TypeError)); |
| 2290 |
PyErr_Clear(); |
| 2291 |
|
| 2292 |
Py_DECREF(stdout_obj); |
| 2293 |
Py_DECREF(nonexistent_obj); |
| 2294 |
Py_RETURN_NONE; |
| 2295 |
} |
| 2296 |
|
| 2297 |
|
| 2298 |
static int |
| 2299 |
test_byteswriter_highlevel(void) |
| 2300 |
{ |
| 2301 |
PyObject *obj; |
| 2302 |
PyBytesWriter *writer = PyBytesWriter_Create(0); |
| 2303 |
if (writer == NULL) { |
| 2304 |
goto error; |
| 2305 |
} |
| 2306 |
if (PyBytesWriter_WriteBytes(writer, "Hello", -1) < 0) { |
| 2307 |
goto error; |
| 2308 |
} |
| 2309 |
if (PyBytesWriter_Format(writer, " %s!", "World") < 0) { |
| 2310 |
goto error; |
| 2311 |
} |
| 2312 |
|
| 2313 |
obj = PyBytesWriter_Finish(writer); |
| 2314 |
if (obj == NULL) { |
| 2315 |
return -1; |
| 2316 |
} |
| 2317 |
assert(PyBytes_Check(obj)); |
| 2318 |
assert(strcmp(PyBytes_AS_STRING(obj), "Hello World!") == 0); |
| 2319 |
Py_DECREF(obj); |
| 2320 |
return 0; |
| 2321 |
|
| 2322 |
error: |
| 2323 |
PyBytesWriter_Discard(writer); |
| 2324 |
return -1; |
| 2325 |
} |
| 2326 |
|
| 2327 |
static int |
| 2328 |
test_byteswriter_abc(void) |
| 2329 |
{ |
| 2330 |
PyBytesWriter *writer = PyBytesWriter_Create(3); |
| 2331 |
if (writer == NULL) { |
| 2332 |
return -1; |
| 2333 |
} |
| 2334 |
|
| 2335 |
char *str = (char*)PyBytesWriter_GetData(writer); |
| 2336 |
memcpy(str, "abc", 3); |
| 2337 |
|
| 2338 |
PyObject *obj = PyBytesWriter_Finish(writer); |
| 2339 |
if (obj == NULL) { |
| 2340 |
return -1; |
| 2341 |
} |
| 2342 |
assert(PyBytes_Check(obj)); |
| 2343 |
assert(strcmp(PyBytes_AS_STRING(obj), "abc") == 0); |
| 2344 |
Py_DECREF(obj); |
| 2345 |
return 0; |
| 2346 |
} |
| 2347 |
|
| 2348 |
static int |
| 2349 |
test_byteswriter_grow(void) |
| 2350 |
{ |
| 2351 |
PyBytesWriter *writer = PyBytesWriter_Create(10); |
| 2352 |
if (writer == NULL) { |
| 2353 |
return -1; |
| 2354 |
} |
| 2355 |
|
| 2356 |
char *buf = (char*)PyBytesWriter_GetData(writer); |
| 2357 |
memcpy(buf, "Hello ", strlen("Hello ")); |
| 2358 |
buf += strlen("Hello "); |
| 2359 |
|
| 2360 |
buf = (char*)PyBytesWriter_GrowAndUpdatePointer(writer, 10, buf); |
| 2361 |
if (buf == NULL) { |
| 2362 |
PyBytesWriter_Discard(writer); |
| 2363 |
return -1; |
| 2364 |
} |
| 2365 |
|
| 2366 |
memcpy(buf, "World", strlen("World")); |
| 2367 |
buf += strlen("World"); |
| 2368 |
|
| 2369 |
PyObject *obj = PyBytesWriter_FinishWithPointer(writer, buf); |
| 2370 |
if (obj == NULL) { |
| 2371 |
return -1; |
| 2372 |
} |
| 2373 |
assert(PyBytes_Check(obj)); |
| 2374 |
assert(strcmp(PyBytes_AS_STRING(obj), "Hello World") == 0); |
| 2375 |
Py_DECREF(obj); |
| 2376 |
return 0; |
| 2377 |
} |
| 2378 |
|
| 2379 |
static PyObject * |
| 2380 |
test_byteswriter(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args)) |
| 2381 |
{ |
| 2382 |
if (test_byteswriter_highlevel() < 0) { |
| 2383 |
return NULL; |
| 2384 |
} |
| 2385 |
if (test_byteswriter_abc() < 0) { |
| 2386 |
return NULL; |
| 2387 |
} |
| 2388 |
if (test_byteswriter_grow() < 0) { |
| 2389 |
return NULL; |
| 2390 |
} |
| 2391 |
Py_RETURN_NONE; |
| 2392 |
} |
| 2393 |
|
| 2394 |
|
| 2395 |
static struct PyMethodDef methods[] = { |
| 2396 |
{"test_object", test_object, METH_NOARGS, _Py_NULL}, |
| 2397 |
{"test_py_is", test_py_is, METH_NOARGS, _Py_NULL}, |
| 2398 |
#ifndef PYPY_VERSION |
| 2399 |
{"test_frame", test_frame, METH_NOARGS, _Py_NULL}, |
| 2400 |
#endif |
| 2401 |
{"test_thread_state", test_thread_state, METH_NOARGS, _Py_NULL}, |
| 2402 |
{"test_interpreter", test_interpreter, METH_NOARGS, _Py_NULL}, |
| 2403 |
{"test_calls", test_calls, METH_NOARGS, _Py_NULL}, |
| 2404 |
{"test_gc", test_gc, METH_NOARGS, _Py_NULL}, |
| 2405 |
{"test_module", test_module, METH_NOARGS, _Py_NULL}, |
| 2406 |
#if (PY_VERSION_HEX <= 0x030B00A1 || 0x030B00A7 <= PY_VERSION_HEX) && !defined(PYPY_VERSION) |
| 2407 |
{"test_float_pack", test_float_pack, METH_NOARGS, _Py_NULL}, |
| 2408 |
#endif |
| 2409 |
#ifndef PYPY_VERSION |
| 2410 |
{"test_code", test_code, METH_NOARGS, _Py_NULL}, |
| 2411 |
#endif |
| 2412 |
{"test_api_casts", test_api_casts, METH_NOARGS, _Py_NULL}, |
| 2413 |
{"test_import", test_import, METH_NOARGS, _Py_NULL}, |
| 2414 |
{"test_weakref", test_weakref, METH_NOARGS, _Py_NULL}, |
| 2415 |
{"func_varargs", (PyCFunction)(void*)func_varargs, METH_VARARGS | METH_KEYWORDS, _Py_NULL}, |
| 2416 |
{"test_vectorcall", test_vectorcall, METH_NOARGS, _Py_NULL}, |
| 2417 |
{"test_getattr", test_getattr, METH_NOARGS, _Py_NULL}, |
| 2418 |
{"test_getitem", test_getitem, METH_NOARGS, _Py_NULL}, |
| 2419 |
{"test_dict_api", test_dict_api, METH_NOARGS, _Py_NULL}, |
| 2420 |
{"test_dict_pop", test_dict_pop, METH_NOARGS, _Py_NULL}, |
| 2421 |
{"test_dict_setdefault", test_dict_setdefault, METH_NOARGS, _Py_NULL}, |
| 2422 |
{"test_long_api", test_long_api, METH_NOARGS, _Py_NULL}, |
| 2423 |
#ifdef TEST_MANAGED_DICT |
| 2424 |
{"test_managed_dict", test_managed_dict, METH_NOARGS, _Py_NULL}, |
| 2425 |
#endif |
| 2426 |
{"test_unicode", test_unicode, METH_NOARGS, _Py_NULL}, |
| 2427 |
{"test_list", test_list, METH_NOARGS, _Py_NULL}, |
| 2428 |
{"test_hash", test_hash, METH_NOARGS, _Py_NULL}, |
| 2429 |
#ifdef TEST_PYTIME |
| 2430 |
{"test_time", test_time, METH_NOARGS, _Py_NULL}, |
| 2431 |
#endif |
| 2432 |
{"test_get_constant", test_get_constant, METH_NOARGS, _Py_NULL}, |
| 2433 |
#ifdef TEST_UNICODEWRITER |
| 2434 |
{"test_unicodewriter", test_unicodewriter, METH_NOARGS, _Py_NULL}, |
| 2435 |
{"test_unicodewriter_widechar", test_unicodewriter_widechar, METH_NOARGS, _Py_NULL}, |
| 2436 |
{"test_unicodewriter_format", test_unicodewriter_format, METH_NOARGS, _Py_NULL}, |
| 2437 |
#endif |
| 2438 |
{"test_bytes", test_bytes, METH_NOARGS, _Py_NULL}, |
| 2439 |
{"test_iter", test_iter, METH_NOARGS, _Py_NULL}, |
| 2440 |
{"test_long_stdint", test_long_stdint, METH_NOARGS, _Py_NULL}, |
| 2441 |
{"test_structmember", test_structmember, METH_NOARGS, _Py_NULL}, |
| 2442 |
{"test_file", test_file, METH_NOARGS, _Py_NULL}, |
| 2443 |
#if 0x03090000 <= PY_VERSION_HEX && !defined(PYPY_VERSION) |
| 2444 |
{"test_config", test_config, METH_NOARGS, _Py_NULL}, |
| 2445 |
#endif |
| 2446 |
{"test_sys", test_sys, METH_NOARGS, _Py_NULL}, |
| 2447 |
{"test_uniquely_referenced", test_uniquely_referenced, METH_NOARGS, _Py_NULL}, |
| 2448 |
{"test_byteswriter", test_byteswriter, METH_NOARGS, _Py_NULL}, |
| 2449 |
{_Py_NULL, _Py_NULL, 0, _Py_NULL} |
| 2450 |
}; |
| 2451 |
|
| 2452 |
|
| 2453 |
static int |
| 2454 |
module_exec(PyObject *module) |
| 2455 |
{ |
| 2456 |
#ifdef __cplusplus |
| 2457 |
if (PyModule_AddIntMacro(module, __cplusplus)) { |
| 2458 |
return -1; |
| 2459 |
} |
| 2460 |
#endif |
| 2461 |
if (PyModule_AddStringMacro(module, PY_VERSION)) { |
| 2462 |
return -1; |
| 2463 |
} |
| 2464 |
if (PyModule_AddIntMacro(module, PY_VERSION_HEX)) { |
| 2465 |
return -1; |
| 2466 |
} |
| 2467 |
#ifdef PYPY_VERSION |
| 2468 |
if (PyModule_AddStringMacro(module, PYPY_VERSION)) { |
| 2469 |
return -1; |
| 2470 |
} |
| 2471 |
#endif |
| 2472 |
#ifdef PYPY_VERSION_NUM |
| 2473 |
if (PyModule_AddIntMacro(module, PYPY_VERSION_NUM)) { |
| 2474 |
return -1; |
| 2475 |
} |
| 2476 |
#endif |
| 2477 |
return 0; |
| 2478 |
} |
| 2479 |
|
| 2480 |
|
| 2481 |
#if PY_VERSION_HEX >= 0x03050000 |
| 2482 |
static PyModuleDef_Slot module_slots[] = { |
| 2483 |
{Py_mod_exec, _Py_CAST(void*, module_exec)}, |
| 2484 |
{0, _Py_NULL} |
| 2485 |
}; |
| 2486 |
#endif |
| 2487 |
|
| 2488 |
|
| 2489 |
#ifdef PYTHON3 |
| 2490 |
static struct PyModuleDef module_def = { |
| 2491 |
PyModuleDef_HEAD_INIT, |
| 2492 |
MODULE_NAME_STR, // m_name |
| 2493 |
_Py_NULL, // m_doc |
| 2494 |
0, // m_size |
| 2495 |
methods, // m_methods |
| 2496 |
#if PY_VERSION_HEX >= 0x03050000 |
| 2497 |
module_slots, // m_slots |
| 2498 |
#else |
| 2499 |
_Py_NULL, // m_reload |
| 2500 |
#endif |
| 2501 |
_Py_NULL, // m_traverse |
| 2502 |
_Py_NULL, // m_clear |
| 2503 |
_Py_NULL, // m_free |
| 2504 |
}; |
| 2505 |
|
| 2506 |
|
| 2507 |
#define INIT_FUNC CONCAT(PyInit_, MODULE_NAME) |
| 2508 |
|
| 2509 |
#if PY_VERSION_HEX >= 0x03050000 |
| 2510 |
PyMODINIT_FUNC |
| 2511 |
INIT_FUNC(void) |
| 2512 |
{ |
| 2513 |
return PyModuleDef_Init(&module_def); |
| 2514 |
} |
| 2515 |
#else |
| 2516 |
// Python 3.4 |
| 2517 |
PyMODINIT_FUNC |
| 2518 |
INIT_FUNC(void) |
| 2519 |
{ |
| 2520 |
PyObject *module = PyModule_Create(&module_def); |
| 2521 |
if (module == _Py_NULL) { |
| 2522 |
return _Py_NULL; |
| 2523 |
} |
| 2524 |
if (module_exec(module) < 0) { |
| 2525 |
Py_DECREF(module); |
| 2526 |
return _Py_NULL; |
| 2527 |
} |
| 2528 |
return module; |
| 2529 |
} |
| 2530 |
#endif |
| 2531 |
|
| 2532 |
#else |
| 2533 |
// Python 2 |
| 2534 |
|
| 2535 |
#define INIT_FUNC CONCAT(init, MODULE_NAME) |
| 2536 |
|
| 2537 |
PyMODINIT_FUNC |
| 2538 |
INIT_FUNC(void) |
| 2539 |
{ |
| 2540 |
PyObject *module; |
| 2541 |
module = Py_InitModule4(MODULE_NAME_STR, |
| 2542 |
methods, |
| 2543 |
_Py_NULL, |
| 2544 |
_Py_NULL, |
| 2545 |
PYTHON_API_VERSION); |
| 2546 |
if (module == _Py_NULL) { |
| 2547 |
return; |
| 2548 |
} |
| 2549 |
|
| 2550 |
if (module_exec(module) < 0) { |
| 2551 |
return; |
| 2552 |
} |
| 2553 |
} |
| 2554 |
#endif |