mirror of
https://github.com/python/cpython.git
synced 2026-04-17 01:10:46 +00:00
The long_from_string_base() might return a small integer, when the
_pylong.py is used to do conversion. Hence, we must be careful here to
not smash it "small int" bit by using the _PyLong_FlipSign().
Co-authored-by: Victor Stinner <vstinner@python.org>
(cherry picked from commit db5936c5b8)
Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
60 lines
1.6 KiB
C
60 lines
1.6 KiB
C
#include "parts.h"
|
|
|
|
#define Py_BUILD_CORE
|
|
#include "internal/pycore_long.h" // _PyLong_IsSmallInt()
|
|
|
|
int verify_immortality(PyObject *object)
|
|
{
|
|
assert(_Py_IsImmortal(object));
|
|
Py_ssize_t old_count = Py_REFCNT(object);
|
|
for (int j = 0; j < 10000; j++) {
|
|
Py_DECREF(object);
|
|
}
|
|
Py_ssize_t current_count = Py_REFCNT(object);
|
|
return old_count == current_count;
|
|
}
|
|
|
|
static PyObject *
|
|
test_immortal_builtins(PyObject *self, PyObject *Py_UNUSED(ignored))
|
|
{
|
|
PyObject *objects[] = {Py_True, Py_False, Py_None, Py_Ellipsis};
|
|
Py_ssize_t n = Py_ARRAY_LENGTH(objects);
|
|
for (Py_ssize_t i = 0; i < n; i++) {
|
|
assert(verify_immortality(objects[i]));
|
|
}
|
|
Py_RETURN_NONE;
|
|
}
|
|
|
|
static PyObject *
|
|
test_immortal_small_ints(PyObject *self, PyObject *Py_UNUSED(ignored))
|
|
{
|
|
for (int i = -5; i <= 256; i++) {
|
|
PyObject *obj = PyLong_FromLong(i);
|
|
assert(verify_immortality(obj));
|
|
int is_small_int = _PyLong_IsSmallInt((PyLongObject *)obj);
|
|
assert(is_small_int);
|
|
}
|
|
for (int i = 257; i <= 260; i++) {
|
|
PyObject *obj = PyLong_FromLong(i);
|
|
assert(obj);
|
|
int is_small_int = _PyLong_IsSmallInt((PyLongObject *)obj);
|
|
assert(!is_small_int);
|
|
Py_DECREF(obj);
|
|
}
|
|
Py_RETURN_NONE;
|
|
}
|
|
|
|
static PyMethodDef test_methods[] = {
|
|
{"test_immortal_builtins", test_immortal_builtins, METH_NOARGS},
|
|
{"test_immortal_small_ints", test_immortal_small_ints, METH_NOARGS},
|
|
{NULL},
|
|
};
|
|
|
|
int
|
|
_PyTestCapi_Init_Immortal(PyObject *mod)
|
|
{
|
|
if (PyModule_AddFunctions(mod, test_methods) < 0) {
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|