Mercurial > cpython-lru_cache-weakref
annotate Objects/cellobject.c @ 58765:2ba7677bfa95 legacy-trunk
testCopysign was defined twice in test_math; combine the definitions
| author | Mark Dickinson <dickinsm@gmail.com> |
|---|---|
| date | Sat, 06 Feb 2010 23:11:25 +0000 |
| parents | 52294af4b663 |
| children | fd25030223b0 |
| 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); |
|
38969
1392561d5dd7
Another problem reported by Coverity. Backport candidate.
Neal Norwitz <nnorwitz@gmail.com>
parents:
37756
diff
changeset
|
11 if (op == NULL) |
|
1392561d5dd7
Another problem reported by Coverity. Backport candidate.
Neal Norwitz <nnorwitz@gmail.com>
parents:
37756
diff
changeset
|
12 return NULL; |
| 16256 | 13 op->ob_ref = obj; |
| 14 Py_XINCREF(obj); | |
| 15 | |
|
19581
fd48a55ac364
Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
19476
diff
changeset
|
16 _PyObject_GC_TRACK(op); |
| 16256 | 17 return (PyObject *)op; |
| 18 } | |
| 19 | |
| 20 PyObject * | |
| 21 PyCell_Get(PyObject *op) | |
| 22 { | |
| 23 if (!PyCell_Check(op)) { | |
| 24 PyErr_BadInternalCall(); | |
| 25 return NULL; | |
| 26 } | |
| 27 Py_XINCREF(((PyCellObject*)op)->ob_ref); | |
| 28 return PyCell_GET(op); | |
| 29 } | |
| 30 | |
| 31 int | |
| 32 PyCell_Set(PyObject *op, PyObject *obj) | |
| 33 { | |
|
45374
270fcd2e9782
Prevent a crash with nested scopes, again caused by calling Py_DECREF when the pointer
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
44010
diff
changeset
|
34 PyObject* oldobj; |
| 16256 | 35 if (!PyCell_Check(op)) { |
| 36 PyErr_BadInternalCall(); | |
| 37 return -1; | |
| 38 } | |
|
45374
270fcd2e9782
Prevent a crash with nested scopes, again caused by calling Py_DECREF when the pointer
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
44010
diff
changeset
|
39 oldobj = PyCell_GET(op); |
| 16256 | 40 Py_XINCREF(obj); |
| 41 PyCell_SET(op, obj); | |
|
45374
270fcd2e9782
Prevent a crash with nested scopes, again caused by calling Py_DECREF when the pointer
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
44010
diff
changeset
|
42 Py_XDECREF(oldobj); |
| 16256 | 43 return 0; |
| 44 } | |
| 45 | |
| 46 static void | |
| 47 cell_dealloc(PyCellObject *op) | |
| 48 { | |
|
19581
fd48a55ac364
Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
19476
diff
changeset
|
49 _PyObject_GC_UNTRACK(op); |
| 16256 | 50 Py_XDECREF(op->ob_ref); |
|
19581
fd48a55ac364
Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
19476
diff
changeset
|
51 PyObject_GC_Del(op); |
| 16256 | 52 } |
| 53 | |
| 54 static int | |
| 55 cell_compare(PyCellObject *a, PyCellObject *b) | |
| 56 { | |
|
45860
50771a3fad2c
Add py3k warnings for object, type, cell and dict comparisons. This should resolve issue2342 and partly resolve issue2373.
Steven Bethard <steven.bethard@gmail.com>
parents:
45374
diff
changeset
|
57 /* Py3K warning for comparisons */ |
|
46573
aef3501d8e77
Use PyErr_WarnPy3k throughout
Benjamin Peterson <benjamin@python.org>
parents:
46076
diff
changeset
|
58 if (PyErr_WarnPy3k("cell comparisons not supported in 3.x", |
|
46578
7670005da8c5
A little reformating of Py3k warnings
Benjamin Peterson <benjamin@python.org>
parents:
46573
diff
changeset
|
59 1) < 0) { |
|
45866
cc758e8ddc9d
cell_compare needs to return -2 instead of NULL.
Steven Bethard <steven.bethard@gmail.com>
parents:
45860
diff
changeset
|
60 return -2; |
|
45860
50771a3fad2c
Add py3k warnings for object, type, cell and dict comparisons. This should resolve issue2342 and partly resolve issue2373.
Steven Bethard <steven.bethard@gmail.com>
parents:
45374
diff
changeset
|
61 } |
|
50771a3fad2c
Add py3k warnings for object, type, cell and dict comparisons. This should resolve issue2342 and partly resolve issue2373.
Steven Bethard <steven.bethard@gmail.com>
parents:
45374
diff
changeset
|
62 |
| 16256 | 63 if (a->ob_ref == NULL) { |
| 64 if (b->ob_ref == NULL) | |
| 65 return 0; | |
| 66 return -1; | |
| 67 } else if (b->ob_ref == NULL) | |
| 68 return 1; | |
| 69 return PyObject_Compare(a->ob_ref, b->ob_ref); | |
| 70 } | |
| 71 | |
| 72 static PyObject * | |
| 73 cell_repr(PyCellObject *op) | |
| 74 { | |
|
19476
0e70df98424e
repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents:
18933
diff
changeset
|
75 if (op->ob_ref == NULL) |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
76 return PyString_FromFormat("<cell at %p: empty>", op); |
| 16256 | 77 |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
78 return PyString_FromFormat("<cell at %p: %.80s object at %p>", |
|
19476
0e70df98424e
repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents:
18933
diff
changeset
|
79 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
|
80 op->ob_ref); |
| 16256 | 81 } |
| 82 | |
| 83 static int | |
| 84 cell_traverse(PyCellObject *op, visitproc visit, void *arg) | |
| 85 { | |
|
37756
7b2e436a4a21
Use Py_VISIT in all tp_traverse methods, instead of traversing manually or
Thomas Wouters <thomas@python.org>
parents:
37748
diff
changeset
|
86 Py_VISIT(op->ob_ref); |
| 16256 | 87 return 0; |
| 88 } | |
| 89 | |
| 90 static int | |
| 91 cell_clear(PyCellObject *op) | |
| 92 { | |
|
37748
d1547426ab61
Use Py_CLEAR instead of in-place DECREF/XDECREF or custom macros, for
Thomas Wouters <thomas@python.org>
parents:
37073
diff
changeset
|
93 Py_CLEAR(op->ob_ref); |
| 16256 | 94 return 0; |
| 95 } | |
| 96 | |
|
37073
81d0e93208d9
Previously, Python code had no easy way to access the contents of a
Georg Brandl <georg@python.org>
parents:
19581
diff
changeset
|
97 static PyObject * |
|
81d0e93208d9
Previously, Python code had no easy way to access the contents of a
Georg Brandl <georg@python.org>
parents:
19581
diff
changeset
|
98 cell_get_contents(PyCellObject *op, void *closure) |
|
81d0e93208d9
Previously, Python code had no easy way to access the contents of a
Georg Brandl <georg@python.org>
parents:
19581
diff
changeset
|
99 { |
|
44010
2183d72abfaa
Issue #1445: Fix a SystemError when accessing the ``cell_contents``
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
42333
diff
changeset
|
100 if (op->ob_ref == NULL) |
|
2183d72abfaa
Issue #1445: Fix a SystemError when accessing the ``cell_contents``
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
42333
diff
changeset
|
101 { |
|
2183d72abfaa
Issue #1445: Fix a SystemError when accessing the ``cell_contents``
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
42333
diff
changeset
|
102 PyErr_SetString(PyExc_ValueError, "Cell is empty"); |
|
2183d72abfaa
Issue #1445: Fix a SystemError when accessing the ``cell_contents``
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
42333
diff
changeset
|
103 return NULL; |
|
2183d72abfaa
Issue #1445: Fix a SystemError when accessing the ``cell_contents``
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
42333
diff
changeset
|
104 } |
|
2183d72abfaa
Issue #1445: Fix a SystemError when accessing the ``cell_contents``
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
42333
diff
changeset
|
105 Py_INCREF(op->ob_ref); |
|
37073
81d0e93208d9
Previously, Python code had no easy way to access the contents of a
Georg Brandl <georg@python.org>
parents:
19581
diff
changeset
|
106 return op->ob_ref; |
|
81d0e93208d9
Previously, Python code had no easy way to access the contents of a
Georg Brandl <georg@python.org>
parents:
19581
diff
changeset
|
107 } |
|
81d0e93208d9
Previously, Python code had no easy way to access the contents of a
Georg Brandl <georg@python.org>
parents:
19581
diff
changeset
|
108 |
|
81d0e93208d9
Previously, Python code had no easy way to access the contents of a
Georg Brandl <georg@python.org>
parents:
19581
diff
changeset
|
109 static PyGetSetDef cell_getsetlist[] = { |
|
81d0e93208d9
Previously, Python code had no easy way to access the contents of a
Georg Brandl <georg@python.org>
parents:
19581
diff
changeset
|
110 {"cell_contents", (getter)cell_get_contents, NULL}, |
|
81d0e93208d9
Previously, Python code had no easy way to access the contents of a
Georg Brandl <georg@python.org>
parents:
19581
diff
changeset
|
111 {NULL} /* sentinel */ |
|
81d0e93208d9
Previously, Python code had no easy way to access the contents of a
Georg Brandl <georg@python.org>
parents:
19581
diff
changeset
|
112 }; |
|
81d0e93208d9
Previously, Python code had no easy way to access the contents of a
Georg Brandl <georg@python.org>
parents:
19581
diff
changeset
|
113 |
| 16256 | 114 PyTypeObject PyCell_Type = { |
|
42333
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
38969
diff
changeset
|
115 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 16256 | 116 "cell", |
|
19581
fd48a55ac364
Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
19476
diff
changeset
|
117 sizeof(PyCellObject), |
| 16256 | 118 0, |
| 119 (destructor)cell_dealloc, /* tp_dealloc */ | |
| 120 0, /* tp_print */ | |
| 121 0, /* tp_getattr */ | |
| 122 0, /* tp_setattr */ | |
| 123 (cmpfunc)cell_compare, /* tp_compare */ | |
| 124 (reprfunc)cell_repr, /* tp_repr */ | |
| 125 0, /* tp_as_number */ | |
| 126 0, /* tp_as_sequence */ | |
| 127 0, /* tp_as_mapping */ | |
| 128 0, /* tp_hash */ | |
| 129 0, /* tp_call */ | |
| 130 0, /* tp_str */ | |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17034
diff
changeset
|
131 PyObject_GenericGetAttr, /* tp_getattro */ |
| 16256 | 132 0, /* tp_setattro */ |
| 133 0, /* tp_as_buffer */ | |
|
19581
fd48a55ac364
Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
19476
diff
changeset
|
134 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 16256 | 135 0, /* tp_doc */ |
| 136 (traverseproc)cell_traverse, /* tp_traverse */ | |
| 137 (inquiry)cell_clear, /* tp_clear */ | |
|
37073
81d0e93208d9
Previously, Python code had no easy way to access the contents of a
Georg Brandl <georg@python.org>
parents:
19581
diff
changeset
|
138 0, /* tp_richcompare */ |
|
81d0e93208d9
Previously, Python code had no easy way to access the contents of a
Georg Brandl <georg@python.org>
parents:
19581
diff
changeset
|
139 0, /* tp_weaklistoffset */ |
|
81d0e93208d9
Previously, Python code had no easy way to access the contents of a
Georg Brandl <georg@python.org>
parents:
19581
diff
changeset
|
140 0, /* tp_iter */ |
|
81d0e93208d9
Previously, Python code had no easy way to access the contents of a
Georg Brandl <georg@python.org>
parents:
19581
diff
changeset
|
141 0, /* tp_iternext */ |
|
81d0e93208d9
Previously, Python code had no easy way to access the contents of a
Georg Brandl <georg@python.org>
parents:
19581
diff
changeset
|
142 0, /* tp_methods */ |
|
81d0e93208d9
Previously, Python code had no easy way to access the contents of a
Georg Brandl <georg@python.org>
parents:
19581
diff
changeset
|
143 0, /* tp_members */ |
|
81d0e93208d9
Previously, Python code had no easy way to access the contents of a
Georg Brandl <georg@python.org>
parents:
19581
diff
changeset
|
144 cell_getsetlist, /* tp_getset */ |
| 16256 | 145 }; |
