| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "Python.h" |
| 9 |
#include <float.h> |
| 10 |
#include "structmember.h" |
| 11 |
#include "datetime.h" |
| 12 |
#include "marshal.h" |
| 13 |
#include <signal.h> |
| 14 |
#ifdef MS_WINDOWS |
| 15 |
# include <crtdbg.h> |
| 16 |
#endif |
| 17 |
|
| 18 |
#ifdef HAVE_SYS_WAIT_H |
| 19 |
#include <sys/wait.h> |
| 20 |
#endif |
| 21 |
|
| 22 |
#ifdef WITH_THREAD |
| 23 |
#include "pythread.h" |
| 24 |
#endif |
| 25 |
static PyObject *TestError; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
static PyObject * |
| 30 |
raiseTestError(const char* test_name, const char* msg) |
| 31 |
{ |
| 32 |
char buf[2048]; |
| 33 |
|
| 34 |
if (strlen(test_name) + strlen(msg) > sizeof(buf) - 50) |
| 35 |
PyErr_SetString(TestError, "internal error msg too large"); |
| 36 |
else { |
| 37 |
PyOS_snprintf(buf, sizeof(buf), "%s: %s", test_name, msg); |
| 38 |
PyErr_SetString(TestError, buf); |
| 39 |
} |
| 40 |
return NULL; |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
static PyObject* |
| 50 |
sizeof_error(const char* fatname, const char* typname, |
| 51 |
int expected, int got) |
| 52 |
{ |
| 53 |
char buf[1024]; |
| 54 |
PyOS_snprintf(buf, sizeof(buf), |
| 55 |
"%.200s #define == %d but sizeof(%.200s) == %d", |
| 56 |
fatname, expected, typname, got); |
| 57 |
PyErr_SetString(TestError, buf); |
| 58 |
return (PyObject*)NULL; |
| 59 |
} |
| 60 |
|
| 61 |
static PyObject* |
| 62 |
test_config(PyObject *self) |
| 63 |
{ |
| 64 |
#define CHECK_SIZEOF(FATNAME, TYPE) \ |
| 65 |
if (FATNAME != sizeof(TYPE)) \ |
| 66 |
return sizeof_error(#FATNAME, #TYPE, FATNAME, sizeof(TYPE)) |
| 67 |
|
| 68 |
CHECK_SIZEOF(SIZEOF_SHORT, short); |
| 69 |
CHECK_SIZEOF(SIZEOF_INT, int); |
| 70 |
CHECK_SIZEOF(SIZEOF_LONG, long); |
| 71 |
CHECK_SIZEOF(SIZEOF_VOID_P, void*); |
| 72 |
CHECK_SIZEOF(SIZEOF_TIME_T, time_t); |
| 73 |
#ifdef HAVE_LONG_LONG |
| 74 |
CHECK_SIZEOF(SIZEOF_LONG_LONG, PY_LONG_LONG); |
| 75 |
#endif |
| 76 |
|
| 77 |
#undef CHECK_SIZEOF |
| 78 |
|
| 79 |
Py_INCREF(Py_None); |
| 80 |
return Py_None; |
| 81 |
} |
| 82 |
|
| 83 |
static PyObject* |
| 84 |
test_list_api(PyObject *self) |
| 85 |
{ |
| 86 |
PyObject* list; |
| 87 |
int i; |
| 88 |
|
| 89 |
|
| 90 |
#define NLIST 30 |
| 91 |
list = PyList_New(NLIST); |
| 92 |
if (list == (PyObject*)NULL) |
| 93 |
return (PyObject*)NULL; |
| 94 |
|
| 95 |
for (i = 0; i < NLIST; ++i) { |
| 96 |
PyObject* anint = PyInt_FromLong(i); |
| 97 |
if (anint == (PyObject*)NULL) { |
| 98 |
Py_DECREF(list); |
| 99 |
return (PyObject*)NULL; |
| 100 |
} |
| 101 |
PyList_SET_ITEM(list, i, anint); |
| 102 |
} |
| 103 |
|
| 104 |
i = PyList_Reverse(list); |
| 105 |
if (i != 0) { |
| 106 |
Py_DECREF(list); |
| 107 |
return (PyObject*)NULL; |
| 108 |
} |
| 109 |
|
| 110 |
for (i = 0; i < NLIST; ++i) { |
| 111 |
PyObject* anint = PyList_GET_ITEM(list, i); |
| 112 |
if (PyInt_AS_LONG(anint) != NLIST-1-i) { |
| 113 |
PyErr_SetString(TestError, |
| 114 |
"test_list_api: reverse screwed up"); |
| 115 |
Py_DECREF(list); |
| 116 |
return (PyObject*)NULL; |
| 117 |
} |
| 118 |
} |
| 119 |
Py_DECREF(list); |
| 120 |
#undef NLIST |
| 121 |
|
| 122 |
Py_INCREF(Py_None); |
| 123 |
return Py_None; |
| 124 |
} |
| 125 |
|
| 126 |
static int |
| 127 |
test_dict_inner(int count) |
| 128 |
{ |
| 129 |
Py_ssize_t pos = 0, iterations = 0; |
| 130 |
int i; |
| 131 |
PyObject *dict = PyDict_New(); |
| 132 |
PyObject *v, *k; |
| 133 |
|
| 134 |
if (dict == NULL) |
| 135 |
return -1; |
| 136 |
|
| 137 |
for (i = 0; i < count; i++) { |
| 138 |
v = PyInt_FromLong(i); |
| 139 |
PyDict_SetItem(dict, v, v); |
| 140 |
Py_DECREF(v); |
| 141 |
} |
| 142 |
|
| 143 |
while (PyDict_Next(dict, &pos, &k, &v)) { |
| 144 |
PyObject *o; |
| 145 |
iterations++; |
| 146 |
|
| 147 |
i = PyInt_AS_LONG(v) + 1; |
| 148 |
o = PyInt_FromLong(i); |
| 149 |
if (o == NULL) |
| 150 |
return -1; |
| 151 |
if (PyDict_SetItem(dict, k, o) < 0) { |
| 152 |
Py_DECREF(o); |
| 153 |
return -1; |
| 154 |
} |
| 155 |
Py_DECREF(o); |
| 156 |
} |
| 157 |
|
| 158 |
Py_DECREF(dict); |
| 159 |
|
| 160 |
if (iterations != count) { |
| 161 |
PyErr_SetString( |
| 162 |
TestError, |
| 163 |
"test_dict_iteration: dict iteration went wrong "); |
| 164 |
return -1; |
| 165 |
} else { |
| 166 |
return 0; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
static PyObject* |
| 171 |
test_dict_iteration(PyObject* self) |
| 172 |
{ |
| 173 |
int i; |
| 174 |
|
| 175 |
for (i = 0; i < 200; i++) { |
| 176 |
if (test_dict_inner(i) < 0) { |
| 177 |
return NULL; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
Py_INCREF(Py_None); |
| 182 |
return Py_None; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
static PyTypeObject _HashInheritanceTester_Type = { |
| 190 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 191 |
"hashinheritancetester", |
| 192 |
sizeof(PyObject), |
| 193 |
0, |
| 194 |
(destructor)PyObject_Del, |
| 195 |
0, |
| 196 |
0, |
| 197 |
0, |
| 198 |
0, |
| 199 |
0, |
| 200 |
0, |
| 201 |
0, |
| 202 |
0, |
| 203 |
0, |
| 204 |
0, |
| 205 |
0, |
| 206 |
PyObject_GenericGetAttr, |
| 207 |
0, |
| 208 |
0, |
| 209 |
Py_TPFLAGS_DEFAULT, |
| 210 |
0, |
| 211 |
0, |
| 212 |
0, |
| 213 |
0, |
| 214 |
0, |
| 215 |
0, |
| 216 |
0, |
| 217 |
0, |
| 218 |
0, |
| 219 |
0, |
| 220 |
0, |
| 221 |
0, |
| 222 |
0, |
| 223 |
0, |
| 224 |
0, |
| 225 |
0, |
| 226 |
0, |
| 227 |
PyType_GenericNew, |
| 228 |
}; |
| 229 |
|
| 230 |
static PyObject* |
| 231 |
test_lazy_hash_inheritance(PyObject* self) |
| 232 |
{ |
| 233 |
PyTypeObject *type; |
| 234 |
PyObject *obj; |
| 235 |
long hash; |
| 236 |
|
| 237 |
type = &_HashInheritanceTester_Type; |
| 238 |
|
| 239 |
if (type->tp_dict != NULL) |
| 240 |
|
| 241 |
|
| 242 |
Py_RETURN_NONE; |
| 243 |
|
| 244 |
|
| 245 |
obj = PyObject_New(PyObject, type); |
| 246 |
if (obj == NULL) { |
| 247 |
PyErr_Clear(); |
| 248 |
PyErr_SetString( |
| 249 |
TestError, |
| 250 |
"test_lazy_hash_inheritance: failed to create object"); |
| 251 |
return NULL; |
| 252 |
} |
| 253 |
|
| 254 |
if (type->tp_dict != NULL) { |
| 255 |
PyErr_SetString( |
| 256 |
TestError, |
| 257 |
"test_lazy_hash_inheritance: type initialised too soon"); |
| 258 |
Py_DECREF(obj); |
| 259 |
return NULL; |
| 260 |
} |
| 261 |
|
| 262 |
hash = PyObject_Hash(obj); |
| 263 |
if ((hash == -1) && PyErr_Occurred()) { |
| 264 |
PyErr_Clear(); |
| 265 |
PyErr_SetString( |
| 266 |
TestError, |
| 267 |
"test_lazy_hash_inheritance: could not hash object"); |
| 268 |
Py_DECREF(obj); |
| 269 |
return NULL; |
| 270 |
} |
| 271 |
|
| 272 |
if (type->tp_dict == NULL) { |
| 273 |
PyErr_SetString( |
| 274 |
TestError, |
| 275 |
"test_lazy_hash_inheritance: type not initialised by hash()"); |
| 276 |
Py_DECREF(obj); |
| 277 |
return NULL; |
| 278 |
} |
| 279 |
|
| 280 |
if (type->tp_hash != PyType_Type.tp_hash) { |
| 281 |
PyErr_SetString( |
| 282 |
TestError, |
| 283 |
"test_lazy_hash_inheritance: unexpected hash function"); |
| 284 |
Py_DECREF(obj); |
| 285 |
return NULL; |
| 286 |
} |
| 287 |
|
| 288 |
Py_DECREF(obj); |
| 289 |
|
| 290 |
Py_RETURN_NONE; |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
static int |
| 299 |
broken_buffer_getbuffer(PyObject *self, Py_buffer *view, int flags) |
| 300 |
{ |
| 301 |
PyErr_SetString( |
| 302 |
TestError, |
| 303 |
"test_broken_memoryview: expected error in bf_getbuffer"); |
| 304 |
return -1; |
| 305 |
} |
| 306 |
|
| 307 |
static PyBufferProcs memoryviewtester_as_buffer = { |
| 308 |
0, |
| 309 |
0, |
| 310 |
0, |
| 311 |
0, |
| 312 |
(getbufferproc)broken_buffer_getbuffer, |
| 313 |
0, |
| 314 |
}; |
| 315 |
|
| 316 |
static PyTypeObject _MemoryViewTester_Type = { |
| 317 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 318 |
"memoryviewtester", |
| 319 |
sizeof(PyObject), |
| 320 |
0, |
| 321 |
(destructor)PyObject_Del, |
| 322 |
0, |
| 323 |
0, |
| 324 |
0, |
| 325 |
0, |
| 326 |
0, |
| 327 |
0, |
| 328 |
0, |
| 329 |
0, |
| 330 |
0, |
| 331 |
0, |
| 332 |
0, |
| 333 |
PyObject_GenericGetAttr, |
| 334 |
0, |
| 335 |
&memoryviewtester_as_buffer, |
| 336 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_NEWBUFFER, |
| 337 |
0, |
| 338 |
0, |
| 339 |
0, |
| 340 |
0, |
| 341 |
0, |
| 342 |
0, |
| 343 |
0, |
| 344 |
0, |
| 345 |
0, |
| 346 |
0, |
| 347 |
0, |
| 348 |
0, |
| 349 |
0, |
| 350 |
0, |
| 351 |
0, |
| 352 |
0, |
| 353 |
0, |
| 354 |
PyType_GenericNew, |
| 355 |
}; |
| 356 |
|
| 357 |
static PyObject* |
| 358 |
test_broken_memoryview(PyObject* self) |
| 359 |
{ |
| 360 |
PyObject *obj = PyObject_New(PyObject, &_MemoryViewTester_Type); |
| 361 |
PyObject *res; |
| 362 |
|
| 363 |
if (obj == NULL) { |
| 364 |
PyErr_Clear(); |
| 365 |
PyErr_SetString( |
| 366 |
TestError, |
| 367 |
"test_broken_memoryview: failed to create object"); |
| 368 |
return NULL; |
| 369 |
} |
| 370 |
|
| 371 |
res = PyMemoryView_FromObject(obj); |
| 372 |
if (res || !PyErr_Occurred()){ |
| 373 |
PyErr_SetString( |
| 374 |
TestError, |
| 375 |
"test_broken_memoryview: memoryview() didn't raise an Exception"); |
| 376 |
Py_XDECREF(res); |
| 377 |
Py_DECREF(obj); |
| 378 |
return NULL; |
| 379 |
} |
| 380 |
|
| 381 |
PyErr_Clear(); |
| 382 |
Py_DECREF(obj); |
| 383 |
Py_RETURN_NONE; |
| 384 |
} |
| 385 |
|
| 386 |
static PyObject * |
| 387 |
test_to_contiguous(PyObject* self, PyObject *noargs) |
| 388 |
{ |
| 389 |
int data[9] = {0, -1, 1, -1, 2, -1, 3, -1, 4}; |
| 390 |
int result[5]; |
| 391 |
Py_ssize_t itemsize = sizeof(int); |
| 392 |
Py_ssize_t shape = 5; |
| 393 |
Py_ssize_t strides = 2 * itemsize; |
| 394 |
Py_buffer view = { |
| 395 |
data, |
| 396 |
NULL, |
| 397 |
5 * itemsize, |
| 398 |
itemsize, |
| 399 |
1, |
| 400 |
1, |
| 401 |
NULL, |
| 402 |
&shape, |
| 403 |
&strides, |
| 404 |
NULL, |
| 405 |
{0, 0}, |
| 406 |
NULL |
| 407 |
}; |
| 408 |
int i; |
| 409 |
|
| 410 |
PyBuffer_ToContiguous(result, &view, view.len, 'C'); |
| 411 |
for (i = 0; i < 5; i++) { |
| 412 |
if (result[i] != i) { |
| 413 |
PyErr_SetString(TestError, |
| 414 |
"test_to_contiguous: incorrect result"); |
| 415 |
return NULL; |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
view.buf = &data[8]; |
| 420 |
view.strides[0] = -2 * itemsize; |
| 421 |
|
| 422 |
PyBuffer_ToContiguous(result, &view, view.len, 'C'); |
| 423 |
for (i = 0; i < 5; i++) { |
| 424 |
if (result[i] != 4-i) { |
| 425 |
PyErr_SetString(TestError, |
| 426 |
"test_to_contiguous: incorrect result"); |
| 427 |
return NULL; |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
Py_RETURN_NONE; |
| 432 |
} |
| 433 |
|
| 434 |
static PyObject * |
| 435 |
test_from_contiguous(PyObject* self, PyObject *noargs) |
| 436 |
{ |
| 437 |
int data[9] = {-1,-1,-1,-1,-1,-1,-1,-1,-1}; |
| 438 |
int init[5] = {0, 1, 2, 3, 4}; |
| 439 |
Py_ssize_t itemsize = sizeof(int); |
| 440 |
Py_ssize_t shape = 5; |
| 441 |
Py_ssize_t strides = 2 * itemsize; |
| 442 |
Py_buffer view = { |
| 443 |
data, |
| 444 |
NULL, |
| 445 |
5 * itemsize, |
| 446 |
itemsize, |
| 447 |
1, |
| 448 |
1, |
| 449 |
NULL, |
| 450 |
&shape, |
| 451 |
&strides, |
| 452 |
NULL, |
| 453 |
{0, 0}, |
| 454 |
NULL |
| 455 |
}; |
| 456 |
int *ptr; |
| 457 |
int i; |
| 458 |
|
| 459 |
PyBuffer_FromContiguous(&view, init, view.len, 'C'); |
| 460 |
ptr = view.buf; |
| 461 |
for (i = 0; i < 5; i++) { |
| 462 |
if (ptr[2*i] != i) { |
| 463 |
PyErr_SetString(TestError, |
| 464 |
"test_from_contiguous: incorrect result"); |
| 465 |
return NULL; |
| 466 |
} |
| 467 |
} |
| 468 |
|
| 469 |
view.buf = &data[8]; |
| 470 |
view.strides[0] = -2 * itemsize; |
| 471 |
|
| 472 |
PyBuffer_FromContiguous(&view, init, view.len, 'C'); |
| 473 |
ptr = view.buf; |
| 474 |
for (i = 0; i < 5; i++) { |
| 475 |
if (*(ptr-2*i) != i) { |
| 476 |
PyErr_SetString(TestError, |
| 477 |
"test_from_contiguous: incorrect result"); |
| 478 |
return NULL; |
| 479 |
} |
| 480 |
} |
| 481 |
|
| 482 |
Py_RETURN_NONE; |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
#define UNBIND(X) Py_DECREF(X); (X) = NULL |
| 502 |
|
| 503 |
static PyObject * |
| 504 |
raise_test_long_error(const char* msg) |
| 505 |
{ |
| 506 |
return raiseTestError("test_long_api", msg); |
| 507 |
} |
| 508 |
|
| 509 |
#define TESTNAME test_long_api_inner |
| 510 |
#define TYPENAME long |
| 511 |
#define F_S_TO_PY PyLong_FromLong |
| 512 |
#define F_PY_TO_S PyLong_AsLong |
| 513 |
#define F_U_TO_PY PyLong_FromUnsignedLong |
| 514 |
#define F_PY_TO_U PyLong_AsUnsignedLong |
| 515 |
|
| 516 |
#include "testcapi_long.h" |
| 517 |
|
| 518 |
static PyObject * |
| 519 |
test_long_api(PyObject* self) |
| 520 |
{ |
| 521 |
return TESTNAME(raise_test_long_error); |
| 522 |
} |
| 523 |
|
| 524 |
#undef TESTNAME |
| 525 |
#undef TYPENAME |
| 526 |
#undef F_S_TO_PY |
| 527 |
#undef F_PY_TO_S |
| 528 |
#undef F_U_TO_PY |
| 529 |
#undef F_PY_TO_U |
| 530 |
|
| 531 |
#ifdef HAVE_LONG_LONG |
| 532 |
|
| 533 |
static PyObject * |
| 534 |
raise_test_longlong_error(const char* msg) |
| 535 |
{ |
| 536 |
return raiseTestError("test_longlong_api", msg); |
| 537 |
} |
| 538 |
|
| 539 |
#define TESTNAME test_longlong_api_inner |
| 540 |
#define TYPENAME PY_LONG_LONG |
| 541 |
#define F_S_TO_PY PyLong_FromLongLong |
| 542 |
#define F_PY_TO_S PyLong_AsLongLong |
| 543 |
#define F_U_TO_PY PyLong_FromUnsignedLongLong |
| 544 |
#define F_PY_TO_U PyLong_AsUnsignedLongLong |
| 545 |
|
| 546 |
#include "testcapi_long.h" |
| 547 |
|
| 548 |
static PyObject * |
| 549 |
test_longlong_api(PyObject* self, PyObject *args) |
| 550 |
{ |
| 551 |
return TESTNAME(raise_test_longlong_error); |
| 552 |
} |
| 553 |
|
| 554 |
#undef TESTNAME |
| 555 |
#undef TYPENAME |
| 556 |
#undef F_S_TO_PY |
| 557 |
#undef F_PY_TO_S |
| 558 |
#undef F_U_TO_PY |
| 559 |
#undef F_PY_TO_U |
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
static PyObject * |
| 567 |
test_long_and_overflow(PyObject *self) |
| 568 |
{ |
| 569 |
PyObject *num, *one, *temp; |
| 570 |
long value; |
| 571 |
int overflow; |
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 576 |
if (num == NULL) |
| 577 |
return NULL; |
| 578 |
overflow = 1234; |
| 579 |
value = PyLong_AsLongAndOverflow(num, &overflow); |
| 580 |
Py_DECREF(num); |
| 581 |
if (value == -1 && PyErr_Occurred()) |
| 582 |
return NULL; |
| 583 |
if (value != -1) |
| 584 |
return raiseTestError("test_long_and_overflow", |
| 585 |
"return value was not set to -1"); |
| 586 |
if (overflow != 1) |
| 587 |
return raiseTestError("test_long_and_overflow", |
| 588 |
"overflow was not set to 1"); |
| 589 |
|
| 590 |
|
| 591 |
num = PyLong_FromLong(LONG_MAX); |
| 592 |
if (num == NULL) |
| 593 |
return NULL; |
| 594 |
one = PyLong_FromLong(1L); |
| 595 |
if (one == NULL) { |
| 596 |
Py_DECREF(num); |
| 597 |
return NULL; |
| 598 |
} |
| 599 |
temp = PyNumber_Add(num, one); |
| 600 |
Py_DECREF(one); |
| 601 |
Py_DECREF(num); |
| 602 |
num = temp; |
| 603 |
if (num == NULL) |
| 604 |
return NULL; |
| 605 |
overflow = 0; |
| 606 |
value = PyLong_AsLongAndOverflow(num, &overflow); |
| 607 |
Py_DECREF(num); |
| 608 |
if (value == -1 && PyErr_Occurred()) |
| 609 |
return NULL; |
| 610 |
if (value != -1) |
| 611 |
return raiseTestError("test_long_and_overflow", |
| 612 |
"return value was not set to -1"); |
| 613 |
if (overflow != 1) |
| 614 |
return raiseTestError("test_long_and_overflow", |
| 615 |
"overflow was not set to 1"); |
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
num = PyLong_FromString("-FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 620 |
if (num == NULL) |
| 621 |
return NULL; |
| 622 |
overflow = 1234; |
| 623 |
value = PyLong_AsLongAndOverflow(num, &overflow); |
| 624 |
Py_DECREF(num); |
| 625 |
if (value == -1 && PyErr_Occurred()) |
| 626 |
return NULL; |
| 627 |
if (value != -1) |
| 628 |
return raiseTestError("test_long_and_overflow", |
| 629 |
"return value was not set to -1"); |
| 630 |
if (overflow != -1) |
| 631 |
return raiseTestError("test_long_and_overflow", |
| 632 |
"overflow was not set to -1"); |
| 633 |
|
| 634 |
|
| 635 |
num = PyLong_FromLong(LONG_MIN); |
| 636 |
if (num == NULL) |
| 637 |
return NULL; |
| 638 |
one = PyLong_FromLong(1L); |
| 639 |
if (one == NULL) { |
| 640 |
Py_DECREF(num); |
| 641 |
return NULL; |
| 642 |
} |
| 643 |
temp = PyNumber_Subtract(num, one); |
| 644 |
Py_DECREF(one); |
| 645 |
Py_DECREF(num); |
| 646 |
num = temp; |
| 647 |
if (num == NULL) |
| 648 |
return NULL; |
| 649 |
overflow = 0; |
| 650 |
value = PyLong_AsLongAndOverflow(num, &overflow); |
| 651 |
Py_DECREF(num); |
| 652 |
if (value == -1 && PyErr_Occurred()) |
| 653 |
return NULL; |
| 654 |
if (value != -1) |
| 655 |
return raiseTestError("test_long_and_overflow", |
| 656 |
"return value was not set to -1"); |
| 657 |
if (overflow != -1) |
| 658 |
return raiseTestError("test_long_and_overflow", |
| 659 |
"overflow was not set to -1"); |
| 660 |
|
| 661 |
|
| 662 |
num = PyLong_FromString("FF", NULL, 16); |
| 663 |
if (num == NULL) |
| 664 |
return NULL; |
| 665 |
overflow = 1234; |
| 666 |
value = PyLong_AsLongAndOverflow(num, &overflow); |
| 667 |
Py_DECREF(num); |
| 668 |
if (value == -1 && PyErr_Occurred()) |
| 669 |
return NULL; |
| 670 |
if (value != 0xFF) |
| 671 |
return raiseTestError("test_long_and_overflow", |
| 672 |
"expected return value 0xFF"); |
| 673 |
if (overflow != 0) |
| 674 |
return raiseTestError("test_long_and_overflow", |
| 675 |
"overflow was not cleared"); |
| 676 |
|
| 677 |
num = PyLong_FromString("-FF", NULL, 16); |
| 678 |
if (num == NULL) |
| 679 |
return NULL; |
| 680 |
overflow = 0; |
| 681 |
value = PyLong_AsLongAndOverflow(num, &overflow); |
| 682 |
Py_DECREF(num); |
| 683 |
if (value == -1 && PyErr_Occurred()) |
| 684 |
return NULL; |
| 685 |
if (value != -0xFF) |
| 686 |
return raiseTestError("test_long_and_overflow", |
| 687 |
"expected return value 0xFF"); |
| 688 |
if (overflow != 0) |
| 689 |
return raiseTestError("test_long_and_overflow", |
| 690 |
"overflow was set incorrectly"); |
| 691 |
|
| 692 |
num = PyLong_FromLong(LONG_MAX); |
| 693 |
if (num == NULL) |
| 694 |
return NULL; |
| 695 |
overflow = 1234; |
| 696 |
value = PyLong_AsLongAndOverflow(num, &overflow); |
| 697 |
Py_DECREF(num); |
| 698 |
if (value == -1 && PyErr_Occurred()) |
| 699 |
return NULL; |
| 700 |
if (value != LONG_MAX) |
| 701 |
return raiseTestError("test_long_and_overflow", |
| 702 |
"expected return value LONG_MAX"); |
| 703 |
if (overflow != 0) |
| 704 |
return raiseTestError("test_long_and_overflow", |
| 705 |
"overflow was not cleared"); |
| 706 |
|
| 707 |
num = PyLong_FromLong(LONG_MIN); |
| 708 |
if (num == NULL) |
| 709 |
return NULL; |
| 710 |
overflow = 0; |
| 711 |
value = PyLong_AsLongAndOverflow(num, &overflow); |
| 712 |
Py_DECREF(num); |
| 713 |
if (value == -1 && PyErr_Occurred()) |
| 714 |
return NULL; |
| 715 |
if (value != LONG_MIN) |
| 716 |
return raiseTestError("test_long_and_overflow", |
| 717 |
"expected return value LONG_MIN"); |
| 718 |
if (overflow != 0) |
| 719 |
return raiseTestError("test_long_and_overflow", |
| 720 |
"overflow was not cleared"); |
| 721 |
|
| 722 |
Py_INCREF(Py_None); |
| 723 |
return Py_None; |
| 724 |
} |
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
static PyObject * |
| 732 |
test_long_long_and_overflow(PyObject *self) |
| 733 |
{ |
| 734 |
PyObject *num, *one, *temp; |
| 735 |
PY_LONG_LONG value; |
| 736 |
int overflow; |
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 741 |
if (num == NULL) |
| 742 |
return NULL; |
| 743 |
overflow = 1234; |
| 744 |
value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 745 |
Py_DECREF(num); |
| 746 |
if (value == -1 && PyErr_Occurred()) |
| 747 |
return NULL; |
| 748 |
if (value != -1) |
| 749 |
return raiseTestError("test_long_long_and_overflow", |
| 750 |
"return value was not set to -1"); |
| 751 |
if (overflow != 1) |
| 752 |
return raiseTestError("test_long_long_and_overflow", |
| 753 |
"overflow was not set to 1"); |
| 754 |
|
| 755 |
|
| 756 |
num = PyLong_FromLongLong(PY_LLONG_MAX); |
| 757 |
if (num == NULL) |
| 758 |
return NULL; |
| 759 |
one = PyLong_FromLong(1L); |
| 760 |
if (one == NULL) { |
| 761 |
Py_DECREF(num); |
| 762 |
return NULL; |
| 763 |
} |
| 764 |
temp = PyNumber_Add(num, one); |
| 765 |
Py_DECREF(one); |
| 766 |
Py_DECREF(num); |
| 767 |
num = temp; |
| 768 |
if (num == NULL) |
| 769 |
return NULL; |
| 770 |
overflow = 0; |
| 771 |
value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 772 |
Py_DECREF(num); |
| 773 |
if (value == -1 && PyErr_Occurred()) |
| 774 |
return NULL; |
| 775 |
if (value != -1) |
| 776 |
return raiseTestError("test_long_long_and_overflow", |
| 777 |
"return value was not set to -1"); |
| 778 |
if (overflow != 1) |
| 779 |
return raiseTestError("test_long_long_and_overflow", |
| 780 |
"overflow was not set to 1"); |
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
num = PyLong_FromString("-FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 785 |
if (num == NULL) |
| 786 |
return NULL; |
| 787 |
overflow = 1234; |
| 788 |
value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 789 |
Py_DECREF(num); |
| 790 |
if (value == -1 && PyErr_Occurred()) |
| 791 |
return NULL; |
| 792 |
if (value != -1) |
| 793 |
return raiseTestError("test_long_long_and_overflow", |
| 794 |
"return value was not set to -1"); |
| 795 |
if (overflow != -1) |
| 796 |
return raiseTestError("test_long_long_and_overflow", |
| 797 |
"overflow was not set to -1"); |
| 798 |
|
| 799 |
|
| 800 |
num = PyLong_FromLongLong(PY_LLONG_MIN); |
| 801 |
if (num == NULL) |
| 802 |
return NULL; |
| 803 |
one = PyLong_FromLong(1L); |
| 804 |
if (one == NULL) { |
| 805 |
Py_DECREF(num); |
| 806 |
return NULL; |
| 807 |
} |
| 808 |
temp = PyNumber_Subtract(num, one); |
| 809 |
Py_DECREF(one); |
| 810 |
Py_DECREF(num); |
| 811 |
num = temp; |
| 812 |
if (num == NULL) |
| 813 |
return NULL; |
| 814 |
overflow = 0; |
| 815 |
value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 816 |
Py_DECREF(num); |
| 817 |
if (value == -1 && PyErr_Occurred()) |
| 818 |
return NULL; |
| 819 |
if (value != -1) |
| 820 |
return raiseTestError("test_long_long_and_overflow", |
| 821 |
"return value was not set to -1"); |
| 822 |
if (overflow != -1) |
| 823 |
return raiseTestError("test_long_long_and_overflow", |
| 824 |
"overflow was not set to -1"); |
| 825 |
|
| 826 |
|
| 827 |
num = PyLong_FromString("FF", NULL, 16); |
| 828 |
if (num == NULL) |
| 829 |
return NULL; |
| 830 |
overflow = 1234; |
| 831 |
value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 832 |
Py_DECREF(num); |
| 833 |
if (value == -1 && PyErr_Occurred()) |
| 834 |
return NULL; |
| 835 |
if (value != 0xFF) |
| 836 |
return raiseTestError("test_long_long_and_overflow", |
| 837 |
"expected return value 0xFF"); |
| 838 |
if (overflow != 0) |
| 839 |
return raiseTestError("test_long_long_and_overflow", |
| 840 |
"overflow was not cleared"); |
| 841 |
|
| 842 |
num = PyLong_FromString("-FF", NULL, 16); |
| 843 |
if (num == NULL) |
| 844 |
return NULL; |
| 845 |
overflow = 0; |
| 846 |
value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 847 |
Py_DECREF(num); |
| 848 |
if (value == -1 && PyErr_Occurred()) |
| 849 |
return NULL; |
| 850 |
if (value != -0xFF) |
| 851 |
return raiseTestError("test_long_long_and_overflow", |
| 852 |
"expected return value 0xFF"); |
| 853 |
if (overflow != 0) |
| 854 |
return raiseTestError("test_long_long_and_overflow", |
| 855 |
"overflow was set incorrectly"); |
| 856 |
|
| 857 |
num = PyLong_FromLongLong(PY_LLONG_MAX); |
| 858 |
if (num == NULL) |
| 859 |
return NULL; |
| 860 |
overflow = 1234; |
| 861 |
value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 862 |
Py_DECREF(num); |
| 863 |
if (value == -1 && PyErr_Occurred()) |
| 864 |
return NULL; |
| 865 |
if (value != PY_LLONG_MAX) |
| 866 |
return raiseTestError("test_long_long_and_overflow", |
| 867 |
"expected return value PY_LLONG_MAX"); |
| 868 |
if (overflow != 0) |
| 869 |
return raiseTestError("test_long_long_and_overflow", |
| 870 |
"overflow was not cleared"); |
| 871 |
|
| 872 |
num = PyLong_FromLongLong(PY_LLONG_MIN); |
| 873 |
if (num == NULL) |
| 874 |
return NULL; |
| 875 |
overflow = 0; |
| 876 |
value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 877 |
Py_DECREF(num); |
| 878 |
if (value == -1 && PyErr_Occurred()) |
| 879 |
return NULL; |
| 880 |
if (value != PY_LLONG_MIN) |
| 881 |
return raiseTestError("test_long_long_and_overflow", |
| 882 |
"expected return value PY_LLONG_MIN"); |
| 883 |
if (overflow != 0) |
| 884 |
return raiseTestError("test_long_long_and_overflow", |
| 885 |
"overflow was not cleared"); |
| 886 |
|
| 887 |
Py_INCREF(Py_None); |
| 888 |
return Py_None; |
| 889 |
} |
| 890 |
|
| 891 |
static PyObject * |
| 892 |
test_long_as_unsigned_long_long_mask(PyObject *self) |
| 893 |
{ |
| 894 |
unsigned PY_LONG_LONG res = PyLong_AsUnsignedLongLongMask(NULL); |
| 895 |
|
| 896 |
if (res != (unsigned PY_LONG_LONG)-1 || !PyErr_Occurred()) { |
| 897 |
return raiseTestError("test_long_as_unsigned_long_long_mask", |
| 898 |
"PyLong_AsUnsignedLongLongMask(NULL) didn't " |
| 899 |
"complain"); |
| 900 |
} |
| 901 |
if (!PyErr_ExceptionMatches(PyExc_SystemError)) { |
| 902 |
return raiseTestError("test_long_as_unsigned_long_long_mask", |
| 903 |
"PyLong_AsUnsignedLongLongMask(NULL) raised " |
| 904 |
"something other than SystemError"); |
| 905 |
} |
| 906 |
PyErr_Clear(); |
| 907 |
Py_INCREF(Py_None); |
| 908 |
return Py_None; |
| 909 |
} |
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
static PyObject * |
| 916 |
test_L_code(PyObject *self) |
| 917 |
{ |
| 918 |
PyObject *tuple, *num; |
| 919 |
PY_LONG_LONG value; |
| 920 |
|
| 921 |
tuple = PyTuple_New(1); |
| 922 |
if (tuple == NULL) |
| 923 |
return NULL; |
| 924 |
|
| 925 |
num = PyLong_FromLong(42); |
| 926 |
if (num == NULL) |
| 927 |
return NULL; |
| 928 |
|
| 929 |
PyTuple_SET_ITEM(tuple, 0, num); |
| 930 |
|
| 931 |
value = -1; |
| 932 |
if (!PyArg_ParseTuple(tuple, "L:test_L_code", &value)) { |
| 933 |
return NULL; |
| 934 |
} |
| 935 |
if (value != 42) |
| 936 |
return raiseTestError("test_L_code", |
| 937 |
"L code returned wrong value for long 42"); |
| 938 |
|
| 939 |
Py_DECREF(num); |
| 940 |
num = PyInt_FromLong(42); |
| 941 |
if (num == NULL) |
| 942 |
return NULL; |
| 943 |
|
| 944 |
PyTuple_SET_ITEM(tuple, 0, num); |
| 945 |
|
| 946 |
value = -1; |
| 947 |
if (!PyArg_ParseTuple(tuple, "L:test_L_code", &value)) { |
| 948 |
return NULL; |
| 949 |
} |
| 950 |
if (value != 42) |
| 951 |
return raiseTestError("test_L_code", |
| 952 |
"L code returned wrong value for int 42"); |
| 953 |
|
| 954 |
Py_DECREF(tuple); |
| 955 |
Py_INCREF(Py_None); |
| 956 |
return Py_None; |
| 957 |
} |
| 958 |
|
| 959 |
#endif |
| 960 |
|
| 961 |
static PyObject * |
| 962 |
return_none(void *unused) |
| 963 |
{ |
| 964 |
Py_RETURN_NONE; |
| 965 |
} |
| 966 |
|
| 967 |
static PyObject * |
| 968 |
raise_error(void *unused) |
| 969 |
{ |
| 970 |
PyErr_SetNone(PyExc_ValueError); |
| 971 |
return NULL; |
| 972 |
} |
| 973 |
|
| 974 |
static int |
| 975 |
test_buildvalue_N_error(const char *fmt) |
| 976 |
{ |
| 977 |
PyObject *arg, *res; |
| 978 |
|
| 979 |
arg = PyList_New(0); |
| 980 |
if (arg == NULL) { |
| 981 |
return -1; |
| 982 |
} |
| 983 |
|
| 984 |
Py_INCREF(arg); |
| 985 |
res = Py_BuildValue(fmt, return_none, NULL, arg); |
| 986 |
if (res == NULL) { |
| 987 |
return -1; |
| 988 |
} |
| 989 |
Py_DECREF(res); |
| 990 |
if (Py_REFCNT(arg) != 1) { |
| 991 |
PyErr_Format(TestError, "test_buildvalue_N: " |
| 992 |
"arg was not decrefed in successful " |
| 993 |
"Py_BuildValue(\"%s\")", fmt); |
| 994 |
return -1; |
| 995 |
} |
| 996 |
|
| 997 |
Py_INCREF(arg); |
| 998 |
res = Py_BuildValue(fmt, raise_error, NULL, arg); |
| 999 |
if (res != NULL || !PyErr_Occurred()) { |
| 1000 |
PyErr_Format(TestError, "test_buildvalue_N: " |
| 1001 |
"Py_BuildValue(\"%s\") didn't complain", fmt); |
| 1002 |
return -1; |
| 1003 |
} |
| 1004 |
PyErr_Clear(); |
| 1005 |
if (Py_REFCNT(arg) != 1) { |
| 1006 |
PyErr_Format(TestError, "test_buildvalue_N: " |
| 1007 |
"arg was not decrefed in failed " |
| 1008 |
"Py_BuildValue(\"%s\")", fmt); |
| 1009 |
return -1; |
| 1010 |
} |
| 1011 |
Py_DECREF(arg); |
| 1012 |
return 0; |
| 1013 |
} |
| 1014 |
|
| 1015 |
static PyObject * |
| 1016 |
test_buildvalue_N(PyObject *self, PyObject *noargs) |
| 1017 |
{ |
| 1018 |
PyObject *arg, *res; |
| 1019 |
|
| 1020 |
arg = PyList_New(0); |
| 1021 |
if (arg == NULL) { |
| 1022 |
return NULL; |
| 1023 |
} |
| 1024 |
Py_INCREF(arg); |
| 1025 |
res = Py_BuildValue("N", arg); |
| 1026 |
if (res == NULL) { |
| 1027 |
return NULL; |
| 1028 |
} |
| 1029 |
if (res != arg) { |
| 1030 |
return raiseTestError("test_buildvalue_N", |
| 1031 |
"Py_BuildValue(\"N\") returned wrong result"); |
| 1032 |
} |
| 1033 |
if (Py_REFCNT(arg) != 2) { |
| 1034 |
return raiseTestError("test_buildvalue_N", |
| 1035 |
"arg was not decrefed in Py_BuildValue(\"N\")"); |
| 1036 |
} |
| 1037 |
Py_DECREF(res); |
| 1038 |
Py_DECREF(arg); |
| 1039 |
|
| 1040 |
if (test_buildvalue_N_error("O&N") < 0) |
| 1041 |
return NULL; |
| 1042 |
if (test_buildvalue_N_error("(O&N)") < 0) |
| 1043 |
return NULL; |
| 1044 |
if (test_buildvalue_N_error("[O&N]") < 0) |
| 1045 |
return NULL; |
| 1046 |
if (test_buildvalue_N_error("{O&N}") < 0) |
| 1047 |
return NULL; |
| 1048 |
if (test_buildvalue_N_error("{()O&(())N}") < 0) |
| 1049 |
return NULL; |
| 1050 |
|
| 1051 |
Py_RETURN_NONE; |
| 1052 |
} |
| 1053 |
|
| 1054 |
|
| 1055 |
static PyObject * |
| 1056 |
get_args(PyObject *self, PyObject *args) |
| 1057 |
{ |
| 1058 |
if (args == NULL) { |
| 1059 |
args = Py_None; |
| 1060 |
} |
| 1061 |
Py_INCREF(args); |
| 1062 |
return args; |
| 1063 |
} |
| 1064 |
|
| 1065 |
static PyObject * |
| 1066 |
get_kwargs(PyObject *self, PyObject *args, PyObject *kwargs) |
| 1067 |
{ |
| 1068 |
if (kwargs == NULL) { |
| 1069 |
kwargs = Py_None; |
| 1070 |
} |
| 1071 |
Py_INCREF(kwargs); |
| 1072 |
return kwargs; |
| 1073 |
} |
| 1074 |
|
| 1075 |
|
| 1076 |
static PyObject * |
| 1077 |
getargs_tuple(PyObject *self, PyObject *args) |
| 1078 |
{ |
| 1079 |
int a, b, c; |
| 1080 |
if (!PyArg_ParseTuple(args, "i(ii)", &a, &b, &c)) |
| 1081 |
return NULL; |
| 1082 |
return Py_BuildValue("iii", a, b, c); |
| 1083 |
} |
| 1084 |
|
| 1085 |
|
| 1086 |
static PyObject *getargs_keywords(PyObject *self, PyObject *args, PyObject *kwargs) |
| 1087 |
{ |
| 1088 |
static char *keywords[] = {"arg1","arg2","arg3","arg4","arg5", NULL}; |
| 1089 |
static char *fmt="(ii)i|(i(ii))(iii)i"; |
| 1090 |
int int_args[10]={-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}; |
| 1091 |
|
| 1092 |
if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords, |
| 1093 |
&int_args[0], &int_args[1], &int_args[2], &int_args[3], &int_args[4], |
| 1094 |
&int_args[5], &int_args[6], &int_args[7], &int_args[8], &int_args[9])) |
| 1095 |
return NULL; |
| 1096 |
return Py_BuildValue("iiiiiiiiii", |
| 1097 |
int_args[0], int_args[1], int_args[2], int_args[3], int_args[4], |
| 1098 |
int_args[5], int_args[6], int_args[7], int_args[8], int_args[9]); |
| 1099 |
} |
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
static PyObject * |
| 1105 |
getargs_b(PyObject *self, PyObject *args) |
| 1106 |
{ |
| 1107 |
unsigned char value; |
| 1108 |
if (!PyArg_ParseTuple(args, "b", &value)) |
| 1109 |
return NULL; |
| 1110 |
return PyLong_FromUnsignedLong((unsigned long)value); |
| 1111 |
} |
| 1112 |
|
| 1113 |
static PyObject * |
| 1114 |
getargs_B(PyObject *self, PyObject *args) |
| 1115 |
{ |
| 1116 |
unsigned char value; |
| 1117 |
if (!PyArg_ParseTuple(args, "B", &value)) |
| 1118 |
return NULL; |
| 1119 |
return PyLong_FromUnsignedLong((unsigned long)value); |
| 1120 |
} |
| 1121 |
|
| 1122 |
static PyObject * |
| 1123 |
getargs_h(PyObject *self, PyObject *args) |
| 1124 |
{ |
| 1125 |
short value; |
| 1126 |
if (!PyArg_ParseTuple(args, "h", &value)) |
| 1127 |
return NULL; |
| 1128 |
return PyLong_FromLong((long)value); |
| 1129 |
} |
| 1130 |
|
| 1131 |
static PyObject * |
| 1132 |
getargs_H(PyObject *self, PyObject *args) |
| 1133 |
{ |
| 1134 |
unsigned short value; |
| 1135 |
if (!PyArg_ParseTuple(args, "H", &value)) |
| 1136 |
return NULL; |
| 1137 |
return PyLong_FromUnsignedLong((unsigned long)value); |
| 1138 |
} |
| 1139 |
|
| 1140 |
static PyObject * |
| 1141 |
getargs_I(PyObject *self, PyObject *args) |
| 1142 |
{ |
| 1143 |
unsigned int value; |
| 1144 |
if (!PyArg_ParseTuple(args, "I", &value)) |
| 1145 |
return NULL; |
| 1146 |
return PyLong_FromUnsignedLong((unsigned long)value); |
| 1147 |
} |
| 1148 |
|
| 1149 |
static PyObject * |
| 1150 |
getargs_k(PyObject *self, PyObject *args) |
| 1151 |
{ |
| 1152 |
unsigned long value; |
| 1153 |
if (!PyArg_ParseTuple(args, "k", &value)) |
| 1154 |
return NULL; |
| 1155 |
return PyLong_FromUnsignedLong(value); |
| 1156 |
} |
| 1157 |
|
| 1158 |
static PyObject * |
| 1159 |
getargs_i(PyObject *self, PyObject *args) |
| 1160 |
{ |
| 1161 |
int value; |
| 1162 |
if (!PyArg_ParseTuple(args, "i", &value)) |
| 1163 |
return NULL; |
| 1164 |
return PyLong_FromLong((long)value); |
| 1165 |
} |
| 1166 |
|
| 1167 |
static PyObject * |
| 1168 |
getargs_l(PyObject *self, PyObject *args) |
| 1169 |
{ |
| 1170 |
long value; |
| 1171 |
if (!PyArg_ParseTuple(args, "l", &value)) |
| 1172 |
return NULL; |
| 1173 |
return PyLong_FromLong(value); |
| 1174 |
} |
| 1175 |
|
| 1176 |
static PyObject * |
| 1177 |
getargs_n(PyObject *self, PyObject *args) |
| 1178 |
{ |
| 1179 |
Py_ssize_t value; |
| 1180 |
if (!PyArg_ParseTuple(args, "n", &value)) |
| 1181 |
return NULL; |
| 1182 |
return PyInt_FromSsize_t(value); |
| 1183 |
} |
| 1184 |
|
| 1185 |
#ifdef HAVE_LONG_LONG |
| 1186 |
static PyObject * |
| 1187 |
getargs_L(PyObject *self, PyObject *args) |
| 1188 |
{ |
| 1189 |
PY_LONG_LONG value; |
| 1190 |
if (!PyArg_ParseTuple(args, "L", &value)) |
| 1191 |
return NULL; |
| 1192 |
return PyLong_FromLongLong(value); |
| 1193 |
} |
| 1194 |
|
| 1195 |
static PyObject * |
| 1196 |
getargs_K(PyObject *self, PyObject *args) |
| 1197 |
{ |
| 1198 |
unsigned PY_LONG_LONG value; |
| 1199 |
if (!PyArg_ParseTuple(args, "K", &value)) |
| 1200 |
return NULL; |
| 1201 |
return PyLong_FromUnsignedLongLong(value); |
| 1202 |
} |
| 1203 |
#endif |
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
static PyObject * |
| 1208 |
test_k_code(PyObject *self) |
| 1209 |
{ |
| 1210 |
PyObject *tuple, *num; |
| 1211 |
unsigned long value; |
| 1212 |
|
| 1213 |
tuple = PyTuple_New(1); |
| 1214 |
if (tuple == NULL) |
| 1215 |
return NULL; |
| 1216 |
|
| 1217 |
|
| 1218 |
num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 1219 |
if (num == NULL) |
| 1220 |
return NULL; |
| 1221 |
|
| 1222 |
value = PyInt_AsUnsignedLongMask(num); |
| 1223 |
if (value != ULONG_MAX) |
| 1224 |
return raiseTestError("test_k_code", |
| 1225 |
"PyInt_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF"); |
| 1226 |
|
| 1227 |
PyTuple_SET_ITEM(tuple, 0, num); |
| 1228 |
|
| 1229 |
value = 0; |
| 1230 |
if (!PyArg_ParseTuple(tuple, "k:test_k_code", &value)) { |
| 1231 |
return NULL; |
| 1232 |
} |
| 1233 |
if (value != ULONG_MAX) |
| 1234 |
return raiseTestError("test_k_code", |
| 1235 |
"k code returned wrong value for long 0xFFF...FFF"); |
| 1236 |
|
| 1237 |
Py_DECREF(num); |
| 1238 |
num = PyLong_FromString("-FFFFFFFF000000000000000042", NULL, 16); |
| 1239 |
if (num == NULL) |
| 1240 |
return NULL; |
| 1241 |
|
| 1242 |
value = PyInt_AsUnsignedLongMask(num); |
| 1243 |
if (value != (unsigned long)-0x42) |
| 1244 |
return raiseTestError("test_k_code", |
| 1245 |
"PyInt_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF"); |
| 1246 |
|
| 1247 |
PyTuple_SET_ITEM(tuple, 0, num); |
| 1248 |
|
| 1249 |
value = 0; |
| 1250 |
if (!PyArg_ParseTuple(tuple, "k:test_k_code", &value)) { |
| 1251 |
return NULL; |
| 1252 |
} |
| 1253 |
if (value != (unsigned long)-0x42) |
| 1254 |
return raiseTestError("test_k_code", |
| 1255 |
"k code returned wrong value for long -0xFFF..000042"); |
| 1256 |
|
| 1257 |
Py_DECREF(tuple); |
| 1258 |
Py_INCREF(Py_None); |
| 1259 |
return Py_None; |
| 1260 |
} |
| 1261 |
|
| 1262 |
static PyObject * |
| 1263 |
getargs_f(PyObject *self, PyObject *args) |
| 1264 |
{ |
| 1265 |
float f; |
| 1266 |
if (!PyArg_ParseTuple(args, "f", &f)) |
| 1267 |
return NULL; |
| 1268 |
return PyFloat_FromDouble(f); |
| 1269 |
} |
| 1270 |
|
| 1271 |
static PyObject * |
| 1272 |
getargs_d(PyObject *self, PyObject *args) |
| 1273 |
{ |
| 1274 |
double d; |
| 1275 |
if (!PyArg_ParseTuple(args, "d", &d)) |
| 1276 |
return NULL; |
| 1277 |
return PyFloat_FromDouble(d); |
| 1278 |
} |
| 1279 |
|
| 1280 |
static PyObject * |
| 1281 |
getargs_D(PyObject *self, PyObject *args) |
| 1282 |
{ |
| 1283 |
Py_complex cval; |
| 1284 |
if (!PyArg_ParseTuple(args, "D", &cval)) |
| 1285 |
return NULL; |
| 1286 |
return PyComplex_FromCComplex(cval); |
| 1287 |
} |
| 1288 |
|
| 1289 |
static PyObject * |
| 1290 |
getargs_S(PyObject *self, PyObject *args) |
| 1291 |
{ |
| 1292 |
PyObject *obj; |
| 1293 |
if (!PyArg_ParseTuple(args, "S", &obj)) |
| 1294 |
return NULL; |
| 1295 |
Py_INCREF(obj); |
| 1296 |
return obj; |
| 1297 |
} |
| 1298 |
|
| 1299 |
static PyObject * |
| 1300 |
getargs_Y(PyObject *self, PyObject *args) |
| 1301 |
{ |
| 1302 |
PyObject *obj; |
| 1303 |
if (!PyArg_ParseTuple(args, "Y", &obj)) |
| 1304 |
return NULL; |
| 1305 |
Py_INCREF(obj); |
| 1306 |
return obj; |
| 1307 |
} |
| 1308 |
|
| 1309 |
#ifdef Py_USING_UNICODE |
| 1310 |
static PyObject * |
| 1311 |
getargs_U(PyObject *self, PyObject *args) |
| 1312 |
{ |
| 1313 |
PyObject *obj; |
| 1314 |
if (!PyArg_ParseTuple(args, "U", &obj)) |
| 1315 |
return NULL; |
| 1316 |
Py_INCREF(obj); |
| 1317 |
return obj; |
| 1318 |
} |
| 1319 |
#endif |
| 1320 |
|
| 1321 |
static PyObject * |
| 1322 |
getargs_c(PyObject *self, PyObject *args) |
| 1323 |
{ |
| 1324 |
char c; |
| 1325 |
if (!PyArg_ParseTuple(args, "c", &c)) |
| 1326 |
return NULL; |
| 1327 |
return PyInt_FromLong((unsigned char)c); |
| 1328 |
} |
| 1329 |
|
| 1330 |
static PyObject * |
| 1331 |
getargs_s(PyObject *self, PyObject *args) |
| 1332 |
{ |
| 1333 |
const char *str; |
| 1334 |
if (!PyArg_ParseTuple(args, "s", &str)) |
| 1335 |
return NULL; |
| 1336 |
return PyBytes_FromString(str); |
| 1337 |
} |
| 1338 |
|
| 1339 |
static PyObject * |
| 1340 |
getargs_s_star(PyObject *self, PyObject *args) |
| 1341 |
{ |
| 1342 |
Py_buffer buffer; |
| 1343 |
PyObject *bytes; |
| 1344 |
if (!PyArg_ParseTuple(args, "s*", &buffer)) |
| 1345 |
return NULL; |
| 1346 |
bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len); |
| 1347 |
PyBuffer_Release(&buffer); |
| 1348 |
return bytes; |
| 1349 |
} |
| 1350 |
|
| 1351 |
static PyObject * |
| 1352 |
getargs_s_hash(PyObject *self, PyObject *args) |
| 1353 |
{ |
| 1354 |
const char *str; |
| 1355 |
int size; |
| 1356 |
if (!PyArg_ParseTuple(args, "s#", &str, &size)) |
| 1357 |
return NULL; |
| 1358 |
return PyBytes_FromStringAndSize(str, size); |
| 1359 |
} |
| 1360 |
|
| 1361 |
static PyObject * |
| 1362 |
getargs_t_hash(PyObject *self, PyObject *args) |
| 1363 |
{ |
| 1364 |
const char *str; |
| 1365 |
int size; |
| 1366 |
if (!PyArg_ParseTuple(args, "t#", &str, &size)) |
| 1367 |
return NULL; |
| 1368 |
return PyBytes_FromStringAndSize(str, size); |
| 1369 |
} |
| 1370 |
|
| 1371 |
static PyObject * |
| 1372 |
getargs_z(PyObject *self, PyObject *args) |
| 1373 |
{ |
| 1374 |
const char *str; |
| 1375 |
if (!PyArg_ParseTuple(args, "z", &str)) |
| 1376 |
return NULL; |
| 1377 |
if (str != NULL) |
| 1378 |
return PyBytes_FromString(str); |
| 1379 |
else |
| 1380 |
Py_RETURN_NONE; |
| 1381 |
} |
| 1382 |
|
| 1383 |
static PyObject * |
| 1384 |
getargs_z_star(PyObject *self, PyObject *args) |
| 1385 |
{ |
| 1386 |
Py_buffer buffer; |
| 1387 |
PyObject *bytes; |
| 1388 |
if (!PyArg_ParseTuple(args, "z*", &buffer)) |
| 1389 |
return NULL; |
| 1390 |
if (buffer.buf != NULL) |
| 1391 |
bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len); |
| 1392 |
else { |
| 1393 |
Py_INCREF(Py_None); |
| 1394 |
bytes = Py_None; |
| 1395 |
} |
| 1396 |
PyBuffer_Release(&buffer); |
| 1397 |
return bytes; |
| 1398 |
} |
| 1399 |
|
| 1400 |
static PyObject * |
| 1401 |
getargs_z_hash(PyObject *self, PyObject *args) |
| 1402 |
{ |
| 1403 |
const char *str; |
| 1404 |
int size; |
| 1405 |
if (!PyArg_ParseTuple(args, "z#", &str, &size)) |
| 1406 |
return NULL; |
| 1407 |
if (str != NULL) |
| 1408 |
return PyBytes_FromStringAndSize(str, size); |
| 1409 |
else |
| 1410 |
Py_RETURN_NONE; |
| 1411 |
} |
| 1412 |
|
| 1413 |
static PyObject * |
| 1414 |
getargs_w(PyObject *self, PyObject *args) |
| 1415 |
{ |
| 1416 |
char *str; |
| 1417 |
Py_ssize_t size; |
| 1418 |
|
| 1419 |
if (!PyArg_ParseTuple(args, "wn", &str, &size)) |
| 1420 |
return NULL; |
| 1421 |
|
| 1422 |
if (2 <= size) { |
| 1423 |
str[0] = '['; |
| 1424 |
str[size-1] = ']'; |
| 1425 |
} |
| 1426 |
|
| 1427 |
return PyBytes_FromStringAndSize(str, size); |
| 1428 |
} |
| 1429 |
|
| 1430 |
static PyObject * |
| 1431 |
getargs_w_hash(PyObject *self, PyObject *args) |
| 1432 |
{ |
| 1433 |
char *str; |
| 1434 |
int size; |
| 1435 |
|
| 1436 |
if (!PyArg_ParseTuple(args, "w#", &str, &size)) |
| 1437 |
return NULL; |
| 1438 |
|
| 1439 |
if (2 <= size) { |
| 1440 |
str[0] = '['; |
| 1441 |
str[size-1] = ']'; |
| 1442 |
} |
| 1443 |
|
| 1444 |
return PyBytes_FromStringAndSize(str, size); |
| 1445 |
} |
| 1446 |
|
| 1447 |
static PyObject * |
| 1448 |
getargs_w_star(PyObject *self, PyObject *args) |
| 1449 |
{ |
| 1450 |
Py_buffer buffer; |
| 1451 |
PyObject *result; |
| 1452 |
char *str; |
| 1453 |
|
| 1454 |
if (!PyArg_ParseTuple(args, "w*", &buffer)) |
| 1455 |
return NULL; |
| 1456 |
|
| 1457 |
if (2 <= buffer.len) { |
| 1458 |
str = buffer.buf; |
| 1459 |
str[0] = '['; |
| 1460 |
str[buffer.len-1] = ']'; |
| 1461 |
} |
| 1462 |
|
| 1463 |
result = PyBytes_FromStringAndSize(buffer.buf, buffer.len); |
| 1464 |
PyBuffer_Release(&buffer); |
| 1465 |
return result; |
| 1466 |
} |
| 1467 |
|
| 1468 |
#ifdef Py_USING_UNICODE |
| 1469 |
|
| 1470 |
static int |
| 1471 |
_ustrlen(const Py_UNICODE *u) |
| 1472 |
{ |
| 1473 |
int i = 0; |
| 1474 |
const Py_UNICODE *v = u; |
| 1475 |
while (*v != 0) { i++; v++; } |
| 1476 |
return i; |
| 1477 |
} |
| 1478 |
|
| 1479 |
static PyObject * |
| 1480 |
getargs_u(PyObject *self, PyObject *args) |
| 1481 |
{ |
| 1482 |
const Py_UNICODE *str; |
| 1483 |
int size; |
| 1484 |
if (!PyArg_ParseTuple(args, "u", &str)) |
| 1485 |
return NULL; |
| 1486 |
size = _ustrlen(str); |
| 1487 |
return PyUnicode_FromUnicode(str, size); |
| 1488 |
} |
| 1489 |
|
| 1490 |
static PyObject * |
| 1491 |
getargs_u_hash(PyObject *self, PyObject *args) |
| 1492 |
{ |
| 1493 |
const Py_UNICODE *str; |
| 1494 |
int size; |
| 1495 |
if (!PyArg_ParseTuple(args, "u#", &str, &size)) |
| 1496 |
return NULL; |
| 1497 |
return PyUnicode_FromUnicode(str, size); |
| 1498 |
} |
| 1499 |
|
| 1500 |
#endif |
| 1501 |
|
| 1502 |
static PyObject * |
| 1503 |
getargs_es(PyObject *self, PyObject *args) |
| 1504 |
{ |
| 1505 |
PyObject *arg, *result; |
| 1506 |
const char *encoding = NULL; |
| 1507 |
char *str; |
| 1508 |
|
| 1509 |
if (!PyArg_ParseTuple(args, "O|s", &arg, &encoding)) |
| 1510 |
return NULL; |
| 1511 |
if (!PyArg_Parse(arg, "es", encoding, &str)) |
| 1512 |
return NULL; |
| 1513 |
result = PyBytes_FromString(str); |
| 1514 |
PyMem_Free(str); |
| 1515 |
return result; |
| 1516 |
} |
| 1517 |
|
| 1518 |
static PyObject * |
| 1519 |
getargs_et(PyObject *self, PyObject *args) |
| 1520 |
{ |
| 1521 |
PyObject *arg, *result; |
| 1522 |
const char *encoding = NULL; |
| 1523 |
char *str; |
| 1524 |
|
| 1525 |
if (!PyArg_ParseTuple(args, "O|s", &arg, &encoding)) |
| 1526 |
return NULL; |
| 1527 |
if (!PyArg_Parse(arg, "et", encoding, &str)) |
| 1528 |
return NULL; |
| 1529 |
result = PyBytes_FromString(str); |
| 1530 |
PyMem_Free(str); |
| 1531 |
return result; |
| 1532 |
} |
| 1533 |
|
| 1534 |
static PyObject * |
| 1535 |
getargs_es_hash(PyObject *self, PyObject *args) |
| 1536 |
{ |
| 1537 |
PyObject *arg, *result; |
| 1538 |
const char *encoding = NULL; |
| 1539 |
PyByteArrayObject *buffer = NULL; |
| 1540 |
char *str = NULL; |
| 1541 |
int size; |
| 1542 |
|
| 1543 |
if (!PyArg_ParseTuple(args, "O|sO!", |
| 1544 |
&arg, &encoding, &PyByteArray_Type, &buffer)) |
| 1545 |
return NULL; |
| 1546 |
if (buffer != NULL) { |
| 1547 |
str = PyByteArray_AS_STRING(buffer); |
| 1548 |
size = PyByteArray_GET_SIZE(buffer); |
| 1549 |
} |
| 1550 |
if (!PyArg_Parse(arg, "es#", encoding, &str, &size)) |
| 1551 |
return NULL; |
| 1552 |
result = PyBytes_FromStringAndSize(str, size); |
| 1553 |
if (buffer == NULL) |
| 1554 |
PyMem_Free(str); |
| 1555 |
return result; |
| 1556 |
} |
| 1557 |
|
| 1558 |
static PyObject * |
| 1559 |
getargs_et_hash(PyObject *self, PyObject *args) |
| 1560 |
{ |
| 1561 |
PyObject *arg, *result; |
| 1562 |
const char *encoding = NULL; |
| 1563 |
PyByteArrayObject *buffer = NULL; |
| 1564 |
char *str = NULL; |
| 1565 |
int size; |
| 1566 |
|
| 1567 |
if (!PyArg_ParseTuple(args, "O|sO!", |
| 1568 |
&arg, &encoding, &PyByteArray_Type, &buffer)) |
| 1569 |
return NULL; |
| 1570 |
if (buffer != NULL) { |
| 1571 |
str = PyByteArray_AS_STRING(buffer); |
| 1572 |
size = PyByteArray_GET_SIZE(buffer); |
| 1573 |
} |
| 1574 |
if (!PyArg_Parse(arg, "et#", encoding, &str, &size)) |
| 1575 |
return NULL; |
| 1576 |
result = PyBytes_FromStringAndSize(str, size); |
| 1577 |
if (buffer == NULL) |
| 1578 |
PyMem_Free(str); |
| 1579 |
return result; |
| 1580 |
} |
| 1581 |
|
| 1582 |
static PyObject * |
| 1583 |
get_indices(PyObject *self, PyObject *args) |
| 1584 |
{ |
| 1585 |
int result; |
| 1586 |
PySliceObject *slice; |
| 1587 |
Py_ssize_t length, start, stop, step; |
| 1588 |
|
| 1589 |
if (!PyArg_ParseTuple(args, "On", &slice, &length)) |
| 1590 |
return NULL; |
| 1591 |
|
| 1592 |
result = PySlice_GetIndices(slice, length, &start, &stop, &step); |
| 1593 |
|
| 1594 |
if (PyErr_Occurred()) { |
| 1595 |
assert(result == -1); |
| 1596 |
return NULL; |
| 1597 |
} |
| 1598 |
|
| 1599 |
if (result == -1) { |
| 1600 |
Py_RETURN_NONE; |
| 1601 |
} |
| 1602 |
return Py_BuildValue("innn", result, start, stop, step); |
| 1603 |
} |
| 1604 |
|
| 1605 |
static PyObject * |
| 1606 |
parse_tuple_and_keywords(PyObject *self, PyObject *args) |
| 1607 |
{ |
| 1608 |
PyObject *sub_args; |
| 1609 |
PyObject *sub_kwargs; |
| 1610 |
const char *sub_format; |
| 1611 |
PyObject *sub_keywords; |
| 1612 |
|
| 1613 |
Py_ssize_t i, size; |
| 1614 |
char *keywords[8 + 1]; |
| 1615 |
PyObject *o; |
| 1616 |
|
| 1617 |
int result; |
| 1618 |
PyObject *return_value = NULL; |
| 1619 |
|
| 1620 |
double buffers[8][4]; |
| 1621 |
|
| 1622 |
if (!PyArg_ParseTuple(args, "OOsO:parse_tuple_and_keywords", |
| 1623 |
&sub_args, &sub_kwargs, |
| 1624 |
&sub_format, &sub_keywords)) |
| 1625 |
return NULL; |
| 1626 |
|
| 1627 |
if (!(PyList_CheckExact(sub_keywords) || PyTuple_CheckExact(sub_keywords))) { |
| 1628 |
PyErr_SetString(PyExc_ValueError, |
| 1629 |
"parse_tuple_and_keywords: sub_keywords must be either list or tuple"); |
| 1630 |
return NULL; |
| 1631 |
} |
| 1632 |
|
| 1633 |
memset(buffers, 0, sizeof(buffers)); |
| 1634 |
memset(keywords, 0, sizeof(keywords)); |
| 1635 |
|
| 1636 |
size = PySequence_Fast_GET_SIZE(sub_keywords); |
| 1637 |
if (size > 8) { |
| 1638 |
PyErr_SetString(PyExc_ValueError, |
| 1639 |
"parse_tuple_and_keywords: too many keywords in sub_keywords"); |
| 1640 |
goto exit; |
| 1641 |
} |
| 1642 |
|
| 1643 |
for (i = 0; i < size; i++) { |
| 1644 |
o = PySequence_Fast_GET_ITEM(sub_keywords, i); |
| 1645 |
keywords[i] = PyString_AsString(o); |
| 1646 |
if (keywords[i] == NULL) { |
| 1647 |
goto exit; |
| 1648 |
} |
| 1649 |
} |
| 1650 |
|
| 1651 |
result = PyArg_ParseTupleAndKeywords(sub_args, sub_kwargs, |
| 1652 |
sub_format, keywords, |
| 1653 |
buffers + 0, buffers + 1, buffers + 2, buffers + 3, |
| 1654 |
buffers + 4, buffers + 5, buffers + 6, buffers + 7); |
| 1655 |
|
| 1656 |
if (result) { |
| 1657 |
return_value = Py_None; |
| 1658 |
Py_INCREF(Py_None); |
| 1659 |
} |
| 1660 |
|
| 1661 |
exit: |
| 1662 |
return return_value; |
| 1663 |
} |
| 1664 |
|
| 1665 |
#ifdef Py_USING_UNICODE |
| 1666 |
|
| 1667 |
static volatile int x; |
| 1668 |
|
| 1669 |
|
| 1670 |
|
| 1671 |
|
| 1672 |
static PyObject * |
| 1673 |
test_u_code(PyObject *self) |
| 1674 |
{ |
| 1675 |
PyObject *tuple, *obj; |
| 1676 |
Py_UNICODE *value; |
| 1677 |
int len; |
| 1678 |
|
| 1679 |
|
| 1680 |
|
| 1681 |
x = Py_UNICODE_ISSPACE(25); |
| 1682 |
|
| 1683 |
tuple = PyTuple_New(1); |
| 1684 |
if (tuple == NULL) |
| 1685 |
return NULL; |
| 1686 |
|
| 1687 |
obj = PyUnicode_Decode("test", strlen("test"), |
| 1688 |
"ascii", NULL); |
| 1689 |
if (obj == NULL) |
| 1690 |
return NULL; |
| 1691 |
|
| 1692 |
PyTuple_SET_ITEM(tuple, 0, obj); |
| 1693 |
|
| 1694 |
value = 0; |
| 1695 |
if (!PyArg_ParseTuple(tuple, "u:test_u_code", &value)) { |
| 1696 |
return NULL; |
| 1697 |
} |
| 1698 |
if (value != PyUnicode_AS_UNICODE(obj)) |
| 1699 |
return raiseTestError("test_u_code", |
| 1700 |
"u code returned wrong value for u'test'"); |
| 1701 |
value = 0; |
| 1702 |
if (!PyArg_ParseTuple(tuple, "u#:test_u_code", &value, &len)) { |
| 1703 |
return NULL; |
| 1704 |
} |
| 1705 |
if (value != PyUnicode_AS_UNICODE(obj) || |
| 1706 |
len != PyUnicode_GET_SIZE(obj)) |
| 1707 |
return raiseTestError("test_u_code", |
| 1708 |
"u# code returned wrong values for u'test'"); |
| 1709 |
|
| 1710 |
Py_DECREF(tuple); |
| 1711 |
Py_INCREF(Py_None); |
| 1712 |
return Py_None; |
| 1713 |
} |
| 1714 |
|
| 1715 |
static PyObject * |
| 1716 |
test_widechar(PyObject *self) |
| 1717 |
{ |
| 1718 |
#if defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4) |
| 1719 |
const wchar_t wtext[2] = {(wchar_t)0x10ABCDu}; |
| 1720 |
size_t wtextlen = 1; |
| 1721 |
#else |
| 1722 |
const wchar_t wtext[3] = {(wchar_t)0xDBEAu, (wchar_t)0xDFCDu}; |
| 1723 |
size_t wtextlen = 2; |
| 1724 |
#endif |
| 1725 |
PyObject *wide, *utf8; |
| 1726 |
|
| 1727 |
wide = PyUnicode_FromWideChar(wtext, wtextlen); |
| 1728 |
if (wide == NULL) |
| 1729 |
return NULL; |
| 1730 |
|
| 1731 |
utf8 = PyUnicode_FromString("\xf4\x8a\xaf\x8d"); |
| 1732 |
if (utf8 == NULL) { |
| 1733 |
Py_DECREF(wide); |
| 1734 |
return NULL; |
| 1735 |
} |
| 1736 |
|
| 1737 |
if (PyUnicode_GET_SIZE(wide) != PyUnicode_GET_SIZE(utf8)) { |
| 1738 |
Py_DECREF(wide); |
| 1739 |
Py_DECREF(utf8); |
| 1740 |
return raiseTestError("test_widechar", |
| 1741 |
"wide string and utf8 string have different length"); |
| 1742 |
} |
| 1743 |
if (PyUnicode_Compare(wide, utf8)) { |
| 1744 |
Py_DECREF(wide); |
| 1745 |
Py_DECREF(utf8); |
| 1746 |
if (PyErr_Occurred()) |
| 1747 |
return NULL; |
| 1748 |
return raiseTestError("test_widechar", |
| 1749 |
"wide string and utf8 string are differents"); |
| 1750 |
} |
| 1751 |
|
| 1752 |
Py_DECREF(wide); |
| 1753 |
Py_DECREF(utf8); |
| 1754 |
Py_RETURN_NONE; |
| 1755 |
} |
| 1756 |
|
| 1757 |
static PyObject * |
| 1758 |
unicode_encodedecimal(PyObject *self, PyObject *args) |
| 1759 |
{ |
| 1760 |
Py_UNICODE *unicode; |
| 1761 |
int length; |
| 1762 |
char *errors = NULL; |
| 1763 |
PyObject *decimal; |
| 1764 |
Py_ssize_t decimal_length, new_length; |
| 1765 |
int res; |
| 1766 |
|
| 1767 |
if (!PyArg_ParseTuple(args, "u#|s", &unicode, &length, &errors)) |
| 1768 |
return NULL; |
| 1769 |
|
| 1770 |
decimal_length = length * 10; |
| 1771 |
decimal = PyBytes_FromStringAndSize(NULL, decimal_length); |
| 1772 |
if (decimal == NULL) |
| 1773 |
return NULL; |
| 1774 |
|
| 1775 |
res = PyUnicode_EncodeDecimal(unicode, length, |
| 1776 |
PyBytes_AS_STRING(decimal), |
| 1777 |
errors); |
| 1778 |
if (res < 0) { |
| 1779 |
Py_DECREF(decimal); |
| 1780 |
return NULL; |
| 1781 |
} |
| 1782 |
|
| 1783 |
new_length = strlen(PyBytes_AS_STRING(decimal)); |
| 1784 |
assert(new_length <= decimal_length); |
| 1785 |
res = _PyBytes_Resize(&decimal, new_length); |
| 1786 |
if (res < 0) |
| 1787 |
return NULL; |
| 1788 |
|
| 1789 |
return decimal; |
| 1790 |
} |
| 1791 |
|
| 1792 |
static PyObject * |
| 1793 |
test_empty_argparse(PyObject *self) |
| 1794 |
{ |
| 1795 |
|
| 1796 |
PyObject *tuple, *dict = NULL; |
| 1797 |
static char *kwlist[] = {NULL}; |
| 1798 |
int result; |
| 1799 |
tuple = PyTuple_New(0); |
| 1800 |
if (!tuple) |
| 1801 |
return NULL; |
| 1802 |
if (!(result = PyArg_ParseTuple(tuple, "|:test_empty_argparse"))) { |
| 1803 |
goto done; |
| 1804 |
} |
| 1805 |
dict = PyDict_New(); |
| 1806 |
if (!dict) |
| 1807 |
goto done; |
| 1808 |
result = PyArg_ParseTupleAndKeywords(tuple, dict, "|:test_empty_argparse", kwlist); |
| 1809 |
done: |
| 1810 |
Py_DECREF(tuple); |
| 1811 |
Py_XDECREF(dict); |
| 1812 |
if (!result) { |
| 1813 |
return NULL; |
| 1814 |
} |
| 1815 |
else { |
| 1816 |
Py_RETURN_NONE; |
| 1817 |
} |
| 1818 |
} |
| 1819 |
|
| 1820 |
static PyObject * |
| 1821 |
codec_incrementalencoder(PyObject *self, PyObject *args) |
| 1822 |
{ |
| 1823 |
const char *encoding, *errors = NULL; |
| 1824 |
if (!PyArg_ParseTuple(args, "s|s:test_incrementalencoder", |
| 1825 |
&encoding, &errors)) |
| 1826 |
return NULL; |
| 1827 |
return PyCodec_IncrementalEncoder(encoding, errors); |
| 1828 |
} |
| 1829 |
|
| 1830 |
static PyObject * |
| 1831 |
codec_incrementaldecoder(PyObject *self, PyObject *args) |
| 1832 |
{ |
| 1833 |
const char *encoding, *errors = NULL; |
| 1834 |
if (!PyArg_ParseTuple(args, "s|s:test_incrementaldecoder", |
| 1835 |
&encoding, &errors)) |
| 1836 |
return NULL; |
| 1837 |
return PyCodec_IncrementalDecoder(encoding, errors); |
| 1838 |
} |
| 1839 |
|
| 1840 |
#endif |
| 1841 |
|
| 1842 |
|
| 1843 |
static PyObject * |
| 1844 |
test_long_numbits(PyObject *self) |
| 1845 |
{ |
| 1846 |
struct triple { |
| 1847 |
long input; |
| 1848 |
size_t nbits; |
| 1849 |
int sign; |
| 1850 |
} testcases[] = {{0, 0, 0}, |
| 1851 |
{1L, 1, 1}, |
| 1852 |
{-1L, 1, -1}, |
| 1853 |
{2L, 2, 1}, |
| 1854 |
{-2L, 2, -1}, |
| 1855 |
{3L, 2, 1}, |
| 1856 |
{-3L, 2, -1}, |
| 1857 |
{4L, 3, 1}, |
| 1858 |
{-4L, 3, -1}, |
| 1859 |
{0x7fffL, 15, 1}, |
| 1860 |
{-0x7fffL, 15, -1}, |
| 1861 |
{0xffffL, 16, 1}, |
| 1862 |
{-0xffffL, 16, -1}, |
| 1863 |
{0xfffffffL, 28, 1}, |
| 1864 |
{-0xfffffffL, 28, -1}}; |
| 1865 |
int i; |
| 1866 |
|
| 1867 |
for (i = 0; i < sizeof(testcases) / sizeof(struct triple); ++i) { |
| 1868 |
PyObject *plong = PyLong_FromLong(testcases[i].input); |
| 1869 |
size_t nbits = _PyLong_NumBits(plong); |
| 1870 |
int sign = _PyLong_Sign(plong); |
| 1871 |
|
| 1872 |
Py_DECREF(plong); |
| 1873 |
if (nbits != testcases[i].nbits) |
| 1874 |
return raiseTestError("test_long_numbits", |
| 1875 |
"wrong result for _PyLong_NumBits"); |
| 1876 |
if (sign != testcases[i].sign) |
| 1877 |
return raiseTestError("test_long_numbits", |
| 1878 |
"wrong result for _PyLong_Sign"); |
| 1879 |
} |
| 1880 |
Py_INCREF(Py_None); |
| 1881 |
return Py_None; |
| 1882 |
} |
| 1883 |
|
| 1884 |
|
| 1885 |
|
| 1886 |
static PyObject * |
| 1887 |
test_null_strings(PyObject *self) |
| 1888 |
{ |
| 1889 |
PyObject *o1 = PyObject_Str(NULL); |
| 1890 |
#ifdef Py_USING_UNICODE |
| 1891 |
PyObject *o2 = PyObject_Unicode(NULL); |
| 1892 |
#else |
| 1893 |
PyObject *o2 = PyObject_Str(NULL); |
| 1894 |
#endif |
| 1895 |
PyObject *tuple = PyTuple_Pack(2, o1, o2); |
| 1896 |
Py_XDECREF(o1); |
| 1897 |
Py_XDECREF(o2); |
| 1898 |
return tuple; |
| 1899 |
} |
| 1900 |
|
| 1901 |
static PyObject * |
| 1902 |
raise_exception(PyObject *self, PyObject *args) |
| 1903 |
{ |
| 1904 |
PyObject *exc; |
| 1905 |
PyObject *exc_args, *v; |
| 1906 |
int num_args, i; |
| 1907 |
|
| 1908 |
if (!PyArg_ParseTuple(args, "Oi:raise_exception", |
| 1909 |
&exc, &num_args)) |
| 1910 |
return NULL; |
| 1911 |
if (!PyExceptionClass_Check(exc)) { |
| 1912 |
PyErr_Format(PyExc_TypeError, "an exception class is required"); |
| 1913 |
return NULL; |
| 1914 |
} |
| 1915 |
|
| 1916 |
exc_args = PyTuple_New(num_args); |
| 1917 |
if (exc_args == NULL) |
| 1918 |
return NULL; |
| 1919 |
for (i = 0; i < num_args; ++i) { |
| 1920 |
v = PyInt_FromLong(i); |
| 1921 |
if (v == NULL) { |
| 1922 |
Py_DECREF(exc_args); |
| 1923 |
return NULL; |
| 1924 |
} |
| 1925 |
PyTuple_SET_ITEM(exc_args, i, v); |
| 1926 |
} |
| 1927 |
PyErr_SetObject(exc, exc_args); |
| 1928 |
Py_DECREF(exc_args); |
| 1929 |
return NULL; |
| 1930 |
} |
| 1931 |
|
| 1932 |
static PyObject * |
| 1933 |
set_errno(PyObject *self, PyObject *args) |
| 1934 |
{ |
| 1935 |
int new_errno; |
| 1936 |
|
| 1937 |
if (!PyArg_ParseTuple(args, "i:set_errno", &new_errno)) |
| 1938 |
return NULL; |
| 1939 |
|
| 1940 |
errno = new_errno; |
| 1941 |
Py_RETURN_NONE; |
| 1942 |
} |
| 1943 |
|
| 1944 |
#if defined(Py_USING_UNICODE) && !defined(Py_BUILD_CORE) |
| 1945 |
static int test_run_counter = 0; |
| 1946 |
|
| 1947 |
static PyObject * |
| 1948 |
test_datetime_capi(PyObject *self, PyObject *args) { |
| 1949 |
if (PyDateTimeAPI) { |
| 1950 |
if (test_run_counter) { |
| 1951 |
|
| 1952 |
Py_RETURN_NONE; |
| 1953 |
} |
| 1954 |
else { |
| 1955 |
PyErr_SetString(PyExc_AssertionError, |
| 1956 |
"PyDateTime_CAPI somehow initialized"); |
| 1957 |
return NULL; |
| 1958 |
} |
| 1959 |
} |
| 1960 |
test_run_counter++; |
| 1961 |
PyDateTime_IMPORT; |
| 1962 |
if (PyDateTimeAPI) |
| 1963 |
Py_RETURN_NONE; |
| 1964 |
else |
| 1965 |
return NULL; |
| 1966 |
} |
| 1967 |
#endif |
| 1968 |
|
| 1969 |
|
| 1970 |
#ifdef WITH_THREAD |
| 1971 |
|
| 1972 |
|
| 1973 |
|
| 1974 |
|
| 1975 |
|
| 1976 |
|
| 1977 |
|
| 1978 |
|
| 1979 |
static PyThread_type_lock thread_done = NULL; |
| 1980 |
|
| 1981 |
static int |
| 1982 |
_make_call(void *callable) |
| 1983 |
{ |
| 1984 |
PyObject *rc; |
| 1985 |
int success; |
| 1986 |
PyGILState_STATE s = PyGILState_Ensure(); |
| 1987 |
rc = PyObject_CallFunction((PyObject *)callable, ""); |
| 1988 |
success = (rc != NULL); |
| 1989 |
Py_XDECREF(rc); |
| 1990 |
PyGILState_Release(s); |
| 1991 |
return success; |
| 1992 |
} |
| 1993 |
|
| 1994 |
|
| 1995 |
|
| 1996 |
|
| 1997 |
static void |
| 1998 |
_make_call_from_thread(void *callable) |
| 1999 |
{ |
| 2000 |
_make_call(callable); |
| 2001 |
PyThread_release_lock(thread_done); |
| 2002 |
} |
| 2003 |
|
| 2004 |
static PyObject * |
| 2005 |
test_thread_state(PyObject *self, PyObject *args) |
| 2006 |
{ |
| 2007 |
PyObject *fn; |
| 2008 |
int success = 1; |
| 2009 |
|
| 2010 |
if (!PyArg_ParseTuple(args, "O:test_thread_state", &fn)) |
| 2011 |
return NULL; |
| 2012 |
|
| 2013 |
if (!PyCallable_Check(fn)) { |
| 2014 |
PyErr_Format(PyExc_TypeError, "'%s' object is not callable", |
| 2015 |
fn->ob_type->tp_name); |
| 2016 |
return NULL; |
| 2017 |
} |
| 2018 |
|
| 2019 |
|
| 2020 |
PyEval_InitThreads(); |
| 2021 |
thread_done = PyThread_allocate_lock(); |
| 2022 |
if (thread_done == NULL) |
| 2023 |
return PyErr_NoMemory(); |
| 2024 |
PyThread_acquire_lock(thread_done, 1); |
| 2025 |
|
| 2026 |
|
| 2027 |
PyThread_start_new_thread(_make_call_from_thread, fn); |
| 2028 |
|
| 2029 |
success &= _make_call(fn); |
| 2030 |
|
| 2031 |
Py_BEGIN_ALLOW_THREADS |
| 2032 |
success &= _make_call(fn); |
| 2033 |
PyThread_acquire_lock(thread_done, 1); |
| 2034 |
Py_END_ALLOW_THREADS |
| 2035 |
|
| 2036 |
|
| 2037 |
|
| 2038 |
|
| 2039 |
|
| 2040 |
Py_BEGIN_ALLOW_THREADS |
| 2041 |
PyThread_start_new_thread(_make_call_from_thread, fn); |
| 2042 |
success &= _make_call(fn); |
| 2043 |
PyThread_acquire_lock(thread_done, 1); |
| 2044 |
Py_END_ALLOW_THREADS |
| 2045 |
|
| 2046 |
|
| 2047 |
PyThread_release_lock(thread_done); |
| 2048 |
|
| 2049 |
PyThread_free_lock(thread_done); |
| 2050 |
if (!success) |
| 2051 |
return NULL; |
| 2052 |
Py_RETURN_NONE; |
| 2053 |
} |
| 2054 |
|
| 2055 |
|
| 2056 |
static int _pending_callback(void *arg) |
| 2057 |
{ |
| 2058 |
|
| 2059 |
PyObject *callable = (PyObject *)arg; |
| 2060 |
PyObject *r = PyObject_CallObject(callable, NULL); |
| 2061 |
Py_DECREF(callable); |
| 2062 |
Py_XDECREF(r); |
| 2063 |
return r != NULL ? 0 : -1; |
| 2064 |
} |
| 2065 |
|
| 2066 |
|
| 2067 |
|
| 2068 |
|
| 2069 |
static PyObject * |
| 2070 |
pending_threadfunc(PyObject *self, PyObject *arg) |
| 2071 |
{ |
| 2072 |
PyObject *callable; |
| 2073 |
int r; |
| 2074 |
if (PyArg_ParseTuple(arg, "O", &callable) == 0) |
| 2075 |
return NULL; |
| 2076 |
|
| 2077 |
|
| 2078 |
Py_INCREF(callable); |
| 2079 |
|
| 2080 |
Py_BEGIN_ALLOW_THREADS |
| 2081 |
r = Py_AddPendingCall(&_pending_callback, callable); |
| 2082 |
Py_END_ALLOW_THREADS |
| 2083 |
|
| 2084 |
if (r<0) { |
| 2085 |
Py_DECREF(callable); |
| 2086 |
Py_INCREF(Py_False); |
| 2087 |
return Py_False; |
| 2088 |
} |
| 2089 |
Py_INCREF(Py_True); |
| 2090 |
return Py_True; |
| 2091 |
} |
| 2092 |
#endif |
| 2093 |
|
| 2094 |
|
| 2095 |
static PyObject * |
| 2096 |
test_string_from_format(PyObject *self, PyObject *args) |
| 2097 |
{ |
| 2098 |
PyObject *result; |
| 2099 |
char *msg; |
| 2100 |
|
| 2101 |
#define CHECK_1_FORMAT(FORMAT, TYPE) \ |
| 2102 |
result = PyString_FromFormat(FORMAT, (TYPE)1); \ |
| 2103 |
if (result == NULL) \ |
| 2104 |
return NULL; \ |
| 2105 |
if (strcmp(PyString_AsString(result), "1")) { \ |
| 2106 |
msg = FORMAT " failed at 1"; \ |
| 2107 |
goto Fail; \ |
| 2108 |
} \ |
| 2109 |
Py_DECREF(result) |
| 2110 |
|
| 2111 |
CHECK_1_FORMAT("%d", int); |
| 2112 |
CHECK_1_FORMAT("%ld", long); |
| 2113 |
|
| 2114 |
CHECK_1_FORMAT("%zd", Py_ssize_t); |
| 2115 |
|
| 2116 |
|
| 2117 |
CHECK_1_FORMAT("%u", unsigned int); |
| 2118 |
CHECK_1_FORMAT("%lu", unsigned long); |
| 2119 |
CHECK_1_FORMAT("%zu", size_t); |
| 2120 |
|
| 2121 |
|
| 2122 |
#ifdef HAVE_LONG_LONG |
| 2123 |
CHECK_1_FORMAT("%llu", unsigned PY_LONG_LONG); |
| 2124 |
CHECK_1_FORMAT("%lld", PY_LONG_LONG); |
| 2125 |
#endif |
| 2126 |
|
| 2127 |
Py_RETURN_NONE; |
| 2128 |
|
| 2129 |
Fail: |
| 2130 |
Py_XDECREF(result); |
| 2131 |
return raiseTestError("test_string_from_format", msg); |
| 2132 |
|
| 2133 |
#undef CHECK_1_FORMAT |
| 2134 |
} |
| 2135 |
|
| 2136 |
|
| 2137 |
|
| 2138 |
static const char *capsule_name = "capsule name"; |
| 2139 |
static char *capsule_pointer = "capsule pointer"; |
| 2140 |
static char *capsule_context = "capsule context"; |
| 2141 |
static const char *capsule_error = NULL; |
| 2142 |
static int |
| 2143 |
capsule_destructor_call_count = 0; |
| 2144 |
|
| 2145 |
static void |
| 2146 |
capsule_destructor(PyObject *o) { |
| 2147 |
capsule_destructor_call_count++; |
| 2148 |
if (PyCapsule_GetContext(o) != capsule_context) { |
| 2149 |
capsule_error = "context did not match in destructor!"; |
| 2150 |
} else if (PyCapsule_GetDestructor(o) != capsule_destructor) { |
| 2151 |
capsule_error = "destructor did not match in destructor! (woah!)"; |
| 2152 |
} else if (PyCapsule_GetName(o) != capsule_name) { |
| 2153 |
capsule_error = "name did not match in destructor!"; |
| 2154 |
} else if (PyCapsule_GetPointer(o, capsule_name) != capsule_pointer) { |
| 2155 |
capsule_error = "pointer did not match in destructor!"; |
| 2156 |
} |
| 2157 |
} |
| 2158 |
|
| 2159 |
typedef struct { |
| 2160 |
char *name; |
| 2161 |
char *module; |
| 2162 |
char *attribute; |
| 2163 |
} known_capsule; |
| 2164 |
|
| 2165 |
static PyObject * |
| 2166 |
test_capsule(PyObject *self, PyObject *args) |
| 2167 |
{ |
| 2168 |
PyObject *object; |
| 2169 |
const char *error = NULL; |
| 2170 |
void *pointer; |
| 2171 |
void *pointer2; |
| 2172 |
known_capsule known_capsules[] = { |
| 2173 |
#define KNOWN_CAPSULE(module, name) { module "." name, module, name } |
| 2174 |
KNOWN_CAPSULE("_socket", "CAPI"), |
| 2175 |
KNOWN_CAPSULE("_curses", "_C_API"), |
| 2176 |
KNOWN_CAPSULE("datetime", "datetime_CAPI"), |
| 2177 |
{ NULL, NULL }, |
| 2178 |
}; |
| 2179 |
known_capsule *known = &known_capsules[0]; |
| 2180 |
|
| 2181 |
#define FAIL(x) { error = (x); goto exit; } |
| 2182 |
|
| 2183 |
#define CHECK_DESTRUCTOR \ |
| 2184 |
if (capsule_error) { \ |
| 2185 |
FAIL(capsule_error); \ |
| 2186 |
} \ |
| 2187 |
else if (!capsule_destructor_call_count) { \ |
| 2188 |
FAIL("destructor not called!"); \ |
| 2189 |
} \ |
| 2190 |
capsule_destructor_call_count = 0; \ |
| 2191 |
|
| 2192 |
object = PyCapsule_New(capsule_pointer, capsule_name, capsule_destructor); |
| 2193 |
PyCapsule_SetContext(object, capsule_context); |
| 2194 |
capsule_destructor(object); |
| 2195 |
CHECK_DESTRUCTOR; |
| 2196 |
Py_DECREF(object); |
| 2197 |
CHECK_DESTRUCTOR; |
| 2198 |
|
| 2199 |
object = PyCapsule_New(known, "ignored", NULL); |
| 2200 |
PyCapsule_SetPointer(object, capsule_pointer); |
| 2201 |
PyCapsule_SetName(object, capsule_name); |
| 2202 |
PyCapsule_SetDestructor(object, capsule_destructor); |
| 2203 |
PyCapsule_SetContext(object, capsule_context); |
| 2204 |
capsule_destructor(object); |
| 2205 |
CHECK_DESTRUCTOR; |
| 2206 |
|
| 2207 |
pointer2 = PyCapsule_GetPointer(object, "the wrong name"); |
| 2208 |
if (!PyErr_Occurred()) { |
| 2209 |
FAIL("PyCapsule_GetPointer should have failed but did not!"); |
| 2210 |
} |
| 2211 |
PyErr_Clear(); |
| 2212 |
if (pointer2) { |
| 2213 |
if (pointer2 == capsule_pointer) { |
| 2214 |
FAIL("PyCapsule_GetPointer should not have" |
| 2215 |
" returned the internal pointer!"); |
| 2216 |
} else { |
| 2217 |
FAIL("PyCapsule_GetPointer should have " |
| 2218 |
"returned NULL pointer but did not!"); |
| 2219 |
} |
| 2220 |
} |
| 2221 |
PyCapsule_SetDestructor(object, NULL); |
| 2222 |
Py_DECREF(object); |
| 2223 |
if (capsule_destructor_call_count) { |
| 2224 |
FAIL("destructor called when it should not have been!"); |
| 2225 |
} |
| 2226 |
|
| 2227 |
for (known = &known_capsules[0]; known->module != NULL; known++) { |
| 2228 |
|
| 2229 |
|
| 2230 |
|
| 2231 |
static char buffer[256]; |
| 2232 |
#undef FAIL |
| 2233 |
#define FAIL(x) \ |
| 2234 |
{ \ |
| 2235 |
sprintf(buffer, "%s module: \"%s\" attribute: \"%s\"", \ |
| 2236 |
x, known->module, known->attribute); \ |
| 2237 |
error = buffer; \ |
| 2238 |
goto exit; \ |
| 2239 |
} \ |
| 2240 |
|
| 2241 |
PyObject *module = PyImport_ImportModule(known->module); |
| 2242 |
if (module) { |
| 2243 |
pointer = PyCapsule_Import(known->name, 0); |
| 2244 |
if (!pointer) { |
| 2245 |
Py_DECREF(module); |
| 2246 |
FAIL("PyCapsule_GetPointer returned NULL unexpectedly!"); |
| 2247 |
} |
| 2248 |
object = PyObject_GetAttrString(module, known->attribute); |
| 2249 |
if (!object) { |
| 2250 |
Py_DECREF(module); |
| 2251 |
return NULL; |
| 2252 |
} |
| 2253 |
pointer2 = PyCapsule_GetPointer(object, |
| 2254 |
"weebles wobble but they don't fall down"); |
| 2255 |
if (!PyErr_Occurred()) { |
| 2256 |
Py_DECREF(object); |
| 2257 |
Py_DECREF(module); |
| 2258 |
FAIL("PyCapsule_GetPointer should have failed but did not!"); |
| 2259 |
} |
| 2260 |
PyErr_Clear(); |
| 2261 |
if (pointer2) { |
| 2262 |
Py_DECREF(module); |
| 2263 |
Py_DECREF(object); |
| 2264 |
if (pointer2 == pointer) { |
| 2265 |
FAIL("PyCapsule_GetPointer should not have" |
| 2266 |
" returned its internal pointer!"); |
| 2267 |
} else { |
| 2268 |
FAIL("PyCapsule_GetPointer should have" |
| 2269 |
" returned NULL pointer but did not!"); |
| 2270 |
} |
| 2271 |
} |
| 2272 |
Py_DECREF(object); |
| 2273 |
Py_DECREF(module); |
| 2274 |
} |
| 2275 |
else |
| 2276 |
PyErr_Clear(); |
| 2277 |
} |
| 2278 |
|
| 2279 |
exit: |
| 2280 |
if (error) { |
| 2281 |
return raiseTestError("test_capsule", error); |
| 2282 |
} |
| 2283 |
Py_RETURN_NONE; |
| 2284 |
#undef FAIL |
| 2285 |
} |
| 2286 |
|
| 2287 |
|
| 2288 |
static PyObject * |
| 2289 |
test_with_docstring(PyObject *self) |
| 2290 |
{ |
| 2291 |
Py_RETURN_NONE; |
| 2292 |
} |
| 2293 |
|
| 2294 |
|
| 2295 |
static PyObject * |
| 2296 |
traceback_print(PyObject *self, PyObject *args) |
| 2297 |
{ |
| 2298 |
PyObject *file; |
| 2299 |
PyObject *traceback; |
| 2300 |
int result; |
| 2301 |
|
| 2302 |
if (!PyArg_ParseTuple(args, "OO:traceback_print", |
| 2303 |
&traceback, &file)) |
| 2304 |
return NULL; |
| 2305 |
|
| 2306 |
result = PyTraceBack_Print(traceback, file); |
| 2307 |
if (result < 0) |
| 2308 |
return NULL; |
| 2309 |
Py_RETURN_NONE; |
| 2310 |
} |
| 2311 |
|
| 2312 |
|
| 2313 |
static PyObject * |
| 2314 |
code_newempty(PyObject *self, PyObject *args) |
| 2315 |
{ |
| 2316 |
const char *filename; |
| 2317 |
const char *funcname; |
| 2318 |
int firstlineno; |
| 2319 |
|
| 2320 |
if (!PyArg_ParseTuple(args, "ssi:code_newempty", |
| 2321 |
&filename, &funcname, &firstlineno)) |
| 2322 |
return NULL; |
| 2323 |
|
| 2324 |
return (PyObject *)PyCode_NewEmpty(filename, funcname, firstlineno); |
| 2325 |
} |
| 2326 |
|
| 2327 |
|
| 2328 |
|
| 2329 |
static PyObject * |
| 2330 |
make_exception_with_doc(PyObject *self, PyObject *args, PyObject *kwargs) |
| 2331 |
{ |
| 2332 |
char *name; |
| 2333 |
char *doc = NULL; |
| 2334 |
PyObject *base = NULL; |
| 2335 |
PyObject *dict = NULL; |
| 2336 |
|
| 2337 |
static char *kwlist[] = {"name", "doc", "base", "dict", NULL}; |
| 2338 |
|
| 2339 |
if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 2340 |
"s|sOO:make_exception_with_doc", kwlist, |
| 2341 |
&name, &doc, &base, &dict)) |
| 2342 |
return NULL; |
| 2343 |
|
| 2344 |
return PyErr_NewExceptionWithDoc(name, doc, base, dict); |
| 2345 |
} |
| 2346 |
|
| 2347 |
static PyObject * |
| 2348 |
sequence_delitem(PyObject *self, PyObject *args) |
| 2349 |
{ |
| 2350 |
PyObject *seq; |
| 2351 |
Py_ssize_t i; |
| 2352 |
|
| 2353 |
if (!PyArg_ParseTuple(args, "On", &seq, &i)) |
| 2354 |
return NULL; |
| 2355 |
if (PySequence_DelItem(seq, i) < 0) |
| 2356 |
return NULL; |
| 2357 |
Py_RETURN_NONE; |
| 2358 |
} |
| 2359 |
|
| 2360 |
#ifdef WITH_THREAD |
| 2361 |
typedef struct { |
| 2362 |
PyThread_type_lock start_event; |
| 2363 |
PyThread_type_lock exit_event; |
| 2364 |
PyObject *callback; |
| 2365 |
} test_c_thread_t; |
| 2366 |
|
| 2367 |
static void |
| 2368 |
temporary_c_thread(void *data) |
| 2369 |
{ |
| 2370 |
test_c_thread_t *test_c_thread = data; |
| 2371 |
PyGILState_STATE state; |
| 2372 |
PyObject *res; |
| 2373 |
|
| 2374 |
PyThread_release_lock(test_c_thread->start_event); |
| 2375 |
|
| 2376 |
|
| 2377 |
state = PyGILState_Ensure(); |
| 2378 |
|
| 2379 |
res = PyObject_CallFunction(test_c_thread->callback, "", NULL); |
| 2380 |
Py_CLEAR(test_c_thread->callback); |
| 2381 |
|
| 2382 |
if (res == NULL) { |
| 2383 |
PyErr_Print(); |
| 2384 |
} |
| 2385 |
else { |
| 2386 |
Py_DECREF(res); |
| 2387 |
} |
| 2388 |
|
| 2389 |
|
| 2390 |
PyGILState_Release(state); |
| 2391 |
|
| 2392 |
PyThread_release_lock(test_c_thread->exit_event); |
| 2393 |
|
| 2394 |
PyThread_exit_thread(); |
| 2395 |
} |
| 2396 |
|
| 2397 |
static PyObject * |
| 2398 |
call_in_temporary_c_thread(PyObject *self, PyObject *callback) |
| 2399 |
{ |
| 2400 |
PyObject *res = NULL; |
| 2401 |
test_c_thread_t test_c_thread; |
| 2402 |
long thread; |
| 2403 |
|
| 2404 |
PyEval_InitThreads(); |
| 2405 |
|
| 2406 |
test_c_thread.start_event = PyThread_allocate_lock(); |
| 2407 |
test_c_thread.exit_event = PyThread_allocate_lock(); |
| 2408 |
test_c_thread.callback = NULL; |
| 2409 |
if (!test_c_thread.start_event || !test_c_thread.exit_event) { |
| 2410 |
PyErr_SetString(PyExc_RuntimeError, "could not allocate lock"); |
| 2411 |
goto exit; |
| 2412 |
} |
| 2413 |
|
| 2414 |
Py_INCREF(callback); |
| 2415 |
test_c_thread.callback = callback; |
| 2416 |
|
| 2417 |
PyThread_acquire_lock(test_c_thread.start_event, 1); |
| 2418 |
PyThread_acquire_lock(test_c_thread.exit_event, 1); |
| 2419 |
|
| 2420 |
thread = PyThread_start_new_thread(temporary_c_thread, &test_c_thread); |
| 2421 |
if (thread == -1) { |
| 2422 |
PyErr_SetString(PyExc_RuntimeError, "unable to start the thread"); |
| 2423 |
PyThread_release_lock(test_c_thread.start_event); |
| 2424 |
PyThread_release_lock(test_c_thread.exit_event); |
| 2425 |
goto exit; |
| 2426 |
} |
| 2427 |
|
| 2428 |
PyThread_acquire_lock(test_c_thread.start_event, 1); |
| 2429 |
PyThread_release_lock(test_c_thread.start_event); |
| 2430 |
|
| 2431 |
Py_BEGIN_ALLOW_THREADS |
| 2432 |
PyThread_acquire_lock(test_c_thread.exit_event, 1); |
| 2433 |
PyThread_release_lock(test_c_thread.exit_event); |
| 2434 |
Py_END_ALLOW_THREADS |
| 2435 |
|
| 2436 |
Py_INCREF(Py_None); |
| 2437 |
res = Py_None; |
| 2438 |
|
| 2439 |
exit: |
| 2440 |
Py_CLEAR(test_c_thread.callback); |
| 2441 |
if (test_c_thread.start_event) |
| 2442 |
PyThread_free_lock(test_c_thread.start_event); |
| 2443 |
if (test_c_thread.exit_event) |
| 2444 |
PyThread_free_lock(test_c_thread.exit_event); |
| 2445 |
return res; |
| 2446 |
} |
| 2447 |
#endif |
| 2448 |
|
| 2449 |
|
| 2450 |
|
| 2451 |
static PyObject* |
| 2452 |
pymarshal_write_long_to_file(PyObject* self, PyObject *args) |
| 2453 |
{ |
| 2454 |
long value; |
| 2455 |
char *filename; |
| 2456 |
int version; |
| 2457 |
FILE *fp; |
| 2458 |
|
| 2459 |
if (!PyArg_ParseTuple(args, "lsi:pymarshal_write_long_to_file", |
| 2460 |
&value, &filename, &version)) |
| 2461 |
return NULL; |
| 2462 |
|
| 2463 |
fp = fopen(filename, "wb"); |
| 2464 |
if (fp == NULL) { |
| 2465 |
PyErr_SetFromErrno(PyExc_OSError); |
| 2466 |
return NULL; |
| 2467 |
} |
| 2468 |
|
| 2469 |
PyMarshal_WriteLongToFile(value, fp, version); |
| 2470 |
|
| 2471 |
fclose(fp); |
| 2472 |
if (PyErr_Occurred()) |
| 2473 |
return NULL; |
| 2474 |
Py_RETURN_NONE; |
| 2475 |
} |
| 2476 |
|
| 2477 |
static PyObject* |
| 2478 |
pymarshal_write_object_to_file(PyObject* self, PyObject *args) |
| 2479 |
{ |
| 2480 |
PyObject *obj; |
| 2481 |
char *filename; |
| 2482 |
int version; |
| 2483 |
FILE *fp; |
| 2484 |
|
| 2485 |
if (!PyArg_ParseTuple(args, "Osi:pymarshal_write_object_to_file", |
| 2486 |
&obj, &filename, &version)) |
| 2487 |
return NULL; |
| 2488 |
|
| 2489 |
fp = fopen(filename, "wb"); |
| 2490 |
if (fp == NULL) { |
| 2491 |
PyErr_SetFromErrno(PyExc_OSError); |
| 2492 |
return NULL; |
| 2493 |
} |
| 2494 |
|
| 2495 |
PyMarshal_WriteObjectToFile(obj, fp, version); |
| 2496 |
|
| 2497 |
fclose(fp); |
| 2498 |
if (PyErr_Occurred()) |
| 2499 |
return NULL; |
| 2500 |
Py_RETURN_NONE; |
| 2501 |
} |
| 2502 |
|
| 2503 |
static PyObject* |
| 2504 |
pymarshal_read_short_from_file(PyObject* self, PyObject *args) |
| 2505 |
{ |
| 2506 |
int value; |
| 2507 |
long pos; |
| 2508 |
char *filename; |
| 2509 |
FILE *fp; |
| 2510 |
|
| 2511 |
if (!PyArg_ParseTuple(args, "s:pymarshal_read_short_from_file", &filename)) |
| 2512 |
return NULL; |
| 2513 |
|
| 2514 |
fp = fopen(filename, "rb"); |
| 2515 |
if (fp == NULL) { |
| 2516 |
PyErr_SetFromErrno(PyExc_OSError); |
| 2517 |
return NULL; |
| 2518 |
} |
| 2519 |
|
| 2520 |
value = PyMarshal_ReadShortFromFile(fp); |
| 2521 |
pos = ftell(fp); |
| 2522 |
|
| 2523 |
fclose(fp); |
| 2524 |
if (PyErr_Occurred()) |
| 2525 |
return NULL; |
| 2526 |
return Py_BuildValue("il", value, pos); |
| 2527 |
} |
| 2528 |
|
| 2529 |
static PyObject* |
| 2530 |
pymarshal_read_long_from_file(PyObject* self, PyObject *args) |
| 2531 |
{ |
| 2532 |
long value, pos; |
| 2533 |
char *filename; |
| 2534 |
FILE *fp; |
| 2535 |
|
| 2536 |
if (!PyArg_ParseTuple(args, "s:pymarshal_read_long_from_file", &filename)) |
| 2537 |
return NULL; |
| 2538 |
|
| 2539 |
fp = fopen(filename, "rb"); |
| 2540 |
if (fp == NULL) { |
| 2541 |
PyErr_SetFromErrno(PyExc_OSError); |
| 2542 |
return NULL; |
| 2543 |
} |
| 2544 |
|
| 2545 |
value = PyMarshal_ReadLongFromFile(fp); |
| 2546 |
pos = ftell(fp); |
| 2547 |
|
| 2548 |
fclose(fp); |
| 2549 |
if (PyErr_Occurred()) |
| 2550 |
return NULL; |
| 2551 |
return Py_BuildValue("ll", value, pos); |
| 2552 |
} |
| 2553 |
|
| 2554 |
static PyObject* |
| 2555 |
pymarshal_read_last_object_from_file(PyObject* self, PyObject *args) |
| 2556 |
{ |
| 2557 |
PyObject *obj; |
| 2558 |
long pos; |
| 2559 |
char *filename; |
| 2560 |
FILE *fp; |
| 2561 |
|
| 2562 |
if (!PyArg_ParseTuple(args, "s:pymarshal_read_last_object_from_file", &filename)) |
| 2563 |
return NULL; |
| 2564 |
|
| 2565 |
fp = fopen(filename, "rb"); |
| 2566 |
if (fp == NULL) { |
| 2567 |
PyErr_SetFromErrno(PyExc_OSError); |
| 2568 |
return NULL; |
| 2569 |
} |
| 2570 |
|
| 2571 |
obj = PyMarshal_ReadLastObjectFromFile(fp); |
| 2572 |
pos = ftell(fp); |
| 2573 |
|
| 2574 |
fclose(fp); |
| 2575 |
return Py_BuildValue("Nl", obj, pos); |
| 2576 |
} |
| 2577 |
|
| 2578 |
static PyObject* |
| 2579 |
pymarshal_read_object_from_file(PyObject* self, PyObject *args) |
| 2580 |
{ |
| 2581 |
PyObject *obj; |
| 2582 |
long pos; |
| 2583 |
char *filename; |
| 2584 |
FILE *fp; |
| 2585 |
|
| 2586 |
if (!PyArg_ParseTuple(args, "s:pymarshal_read_object_from_file", &filename)) |
| 2587 |
return NULL; |
| 2588 |
|
| 2589 |
fp = fopen(filename, "rb"); |
| 2590 |
if (fp == NULL) { |
| 2591 |
PyErr_SetFromErrno(PyExc_OSError); |
| 2592 |
return NULL; |
| 2593 |
} |
| 2594 |
|
| 2595 |
obj = PyMarshal_ReadObjectFromFile(fp); |
| 2596 |
pos = ftell(fp); |
| 2597 |
|
| 2598 |
fclose(fp); |
| 2599 |
return Py_BuildValue("Nl", obj, pos); |
| 2600 |
} |
| 2601 |
|
| 2602 |
static PyObject* |
| 2603 |
test_raise_signal(PyObject* self, PyObject *args) |
| 2604 |
{ |
| 2605 |
int signum, err; |
| 2606 |
|
| 2607 |
if (!PyArg_ParseTuple(args, "i:raise_signal", &signum)) { |
| 2608 |
return NULL; |
| 2609 |
} |
| 2610 |
|
| 2611 |
err = raise(signum); |
| 2612 |
if (err) |
| 2613 |
return PyErr_SetFromErrno(PyExc_OSError); |
| 2614 |
|
| 2615 |
if (PyErr_CheckSignals() < 0) |
| 2616 |
return NULL; |
| 2617 |
|
| 2618 |
Py_RETURN_NONE; |
| 2619 |
} |
| 2620 |
|
| 2621 |
|
| 2622 |
#ifdef MS_WINDOWS |
| 2623 |
static PyObject* |
| 2624 |
msvcrt_CrtSetReportMode(PyObject* self, PyObject *args) |
| 2625 |
{ |
| 2626 |
int type, mode; |
| 2627 |
int res; |
| 2628 |
|
| 2629 |
if (!PyArg_ParseTuple(args, "ii:CrtSetReportMode", &type, &mode)) { |
| 2630 |
return NULL; |
| 2631 |
} |
| 2632 |
|
| 2633 |
res = _CrtSetReportMode(type, mode); |
| 2634 |
if (res == -1) { |
| 2635 |
PyErr_SetFromErrno(PyExc_OSError); |
| 2636 |
return NULL; |
| 2637 |
} |
| 2638 |
return PyInt_FromLong(res); |
| 2639 |
} |
| 2640 |
|
| 2641 |
|
| 2642 |
static PyObject* |
| 2643 |
msvcrt_CrtSetReportFile(PyObject* self, PyObject *args) |
| 2644 |
{ |
| 2645 |
int type, file; |
| 2646 |
long res; |
| 2647 |
|
| 2648 |
if (!PyArg_ParseTuple(args, "ii:CrtSetReportFile", &type, &file)) { |
| 2649 |
return NULL; |
| 2650 |
} |
| 2651 |
|
| 2652 |
res = (long)_CrtSetReportFile(type, (_HFILE)file); |
| 2653 |
|
| 2654 |
return PyInt_FromLong(res); |
| 2655 |
} |
| 2656 |
#endif |
| 2657 |
|
| 2658 |
|
| 2659 |
#ifdef W_STOPCODE |
| 2660 |
static PyObject* |
| 2661 |
py_w_stopcode(PyObject *self, PyObject *args) |
| 2662 |
{ |
| 2663 |
int sig, status; |
| 2664 |
if (!PyArg_ParseTuple(args, "i", &sig)) { |
| 2665 |
return NULL; |
| 2666 |
} |
| 2667 |
status = W_STOPCODE(sig); |
| 2668 |
return PyLong_FromLong(status); |
| 2669 |
} |
| 2670 |
#endif |
| 2671 |
|
| 2672 |
|
| 2673 |
|
| 2674 |
|
| 2675 |
|
| 2676 |
static PyObject * |
| 2677 |
read_null(PyObject *self, PyObject *args) |
| 2678 |
{ |
| 2679 |
volatile int *x; |
| 2680 |
volatile int y; |
| 2681 |
|
| 2682 |
x = NULL; |
| 2683 |
y = *x; |
| 2684 |
return PyLong_FromLong(y); |
| 2685 |
|
| 2686 |
} |
| 2687 |
|
| 2688 |
|
| 2689 |
static PyMethodDef TestMethods[] = { |
| 2690 |
{"raise_exception", raise_exception, METH_VARARGS}, |
| 2691 |
{"set_errno", set_errno, METH_VARARGS}, |
| 2692 |
{"test_config", (PyCFunction)test_config, METH_NOARGS}, |
| 2693 |
#if defined(Py_USING_UNICODE) && !defined(Py_BUILD_CORE) |
| 2694 |
{"test_datetime_capi", test_datetime_capi, METH_NOARGS}, |
| 2695 |
#endif |
| 2696 |
{"test_list_api", (PyCFunction)test_list_api, METH_NOARGS}, |
| 2697 |
{"test_dict_iteration", (PyCFunction)test_dict_iteration,METH_NOARGS}, |
| 2698 |
{"test_lazy_hash_inheritance", (PyCFunction)test_lazy_hash_inheritance,METH_NOARGS}, |
| 2699 |
{"test_broken_memoryview", (PyCFunction)test_broken_memoryview,METH_NOARGS}, |
| 2700 |
{"test_to_contiguous", (PyCFunction)test_to_contiguous, METH_NOARGS}, |
| 2701 |
{"test_from_contiguous", (PyCFunction)test_from_contiguous, METH_NOARGS}, |
| 2702 |
{"test_long_api", (PyCFunction)test_long_api, METH_NOARGS}, |
| 2703 |
{"test_long_and_overflow", (PyCFunction)test_long_and_overflow, |
| 2704 |
METH_NOARGS}, |
| 2705 |
{"test_long_numbits", (PyCFunction)test_long_numbits, METH_NOARGS}, |
| 2706 |
{"test_k_code", (PyCFunction)test_k_code, METH_NOARGS}, |
| 2707 |
#ifdef Py_USING_UNICODE |
| 2708 |
{"test_empty_argparse", (PyCFunction)test_empty_argparse,METH_NOARGS}, |
| 2709 |
#endif |
| 2710 |
{"get_indices", get_indices, METH_VARARGS}, |
| 2711 |
{"parse_tuple_and_keywords", parse_tuple_and_keywords, METH_VARARGS}, |
| 2712 |
{"test_null_strings", (PyCFunction)test_null_strings, METH_NOARGS}, |
| 2713 |
{"test_string_from_format", (PyCFunction)test_string_from_format, METH_NOARGS}, |
| 2714 |
{"test_with_docstring", (PyCFunction)test_with_docstring, METH_NOARGS, |
| 2715 |
PyDoc_STR("This is a pretty normal docstring.")}, |
| 2716 |
|
| 2717 |
{"test_buildvalue_N", test_buildvalue_N, METH_NOARGS}, |
| 2718 |
{"get_args", get_args, METH_VARARGS}, |
| 2719 |
{"get_kwargs", (PyCFunction)get_kwargs, METH_VARARGS|METH_KEYWORDS}, |
| 2720 |
{"getargs_tuple", getargs_tuple, METH_VARARGS}, |
| 2721 |
{"getargs_keywords", (PyCFunction)getargs_keywords, |
| 2722 |
METH_VARARGS|METH_KEYWORDS}, |
| 2723 |
{"getargs_b", getargs_b, METH_VARARGS}, |
| 2724 |
{"getargs_B", getargs_B, METH_VARARGS}, |
| 2725 |
{"getargs_h", getargs_h, METH_VARARGS}, |
| 2726 |
{"getargs_H", getargs_H, METH_VARARGS}, |
| 2727 |
{"getargs_I", getargs_I, METH_VARARGS}, |
| 2728 |
{"getargs_k", getargs_k, METH_VARARGS}, |
| 2729 |
{"getargs_i", getargs_i, METH_VARARGS}, |
| 2730 |
{"getargs_l", getargs_l, METH_VARARGS}, |
| 2731 |
{"getargs_n", getargs_n, METH_VARARGS}, |
| 2732 |
#ifdef HAVE_LONG_LONG |
| 2733 |
{"getargs_L", getargs_L, METH_VARARGS}, |
| 2734 |
{"getargs_K", getargs_K, METH_VARARGS}, |
| 2735 |
{"test_longlong_api", test_longlong_api, METH_NOARGS}, |
| 2736 |
{"test_long_long_and_overflow", |
| 2737 |
(PyCFunction)test_long_long_and_overflow, METH_NOARGS}, |
| 2738 |
{"test_long_as_unsigned_long_long_mask", |
| 2739 |
(PyCFunction)test_long_as_unsigned_long_long_mask, METH_NOARGS}, |
| 2740 |
{"test_L_code", (PyCFunction)test_L_code, METH_NOARGS}, |
| 2741 |
#endif |
| 2742 |
{"getargs_f", getargs_f, METH_VARARGS}, |
| 2743 |
{"getargs_d", getargs_d, METH_VARARGS}, |
| 2744 |
{"getargs_D", getargs_D, METH_VARARGS}, |
| 2745 |
{"getargs_S", getargs_S, METH_VARARGS}, |
| 2746 |
{"getargs_Y", getargs_Y, METH_VARARGS}, |
| 2747 |
#ifdef Py_USING_UNICODE |
| 2748 |
{"getargs_U", getargs_U, METH_VARARGS}, |
| 2749 |
#endif |
| 2750 |
{"getargs_c", getargs_c, METH_VARARGS}, |
| 2751 |
{"getargs_s", getargs_s, METH_VARARGS}, |
| 2752 |
{"getargs_s_star", getargs_s_star, METH_VARARGS}, |
| 2753 |
{"getargs_s_hash", getargs_s_hash, METH_VARARGS}, |
| 2754 |
{"getargs_t_hash", getargs_t_hash, METH_VARARGS}, |
| 2755 |
{"getargs_z", getargs_z, METH_VARARGS}, |
| 2756 |
{"getargs_z_star", getargs_z_star, METH_VARARGS}, |
| 2757 |
{"getargs_z_hash", getargs_z_hash, METH_VARARGS}, |
| 2758 |
{"getargs_w", getargs_w, METH_VARARGS}, |
| 2759 |
{"getargs_w_star", getargs_w_star, METH_VARARGS}, |
| 2760 |
{"getargs_w_hash", getargs_w_hash, METH_VARARGS}, |
| 2761 |
#ifdef Py_USING_UNICODE |
| 2762 |
{"getargs_u", getargs_u, METH_VARARGS}, |
| 2763 |
{"getargs_u_hash", getargs_u_hash, METH_VARARGS}, |
| 2764 |
#endif |
| 2765 |
{"getargs_es", getargs_es, METH_VARARGS}, |
| 2766 |
{"getargs_et", getargs_et, METH_VARARGS}, |
| 2767 |
{"getargs_es_hash", getargs_es_hash, METH_VARARGS}, |
| 2768 |
{"getargs_et_hash", getargs_et_hash, METH_VARARGS}, |
| 2769 |
#ifdef Py_USING_UNICODE |
| 2770 |
{"codec_incrementalencoder", |
| 2771 |
(PyCFunction)codec_incrementalencoder, METH_VARARGS}, |
| 2772 |
{"codec_incrementaldecoder", |
| 2773 |
(PyCFunction)codec_incrementaldecoder, METH_VARARGS}, |
| 2774 |
{"test_u_code", (PyCFunction)test_u_code, METH_NOARGS}, |
| 2775 |
{"test_widechar", (PyCFunction)test_widechar, METH_NOARGS}, |
| 2776 |
{"unicode_encodedecimal", unicode_encodedecimal, METH_VARARGS}, |
| 2777 |
#endif |
| 2778 |
#ifdef WITH_THREAD |
| 2779 |
{"_test_thread_state", test_thread_state, METH_VARARGS}, |
| 2780 |
{"_pending_threadfunc", pending_threadfunc, METH_VARARGS}, |
| 2781 |
#endif |
| 2782 |
{"test_capsule", (PyCFunction)test_capsule, METH_NOARGS}, |
| 2783 |
{"traceback_print", traceback_print, METH_VARARGS}, |
| 2784 |
{"code_newempty", code_newempty, METH_VARARGS}, |
| 2785 |
{"make_exception_with_doc", (PyCFunction)make_exception_with_doc, |
| 2786 |
METH_VARARGS | METH_KEYWORDS}, |
| 2787 |
{"sequence_delitem", (PyCFunction)sequence_delitem, METH_VARARGS}, |
| 2788 |
#ifdef WITH_THREAD |
| 2789 |
{"call_in_temporary_c_thread", call_in_temporary_c_thread, METH_O, |
| 2790 |
PyDoc_STR("set_error_class(error_class) -> None")}, |
| 2791 |
#endif |
| 2792 |
{"pymarshal_write_long_to_file", |
| 2793 |
pymarshal_write_long_to_file, METH_VARARGS}, |
| 2794 |
{"pymarshal_write_object_to_file", |
| 2795 |
pymarshal_write_object_to_file, METH_VARARGS}, |
| 2796 |
{"pymarshal_read_short_from_file", |
| 2797 |
pymarshal_read_short_from_file, METH_VARARGS}, |
| 2798 |
{"pymarshal_read_long_from_file", |
| 2799 |
pymarshal_read_long_from_file, METH_VARARGS}, |
| 2800 |
{"pymarshal_read_last_object_from_file", |
| 2801 |
pymarshal_read_last_object_from_file, METH_VARARGS}, |
| 2802 |
{"pymarshal_read_object_from_file", |
| 2803 |
pymarshal_read_object_from_file, METH_VARARGS}, |
| 2804 |
{"raise_signal", (PyCFunction)test_raise_signal, METH_VARARGS}, |
| 2805 |
#ifdef MS_WINDOWS |
| 2806 |
{"CrtSetReportMode", (PyCFunction)msvcrt_CrtSetReportMode, METH_VARARGS}, |
| 2807 |
{"CrtSetReportFile", (PyCFunction)msvcrt_CrtSetReportFile, METH_VARARGS}, |
| 2808 |
#endif |
| 2809 |
#ifdef W_STOPCODE |
| 2810 |
{"W_STOPCODE", py_w_stopcode, METH_VARARGS}, |
| 2811 |
#endif |
| 2812 |
{"_read_null", (PyCFunction)read_null, METH_NOARGS}, |
| 2813 |
{NULL, NULL} |
| 2814 |
}; |
| 2815 |
|
| 2816 |
#define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); Py_DECREF(o);} |
| 2817 |
|
| 2818 |
typedef struct { |
| 2819 |
char bool_member; |
| 2820 |
char byte_member; |
| 2821 |
unsigned char ubyte_member; |
| 2822 |
short short_member; |
| 2823 |
unsigned short ushort_member; |
| 2824 |
int int_member; |
| 2825 |
unsigned int uint_member; |
| 2826 |
long long_member; |
| 2827 |
unsigned long ulong_member; |
| 2828 |
float float_member; |
| 2829 |
double double_member; |
| 2830 |
char inplace_member[6]; |
| 2831 |
#ifdef HAVE_LONG_LONG |
| 2832 |
PY_LONG_LONG longlong_member; |
| 2833 |
unsigned PY_LONG_LONG ulonglong_member; |
| 2834 |
#endif |
| 2835 |
} all_structmembers; |
| 2836 |
|
| 2837 |
typedef struct { |
| 2838 |
PyObject_HEAD |
| 2839 |
all_structmembers structmembers; |
| 2840 |
} test_structmembers; |
| 2841 |
|
| 2842 |
static struct PyMemberDef test_members[] = { |
| 2843 |
{"T_BOOL", T_BOOL, offsetof(test_structmembers, structmembers.bool_member), 0, NULL}, |
| 2844 |
{"T_BYTE", T_BYTE, offsetof(test_structmembers, structmembers.byte_member), 0, NULL}, |
| 2845 |
{"T_UBYTE", T_UBYTE, offsetof(test_structmembers, structmembers.ubyte_member), 0, NULL}, |
| 2846 |
{"T_SHORT", T_SHORT, offsetof(test_structmembers, structmembers.short_member), 0, NULL}, |
| 2847 |
{"T_USHORT", T_USHORT, offsetof(test_structmembers, structmembers.ushort_member), 0, NULL}, |
| 2848 |
{"T_INT", T_INT, offsetof(test_structmembers, structmembers.int_member), 0, NULL}, |
| 2849 |
{"T_UINT", T_UINT, offsetof(test_structmembers, structmembers.uint_member), 0, NULL}, |
| 2850 |
{"T_LONG", T_LONG, offsetof(test_structmembers, structmembers.long_member), 0, NULL}, |
| 2851 |
{"T_ULONG", T_ULONG, offsetof(test_structmembers, structmembers.ulong_member), 0, NULL}, |
| 2852 |
{"T_FLOAT", T_FLOAT, offsetof(test_structmembers, structmembers.float_member), 0, NULL}, |
| 2853 |
{"T_DOUBLE", T_DOUBLE, offsetof(test_structmembers, structmembers.double_member), 0, NULL}, |
| 2854 |
{"T_STRING_INPLACE", T_STRING_INPLACE, offsetof(test_structmembers, structmembers.inplace_member), 0, NULL}, |
| 2855 |
#ifdef HAVE_LONG_LONG |
| 2856 |
{"T_LONGLONG", T_LONGLONG, offsetof(test_structmembers, structmembers.longlong_member), 0, NULL}, |
| 2857 |
{"T_ULONGLONG", T_ULONGLONG, offsetof(test_structmembers, structmembers.ulonglong_member), 0, NULL}, |
| 2858 |
#endif |
| 2859 |
{NULL} |
| 2860 |
}; |
| 2861 |
|
| 2862 |
|
| 2863 |
static PyObject * |
| 2864 |
test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 2865 |
{ |
| 2866 |
static char *keywords[] = { |
| 2867 |
"T_BOOL", "T_BYTE", "T_UBYTE", "T_SHORT", "T_USHORT", |
| 2868 |
"T_INT", "T_UINT", "T_LONG", "T_ULONG", |
| 2869 |
"T_FLOAT", "T_DOUBLE", "T_STRING_INPLACE", |
| 2870 |
#ifdef HAVE_LONG_LONG |
| 2871 |
"T_LONGLONG", "T_ULONGLONG", |
| 2872 |
#endif |
| 2873 |
NULL}; |
| 2874 |
static char *fmt = "|bbBhHiIlkfds#" |
| 2875 |
#ifdef HAVE_LONG_LONG |
| 2876 |
"LK" |
| 2877 |
#endif |
| 2878 |
; |
| 2879 |
test_structmembers *ob; |
| 2880 |
const char *s = NULL; |
| 2881 |
int string_len = 0; |
| 2882 |
ob = PyObject_New(test_structmembers, type); |
| 2883 |
if (ob == NULL) |
| 2884 |
return NULL; |
| 2885 |
memset(&ob->structmembers, 0, sizeof(all_structmembers)); |
| 2886 |
if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords, |
| 2887 |
&ob->structmembers.bool_member, |
| 2888 |
&ob->structmembers.byte_member, |
| 2889 |
&ob->structmembers.ubyte_member, |
| 2890 |
&ob->structmembers.short_member, |
| 2891 |
&ob->structmembers.ushort_member, |
| 2892 |
&ob->structmembers.int_member, |
| 2893 |
&ob->structmembers.uint_member, |
| 2894 |
&ob->structmembers.long_member, |
| 2895 |
&ob->structmembers.ulong_member, |
| 2896 |
&ob->structmembers.float_member, |
| 2897 |
&ob->structmembers.double_member, |
| 2898 |
&s, &string_len |
| 2899 |
#ifdef HAVE_LONG_LONG |
| 2900 |
, &ob->structmembers.longlong_member, |
| 2901 |
&ob->structmembers.ulonglong_member |
| 2902 |
#endif |
| 2903 |
)) { |
| 2904 |
Py_DECREF(ob); |
| 2905 |
return NULL; |
| 2906 |
} |
| 2907 |
if (s != NULL) { |
| 2908 |
if (string_len > 5) { |
| 2909 |
Py_DECREF(ob); |
| 2910 |
PyErr_SetString(PyExc_ValueError, "string too long"); |
| 2911 |
return NULL; |
| 2912 |
} |
| 2913 |
strcpy(ob->structmembers.inplace_member, s); |
| 2914 |
} |
| 2915 |
else { |
| 2916 |
strcpy(ob->structmembers.inplace_member, ""); |
| 2917 |
} |
| 2918 |
return (PyObject *)ob; |
| 2919 |
} |
| 2920 |
|
| 2921 |
static void |
| 2922 |
test_structmembers_free(PyObject *ob) |
| 2923 |
{ |
| 2924 |
PyObject_FREE(ob); |
| 2925 |
} |
| 2926 |
|
| 2927 |
static PyTypeObject test_structmembersType = { |
| 2928 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 2929 |
"test_structmembersType", |
| 2930 |
sizeof(test_structmembers), |
| 2931 |
0, |
| 2932 |
test_structmembers_free, |
| 2933 |
0, |
| 2934 |
0, |
| 2935 |
0, |
| 2936 |
0, |
| 2937 |
0, |
| 2938 |
0, |
| 2939 |
0, |
| 2940 |
0, |
| 2941 |
0, |
| 2942 |
0, |
| 2943 |
0, |
| 2944 |
PyObject_GenericGetAttr, |
| 2945 |
PyObject_GenericSetAttr, |
| 2946 |
0, |
| 2947 |
0, |
| 2948 |
"Type containing all structmember types", |
| 2949 |
0, |
| 2950 |
0, |
| 2951 |
0, |
| 2952 |
0, |
| 2953 |
0, |
| 2954 |
0, |
| 2955 |
0, |
| 2956 |
test_members, |
| 2957 |
0, |
| 2958 |
0, |
| 2959 |
0, |
| 2960 |
0, |
| 2961 |
0, |
| 2962 |
0, |
| 2963 |
0, |
| 2964 |
0, |
| 2965 |
test_structmembers_new, |
| 2966 |
}; |
| 2967 |
|
| 2968 |
|
| 2969 |
PyMODINIT_FUNC |
| 2970 |
init_testcapi(void) |
| 2971 |
{ |
| 2972 |
PyObject *m; |
| 2973 |
|
| 2974 |
m = Py_InitModule("_testcapi", TestMethods); |
| 2975 |
if (m == NULL) |
| 2976 |
return; |
| 2977 |
|
| 2978 |
Py_TYPE(&_HashInheritanceTester_Type)=&PyType_Type; |
| 2979 |
|
| 2980 |
Py_TYPE(&test_structmembersType)=&PyType_Type; |
| 2981 |
Py_INCREF(&test_structmembersType); |
| 2982 |
|
| 2983 |
|
| 2984 |
PyModule_AddObject(m, "_test_structmembersType", (PyObject *)&test_structmembersType); |
| 2985 |
|
| 2986 |
PyModule_AddObject(m, "CHAR_MAX", PyInt_FromLong(CHAR_MAX)); |
| 2987 |
PyModule_AddObject(m, "CHAR_MIN", PyInt_FromLong(CHAR_MIN)); |
| 2988 |
PyModule_AddObject(m, "UCHAR_MAX", PyInt_FromLong(UCHAR_MAX)); |
| 2989 |
PyModule_AddObject(m, "SHRT_MAX", PyInt_FromLong(SHRT_MAX)); |
| 2990 |
PyModule_AddObject(m, "SHRT_MIN", PyInt_FromLong(SHRT_MIN)); |
| 2991 |
PyModule_AddObject(m, "USHRT_MAX", PyInt_FromLong(USHRT_MAX)); |
| 2992 |
PyModule_AddObject(m, "INT_MAX", PyLong_FromLong(INT_MAX)); |
| 2993 |
PyModule_AddObject(m, "INT_MIN", PyLong_FromLong(INT_MIN)); |
| 2994 |
PyModule_AddObject(m, "UINT_MAX", PyLong_FromUnsignedLong(UINT_MAX)); |
| 2995 |
PyModule_AddObject(m, "LONG_MAX", PyInt_FromLong(LONG_MAX)); |
| 2996 |
PyModule_AddObject(m, "LONG_MIN", PyInt_FromLong(LONG_MIN)); |
| 2997 |
PyModule_AddObject(m, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX)); |
| 2998 |
PyModule_AddObject(m, "FLT_MAX", PyFloat_FromDouble(FLT_MAX)); |
| 2999 |
PyModule_AddObject(m, "FLT_MIN", PyFloat_FromDouble(FLT_MIN)); |
| 3000 |
PyModule_AddObject(m, "DBL_MAX", PyFloat_FromDouble(DBL_MAX)); |
| 3001 |
PyModule_AddObject(m, "DBL_MIN", PyFloat_FromDouble(DBL_MIN)); |
| 3002 |
PyModule_AddObject(m, "LLONG_MAX", PyLong_FromLongLong(PY_LLONG_MAX)); |
| 3003 |
PyModule_AddObject(m, "LLONG_MIN", PyLong_FromLongLong(PY_LLONG_MIN)); |
| 3004 |
PyModule_AddObject(m, "ULLONG_MAX", PyLong_FromUnsignedLongLong(PY_ULLONG_MAX)); |
| 3005 |
PyModule_AddObject(m, "PY_SSIZE_T_MAX", PyInt_FromSsize_t(PY_SSIZE_T_MAX)); |
| 3006 |
PyModule_AddObject(m, "PY_SSIZE_T_MIN", PyInt_FromSsize_t(PY_SSIZE_T_MIN)); |
| 3007 |
PyModule_AddObject(m, "SIZEOF_PYGC_HEAD", PyInt_FromSsize_t(sizeof(PyGC_Head))); |
| 3008 |
|
| 3009 |
#ifdef MS_WINDOWS |
| 3010 |
PyModule_AddIntConstant(m, "CRT_WARN", _CRT_WARN); |
| 3011 |
PyModule_AddIntConstant(m, "CRT_ERROR", _CRT_ERROR); |
| 3012 |
PyModule_AddIntConstant(m, "CRT_ASSERT", _CRT_ASSERT); |
| 3013 |
PyModule_AddIntConstant(m, "CRTDBG_MODE_FILE", _CRTDBG_MODE_FILE); |
| 3014 |
PyModule_AddIntConstant(m, "CRTDBG_FILE_STDERR", (int)_CRTDBG_FILE_STDERR); |
| 3015 |
#endif |
| 3016 |
|
| 3017 |
TestError = PyErr_NewException("_testcapi.error", NULL, NULL); |
| 3018 |
Py_INCREF(TestError); |
| 3019 |
PyModule_AddObject(m, "error", TestError); |
| 3020 |
} |