📄 listobject.c
字号:
/* Portions Copyright (c) 2005 Nokia Corporation */
/* List object implementation */
#include "Python.h"
#ifdef STDC_HEADERS
#include <stddef.h>
#else
#include <sys/types.h> /* For size_t */
#endif
static int
roundupsize(int n)
{
unsigned int nbits = 0;
unsigned int n2 = (unsigned int)n >> 5;
/* Round up:
* If n < 256, to a multiple of 8.
* If n < 2048, to a multiple of 64.
* If n < 16384, to a multiple of 512.
* If n < 131072, to a multiple of 4096.
* If n < 1048576, to a multiple of 32768.
* If n < 8388608, to a multiple of 262144.
* If n < 67108864, to a multiple of 2097152.
* If n < 536870912, to a multiple of 16777216.
* ...
* If n < 2**(5+3*i), to a multiple of 2**(3*i).
*
* This over-allocates proportional to the list size, making room
* for additional growth. The over-allocation is mild, but is
* enough to give linear-time amortized behavior over a long
* sequence of appends() in the presence of a poorly-performing
* system realloc() (which is a reality, e.g., across all flavors
* of Windows, with Win9x behavior being particularly bad -- and
* we've still got address space fragmentation problems on Win9x
* even with this scheme, although it requires much longer lists to
* provoke them than it used to).
*/
do {
n2 >>= 3;
nbits += 3;
} while (n2);
return ((n >> nbits) + 1) << nbits;
}
#define NRESIZE(var, type, nitems) \
do { \
size_t _new_size = roundupsize(nitems); \
if (_new_size <= ((~(size_t)0) / sizeof(type))) \
PyMem_RESIZE(var, type, _new_size); \
else \
var = NULL; \
} while (0)
DL_EXPORT(PyObject *)
PyList_New(int size)
{
int i;
PyListObject *op;
size_t nbytes;
if (size < 0) {
PyErr_BadInternalCall();
return NULL;
}
nbytes = size * sizeof(PyObject *);
/* Check for overflow */
if (nbytes / sizeof(PyObject *) != (size_t)size) {
return PyErr_NoMemory();
}
op = PyObject_GC_New(PyListObject, &PyList_Type);
if (op == NULL) {
return NULL;
}
if (size <= 0) {
op->ob_item = NULL;
}
else {
op->ob_item = (PyObject **) PyMem_MALLOC(nbytes);
if (op->ob_item == NULL) {
return PyErr_NoMemory();
}
}
op->ob_size = size;
for (i = 0; i < size; i++)
op->ob_item[i] = NULL;
_PyObject_GC_TRACK(op);
return (PyObject *) op;
}
DL_EXPORT(int)
PyList_Size(PyObject *op)
{
if (!PyList_Check(op)) {
PyErr_BadInternalCall();
return -1;
}
else
return ((PyListObject *)op) -> ob_size;
}
#ifndef SYMBIAN
static PyObject *indexerr;
#else
#define indexerr (pyglobals->indexerr)
#endif
DL_EXPORT(PyObject *)
PyList_GetItem(PyObject *op, int i)
{
if (!PyList_Check(op)) {
PyErr_BadInternalCall();
return NULL;
}
if (i < 0 || i >= ((PyListObject *)op) -> ob_size) {
#ifdef SYMBIAN
SPy_Python_globals* pyglobals = PYTHON_GLOBALS; // avoid TLS reads
#endif
if (indexerr == NULL)
indexerr = PyString_FromString(
"list index out of range");
PyErr_SetObject(PyExc_IndexError, indexerr);
return NULL;
}
return ((PyListObject *)op) -> ob_item[i];
}
DL_EXPORT(int)
PyList_SetItem(register PyObject *op, register int i,
register PyObject *newitem)
{
register PyObject *olditem;
register PyObject **p;
if (!PyList_Check(op)) {
Py_XDECREF(newitem);
PyErr_BadInternalCall();
return -1;
}
if (i < 0 || i >= ((PyListObject *)op) -> ob_size) {
Py_XDECREF(newitem);
PyErr_SetString(PyExc_IndexError,
"list assignment index out of range");
return -1;
}
p = ((PyListObject *)op) -> ob_item + i;
olditem = *p;
*p = newitem;
Py_XDECREF(olditem);
return 0;
}
static int
ins1(PyListObject *self, int where, PyObject *v)
{
int i;
PyObject **items;
if (v == NULL) {
PyErr_BadInternalCall();
return -1;
}
if (self->ob_size == INT_MAX) {
PyErr_SetString(PyExc_OverflowError,
"cannot add more objects to list");
return -1;
}
items = self->ob_item;
NRESIZE(items, PyObject *, self->ob_size+1);
if (items == NULL) {
PyErr_NoMemory();
return -1;
}
if (where < 0)
where = 0;
if (where > self->ob_size)
where = self->ob_size;
for (i = self->ob_size; --i >= where; )
items[i+1] = items[i];
Py_INCREF(v);
items[where] = v;
self->ob_item = items;
self->ob_size++;
return 0;
}
DL_EXPORT(int)
PyList_Insert(PyObject *op, int where, PyObject *newitem)
{
if (!PyList_Check(op)) {
PyErr_BadInternalCall();
return -1;
}
return ins1((PyListObject *)op, where, newitem);
}
DL_EXPORT(int)
PyList_Append(PyObject *op, PyObject *newitem)
{
if (!PyList_Check(op)) {
PyErr_BadInternalCall();
return -1;
}
return ins1((PyListObject *)op,
(int) ((PyListObject *)op)->ob_size, newitem);
}
/* Methods */
static void
list_dealloc(PyListObject *op)
{
int i;
PyObject_GC_UnTrack(op);
Py_TRASHCAN_SAFE_BEGIN(op)
if (op->ob_item != NULL) {
/* Do it backwards, for Christian Tismer.
There's a simple test case where somehow this reduces
thrashing when a *very* large list is created and
immediately deleted. */
i = op->ob_size;
while (--i >= 0) {
Py_XDECREF(op->ob_item[i]);
}
PyMem_FREE(op->ob_item);
}
op->ob_type->tp_free((PyObject *)op);
Py_TRASHCAN_SAFE_END(op)
}
static int
list_print(PyListObject *op, FILE *fp, int flags)
{
int i;
i = Py_ReprEnter((PyObject*)op);
if (i != 0) {
if (i < 0)
return i;
fprintf(fp, "[...]");
return 0;
}
fprintf(fp, "[");
for (i = 0; i < op->ob_size; i++) {
if (i > 0)
fprintf(fp, ", ");
if (PyObject_Print(op->ob_item[i], fp, 0) != 0) {
Py_ReprLeave((PyObject *)op);
return -1;
}
}
fprintf(fp, "]");
Py_ReprLeave((PyObject *)op);
return 0;
}
static PyObject *
list_repr(PyListObject *v)
{
int i;
PyObject *s, *temp;
PyObject *pieces = NULL, *result = NULL;
i = Py_ReprEnter((PyObject*)v);
if (i != 0) {
return i > 0 ? PyString_FromString("[...]") : NULL;
}
if (v->ob_size == 0) {
result = PyString_FromString("[]");
goto Done;
}
pieces = PyList_New(0);
if (pieces == NULL)
goto Done;
/* Do repr() on each element. Note that this may mutate the list,
so must refetch the list size on each iteration. */
for (i = 0; i < v->ob_size; ++i) {
int status;
s = PyObject_Repr(v->ob_item[i]);
if (s == NULL)
goto Done;
status = PyList_Append(pieces, s);
Py_DECREF(s); /* append created a new ref */
if (status < 0)
goto Done;
}
/* Add "[]" decorations to the first and last items. */
assert(PyList_GET_SIZE(pieces) > 0);
s = PyString_FromString("[");
if (s == NULL)
goto Done;
temp = PyList_GET_ITEM(pieces, 0);
PyString_ConcatAndDel(&s, temp);
PyList_SET_ITEM(pieces, 0, s);
if (s == NULL)
goto Done;
s = PyString_FromString("]");
if (s == NULL)
goto Done;
temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
PyString_ConcatAndDel(&temp, s);
PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
if (temp == NULL)
goto Done;
/* Paste them all together with ", " between. */
s = PyString_FromString(", ");
if (s == NULL)
goto Done;
result = _PyString_Join(s, pieces);
Py_DECREF(s);
Done:
Py_XDECREF(pieces);
Py_ReprLeave((PyObject *)v);
return result;
}
static int
list_length(PyListObject *a)
{
return a->ob_size;
}
static int
list_contains(PyListObject *a, PyObject *el)
{
int i;
for (i = 0; i < a->ob_size; ++i) {
int cmp = PyObject_RichCompareBool(el, PyList_GET_ITEM(a, i),
Py_EQ);
if (cmp > 0)
return 1;
else if (cmp < 0)
return -1;
}
return 0;
}
static PyObject *
list_item(PyListObject *a, int i)
{
if (i < 0 || i >= a->ob_size) {
#ifdef SYMBIAN
SPy_Python_globals* pyglobals = PYTHON_GLOBALS; // avoid TLS reads
#endif
if (indexerr == NULL)
indexerr = PyString_FromString(
"list index out of range");
PyErr_SetObject(PyExc_IndexError, indexerr);
return NULL;
}
Py_INCREF(a->ob_item[i]);
return a->ob_item[i];
}
static PyObject *
list_slice(PyListObject *a, int ilow, int ihigh)
{
PyListObject *np;
int i;
if (ilow < 0)
ilow = 0;
else if (ilow > a->ob_size)
ilow = a->ob_size;
if (ihigh < ilow)
ihigh = ilow;
else if (ihigh > a->ob_size)
ihigh = a->ob_size;
np = (PyListObject *) PyList_New(ihigh - ilow);
if (np == NULL)
return NULL;
for (i = ilow; i < ihigh; i++) {
PyObject *v = a->ob_item[i];
Py_INCREF(v);
np->ob_item[i - ilow] = v;
}
return (PyObject *)np;
}
DL_EXPORT(PyObject *)
PyList_GetSlice(PyObject *a, int ilow, int ihigh)
{
if (!PyList_Check(a)) {
PyErr_BadInternalCall();
return NULL;
}
return list_slice((PyListObject *)a, ilow, ihigh);
}
static PyObject *
list_concat(PyListObject *a, PyObject *bb)
{
int size;
int i;
PyListObject *np;
if (!PyList_Check(bb)) {
PyErr_Format(PyExc_TypeError,
"can only concatenate list (not \"%.200s\") to list",
bb->ob_type->tp_name);
return NULL;
}
#define b ((PyListObject *)bb)
size = a->ob_size + b->ob_size;
if (size < 0)
return PyErr_NoMemory();
np = (PyListObject *) PyList_New(size);
if (np == NULL) {
return NULL;
}
for (i = 0; i < a->ob_size; i++) {
PyObject *v = a->ob_item[i];
Py_INCREF(v);
np->ob_item[i] = v;
}
for (i = 0; i < b->ob_size; i++) {
PyObject *v = b->ob_item[i];
Py_INCREF(v);
np->ob_item[i + a->ob_size] = v;
}
return (PyObject *)np;
#undef b
}
static PyObject *
list_repeat(PyListObject *a, int n)
{
int i, j;
int size;
PyListObject *np;
PyObject **p;
if (n < 0)
n = 0;
size = a->ob_size * n;
if (n && size/n != a->ob_size)
return PyErr_NoMemory();
np = (PyListObject *) PyList_New(size);
if (np == NULL)
return NULL;
p = np->ob_item;
for (i = 0; i < n; i++) {
for (j = 0; j < a->ob_size; j++) {
*p = a->ob_item[j];
Py_INCREF(*p);
p++;
}
}
return (PyObject *) np;
}
static int
list_ass_slice(PyListObject *a, int ilow, int ihigh, PyObject *v)
{
/* Because [X]DECREF can recursively invoke list operations on
this list, we must postpone all [X]DECREF activity until
after the list is back in its canonical shape. Therefore
we must allocate an additional array, 'recycle', into which
we temporarily copy the items that are deleted from the
list. :-( */
PyObject **recycle, **p;
PyObject **item;
int n; /* Size of replacement list */
int d; /* Change in size */
int k; /* Loop index */
#define b ((PyListObject *)v)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -