| 1 |
#include "pyconfig.h" // Py_GIL_DISABLED |
| 2 |
#ifndef Py_GIL_DISABLED |
| 3 |
// Need limited C API 3.14 to test PyLong_AsInt64() |
| 4 |
# define Py_LIMITED_API 0x030e0000 |
| 5 |
#endif |
| 6 |
|
| 7 |
#include "parts.h" |
| 8 |
#include "util.h" |
| 9 |
#include "clinic/long.c.h" |
| 10 |
|
| 11 |
/*[clinic input] |
| 12 |
module _testlimitedcapi |
| 13 |
[clinic start generated code]*/ |
| 14 |
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=2700057f9c1135ba]*/ |
| 15 |
|
| 16 |
|
| 17 |
static PyObject * |
| 18 |
raiseTestError(const char* test_name, const char* msg) |
| 19 |
{ |
| 20 |
PyErr_Format(PyExc_AssertionError, "%s: %s", test_name, msg); |
| 21 |
return NULL; |
| 22 |
} |
| 23 |
|
| 24 |
/* Tests of PyLong_{As, From}{Unsigned,}Long(), and |
| 25 |
PyLong_{As, From}{Unsigned,}LongLong(). |
| 26 |
|
| 27 |
Note that the meat of the test is contained in testcapi_long.h. |
| 28 |
This is revolting, but delicate code duplication is worse: "almost |
| 29 |
exactly the same" code is needed to test long long, but the ubiquitous |
| 30 |
dependence on type names makes it impossible to use a parameterized |
| 31 |
function. A giant macro would be even worse than this. A C++ template |
| 32 |
would be perfect. |
| 33 |
|
| 34 |
The "report an error" functions are deliberately not part of the #include |
| 35 |
file: if the test fails, you can set a breakpoint in the appropriate |
| 36 |
error function directly, and crawl back from there in the debugger. |
| 37 |
*/ |
| 38 |
|
| 39 |
#define UNBIND(X) Py_DECREF(X); (X) = NULL |
| 40 |
|
| 41 |
static PyObject * |
| 42 |
raise_test_long_error(const char* msg) |
| 43 |
{ |
| 44 |
return raiseTestError("test_long_api", msg); |
| 45 |
} |
| 46 |
|
| 47 |
// Test PyLong_FromLong()/PyLong_AsLong() |
| 48 |
// and PyLong_FromUnsignedLong()/PyLong_AsUnsignedLong(). |
| 49 |
|
| 50 |
#define TESTNAME test_long_api_inner |
| 51 |
#define TYPENAME long |
| 52 |
#define F_S_TO_PY PyLong_FromLong |
| 53 |
#define F_PY_TO_S PyLong_AsLong |
| 54 |
#define F_U_TO_PY PyLong_FromUnsignedLong |
| 55 |
#define F_PY_TO_U PyLong_AsUnsignedLong |
| 56 |
|
| 57 |
#include "testcapi_long.h" |
| 58 |
|
| 59 |
/*[clinic input] |
| 60 |
_testlimitedcapi.test_long_api |
| 61 |
[clinic start generated code]*/ |
| 62 |
|
| 63 |
static PyObject * |
| 64 |
_testlimitedcapi_test_long_api_impl(PyObject *module) |
| 65 |
/*[clinic end generated code: output=06a2c02366d1853a input=9012b3d6a483df63]*/ |
| 66 |
{ |
| 67 |
return TESTNAME(raise_test_long_error); |
| 68 |
} |
| 69 |
|
| 70 |
#undef TESTNAME |
| 71 |
#undef TYPENAME |
| 72 |
#undef F_S_TO_PY |
| 73 |
#undef F_PY_TO_S |
| 74 |
#undef F_U_TO_PY |
| 75 |
#undef F_PY_TO_U |
| 76 |
|
| 77 |
// Test PyLong_FromLongLong()/PyLong_AsLongLong() |
| 78 |
// and PyLong_FromUnsignedLongLong()/PyLong_AsUnsignedLongLong(). |
| 79 |
|
| 80 |
static PyObject * |
| 81 |
raise_test_longlong_error(const char* msg) |
| 82 |
{ |
| 83 |
return raiseTestError("test_longlong_api", msg); |
| 84 |
} |
| 85 |
|
| 86 |
#define TESTNAME test_longlong_api_inner |
| 87 |
#define TYPENAME long long |
| 88 |
#define F_S_TO_PY PyLong_FromLongLong |
| 89 |
#define F_PY_TO_S PyLong_AsLongLong |
| 90 |
#define F_U_TO_PY PyLong_FromUnsignedLongLong |
| 91 |
#define F_PY_TO_U PyLong_AsUnsignedLongLong |
| 92 |
|
| 93 |
#include "testcapi_long.h" |
| 94 |
|
| 95 |
/*[clinic input] |
| 96 |
_testlimitedcapi.test_longlong_api |
| 97 |
[clinic start generated code]*/ |
| 98 |
|
| 99 |
static PyObject * |
| 100 |
_testlimitedcapi_test_longlong_api_impl(PyObject *module) |
| 101 |
/*[clinic end generated code: output=8faa10e1c35214bf input=2b582a9d25bd68e7]*/ |
| 102 |
{ |
| 103 |
return TESTNAME(raise_test_longlong_error); |
| 104 |
} |
| 105 |
|
| 106 |
#undef TESTNAME |
| 107 |
#undef TYPENAME |
| 108 |
#undef F_S_TO_PY |
| 109 |
#undef F_PY_TO_S |
| 110 |
#undef F_U_TO_PY |
| 111 |
#undef F_PY_TO_U |
| 112 |
|
| 113 |
|
| 114 |
/*[clinic input] |
| 115 |
_testlimitedcapi.test_long_and_overflow |
| 116 |
|
| 117 |
Test the PyLong_AsLongAndOverflow API. |
| 118 |
|
| 119 |
General conversion to PY_LONG is tested by test_long_api_inner. |
| 120 |
This test will concentrate on proper handling of overflow. |
| 121 |
[clinic start generated code]*/ |
| 122 |
|
| 123 |
static PyObject * |
| 124 |
_testlimitedcapi_test_long_and_overflow_impl(PyObject *module) |
| 125 |
/*[clinic end generated code: output=fdfd3c1eeabb6d14 input=e3a18791de6519fe]*/ |
| 126 |
{ |
| 127 |
PyObject *num, *one, *temp; |
| 128 |
long value; |
| 129 |
int overflow; |
| 130 |
|
| 131 |
/* Test that overflow is set properly for a large value. */ |
| 132 |
/* num is a number larger than LONG_MAX even on 64-bit platforms */ |
| 133 |
num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 134 |
if (num == NULL) |
| 135 |
return NULL; |
| 136 |
overflow = 1234; |
| 137 |
value = PyLong_AsLongAndOverflow(num, &overflow); |
| 138 |
Py_DECREF(num); |
| 139 |
if (value == -1 && PyErr_Occurred()) |
| 140 |
return NULL; |
| 141 |
if (value != -1) |
| 142 |
return raiseTestError("test_long_and_overflow", |
| 143 |
"return value was not set to -1"); |
| 144 |
if (overflow != 1) |
| 145 |
return raiseTestError("test_long_and_overflow", |
| 146 |
"overflow was not set to 1"); |
| 147 |
|
| 148 |
/* Same again, with num = LONG_MAX + 1 */ |
| 149 |
num = PyLong_FromLong(LONG_MAX); |
| 150 |
if (num == NULL) |
| 151 |
return NULL; |
| 152 |
one = PyLong_FromLong(1L); |
| 153 |
if (one == NULL) { |
| 154 |
Py_DECREF(num); |
| 155 |
return NULL; |
| 156 |
} |
| 157 |
temp = PyNumber_Add(num, one); |
| 158 |
Py_DECREF(one); |
| 159 |
Py_DECREF(num); |
| 160 |
num = temp; |
| 161 |
if (num == NULL) |
| 162 |
return NULL; |
| 163 |
overflow = 0; |
| 164 |
value = PyLong_AsLongAndOverflow(num, &overflow); |
| 165 |
Py_DECREF(num); |
| 166 |
if (value == -1 && PyErr_Occurred()) |
| 167 |
return NULL; |
| 168 |
if (value != -1) |
| 169 |
return raiseTestError("test_long_and_overflow", |
| 170 |
"return value was not set to -1"); |
| 171 |
if (overflow != 1) |
| 172 |
return raiseTestError("test_long_and_overflow", |
| 173 |
"overflow was not set to 1"); |
| 174 |
|
| 175 |
/* Test that overflow is set properly for a large negative value. */ |
| 176 |
/* num is a number smaller than LONG_MIN even on 64-bit platforms */ |
| 177 |
num = PyLong_FromString("-FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 178 |
if (num == NULL) |
| 179 |
return NULL; |
| 180 |
overflow = 1234; |
| 181 |
value = PyLong_AsLongAndOverflow(num, &overflow); |
| 182 |
Py_DECREF(num); |
| 183 |
if (value == -1 && PyErr_Occurred()) |
| 184 |
return NULL; |
| 185 |
if (value != -1) |
| 186 |
return raiseTestError("test_long_and_overflow", |
| 187 |
"return value was not set to -1"); |
| 188 |
if (overflow != -1) |
| 189 |
return raiseTestError("test_long_and_overflow", |
| 190 |
"overflow was not set to -1"); |
| 191 |
|
| 192 |
/* Same again, with num = LONG_MIN - 1 */ |
| 193 |
num = PyLong_FromLong(LONG_MIN); |
| 194 |
if (num == NULL) |
| 195 |
return NULL; |
| 196 |
one = PyLong_FromLong(1L); |
| 197 |
if (one == NULL) { |
| 198 |
Py_DECREF(num); |
| 199 |
return NULL; |
| 200 |
} |
| 201 |
temp = PyNumber_Subtract(num, one); |
| 202 |
Py_DECREF(one); |
| 203 |
Py_DECREF(num); num = temp; |
| 204 |
if (num == NULL) |
| 205 |
return NULL; |
| 206 |
overflow = 0; |
| 207 |
value = PyLong_AsLongAndOverflow(num, &overflow); |
| 208 |
Py_DECREF(num); |
| 209 |
if (value == -1 && PyErr_Occurred()) |
| 210 |
return NULL; |
| 211 |
if (value != -1) |
| 212 |
return raiseTestError("test_long_and_overflow", |
| 213 |
"return value was not set to -1"); |
| 214 |
if (overflow != -1) |
| 215 |
return raiseTestError("test_long_and_overflow", |
| 216 |
"overflow was not set to -1"); |
| 217 |
|
| 218 |
/* Test that overflow is cleared properly for small values. */ |
| 219 |
num = PyLong_FromString("FF", NULL, 16); |
| 220 |
if (num == NULL) |
| 221 |
return NULL; |
| 222 |
overflow = 1234; |
| 223 |
value = PyLong_AsLongAndOverflow(num, &overflow); |
| 224 |
Py_DECREF(num); |
| 225 |
if (value == -1 && PyErr_Occurred()) |
| 226 |
return NULL; |
| 227 |
if (value != 0xFF) |
| 228 |
return raiseTestError("test_long_and_overflow", |
| 229 |
"expected return value 0xFF"); |
| 230 |
if (overflow != 0) |
| 231 |
return raiseTestError("test_long_and_overflow", |
| 232 |
"overflow was not cleared"); |
| 233 |
|
| 234 |
num = PyLong_FromString("-FF", NULL, 16); |
| 235 |
if (num == NULL) |
| 236 |
return NULL; |
| 237 |
overflow = 0; |
| 238 |
value = PyLong_AsLongAndOverflow(num, &overflow); |
| 239 |
Py_DECREF(num); |
| 240 |
if (value == -1 && PyErr_Occurred()) |
| 241 |
return NULL; |
| 242 |
if (value != -0xFF) |
| 243 |
return raiseTestError("test_long_and_overflow", |
| 244 |
"expected return value 0xFF"); |
| 245 |
if (overflow != 0) |
| 246 |
return raiseTestError("test_long_and_overflow", |
| 247 |
"overflow was set incorrectly"); |
| 248 |
|
| 249 |
num = PyLong_FromLong(LONG_MAX); |
| 250 |
if (num == NULL) |
| 251 |
return NULL; |
| 252 |
overflow = 1234; |
| 253 |
value = PyLong_AsLongAndOverflow(num, &overflow); |
| 254 |
Py_DECREF(num); |
| 255 |
if (value == -1 && PyErr_Occurred()) |
| 256 |
return NULL; |
| 257 |
if (value != LONG_MAX) |
| 258 |
return raiseTestError("test_long_and_overflow", |
| 259 |
"expected return value LONG_MAX"); |
| 260 |
if (overflow != 0) |
| 261 |
return raiseTestError("test_long_and_overflow", |
| 262 |
"overflow was not cleared"); |
| 263 |
|
| 264 |
num = PyLong_FromLong(LONG_MIN); |
| 265 |
if (num == NULL) |
| 266 |
return NULL; |
| 267 |
overflow = 0; |
| 268 |
value = PyLong_AsLongAndOverflow(num, &overflow); |
| 269 |
Py_DECREF(num); |
| 270 |
if (value == -1 && PyErr_Occurred()) |
| 271 |
return NULL; |
| 272 |
if (value != LONG_MIN) |
| 273 |
return raiseTestError("test_long_and_overflow", |
| 274 |
"expected return value LONG_MIN"); |
| 275 |
if (overflow != 0) |
| 276 |
return raiseTestError("test_long_and_overflow", |
| 277 |
"overflow was not cleared"); |
| 278 |
|
| 279 |
Py_RETURN_NONE; |
| 280 |
} |
| 281 |
|
| 282 |
/*[clinic input] |
| 283 |
_testlimitedcapi.test_long_long_and_overflow |
| 284 |
|
| 285 |
Test the PyLong_AsLongLongAndOverflow API. |
| 286 |
|
| 287 |
General conversion to long long is tested by test_long_api_inner. |
| 288 |
This test will concentrate on proper handling of overflow. |
| 289 |
[clinic start generated code]*/ |
| 290 |
|
| 291 |
static PyObject * |
| 292 |
_testlimitedcapi_test_long_long_and_overflow_impl(PyObject *module) |
| 293 |
/*[clinic end generated code: output=3d2721a49c09a307 input=741c593b606cc6b3]*/ |
| 294 |
{ |
| 295 |
PyObject *num, *one, *temp; |
| 296 |
long long value; |
| 297 |
int overflow; |
| 298 |
|
| 299 |
/* Test that overflow is set properly for a large value. */ |
| 300 |
/* num is a number larger than LLONG_MAX on a typical machine. */ |
| 301 |
num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 302 |
if (num == NULL) |
| 303 |
return NULL; |
| 304 |
overflow = 1234; |
| 305 |
value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 306 |
Py_DECREF(num); |
| 307 |
if (value == -1 && PyErr_Occurred()) |
| 308 |
return NULL; |
| 309 |
if (value != -1) |
| 310 |
return raiseTestError("test_long_long_and_overflow", |
| 311 |
"return value was not set to -1"); |
| 312 |
if (overflow != 1) |
| 313 |
return raiseTestError("test_long_long_and_overflow", |
| 314 |
"overflow was not set to 1"); |
| 315 |
|
| 316 |
/* Same again, with num = LLONG_MAX + 1 */ |
| 317 |
num = PyLong_FromLongLong(LLONG_MAX); |
| 318 |
if (num == NULL) |
| 319 |
return NULL; |
| 320 |
one = PyLong_FromLong(1L); |
| 321 |
if (one == NULL) { |
| 322 |
Py_DECREF(num); |
| 323 |
return NULL; |
| 324 |
} |
| 325 |
temp = PyNumber_Add(num, one); |
| 326 |
Py_DECREF(one); |
| 327 |
Py_DECREF(num); num = temp; |
| 328 |
if (num == NULL) |
| 329 |
return NULL; |
| 330 |
overflow = 0; |
| 331 |
value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 332 |
Py_DECREF(num); |
| 333 |
if (value == -1 && PyErr_Occurred()) |
| 334 |
return NULL; |
| 335 |
if (value != -1) |
| 336 |
return raiseTestError("test_long_long_and_overflow", |
| 337 |
"return value was not set to -1"); |
| 338 |
if (overflow != 1) |
| 339 |
return raiseTestError("test_long_long_and_overflow", |
| 340 |
"overflow was not set to 1"); |
| 341 |
|
| 342 |
/* Test that overflow is set properly for a large negative value. */ |
| 343 |
/* num is a number smaller than LLONG_MIN on a typical platform */ |
| 344 |
num = PyLong_FromString("-FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 345 |
if (num == NULL) |
| 346 |
return NULL; |
| 347 |
overflow = 1234; |
| 348 |
value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 349 |
Py_DECREF(num); |
| 350 |
if (value == -1 && PyErr_Occurred()) |
| 351 |
return NULL; |
| 352 |
if (value != -1) |
| 353 |
return raiseTestError("test_long_long_and_overflow", |
| 354 |
"return value was not set to -1"); |
| 355 |
if (overflow != -1) |
| 356 |
return raiseTestError("test_long_long_and_overflow", |
| 357 |
"overflow was not set to -1"); |
| 358 |
|
| 359 |
/* Same again, with num = LLONG_MIN - 1 */ |
| 360 |
num = PyLong_FromLongLong(LLONG_MIN); |
| 361 |
if (num == NULL) |
| 362 |
return NULL; |
| 363 |
one = PyLong_FromLong(1L); |
| 364 |
if (one == NULL) { |
| 365 |
Py_DECREF(num); |
| 366 |
return NULL; |
| 367 |
} |
| 368 |
temp = PyNumber_Subtract(num, one); |
| 369 |
Py_DECREF(one); |
| 370 |
Py_DECREF(num); num = temp; |
| 371 |
if (num == NULL) |
| 372 |
return NULL; |
| 373 |
overflow = 0; |
| 374 |
value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 375 |
Py_DECREF(num); |
| 376 |
if (value == -1 && PyErr_Occurred()) |
| 377 |
return NULL; |
| 378 |
if (value != -1) |
| 379 |
return raiseTestError("test_long_long_and_overflow", |
| 380 |
"return value was not set to -1"); |
| 381 |
if (overflow != -1) |
| 382 |
return raiseTestError("test_long_long_and_overflow", |
| 383 |
"overflow was not set to -1"); |
| 384 |
|
| 385 |
/* Test that overflow is cleared properly for small values. */ |
| 386 |
num = PyLong_FromString("FF", NULL, 16); |
| 387 |
if (num == NULL) |
| 388 |
return NULL; |
| 389 |
overflow = 1234; |
| 390 |
value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 391 |
Py_DECREF(num); |
| 392 |
if (value == -1 && PyErr_Occurred()) |
| 393 |
return NULL; |
| 394 |
if (value != 0xFF) |
| 395 |
return raiseTestError("test_long_long_and_overflow", |
| 396 |
"expected return value 0xFF"); |
| 397 |
if (overflow != 0) |
| 398 |
return raiseTestError("test_long_long_and_overflow", |
| 399 |
"overflow was not cleared"); |
| 400 |
|
| 401 |
num = PyLong_FromString("-FF", NULL, 16); |
| 402 |
if (num == NULL) |
| 403 |
return NULL; |
| 404 |
overflow = 0; |
| 405 |
value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 406 |
Py_DECREF(num); |
| 407 |
if (value == -1 && PyErr_Occurred()) |
| 408 |
return NULL; |
| 409 |
if (value != -0xFF) |
| 410 |
return raiseTestError("test_long_long_and_overflow", |
| 411 |
"expected return value 0xFF"); |
| 412 |
if (overflow != 0) |
| 413 |
return raiseTestError("test_long_long_and_overflow", |
| 414 |
"overflow was set incorrectly"); |
| 415 |
|
| 416 |
num = PyLong_FromLongLong(LLONG_MAX); |
| 417 |
if (num == NULL) |
| 418 |
return NULL; |
| 419 |
overflow = 1234; |
| 420 |
value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 421 |
Py_DECREF(num); |
| 422 |
if (value == -1 && PyErr_Occurred()) |
| 423 |
return NULL; |
| 424 |
if (value != LLONG_MAX) |
| 425 |
return raiseTestError("test_long_long_and_overflow", |
| 426 |
"expected return value LLONG_MAX"); |
| 427 |
if (overflow != 0) |
| 428 |
return raiseTestError("test_long_long_and_overflow", |
| 429 |
"overflow was not cleared"); |
| 430 |
|
| 431 |
num = PyLong_FromLongLong(LLONG_MIN); |
| 432 |
if (num == NULL) |
| 433 |
return NULL; |
| 434 |
overflow = 0; |
| 435 |
value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 436 |
Py_DECREF(num); |
| 437 |
if (value == -1 && PyErr_Occurred()) |
| 438 |
return NULL; |
| 439 |
if (value != LLONG_MIN) |
| 440 |
return raiseTestError("test_long_long_and_overflow", |
| 441 |
"expected return value LLONG_MIN"); |
| 442 |
if (overflow != 0) |
| 443 |
return raiseTestError("test_long_long_and_overflow", |
| 444 |
"overflow was not cleared"); |
| 445 |
|
| 446 |
Py_RETURN_NONE; |
| 447 |
} |
| 448 |
|
| 449 |
/*[clinic input] |
| 450 |
_testlimitedcapi.test_long_as_size_t |
| 451 |
|
| 452 |
Test the PyLong_As{Size,Ssize}_t API. |
| 453 |
|
| 454 |
At present this just tests that non-integer arguments are handled correctly. |
| 455 |
It should be extended to test overflow handling. |
| 456 |
[clinic start generated code]*/ |
| 457 |
|
| 458 |
static PyObject * |
| 459 |
_testlimitedcapi_test_long_as_size_t_impl(PyObject *module) |
| 460 |
/*[clinic end generated code: output=297a9f14a42f55af input=8923d8f2038c46f4]*/ |
| 461 |
{ |
| 462 |
size_t out_u; |
| 463 |
Py_ssize_t out_s; |
| 464 |
|
| 465 |
Py_INCREF(Py_None); |
| 466 |
|
| 467 |
out_u = PyLong_AsSize_t(Py_None); |
| 468 |
if (out_u != (size_t)-1 || !PyErr_Occurred()) |
| 469 |
return raiseTestError("test_long_as_size_t", |
| 470 |
"PyLong_AsSize_t(None) didn't complain"); |
| 471 |
if (!PyErr_ExceptionMatches(PyExc_TypeError)) |
| 472 |
return raiseTestError("test_long_as_size_t", |
| 473 |
"PyLong_AsSize_t(None) raised " |
| 474 |
"something other than TypeError"); |
| 475 |
PyErr_Clear(); |
| 476 |
|
| 477 |
out_s = PyLong_AsSsize_t(Py_None); |
| 478 |
if (out_s != (Py_ssize_t)-1 || !PyErr_Occurred()) |
| 479 |
return raiseTestError("test_long_as_size_t", |
| 480 |
"PyLong_AsSsize_t(None) didn't complain"); |
| 481 |
if (!PyErr_ExceptionMatches(PyExc_TypeError)) |
| 482 |
return raiseTestError("test_long_as_size_t", |
| 483 |
"PyLong_AsSsize_t(None) raised " |
| 484 |
"something other than TypeError"); |
| 485 |
PyErr_Clear(); |
| 486 |
|
| 487 |
/* Py_INCREF(Py_None) omitted - we already have a reference to it. */ |
| 488 |
return Py_None; |
| 489 |
} |
| 490 |
|
| 491 |
/*[clinic input] |
| 492 |
_testlimitedcapi.test_long_as_unsigned_long_long_mask |
| 493 |
[clinic start generated code]*/ |
| 494 |
|
| 495 |
static PyObject * |
| 496 |
_testlimitedcapi_test_long_as_unsigned_long_long_mask_impl(PyObject *module) |
| 497 |
/*[clinic end generated code: output=90be09ffeec8ecab input=17c660bd58becad5]*/ |
| 498 |
{ |
| 499 |
unsigned long long res = PyLong_AsUnsignedLongLongMask(NULL); |
| 500 |
|
| 501 |
if (res != (unsigned long long)-1 || !PyErr_Occurred()) { |
| 502 |
return raiseTestError("test_long_as_unsigned_long_long_mask", |
| 503 |
"PyLong_AsUnsignedLongLongMask(NULL) didn't " |
| 504 |
"complain"); |
| 505 |
} |
| 506 |
if (!PyErr_ExceptionMatches(PyExc_SystemError)) { |
| 507 |
return raiseTestError("test_long_as_unsigned_long_long_mask", |
| 508 |
"PyLong_AsUnsignedLongLongMask(NULL) raised " |
| 509 |
"something other than SystemError"); |
| 510 |
} |
| 511 |
PyErr_Clear(); |
| 512 |
Py_RETURN_NONE; |
| 513 |
} |
| 514 |
|
| 515 |
/*[clinic input] |
| 516 |
_testlimitedcapi.test_long_as_double |
| 517 |
[clinic start generated code]*/ |
| 518 |
|
| 519 |
static PyObject * |
| 520 |
_testlimitedcapi_test_long_as_double_impl(PyObject *module) |
| 521 |
/*[clinic end generated code: output=0e688c2acf224f88 input=e7b5712385064a48]*/ |
| 522 |
{ |
| 523 |
double out; |
| 524 |
|
| 525 |
Py_INCREF(Py_None); |
| 526 |
|
| 527 |
out = PyLong_AsDouble(Py_None); |
| 528 |
if (out != -1.0 || !PyErr_Occurred()) |
| 529 |
return raiseTestError("test_long_as_double", |
| 530 |
"PyLong_AsDouble(None) didn't complain"); |
| 531 |
if (!PyErr_ExceptionMatches(PyExc_TypeError)) |
| 532 |
return raiseTestError("test_long_as_double", |
| 533 |
"PyLong_AsDouble(None) raised " |
| 534 |
"something other than TypeError"); |
| 535 |
PyErr_Clear(); |
| 536 |
|
| 537 |
/* Py_INCREF(Py_None) omitted - we already have a reference to it. */ |
| 538 |
return Py_None; |
| 539 |
} |
| 540 |
|
| 541 |
static PyObject * |
| 542 |
pylong_check(PyObject *module, PyObject *obj) |
| 543 |
{ |
| 544 |
NULLABLE(obj); |
| 545 |
return PyLong_FromLong(PyLong_Check(obj)); |
| 546 |
} |
| 547 |
|
| 548 |
static PyObject * |
| 549 |
pylong_checkexact(PyObject *module, PyObject *obj) |
| 550 |
{ |
| 551 |
NULLABLE(obj); |
| 552 |
return PyLong_FromLong(PyLong_CheckExact(obj)); |
| 553 |
} |
| 554 |
|
| 555 |
static PyObject * |
| 556 |
pylong_fromdouble(PyObject *module, PyObject *arg) |
| 557 |
{ |
| 558 |
double value; |
| 559 |
if (!PyArg_Parse(arg, "d", &value)) { |
| 560 |
return NULL; |
| 561 |
} |
| 562 |
return PyLong_FromDouble(value); |
| 563 |
} |
| 564 |
|
| 565 |
static PyObject * |
| 566 |
pylong_fromstring(PyObject *module, PyObject *args) |
| 567 |
{ |
| 568 |
const char *str; |
| 569 |
Py_ssize_t len; |
| 570 |
int base; |
| 571 |
char *end = UNINITIALIZED_PTR; |
| 572 |
if (!PyArg_ParseTuple(args, "z#i", &str, &len, &base)) { |
| 573 |
return NULL; |
| 574 |
} |
| 575 |
|
| 576 |
PyObject *result = PyLong_FromString(str, &end, base); |
| 577 |
if (result == NULL) { |
| 578 |
// XXX 'end' is not always set. |
| 579 |
return NULL; |
| 580 |
} |
| 581 |
return Py_BuildValue("Nn", result, (Py_ssize_t)(end - str)); |
| 582 |
} |
| 583 |
|
| 584 |
static PyObject * |
| 585 |
pylong_fromvoidptr(PyObject *module, PyObject *arg) |
| 586 |
{ |
| 587 |
NULLABLE(arg); |
| 588 |
return PyLong_FromVoidPtr((void *)arg); |
| 589 |
} |
| 590 |
|
| 591 |
/*[clinic input] |
| 592 |
_testlimitedcapi.PyLong_AsInt |
| 593 |
arg: object |
| 594 |
/ |
| 595 |
[clinic start generated code]*/ |
| 596 |
|
| 597 |
static PyObject * |
| 598 |
_testlimitedcapi_PyLong_AsInt(PyObject *module, PyObject *arg) |
| 599 |
/*[clinic end generated code: output=d91db4c1287f85fa input=32c66be86f3265a1]*/ |
| 600 |
{ |
| 601 |
NULLABLE(arg); |
| 602 |
assert(!PyErr_Occurred()); |
| 603 |
int value = PyLong_AsInt(arg); |
| 604 |
if (value == -1 && PyErr_Occurred()) { |
| 605 |
return NULL; |
| 606 |
} |
| 607 |
return PyLong_FromLong(value); |
| 608 |
} |
| 609 |
|
| 610 |
static PyObject * |
| 611 |
pylong_aslong(PyObject *module, PyObject *arg) |
| 612 |
{ |
| 613 |
NULLABLE(arg); |
| 614 |
long value = PyLong_AsLong(arg); |
| 615 |
if (value == -1 && PyErr_Occurred()) { |
| 616 |
return NULL; |
| 617 |
} |
| 618 |
return PyLong_FromLong(value); |
| 619 |
} |
| 620 |
|
| 621 |
static PyObject * |
| 622 |
pylong_aslongandoverflow(PyObject *module, PyObject *arg) |
| 623 |
{ |
| 624 |
NULLABLE(arg); |
| 625 |
int overflow = UNINITIALIZED_INT; |
| 626 |
long value = PyLong_AsLongAndOverflow(arg, &overflow); |
| 627 |
if (value == -1 && PyErr_Occurred()) { |
| 628 |
assert(overflow == 0); |
| 629 |
return NULL; |
| 630 |
} |
| 631 |
return Py_BuildValue("li", value, overflow); |
| 632 |
} |
| 633 |
|
| 634 |
static PyObject * |
| 635 |
pylong_asunsignedlong(PyObject *module, PyObject *arg) |
| 636 |
{ |
| 637 |
NULLABLE(arg); |
| 638 |
unsigned long value = PyLong_AsUnsignedLong(arg); |
| 639 |
if (value == (unsigned long)-1 && PyErr_Occurred()) { |
| 640 |
return NULL; |
| 641 |
} |
| 642 |
return PyLong_FromUnsignedLong(value); |
| 643 |
} |
| 644 |
|
| 645 |
static PyObject * |
| 646 |
pylong_asunsignedlongmask(PyObject *module, PyObject *arg) |
| 647 |
{ |
| 648 |
NULLABLE(arg); |
| 649 |
unsigned long value = PyLong_AsUnsignedLongMask(arg); |
| 650 |
if (value == (unsigned long)-1 && PyErr_Occurred()) { |
| 651 |
return NULL; |
| 652 |
} |
| 653 |
return PyLong_FromUnsignedLong(value); |
| 654 |
} |
| 655 |
|
| 656 |
static PyObject * |
| 657 |
pylong_aslonglong(PyObject *module, PyObject *arg) |
| 658 |
{ |
| 659 |
NULLABLE(arg); |
| 660 |
long long value = PyLong_AsLongLong(arg); |
| 661 |
if (value == -1 && PyErr_Occurred()) { |
| 662 |
return NULL; |
| 663 |
} |
| 664 |
return PyLong_FromLongLong(value); |
| 665 |
} |
| 666 |
|
| 667 |
static PyObject * |
| 668 |
pylong_aslonglongandoverflow(PyObject *module, PyObject *arg) |
| 669 |
{ |
| 670 |
NULLABLE(arg); |
| 671 |
int overflow = UNINITIALIZED_INT; |
| 672 |
long long value = PyLong_AsLongLongAndOverflow(arg, &overflow); |
| 673 |
if (value == -1 && PyErr_Occurred()) { |
| 674 |
assert(overflow == 0); |
| 675 |
return NULL; |
| 676 |
} |
| 677 |
return Py_BuildValue("Li", value, overflow); |
| 678 |
} |
| 679 |
|
| 680 |
static PyObject * |
| 681 |
pylong_asunsignedlonglong(PyObject *module, PyObject *arg) |
| 682 |
{ |
| 683 |
NULLABLE(arg); |
| 684 |
unsigned long long value = PyLong_AsUnsignedLongLong(arg); |
| 685 |
if (value == (unsigned long long)-1 && PyErr_Occurred()) { |
| 686 |
return NULL; |
| 687 |
} |
| 688 |
return PyLong_FromUnsignedLongLong(value); |
| 689 |
} |
| 690 |
|
| 691 |
static PyObject * |
| 692 |
pylong_asunsignedlonglongmask(PyObject *module, PyObject *arg) |
| 693 |
{ |
| 694 |
NULLABLE(arg); |
| 695 |
unsigned long long value = PyLong_AsUnsignedLongLongMask(arg); |
| 696 |
if (value == (unsigned long long)-1 && PyErr_Occurred()) { |
| 697 |
return NULL; |
| 698 |
} |
| 699 |
return PyLong_FromUnsignedLongLong(value); |
| 700 |
} |
| 701 |
|
| 702 |
static PyObject * |
| 703 |
pylong_as_ssize_t(PyObject *module, PyObject *arg) |
| 704 |
{ |
| 705 |
NULLABLE(arg); |
| 706 |
Py_ssize_t value = PyLong_AsSsize_t(arg); |
| 707 |
if (value == -1 && PyErr_Occurred()) { |
| 708 |
return NULL; |
| 709 |
} |
| 710 |
return PyLong_FromSsize_t(value); |
| 711 |
} |
| 712 |
|
| 713 |
static PyObject * |
| 714 |
pylong_as_size_t(PyObject *module, PyObject *arg) |
| 715 |
{ |
| 716 |
NULLABLE(arg); |
| 717 |
size_t value = PyLong_AsSize_t(arg); |
| 718 |
if (value == (size_t)-1 && PyErr_Occurred()) { |
| 719 |
return NULL; |
| 720 |
} |
| 721 |
return PyLong_FromSize_t(value); |
| 722 |
} |
| 723 |
|
| 724 |
static PyObject * |
| 725 |
pylong_asdouble(PyObject *module, PyObject *arg) |
| 726 |
{ |
| 727 |
NULLABLE(arg); |
| 728 |
double value = PyLong_AsDouble(arg); |
| 729 |
if (value == -1.0 && PyErr_Occurred()) { |
| 730 |
return NULL; |
| 731 |
} |
| 732 |
return PyFloat_FromDouble(value); |
| 733 |
} |
| 734 |
|
| 735 |
static PyObject * |
| 736 |
pylong_asvoidptr(PyObject *module, PyObject *arg) |
| 737 |
{ |
| 738 |
NULLABLE(arg); |
| 739 |
void *value = PyLong_AsVoidPtr(arg); |
| 740 |
if (value == NULL) { |
| 741 |
if (PyErr_Occurred()) { |
| 742 |
return NULL; |
| 743 |
} |
| 744 |
Py_RETURN_NONE; |
| 745 |
} |
| 746 |
return Py_NewRef((PyObject *)value); |
| 747 |
} |
| 748 |
|
| 749 |
static PyObject * |
| 750 |
pylong_aspid(PyObject *module, PyObject *arg) |
| 751 |
{ |
| 752 |
NULLABLE(arg); |
| 753 |
pid_t value = PyLong_AsPid(arg); |
| 754 |
if (value == -1 && PyErr_Occurred()) { |
| 755 |
return NULL; |
| 756 |
} |
| 757 |
return PyLong_FromPid(value); |
| 758 |
} |
| 759 |
|
| 760 |
|
| 761 |
static PyObject * |
| 762 |
pylong_asint32(PyObject *module, PyObject *arg) |
| 763 |
{ |
| 764 |
NULLABLE(arg); |
| 765 |
int32_t value; |
| 766 |
if (PyLong_AsInt32(arg, &value) < 0) { |
| 767 |
return NULL; |
| 768 |
} |
| 769 |
return PyLong_FromInt32(value); |
| 770 |
} |
| 771 |
|
| 772 |
static PyObject * |
| 773 |
pylong_asuint32(PyObject *module, PyObject *arg) |
| 774 |
{ |
| 775 |
NULLABLE(arg); |
| 776 |
uint32_t value; |
| 777 |
if (PyLong_AsUInt32(arg, &value) < 0) { |
| 778 |
return NULL; |
| 779 |
} |
| 780 |
return PyLong_FromUInt32(value); |
| 781 |
} |
| 782 |
|
| 783 |
|
| 784 |
static PyObject * |
| 785 |
pylong_asint64(PyObject *module, PyObject *arg) |
| 786 |
{ |
| 787 |
NULLABLE(arg); |
| 788 |
int64_t value; |
| 789 |
if (PyLong_AsInt64(arg, &value) < 0) { |
| 790 |
return NULL; |
| 791 |
} |
| 792 |
return PyLong_FromInt64(value); |
| 793 |
} |
| 794 |
|
| 795 |
static PyObject * |
| 796 |
pylong_asuint64(PyObject *module, PyObject *arg) |
| 797 |
{ |
| 798 |
NULLABLE(arg); |
| 799 |
uint64_t value; |
| 800 |
if (PyLong_AsUInt64(arg, &value) < 0) { |
| 801 |
return NULL; |
| 802 |
} |
| 803 |
return PyLong_FromUInt64(value); |
| 804 |
} |
| 805 |
|
| 806 |
|
| 807 |
static PyMethodDef test_methods[] = { |
| 808 |
_TESTLIMITEDCAPI_TEST_LONG_AND_OVERFLOW_METHODDEF |
| 809 |
_TESTLIMITEDCAPI_TEST_LONG_API_METHODDEF |
| 810 |
_TESTLIMITEDCAPI_TEST_LONG_AS_DOUBLE_METHODDEF |
| 811 |
_TESTLIMITEDCAPI_TEST_LONG_AS_SIZE_T_METHODDEF |
| 812 |
_TESTLIMITEDCAPI_TEST_LONG_AS_UNSIGNED_LONG_LONG_MASK_METHODDEF |
| 813 |
_TESTLIMITEDCAPI_TEST_LONG_LONG_AND_OVERFLOW_METHODDEF |
| 814 |
_TESTLIMITEDCAPI_TEST_LONGLONG_API_METHODDEF |
| 815 |
{"pylong_check", pylong_check, METH_O}, |
| 816 |
{"pylong_checkexact", pylong_checkexact, METH_O}, |
| 817 |
{"pylong_fromdouble", pylong_fromdouble, METH_O}, |
| 818 |
{"pylong_fromstring", pylong_fromstring, METH_VARARGS}, |
| 819 |
{"pylong_fromvoidptr", pylong_fromvoidptr, METH_O}, |
| 820 |
_TESTLIMITEDCAPI_PYLONG_ASINT_METHODDEF |
| 821 |
{"pylong_aslong", pylong_aslong, METH_O}, |
| 822 |
{"pylong_aslongandoverflow", pylong_aslongandoverflow, METH_O}, |
| 823 |
{"pylong_asunsignedlong", pylong_asunsignedlong, METH_O}, |
| 824 |
{"pylong_asunsignedlongmask", pylong_asunsignedlongmask, METH_O}, |
| 825 |
{"pylong_aslonglong", pylong_aslonglong, METH_O}, |
| 826 |
{"pylong_aslonglongandoverflow", pylong_aslonglongandoverflow, METH_O}, |
| 827 |
{"pylong_asunsignedlonglong", pylong_asunsignedlonglong, METH_O}, |
| 828 |
{"pylong_asunsignedlonglongmask", pylong_asunsignedlonglongmask, METH_O}, |
| 829 |
{"pylong_as_ssize_t", pylong_as_ssize_t, METH_O}, |
| 830 |
{"pylong_as_size_t", pylong_as_size_t, METH_O}, |
| 831 |
{"pylong_asdouble", pylong_asdouble, METH_O}, |
| 832 |
{"pylong_asvoidptr", pylong_asvoidptr, METH_O}, |
| 833 |
{"pylong_aspid", pylong_aspid, METH_O}, |
| 834 |
{"pylong_asint32", pylong_asint32, METH_O}, |
| 835 |
{"pylong_asuint32", pylong_asuint32, METH_O}, |
| 836 |
{"pylong_asint64", pylong_asint64, METH_O}, |
| 837 |
{"pylong_asuint64", pylong_asuint64, METH_O}, |
| 838 |
{NULL}, |
| 839 |
}; |
| 840 |
|
| 841 |
int |
| 842 |
_PyTestLimitedCAPI_Init_Long(PyObject *mod) |
| 843 |
{ |
| 844 |
if (PyModule_AddFunctions(mod, test_methods) < 0) { |
| 845 |
return -1; |
| 846 |
} |
| 847 |
return 0; |
| 848 |
} |