Mercurial > cpython-lru_cache-weakref
annotate Objects/cellobject.c @ 25838:8540887bcec6 2.2
Backport of 2.183:
Fix memory leak in add_subclass() found by NealN with valgrind.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Fri, 18 Oct 2002 13:42:21 +0000 |
| parents | fd48a55ac364 |
| children | 84afa366eee7 f731859e9b3b 81d0e93208d9 |
| rev | line source |
|---|---|
| 16256 | 1 /* Cell object implementation */ |
| 2 | |
| 3 #include "Python.h" | |
| 4 | |
| 5 PyObject * | |
| 6 PyCell_New(PyObject *obj) | |
| 7 { | |
| 8 PyCellObject *op; | |
| 9 | |
|
19581
fd48a55ac364
Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
19476
diff
changeset
|
10 op = (PyCellObject *)PyObject_GC_New(PyCellObject, &PyCell_Type); |
| 16256 | 11 op->ob_ref = obj; |
| 12 Py_XINCREF(obj); | |
| 13 | |
|
19581
fd48a55ac364
Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
19476
diff
changeset
|
14 _PyObject_GC_TRACK(op); |
| 16256 | 15 return (PyObject *)op; |
| 16 } | |
| 17 | |
| 18 PyObject * | |
| 19 PyCell_Get(PyObject *op) | |
| 20 { | |
| 21 if (!PyCell_Check(op)) { | |
| 22 PyErr_BadInternalCall(); | |
| 23 return NULL; | |
| 24 } | |
| 25 Py_XINCREF(((PyCellObject*)op)->ob_ref); | |
| 26 return PyCell_GET(op); | |
| 27 } | |
| 28 | |
| 29 int | |
| 30 PyCell_Set(PyObject *op, PyObject *obj) | |
| 31 { | |
| 32 if (!PyCell_Check(op)) { | |
| 33 PyErr_BadInternalCall(); | |
| 34 return -1; | |
| 35 } | |
| 36 Py_XDECREF(((PyCellObject*)op)->ob_ref); | |
| 37 Py_XINCREF(obj); | |
| 38 PyCell_SET(op, obj); | |
| 39 return 0; | |
| 40 } | |
| 41 | |
| 42 static void | |
| 43 cell_dealloc(PyCellObject *op) | |
| 44 { | |
|
19581
fd48a55ac364
Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
19476
diff
changeset
|
45 _PyObject_GC_UNTRACK(op); |
| 16256 | 46 Py_XDECREF(op->ob_ref); |
|
19581
fd48a55ac364
Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
19476
diff
changeset
|
47 PyObject_GC_Del(op); |
| 16256 | 48 } |
| 49 | |
| 50 static int | |
| 51 cell_compare(PyCellObject *a, PyCellObject *b) | |
| 52 { | |
| 53 if (a->ob_ref == NULL) { | |
| 54 if (b->ob_ref == NULL) | |
| 55 return 0; | |
| 56 return -1; | |
| 57 } else if (b->ob_ref == NULL) | |
| 58 return 1; | |
| 59 return PyObject_Compare(a->ob_ref, b->ob_ref); | |
| 60 } | |
| 61 | |
| 62 static PyObject * | |
| 63 cell_repr(PyCellObject *op) | |
| 64 { | |
|
19476
0e70df98424e
repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents:
18933
diff
changeset
|
65 if (op->ob_ref == NULL) |
|
0e70df98424e
repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents:
18933
diff
changeset
|
66 return PyString_FromFormat("<cell at %p: empty>", op); |
| 16256 | 67 |
|
19476
0e70df98424e
repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents:
18933
diff
changeset
|
68 return PyString_FromFormat("<cell at %p: %.80s object at %p>", |
|
0e70df98424e
repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents:
18933
diff
changeset
|
69 op, op->ob_ref->ob_type->tp_name, |
|
0e70df98424e
repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents:
18933
diff
changeset
|
70 op->ob_ref); |
| 16256 | 71 } |
| 72 | |
| 73 static int | |
| 74 cell_traverse(PyCellObject *op, visitproc visit, void *arg) | |
| 75 { | |
| 76 if (op->ob_ref) | |
| 77 return visit(op->ob_ref, arg); | |
| 78 return 0; | |
| 79 } | |
| 80 | |
| 81 static int | |
| 82 cell_clear(PyCellObject *op) | |
| 83 { | |
|
17034
ba81d79dc3a3
Variety of small INC/DECREF patches that fix reported memory leaks
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16256
diff
changeset
|
84 Py_XDECREF(op->ob_ref); |
| 16256 | 85 op->ob_ref = NULL; |
| 86 return 0; | |
| 87 } | |
| 88 | |
| 89 PyTypeObject PyCell_Type = { | |
| 90 PyObject_HEAD_INIT(&PyType_Type) | |
| 91 0, | |
| 92 "cell", | |
|
19581
fd48a55ac364
Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
19476
diff
changeset
|
93 sizeof(PyCellObject), |
| 16256 | 94 0, |
| 95 (destructor)cell_dealloc, /* tp_dealloc */ | |
| 96 0, /* tp_print */ | |
| 97 0, /* tp_getattr */ | |
| 98 0, /* tp_setattr */ | |
| 99 (cmpfunc)cell_compare, /* tp_compare */ | |
| 100 (reprfunc)cell_repr, /* tp_repr */ | |
| 101 0, /* tp_as_number */ | |
| 102 0, /* tp_as_sequence */ | |
| 103 0, /* tp_as_mapping */ | |
| 104 0, /* tp_hash */ | |
| 105 0, /* tp_call */ | |
| 106 0, /* tp_str */ | |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17034
diff
changeset
|
107 PyObject_GenericGetAttr, /* tp_getattro */ |
| 16256 | 108 0, /* tp_setattro */ |
| 109 0, /* tp_as_buffer */ | |
|
19581
fd48a55ac364
Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
19476
diff
changeset
|
110 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 16256 | 111 0, /* tp_doc */ |
| 112 (traverseproc)cell_traverse, /* tp_traverse */ | |
| 113 (inquiry)cell_clear, /* tp_clear */ | |
| 114 }; |
