Mercurial > cpython-lru_cache-weakref
comparison Modules/arraymodule.c @ 32159:2c169ca5fa69 legacy-trunk
Add weakref support to array.array and file objects.
| author | Raymond Hettinger <python@rcn.com> |
|---|---|
| date | Mon, 31 May 2004 00:35:52 +0000 |
| parents | 35f56e08727f |
| children | 0b181bf230c9 |
comparison
equal
deleted
inserted
replaced
| 32158:679e5c8effae | 32159:2c169ca5fa69 |
|---|---|
| 2 | 2 |
| 3 /* An array is a uniform list -- all items have the same type. | 3 /* An array is a uniform list -- all items have the same type. |
| 4 The item type is restricted to simple C types like int or float */ | 4 The item type is restricted to simple C types like int or float */ |
| 5 | 5 |
| 6 #include "Python.h" | 6 #include "Python.h" |
| 7 #include "structmember.h" | |
| 7 | 8 |
| 8 #ifdef STDC_HEADERS | 9 #ifdef STDC_HEADERS |
| 9 #include <stddef.h> | 10 #include <stddef.h> |
| 10 #else /* !STDC_HEADERS */ | 11 #else /* !STDC_HEADERS */ |
| 11 #ifndef DONT_HAVE_SYS_TYPES_H | 12 #ifndef DONT_HAVE_SYS_TYPES_H |
| 30 PyObject_HEAD | 31 PyObject_HEAD |
| 31 int ob_size; | 32 int ob_size; |
| 32 char *ob_item; | 33 char *ob_item; |
| 33 int allocated; | 34 int allocated; |
| 34 struct arraydescr *ob_descr; | 35 struct arraydescr *ob_descr; |
| 36 PyObject *weakreflist; /* List of weak references */ | |
| 35 } arrayobject; | 37 } arrayobject; |
| 36 | 38 |
| 37 static PyTypeObject Arraytype; | 39 static PyTypeObject Arraytype; |
| 38 | 40 |
| 39 #define array_Check(op) PyObject_TypeCheck(op, &Arraytype) | 41 #define array_Check(op) PyObject_TypeCheck(op, &Arraytype) |
| 440 return PyErr_NoMemory(); | 442 return PyErr_NoMemory(); |
| 441 } | 443 } |
| 442 } | 444 } |
| 443 op->ob_descr = descr; | 445 op->ob_descr = descr; |
| 444 op->allocated = size; | 446 op->allocated = size; |
| 447 op->weakreflist = NULL; | |
| 445 return (PyObject *) op; | 448 return (PyObject *) op; |
| 446 } | 449 } |
| 447 | 450 |
| 448 static PyObject * | 451 static PyObject * |
| 449 getarrayitem(PyObject *op, int i) | 452 getarrayitem(PyObject *op, int i) |
| 488 /* Methods */ | 491 /* Methods */ |
| 489 | 492 |
| 490 static void | 493 static void |
| 491 array_dealloc(arrayobject *op) | 494 array_dealloc(arrayobject *op) |
| 492 { | 495 { |
| 496 if (op->weakreflist != NULL) | |
| 497 PyObject_ClearWeakRefs((PyObject *) op); | |
| 493 if (op->ob_item != NULL) | 498 if (op->ob_item != NULL) |
| 494 PyMem_DEL(op->ob_item); | 499 PyMem_DEL(op->ob_item); |
| 495 op->ob_type->tp_free((PyObject *)op); | 500 op->ob_type->tp_free((PyObject *)op); |
| 496 } | 501 } |
| 497 | 502 |
| 1948 0, /* tp_call */ | 1953 0, /* tp_call */ |
| 1949 0, /* tp_str */ | 1954 0, /* tp_str */ |
| 1950 PyObject_GenericGetAttr, /* tp_getattro */ | 1955 PyObject_GenericGetAttr, /* tp_getattro */ |
| 1951 0, /* tp_setattro */ | 1956 0, /* tp_setattro */ |
| 1952 &array_as_buffer, /* tp_as_buffer*/ | 1957 &array_as_buffer, /* tp_as_buffer*/ |
| 1953 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ | 1958 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */ |
| 1954 arraytype_doc, /* tp_doc */ | 1959 arraytype_doc, /* tp_doc */ |
| 1955 0, /* tp_traverse */ | 1960 0, /* tp_traverse */ |
| 1956 0, /* tp_clear */ | 1961 0, /* tp_clear */ |
| 1957 array_richcompare, /* tp_richcompare */ | 1962 array_richcompare, /* tp_richcompare */ |
| 1958 0, /* tp_weaklistoffset */ | 1963 offsetof(arrayobject, weakreflist), /* tp_weaklistoffset */ |
| 1959 (getiterfunc)array_iter, /* tp_iter */ | 1964 (getiterfunc)array_iter, /* tp_iter */ |
| 1960 0, /* tp_iternext */ | 1965 0, /* tp_iternext */ |
| 1961 array_methods, /* tp_methods */ | 1966 array_methods, /* tp_methods */ |
| 1962 0, /* tp_members */ | 1967 0, /* tp_members */ |
| 1963 array_getsets, /* tp_getset */ | 1968 array_getsets, /* tp_getset */ |
