| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
#include "Python.h" |
| 13 |
|
| 14 |
|
| 15 |
#ifndef PyTuple_MAXSAVESIZE |
| 16 |
#define PyTuple_MAXSAVESIZE 20 |
| 17 |
#endif |
| 18 |
#ifndef PyTuple_MAXFREELIST |
| 19 |
#define PyTuple_MAXFREELIST 2000 |
| 20 |
#endif |
| 21 |
|
| 22 |
#if PyTuple_MAXSAVESIZE > 0 |
| 23 |
static PyTupleObject *free_list[PyTuple_MAXSAVESIZE]; |
| 24 |
static int numfree[PyTuple_MAXSAVESIZE]; |
| 25 |
#endif |
| 26 |
|
| 27 |
PyObject * |
| 28 |
PyTuple_New(register Py_ssize_t size) |
| 29 |
{ |
| 30 |
register PyTupleObject *op; |
| 31 |
Py_ssize_t i; |
| 32 |
if (size < 0) { |
| 33 |
PyErr_BadInternalCall(); |
| 34 |
return NULL; |
| 35 |
} |
| 36 |
#if PyTuple_MAXSAVESIZE > 0 |
| 37 |
if (size < PyTuple_MAXSAVESIZE && (op = free_list[size]) != NULL) { |
| 38 |
free_list[size] = (PyTupleObject *) op->ob_item[0]; |
| 39 |
numfree[size]--; |
| 40 |
_Py_NewReference((PyObject *)op); |
| 41 |
} |
| 42 |
else |
| 43 |
#endif |
| 44 |
{ |
| 45 |
Py_ssize_t nbytes = size * sizeof(PyObject *); |
| 46 |
|
| 47 |
if (nbytes / sizeof(PyObject *) != (size_t)size || |
| 48 |
(nbytes > PY_SSIZE_T_MAX - sizeof(PyTupleObject) - sizeof(PyObject *))) |
| 49 |
{ |
| 50 |
return PyErr_NoMemory(); |
| 51 |
} |
| 52 |
|
| 53 |
op = PyObject_GC_NewVar(PyTupleObject, &PyTuple_Type, size); |
| 54 |
if (op == NULL) |
| 55 |
return NULL; |
| 56 |
} |
| 57 |
for (i=0; i < size; i++) |
| 58 |
op->ob_item[i] = NULL; |
| 59 |
_PyObject_GC_TRACK(op); |
| 60 |
return (PyObject *) op; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
#ifdef CPYEXT_TESTS |
| 65 |
#define _Py_tuple_dealloc _cpyexttest_tuple_dealloc |
| 66 |
#ifdef __GNUC__ |
| 67 |
__attribute__((visibility("default"))) |
| 68 |
#else |
| 69 |
__declspec(dllexport) |
| 70 |
#endif |
| 71 |
#else |
| 72 |
#define _Py_tuple_dealloc _PyPy_tuple_dealloc |
| 73 |
#endif |
| 74 |
void |
| 75 |
_Py_tuple_dealloc(register PyObject *_op) |
| 76 |
{ |
| 77 |
register PyTupleObject *op = (PyTupleObject *)_op; |
| 78 |
register Py_ssize_t i; |
| 79 |
register Py_ssize_t len = Py_SIZE(op); |
| 80 |
PyObject_GC_UnTrack(op); |
| 81 |
Py_TRASHCAN_SAFE_BEGIN(op) |
| 82 |
if (len >= 0) { |
| 83 |
i = len; |
| 84 |
while (--i >= 0) |
| 85 |
Py_XDECREF(op->ob_item[i]); |
| 86 |
#if PyTuple_MAXSAVESIZE > 0 |
| 87 |
if (len < PyTuple_MAXSAVESIZE && |
| 88 |
numfree[len] < PyTuple_MAXFREELIST && |
| 89 |
Py_TYPE(op) == &PyTuple_Type) |
| 90 |
{ |
| 91 |
op->ob_item[0] = (PyObject *) free_list[len]; |
| 92 |
numfree[len]++; |
| 93 |
free_list[len] = op; |
| 94 |
goto done; |
| 95 |
} |
| 96 |
#endif |
| 97 |
} |
| 98 |
Py_TYPE(op)->tp_free((PyObject *)op); |
| 99 |
done: |
| 100 |
Py_TRASHCAN_SAFE_END(op) |
| 101 |
} |
| 102 |
|
| 103 |
static PyObject * |
| 104 |
tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
#ifdef CPYEXT_TESTS |
| 112 |
#define _Py_tuple_new _cpyexttest_tuple_new |
| 113 |
#ifdef __GNUC__ |
| 114 |
__attribute__((visibility("default"))) |
| 115 |
#else |
| 116 |
__declspec(dllexport) |
| 117 |
#endif |
| 118 |
#else |
| 119 |
#define _Py_tuple_new _PyPy_tuple_new |
| 120 |
#endif |
| 121 |
PyObject * |
| 122 |
_Py_tuple_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 123 |
{ |
| 124 |
PyObject *arg = NULL; |
| 125 |
static char *kwlist[] = {"sequence", 0}; |
| 126 |
|
| 127 |
if (type != &PyTuple_Type) |
| 128 |
return tuple_subtype_new(type, args, kwds); |
| 129 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:tuple", kwlist, &arg)) |
| 130 |
return NULL; |
| 131 |
|
| 132 |
if (arg == NULL) |
| 133 |
return PyTuple_New(0); |
| 134 |
else |
| 135 |
return PySequence_Tuple(arg); |
| 136 |
} |
| 137 |
|
| 138 |
static PyObject * |
| 139 |
tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 140 |
{ |
| 141 |
PyObject *tmp, *newobj, *item; |
| 142 |
Py_ssize_t i, n; |
| 143 |
|
| 144 |
assert(PyType_IsSubtype(type, &PyTuple_Type)); |
| 145 |
tmp = _Py_tuple_new(&PyTuple_Type, args, kwds); |
| 146 |
if (tmp == NULL) |
| 147 |
return NULL; |
| 148 |
assert(PyTuple_Check(tmp)); |
| 149 |
newobj = type->tp_alloc(type, n = PyTuple_GET_SIZE(tmp)); |
| 150 |
if (newobj == NULL) |
| 151 |
return NULL; |
| 152 |
for (i = 0; i < n; i++) { |
| 153 |
item = PyTuple_GET_ITEM(tmp, i); |
| 154 |
Py_INCREF(item); |
| 155 |
PyTuple_SET_ITEM(newobj, i, item); |
| 156 |
} |
| 157 |
Py_DECREF(tmp); |
| 158 |
return newobj; |
| 159 |
} |
| 160 |
|
| 161 |
|