LEFT | RIGHT |
1 | 1 |
2 /* Generic object operations; and implementation of None (NoObject) */ | 2 /* Generic object operations; and implementation of None (NoObject) */ |
3 | 3 |
4 #include "Python.h" | 4 #include "Python.h" |
5 #include "sliceobject.h" /* For PyEllipsis_Type */ | 5 #include "sliceobject.h" /* For PyEllipsis_Type */ |
6 #include "frameobject.h" | 6 #include "frameobject.h" |
7 | 7 |
8 #ifdef __cplusplus | 8 #ifdef __cplusplus |
9 extern "C" { | 9 extern "C" { |
10 #endif | 10 #endif |
11 | 11 |
12 #ifdef Py_REF_DEBUG | 12 #ifdef Py_REF_DEBUG |
13 Py_ssize_t _Py_RefTotal; | 13 Py_ssize_t _Py_RefTotal; |
14 | 14 |
15 Py_ssize_t | 15 Py_ssize_t |
16 _Py_GetRefTotal(void) | 16 _Py_GetRefTotal(void) |
17 { | 17 { |
18 » PyObject *o; | 18 PyObject *o; |
19 » Py_ssize_t total = _Py_RefTotal; | 19 Py_ssize_t total = _Py_RefTotal; |
20 /* ignore the references to the dummy object of the dicts and sets | 20 /* ignore the references to the dummy object of the dicts and sets |
21 because they are not reliable and not useful (now that the | 21 because they are not reliable and not useful (now that the |
22 hash table code is well-tested) */ | 22 hash table code is well-tested) */ |
23 » o = _PyDict_Dummy(); | 23 o = _PyDict_Dummy(); |
24 » if (o != NULL) | 24 if (o != NULL) |
25 » » total -= o->ob_refcnt; | 25 total -= o->ob_refcnt; |
26 » o = _PySet_Dummy(); | 26 o = _PySet_Dummy(); |
27 » if (o != NULL) | 27 if (o != NULL) |
28 » » total -= o->ob_refcnt; | 28 total -= o->ob_refcnt; |
29 » return total; | 29 return total; |
30 } | 30 } |
31 #endif /* Py_REF_DEBUG */ | 31 #endif /* Py_REF_DEBUG */ |
32 | 32 |
33 int Py_DivisionWarningFlag; | 33 int Py_DivisionWarningFlag; |
34 | 34 |
35 /* Object allocation routines used by NEWOBJ and NEWVAROBJ macros. | 35 /* Object allocation routines used by NEWOBJ and NEWVAROBJ macros. |
36 These are used by the individual routines for object creation. | 36 These are used by the individual routines for object creation. |
37 Do not call them otherwise, they do not initialize the object! */ | 37 Do not call them otherwise, they do not initialize the object! */ |
38 | 38 |
39 #ifdef Py_TRACE_REFS | 39 #ifdef Py_TRACE_REFS |
(...skipping 11 matching lines...) Expand all Loading... |
51 * relinking it into the front. | 51 * relinking it into the front. |
52 * Note that objects are normally added to the list via _Py_NewReference, | 52 * Note that objects are normally added to the list via _Py_NewReference, |
53 * which is called by PyObject_Init. Not all objects are initialized that | 53 * which is called by PyObject_Init. Not all objects are initialized that |
54 * way, though; exceptions include statically allocated type objects, and | 54 * way, though; exceptions include statically allocated type objects, and |
55 * statically allocated singletons (like Py_True and Py_None). | 55 * statically allocated singletons (like Py_True and Py_None). |
56 */ | 56 */ |
57 void | 57 void |
58 _Py_AddToAllObjects(PyObject *op, int force) | 58 _Py_AddToAllObjects(PyObject *op, int force) |
59 { | 59 { |
60 #ifdef Py_DEBUG | 60 #ifdef Py_DEBUG |
61 » if (!force) { | 61 if (!force) { |
62 » » /* If it's initialized memory, op must be in or out of | 62 /* If it's initialized memory, op must be in or out of |
63 » » * the list unambiguously. | 63 * the list unambiguously. |
64 » » */ | 64 */ |
65 » » assert((op->_ob_prev == NULL) == (op->_ob_next == NULL)); | 65 assert((op->_ob_prev == NULL) == (op->_ob_next == NULL)); |
66 » } | 66 } |
67 #endif | 67 #endif |
68 » if (force || op->_ob_prev == NULL) { | 68 if (force || op->_ob_prev == NULL) { |
69 » » op->_ob_next = refchain._ob_next; | 69 op->_ob_next = refchain._ob_next; |
70 » » op->_ob_prev = &refchain; | 70 op->_ob_prev = &refchain; |
71 » » refchain._ob_next->_ob_prev = op; | 71 refchain._ob_next->_ob_prev = op; |
72 » » refchain._ob_next = op; | 72 refchain._ob_next = op; |
73 » } | 73 } |
74 } | 74 } |
75 #endif» /* Py_TRACE_REFS */ | 75 #endif /* Py_TRACE_REFS */ |
76 | 76 |
77 #ifdef COUNT_ALLOCS | 77 #ifdef COUNT_ALLOCS |
78 static PyTypeObject *type_list; | 78 static PyTypeObject *type_list; |
79 /* All types are added to type_list, at least when | 79 /* All types are added to type_list, at least when |
80 they get one object created. That makes them | 80 they get one object created. That makes them |
81 immortal, which unfortunately contributes to | 81 immortal, which unfortunately contributes to |
82 garbage itself. If unlist_types_without_objects | 82 garbage itself. If unlist_types_without_objects |
83 is set, they will be removed from the type_list | 83 is set, they will be removed from the type_list |
84 once the last object is deallocated. */ | 84 once the last object is deallocated. */ |
85 static int unlist_types_without_objects; | 85 static int unlist_types_without_objects; |
86 extern Py_ssize_t tuple_zero_allocs, fast_tuple_allocs; | 86 extern Py_ssize_t tuple_zero_allocs, fast_tuple_allocs; |
87 extern Py_ssize_t quick_int_allocs, quick_neg_int_allocs; | 87 extern Py_ssize_t quick_int_allocs, quick_neg_int_allocs; |
88 extern Py_ssize_t null_strings, one_strings; | 88 extern Py_ssize_t null_strings, one_strings; |
89 void | 89 void |
90 dump_counts(FILE* f) | 90 dump_counts(FILE* f) |
91 { | 91 { |
92 » PyTypeObject *tp; | 92 PyTypeObject *tp; |
93 | 93 |
94 » for (tp = type_list; tp; tp = tp->tp_next) | 94 for (tp = type_list; tp; tp = tp->tp_next) |
95 » » fprintf(f, "%s alloc'd: %" PY_FORMAT_SIZE_T "d, " | 95 fprintf(f, "%s alloc'd: %" PY_FORMAT_SIZE_T "d, " |
96 » » » "freed: %" PY_FORMAT_SIZE_T "d, " | 96 "freed: %" PY_FORMAT_SIZE_T "d, " |
97 » » » "max in use: %" PY_FORMAT_SIZE_T "d\n", | 97 "max in use: %" PY_FORMAT_SIZE_T "d\n", |
98 » » » tp->tp_name, tp->tp_allocs, tp->tp_frees, | 98 tp->tp_name, tp->tp_allocs, tp->tp_frees, |
99 » » » tp->tp_maxalloc); | 99 tp->tp_maxalloc); |
100 » fprintf(f, "fast tuple allocs: %" PY_FORMAT_SIZE_T "d, " | 100 fprintf(f, "fast tuple allocs: %" PY_FORMAT_SIZE_T "d, " |
101 » » "empty: %" PY_FORMAT_SIZE_T "d\n", | 101 "empty: %" PY_FORMAT_SIZE_T "d\n", |
102 » » fast_tuple_allocs, tuple_zero_allocs); | 102 fast_tuple_allocs, tuple_zero_allocs); |
103 » fprintf(f, "fast int allocs: pos: %" PY_FORMAT_SIZE_T "d, " | 103 fprintf(f, "fast int allocs: pos: %" PY_FORMAT_SIZE_T "d, " |
104 » » "neg: %" PY_FORMAT_SIZE_T "d\n", | 104 "neg: %" PY_FORMAT_SIZE_T "d\n", |
105 » » quick_int_allocs, quick_neg_int_allocs); | 105 quick_int_allocs, quick_neg_int_allocs); |
106 » fprintf(f, "null strings: %" PY_FORMAT_SIZE_T "d, " | 106 fprintf(f, "null strings: %" PY_FORMAT_SIZE_T "d, " |
107 » » "1-strings: %" PY_FORMAT_SIZE_T "d\n", | 107 "1-strings: %" PY_FORMAT_SIZE_T "d\n", |
108 » » null_strings, one_strings); | 108 null_strings, one_strings); |
109 } | 109 } |
110 | 110 |
111 PyObject * | 111 PyObject * |
112 get_counts(void) | 112 get_counts(void) |
113 { | 113 { |
114 » PyTypeObject *tp; | 114 PyTypeObject *tp; |
115 » PyObject *result; | 115 PyObject *result; |
116 » PyObject *v; | 116 PyObject *v; |
117 | 117 |
118 » result = PyList_New(0); | 118 result = PyList_New(0); |
119 » if (result == NULL) | 119 if (result == NULL) |
120 » » return NULL; | 120 return NULL; |
121 » for (tp = type_list; tp; tp = tp->tp_next) { | 121 for (tp = type_list; tp; tp = tp->tp_next) { |
122 » » v = Py_BuildValue("(snnn)", tp->tp_name, tp->tp_allocs, | 122 v = Py_BuildValue("(snnn)", tp->tp_name, tp->tp_allocs, |
123 » » » » tp->tp_frees, tp->tp_maxalloc); | 123 tp->tp_frees, tp->tp_maxalloc); |
124 » » if (v == NULL) { | 124 if (v == NULL) { |
125 » » » Py_DECREF(result); | 125 Py_DECREF(result); |
126 » » » return NULL; | 126 return NULL; |
127 » » } | 127 } |
128 » » if (PyList_Append(result, v) < 0) { | 128 if (PyList_Append(result, v) < 0) { |
129 » » » Py_DECREF(v); | 129 Py_DECREF(v); |
130 » » » Py_DECREF(result); | 130 Py_DECREF(result); |
131 » » » return NULL; | 131 return NULL; |
132 » » } | 132 } |
133 » » Py_DECREF(v); | 133 Py_DECREF(v); |
134 » } | 134 } |
135 » return result; | 135 return result; |
136 } | 136 } |
137 | 137 |
138 void | 138 void |
139 inc_count(PyTypeObject *tp) | 139 inc_count(PyTypeObject *tp) |
140 { | 140 { |
141 » if (tp->tp_next == NULL && tp->tp_prev == NULL) { | 141 if (tp->tp_next == NULL && tp->tp_prev == NULL) { |
142 » » /* first time; insert in linked list */ | 142 /* first time; insert in linked list */ |
143 » » if (tp->tp_next != NULL) /* sanity check */ | 143 if (tp->tp_next != NULL) /* sanity check */ |
144 » » » Py_FatalError("XXX inc_count sanity check"); | 144 Py_FatalError("XXX inc_count sanity check"); |
145 » » if (type_list) | 145 if (type_list) |
146 » » » type_list->tp_prev = tp; | 146 type_list->tp_prev = tp; |
147 » » tp->tp_next = type_list; | 147 tp->tp_next = type_list; |
148 » » /* Note that as of Python 2.2, heap-allocated type objects | 148 /* Note that as of Python 2.2, heap-allocated type objects |
149 » » * can go away, but this code requires that they stay alive | 149 * can go away, but this code requires that they stay alive |
150 » » * until program exit. That's why we're careful with | 150 * until program exit. That's why we're careful with |
151 » » * refcounts here. type_list gets a new reference to tp, | 151 * refcounts here. type_list gets a new reference to tp, |
152 » » * while ownership of the reference type_list used to hold | 152 * while ownership of the reference type_list used to hold |
153 » » * (if any) was transferred to tp->tp_next in the line above. | 153 * (if any) was transferred to tp->tp_next in the line above. |
154 » » * tp is thus effectively immortal after this. | 154 * tp is thus effectively immortal after this. |
155 » » */ | 155 */ |
156 » » Py_INCREF(tp); | 156 Py_INCREF(tp); |
157 » » type_list = tp; | 157 type_list = tp; |
158 #ifdef Py_TRACE_REFS | 158 #ifdef Py_TRACE_REFS |
159 » » /* Also insert in the doubly-linked list of all objects, | 159 /* Also insert in the doubly-linked list of all objects, |
160 » » * if not already there. | 160 * if not already there. |
161 » » */ | 161 */ |
162 » » _Py_AddToAllObjects((PyObject *)tp, 0); | 162 _Py_AddToAllObjects((PyObject *)tp, 0); |
163 #endif | 163 #endif |
164 » } | 164 } |
165 » tp->tp_allocs++; | 165 tp->tp_allocs++; |
166 » if (tp->tp_allocs - tp->tp_frees > tp->tp_maxalloc) | 166 if (tp->tp_allocs - tp->tp_frees > tp->tp_maxalloc) |
167 » » tp->tp_maxalloc = tp->tp_allocs - tp->tp_frees; | 167 tp->tp_maxalloc = tp->tp_allocs - tp->tp_frees; |
168 } | 168 } |
169 | 169 |
170 void dec_count(PyTypeObject *tp) | 170 void dec_count(PyTypeObject *tp) |
171 { | 171 { |
172 » tp->tp_frees++; | 172 tp->tp_frees++; |
173 » if (unlist_types_without_objects && | 173 if (unlist_types_without_objects && |
174 » tp->tp_allocs == tp->tp_frees) { | 174 tp->tp_allocs == tp->tp_frees) { |
175 » » /* unlink the type from type_list */ | 175 /* unlink the type from type_list */ |
176 » » if (tp->tp_prev) | 176 if (tp->tp_prev) |
177 » » » tp->tp_prev->tp_next = tp->tp_next; | 177 tp->tp_prev->tp_next = tp->tp_next; |
178 » » else | 178 else |
179 » » » type_list = tp->tp_next; | 179 type_list = tp->tp_next; |
180 » » if (tp->tp_next) | 180 if (tp->tp_next) |
181 » » » tp->tp_next->tp_prev = tp->tp_prev; | 181 tp->tp_next->tp_prev = tp->tp_prev; |
182 » » tp->tp_next = tp->tp_prev = NULL; | 182 tp->tp_next = tp->tp_prev = NULL; |
183 » » Py_DECREF(tp); | 183 Py_DECREF(tp); |
184 » } | 184 } |
185 } | 185 } |
186 | 186 |
187 #endif | 187 #endif |
188 | 188 |
189 #ifdef Py_REF_DEBUG | 189 #ifdef Py_REF_DEBUG |
190 /* Log a fatal error; doesn't return. */ | 190 /* Log a fatal error; doesn't return. */ |
191 void | 191 void |
192 _Py_NegativeRefcount(const char *fname, int lineno, PyObject *op) | 192 _Py_NegativeRefcount(const char *fname, int lineno, PyObject *op) |
193 { | 193 { |
194 » char buf[300]; | 194 char buf[300]; |
195 | 195 |
196 » PyOS_snprintf(buf, sizeof(buf), | 196 PyOS_snprintf(buf, sizeof(buf), |
197 » » "%s:%i object at %p has negative ref count " | 197 "%s:%i object at %p has negative ref count " |
198 » » "%" PY_FORMAT_SIZE_T "d", | 198 "%" PY_FORMAT_SIZE_T "d", |
199 » » fname, lineno, op, op->ob_refcnt); | 199 fname, lineno, op, op->ob_refcnt); |
200 » Py_FatalError(buf); | 200 Py_FatalError(buf); |
201 } | 201 } |
202 | 202 |
203 #endif /* Py_REF_DEBUG */ | 203 #endif /* Py_REF_DEBUG */ |
204 | 204 |
205 void | 205 void |
206 Py_IncRef(PyObject *o) | 206 Py_IncRef(PyObject *o) |
207 { | 207 { |
208 Py_XINCREF(o); | 208 Py_XINCREF(o); |
209 } | 209 } |
210 | 210 |
211 void | 211 void |
212 Py_DecRef(PyObject *o) | 212 Py_DecRef(PyObject *o) |
213 { | 213 { |
214 Py_XDECREF(o); | 214 Py_XDECREF(o); |
215 } | 215 } |
216 | 216 |
217 PyObject * | 217 PyObject * |
218 PyObject_Init(PyObject *op, PyTypeObject *tp) | 218 PyObject_Init(PyObject *op, PyTypeObject *tp) |
219 { | 219 { |
220 » if (op == NULL) | 220 if (op == NULL) |
221 » » return PyErr_NoMemory(); | 221 return PyErr_NoMemory(); |
222 » /* Any changes should be reflected in PyObject_INIT (objimpl.h) */ | 222 /* Any changes should be reflected in PyObject_INIT (objimpl.h) */ |
223 » Py_TYPE(op) = tp; | 223 Py_TYPE(op) = tp; |
224 » _Py_NewReference(op); | 224 _Py_NewReference(op); |
225 » return op; | 225 return op; |
226 } | 226 } |
227 | 227 |
228 PyVarObject * | 228 PyVarObject * |
229 PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, Py_ssize_t size) | 229 PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, Py_ssize_t size) |
230 { | 230 { |
231 » if (op == NULL) | 231 if (op == NULL) |
232 » » return (PyVarObject *) PyErr_NoMemory(); | 232 return (PyVarObject *) PyErr_NoMemory(); |
233 » /* Any changes should be reflected in PyObject_INIT_VAR */ | 233 /* Any changes should be reflected in PyObject_INIT_VAR */ |
234 » op->ob_size = size; | 234 op->ob_size = size; |
235 » Py_TYPE(op) = tp; | 235 Py_TYPE(op) = tp; |
236 » _Py_NewReference((PyObject *)op); | 236 _Py_NewReference((PyObject *)op); |
237 » return op; | 237 return op; |
238 } | 238 } |
239 | 239 |
240 PyObject * | 240 PyObject * |
241 _PyObject_New(PyTypeObject *tp) | 241 _PyObject_New(PyTypeObject *tp) |
242 { | 242 { |
243 » PyObject *op; | 243 PyObject *op; |
244 » op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp)); | 244 op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp)); |
245 » if (op == NULL) | 245 if (op == NULL) |
246 » » return PyErr_NoMemory(); | 246 return PyErr_NoMemory(); |
247 » return PyObject_INIT(op, tp); | 247 return PyObject_INIT(op, tp); |
248 } | 248 } |
249 | 249 |
250 PyVarObject * | 250 PyVarObject * |
251 _PyObject_NewVar(PyTypeObject *tp, Py_ssize_t nitems) | 251 _PyObject_NewVar(PyTypeObject *tp, Py_ssize_t nitems) |
252 { | 252 { |
253 » PyVarObject *op; | 253 PyVarObject *op; |
254 » const size_t size = _PyObject_VAR_SIZE(tp, nitems); | 254 const size_t size = _PyObject_VAR_SIZE(tp, nitems); |
255 » op = (PyVarObject *) PyObject_MALLOC(size); | 255 op = (PyVarObject *) PyObject_MALLOC(size); |
256 » if (op == NULL) | 256 if (op == NULL) |
257 » » return (PyVarObject *)PyErr_NoMemory(); | 257 return (PyVarObject *)PyErr_NoMemory(); |
258 » return PyObject_INIT_VAR(op, tp, nitems); | 258 return PyObject_INIT_VAR(op, tp, nitems); |
259 } | 259 } |
260 | 260 |
261 /* Implementation of PyObject_Print with recursion checking */ | 261 /* Implementation of PyObject_Print with recursion checking */ |
262 static int | 262 static int |
263 internal_print(PyObject *op, FILE *fp, int flags, int nesting) | 263 internal_print(PyObject *op, FILE *fp, int flags, int nesting) |
264 { | 264 { |
265 » int ret = 0; | 265 int ret = 0; |
266 » if (nesting > 10) { | 266 if (nesting > 10) { |
267 » » PyErr_SetString(PyExc_RuntimeError, "print recursion"); | 267 PyErr_SetString(PyExc_RuntimeError, "print recursion"); |
268 » » return -1; | 268 return -1; |
269 » } | 269 } |
270 » if (PyErr_CheckSignals()) | 270 if (PyErr_CheckSignals()) |
271 » » return -1; | 271 return -1; |
272 #ifdef USE_STACKCHECK | 272 #ifdef USE_STACKCHECK |
273 » if (PyOS_CheckStack()) { | 273 if (PyOS_CheckStack()) { |
274 » » PyErr_SetString(PyExc_MemoryError, "stack overflow"); | 274 PyErr_SetString(PyExc_MemoryError, "stack overflow"); |
275 » » return -1; | 275 return -1; |
276 » } | 276 } |
277 #endif | 277 #endif |
278 » clearerr(fp); /* Clear any previous error condition */ | 278 clearerr(fp); /* Clear any previous error condition */ |
279 » if (op == NULL) { | 279 if (op == NULL) { |
280 » » Py_BEGIN_ALLOW_THREADS | 280 Py_BEGIN_ALLOW_THREADS |
281 » » fprintf(fp, "<nil>"); | 281 fprintf(fp, "<nil>"); |
282 » » Py_END_ALLOW_THREADS | 282 Py_END_ALLOW_THREADS |
283 » } | 283 } |
284 » else { | 284 else { |
285 » » if (op->ob_refcnt <= 0) | 285 if (op->ob_refcnt <= 0) |
286 » » » /* XXX(twouters) cast refcount to long until %zd is | 286 /* XXX(twouters) cast refcount to long until %zd is |
287 » » » universally available */ | 287 universally available */ |
288 » » » Py_BEGIN_ALLOW_THREADS | 288 Py_BEGIN_ALLOW_THREADS |
289 » » » fprintf(fp, "<refcnt %ld at %p>", | 289 fprintf(fp, "<refcnt %ld at %p>", |
290 » » » » (long)op->ob_refcnt, op); | 290 (long)op->ob_refcnt, op); |
291 » » » Py_END_ALLOW_THREADS | 291 Py_END_ALLOW_THREADS |
292 » » else { | 292 else { |
293 » » » PyObject *s; | 293 PyObject *s; |
294 » » » if (flags & Py_PRINT_RAW) | 294 if (flags & Py_PRINT_RAW) |
295 » » » » s = PyObject_Str(op); | 295 s = PyObject_Str(op); |
296 » » » else | 296 else |
297 » » » » s = PyObject_Repr(op); | 297 s = PyObject_Repr(op); |
298 » » » if (s == NULL) | 298 if (s == NULL) |
299 » » » » ret = -1; | 299 ret = -1; |
300 else if (PyBytes_Check(s)) { | 300 else if (PyBytes_Check(s)) { |
301 » » » » fwrite(PyBytes_AS_STRING(s), 1, | 301 fwrite(PyBytes_AS_STRING(s), 1, |
302 » » » » PyBytes_GET_SIZE(s), fp); | 302 PyBytes_GET_SIZE(s), fp); |
303 » » » } | 303 } |
304 » » » else if (PyUnicode_Check(s)) { | 304 else if (PyUnicode_Check(s)) { |
305 » » » » PyObject *t; | 305 PyObject *t; |
306 » » » » t = _PyUnicode_AsDefaultEncodedString(s, NULL); | 306 t = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(s), |
307 » » » » if (t == NULL) | 307 PyUnicode_GET_SIZE(s), |
308 » » » » » ret = 0; | 308 "backslashreplace"); |
309 » » » » else { | 309 if (t == NULL) |
310 » » » » » fwrite(PyBytes_AS_STRING(t), 1, | 310 ret = 0; |
311 » » » » » PyBytes_GET_SIZE(t), fp); | 311 else { |
312 » » » » } | 312 fwrite(PyBytes_AS_STRING(t), 1, |
313 » » » } | 313 PyBytes_GET_SIZE(t), fp); |
314 » » » else { | 314 Py_DECREF(t); |
315 » » » » PyErr_Format(PyExc_TypeError, | 315 } |
316 » » » » » "str() or repr() returned '%.100s'"
, | 316 } |
317 » » » » » s->ob_type->tp_name); | 317 else { |
318 » » » » ret = -1; | 318 PyErr_Format(PyExc_TypeError, |
319 » » » } | 319 "str() or repr() returned '%.100s'", |
320 » » » Py_XDECREF(s); | 320 s->ob_type->tp_name); |
321 » » } | 321 ret = -1; |
322 » } | 322 } |
323 » if (ret == 0) { | 323 Py_XDECREF(s); |
324 » » if (ferror(fp)) { | 324 } |
325 » » » PyErr_SetFromErrno(PyExc_IOError); | 325 } |
326 » » » clearerr(fp); | 326 if (ret == 0) { |
327 » » » ret = -1; | 327 if (ferror(fp)) { |
328 » » } | 328 PyErr_SetFromErrno(PyExc_IOError); |
329 » } | 329 clearerr(fp); |
330 » return ret; | 330 ret = -1; |
| 331 } |
| 332 } |
| 333 return ret; |
331 } | 334 } |
332 | 335 |
333 int | 336 int |
334 PyObject_Print(PyObject *op, FILE *fp, int flags) | 337 PyObject_Print(PyObject *op, FILE *fp, int flags) |
335 { | 338 { |
336 » return internal_print(op, fp, flags, 0); | 339 return internal_print(op, fp, flags, 0); |
337 } | 340 } |
338 | 341 |
339 /* For debugging convenience. Set a breakpoint here and call it from your DLL *
/ | 342 /* For debugging convenience. Set a breakpoint here and call it from your DLL *
/ |
340 void | 343 void |
341 _Py_BreakPoint(void) | 344 _Py_BreakPoint(void) |
342 { | 345 { |
343 } | 346 } |
344 | 347 |
345 | 348 |
346 /* For debugging convenience. See Misc/gdbinit for some useful gdb hooks */ | 349 /* For debugging convenience. See Misc/gdbinit for some useful gdb hooks */ |
347 void | 350 void |
348 _PyObject_Dump(PyObject* op) | 351 _PyObject_Dump(PyObject* op) |
349 { | 352 { |
350 » if (op == NULL) | 353 if (op == NULL) |
351 » » fprintf(stderr, "NULL\n"); | 354 fprintf(stderr, "NULL\n"); |
352 » else { | 355 else { |
353 #ifdef WITH_THREAD | 356 #ifdef WITH_THREAD |
354 » » PyGILState_STATE gil; | 357 PyGILState_STATE gil; |
355 #endif | 358 #endif |
356 » » fprintf(stderr, "object : "); | 359 fprintf(stderr, "object : "); |
357 #ifdef WITH_THREAD | 360 #ifdef WITH_THREAD |
358 » » gil = PyGILState_Ensure(); | 361 gil = PyGILState_Ensure(); |
359 #endif | 362 #endif |
360 » » (void)PyObject_Print(op, stderr, 0); | 363 (void)PyObject_Print(op, stderr, 0); |
361 #ifdef WITH_THREAD | 364 #ifdef WITH_THREAD |
362 » » PyGILState_Release(gil); | 365 PyGILState_Release(gil); |
363 #endif | 366 #endif |
364 » » /* XXX(twouters) cast refcount to long until %zd is | 367 /* XXX(twouters) cast refcount to long until %zd is |
365 » » universally available */ | 368 universally available */ |
366 » » fprintf(stderr, "\n" | 369 fprintf(stderr, "\n" |
367 » » » "type : %s\n" | 370 "type : %s\n" |
368 » » » "refcount: %ld\n" | 371 "refcount: %ld\n" |
369 » » » "address : %p\n", | 372 "address : %p\n", |
370 » » » Py_TYPE(op)==NULL ? "NULL" : Py_TYPE(op)->tp_name, | 373 Py_TYPE(op)==NULL ? "NULL" : Py_TYPE(op)->tp_name, |
371 » » » (long)op->ob_refcnt, | 374 (long)op->ob_refcnt, |
372 » » » op); | 375 op); |
373 » } | 376 } |
374 } | 377 } |
375 | 378 |
376 PyObject * | 379 PyObject * |
377 PyObject_Repr(PyObject *v) | 380 PyObject_Repr(PyObject *v) |
378 { | 381 { |
379 » PyObject *res; | 382 PyObject *res; |
380 » if (PyErr_CheckSignals()) | 383 if (PyErr_CheckSignals()) |
381 » » return NULL; | 384 return NULL; |
382 #ifdef USE_STACKCHECK | 385 #ifdef USE_STACKCHECK |
383 » if (PyOS_CheckStack()) { | 386 if (PyOS_CheckStack()) { |
384 » » PyErr_SetString(PyExc_MemoryError, "stack overflow"); | 387 PyErr_SetString(PyExc_MemoryError, "stack overflow"); |
385 » » return NULL; | 388 return NULL; |
386 » } | 389 } |
387 #endif | 390 #endif |
388 » if (v == NULL) | 391 if (v == NULL) |
389 » » return PyUnicode_FromString("<NULL>"); | 392 return PyUnicode_FromString("<NULL>"); |
390 » if (Py_TYPE(v)->tp_repr == NULL) | 393 if (Py_TYPE(v)->tp_repr == NULL) |
391 » » return PyUnicode_FromFormat("<%s object at %p>", | 394 return PyUnicode_FromFormat("<%s object at %p>", |
392 v->ob_type->tp_name, v); | 395 v->ob_type->tp_name, v); |
393 res = (*v->ob_type->tp_repr)(v); | 396 res = (*v->ob_type->tp_repr)(v); |
394 if (res != NULL && !PyUnicode_Check(res)) { | 397 if (res != NULL && !PyUnicode_Check(res)) { |
395 » » PyErr_Format(PyExc_TypeError, | 398 PyErr_Format(PyExc_TypeError, |
396 » » » "__repr__ returned non-string (type %.200s)", | 399 "__repr__ returned non-string (type %.200s)", |
397 » » » res->ob_type->tp_name); | 400 res->ob_type->tp_name); |
398 » » Py_DECREF(res); | 401 Py_DECREF(res); |
399 » » return NULL; | 402 return NULL; |
400 } | 403 } |
401 return res; | 404 return res; |
402 } | 405 } |
403 | 406 |
404 PyObject * | 407 PyObject * |
405 PyObject_Str(PyObject *v) | 408 PyObject_Str(PyObject *v) |
406 { | 409 { |
407 » PyObject *res; | 410 PyObject *res; |
408 » if (PyErr_CheckSignals()) | 411 if (PyErr_CheckSignals()) |
409 » » return NULL; | 412 return NULL; |
410 #ifdef USE_STACKCHECK | 413 #ifdef USE_STACKCHECK |
411 » if (PyOS_CheckStack()) { | 414 if (PyOS_CheckStack()) { |
412 » » PyErr_SetString(PyExc_MemoryError, "stack overflow"); | 415 PyErr_SetString(PyExc_MemoryError, "stack overflow"); |
413 » » return NULL; | 416 return NULL; |
414 » } | 417 } |
415 #endif | 418 #endif |
416 » if (v == NULL) | 419 if (v == NULL) |
417 » » return PyUnicode_FromString("<NULL>"); | 420 return PyUnicode_FromString("<NULL>"); |
418 » if (PyUnicode_CheckExact(v)) { | 421 if (PyUnicode_CheckExact(v)) { |
419 » » Py_INCREF(v); | 422 Py_INCREF(v); |
420 » » return v; | 423 return v; |
421 » } | 424 } |
422 » if (Py_TYPE(v)->tp_str == NULL) | 425 if (Py_TYPE(v)->tp_str == NULL) |
423 » » return PyObject_Repr(v); | 426 return PyObject_Repr(v); |
424 | 427 |
425 » /* It is possible for a type to have a tp_str representation that loops | 428 /* It is possible for a type to have a tp_str representation that loops |
426 » infinitely. */ | 429 infinitely. */ |
427 » if (Py_EnterRecursiveCall(" while getting the str of an object")) | 430 if (Py_EnterRecursiveCall(" while getting the str of an object")) |
428 » » return NULL; | 431 return NULL; |
429 » res = (*Py_TYPE(v)->tp_str)(v); | 432 res = (*Py_TYPE(v)->tp_str)(v); |
430 » Py_LeaveRecursiveCall(); | 433 Py_LeaveRecursiveCall(); |
431 » if (res == NULL) | 434 if (res == NULL) |
432 » » return NULL; | 435 return NULL; |
433 » if (!PyUnicode_Check(res)) { | 436 if (!PyUnicode_Check(res)) { |
434 » » PyErr_Format(PyExc_TypeError, | 437 PyErr_Format(PyExc_TypeError, |
435 » » » "__str__ returned non-string (type %.200s)", | 438 "__str__ returned non-string (type %.200s)", |
436 » » » Py_TYPE(res)->tp_name); | 439 Py_TYPE(res)->tp_name); |
437 » » Py_DECREF(res); | 440 Py_DECREF(res); |
438 » » return NULL; | 441 return NULL; |
439 » } | 442 } |
440 » return res; | 443 return res; |
441 } | 444 } |
442 | 445 |
443 PyObject * | 446 PyObject * |
444 PyObject_ASCII(PyObject *v) | 447 PyObject_ASCII(PyObject *v) |
445 { | 448 { |
446 » PyObject *repr, *ascii, *res; | 449 PyObject *repr, *ascii, *res; |
447 »······· | 450 |
448 » repr = PyObject_Repr(v); | 451 repr = PyObject_Repr(v); |
449 » if (repr == NULL) | 452 if (repr == NULL) |
450 » » return NULL; | 453 return NULL; |
451 | 454 |
452 » /* repr is guaranteed to be a PyUnicode object by PyObject_Repr */ | 455 /* repr is guaranteed to be a PyUnicode object by PyObject_Repr */ |
453 » ascii = PyUnicode_EncodeASCII( | 456 ascii = PyUnicode_EncodeASCII( |
454 » » PyUnicode_AS_UNICODE(repr), | 457 PyUnicode_AS_UNICODE(repr), |
455 » » PyUnicode_GET_SIZE(repr), | 458 PyUnicode_GET_SIZE(repr), |
456 » » "backslashreplace"); | 459 "backslashreplace"); |
457 | 460 |
458 » Py_DECREF(repr); | 461 Py_DECREF(repr); |
459 » if (ascii == NULL)· | 462 if (ascii == NULL) |
460 » » return NULL; | 463 return NULL; |
461 | 464 |
462 » res = PyUnicode_DecodeASCII( | 465 res = PyUnicode_DecodeASCII( |
463 » » PyBytes_AS_STRING(ascii), | 466 PyBytes_AS_STRING(ascii), |
464 » » PyBytes_GET_SIZE(ascii), | 467 PyBytes_GET_SIZE(ascii), |
465 » » NULL); | 468 NULL); |
466 | 469 |
467 » Py_DECREF(ascii); | 470 Py_DECREF(ascii); |
468 » return res; | 471 return res; |
469 } | 472 } |
470 | 473 |
471 PyObject * | 474 PyObject * |
472 PyObject_Bytes(PyObject *v) | 475 PyObject_Bytes(PyObject *v) |
473 { | 476 { |
474 » PyObject *result, *func; | 477 PyObject *result, *func; |
475 » static PyObject *bytesstring = NULL; | 478 static PyObject *bytesstring = NULL; |
476 | 479 |
477 » if (v == NULL) | 480 if (v == NULL) |
478 » » return PyBytes_FromString("<NULL>"); | 481 return PyBytes_FromString("<NULL>"); |
479 | 482 |
480 » if (PyBytes_CheckExact(v)) { | 483 if (PyBytes_CheckExact(v)) { |
481 » » Py_INCREF(v); | 484 Py_INCREF(v); |
482 » » return v; | 485 return v; |
483 » } | 486 } |
484 | 487 |
485 » func = _PyObject_LookupSpecial(v, "__bytes__", &bytesstring); | 488 func = _PyObject_LookupSpecial(v, "__bytes__", &bytesstring); |
486 » if (func != NULL) { | 489 if (func != NULL) { |
487 result = PyObject_CallFunctionObjArgs(func, NULL); | 490 result = PyObject_CallFunctionObjArgs(func, NULL); |
488 » Py_DECREF(func); | 491 Py_DECREF(func); |
489 if (result == NULL) | 492 if (result == NULL) |
490 » » return NULL; | 493 return NULL; |
491 if (!PyBytes_Check(result)) { | 494 if (!PyBytes_Check(result)) { |
492 » » PyErr_Format(PyExc_TypeError, | 495 PyErr_Format(PyExc_TypeError, |
493 » » » "__bytes__ returned non-bytes (type %.200s)", | 496 "__bytes__ returned non-bytes (type %.200s)", |
494 » » » Py_TYPE(result)->tp_name); | 497 Py_TYPE(result)->tp_name); |
495 » » Py_DECREF(result); | 498 Py_DECREF(result); |
496 » » return NULL; | 499 return NULL; |
497 } | 500 } |
498 return result; | 501 return result; |
499 » } | 502 } |
500 » else if (PyErr_Occurred()) | 503 else if (PyErr_Occurred()) |
501 » » return NULL; | 504 return NULL; |
502 » return PyBytes_FromObject(v); | 505 return PyBytes_FromObject(v); |
503 } | 506 } |
504 | 507 |
505 /* For Python 3.0.1 and later, the old three-way comparison has been | 508 /* For Python 3.0.1 and later, the old three-way comparison has been |
506 completely removed in favour of rich comparisons. PyObject_Compare() and | 509 completely removed in favour of rich comparisons. PyObject_Compare() and |
507 PyObject_Cmp() are gone, and the builtin cmp function no longer exists. | 510 PyObject_Cmp() are gone, and the builtin cmp function no longer exists. |
508 The old tp_compare slot has been renamed to tp_reserved, and should no | 511 The old tp_compare slot has been renamed to tp_reserved, and should no |
509 longer be used. Use tp_richcompare instead. | 512 longer be used. Use tp_richcompare instead. |
510 | 513 |
511 See (*) below for practical amendments. | 514 See (*) below for practical amendments. |
512 | 515 |
(...skipping 22 matching lines...) Expand all Loading... |
535 /* Map rich comparison operators to their swapped version, e.g. LT <--> GT */ | 538 /* Map rich comparison operators to their swapped version, e.g. LT <--> GT */ |
536 int _Py_SwappedOp[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE}; | 539 int _Py_SwappedOp[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE}; |
537 | 540 |
538 static char *opstrings[] = {"<", "<=", "==", "!=", ">", ">="}; | 541 static char *opstrings[] = {"<", "<=", "==", "!=", ">", ">="}; |
539 | 542 |
540 /* Perform a rich comparison, raising TypeError when the requested comparison | 543 /* Perform a rich comparison, raising TypeError when the requested comparison |
541 operator is not supported. */ | 544 operator is not supported. */ |
542 static PyObject * | 545 static PyObject * |
543 do_richcompare(PyObject *v, PyObject *w, int op) | 546 do_richcompare(PyObject *v, PyObject *w, int op) |
544 { | 547 { |
545 » richcmpfunc f; | 548 richcmpfunc f; |
546 » PyObject *res; | 549 PyObject *res; |
547 » int checked_reverse_op = 0; | 550 int checked_reverse_op = 0; |
548 | 551 |
549 » if (v->ob_type != w->ob_type && | 552 if (v->ob_type != w->ob_type && |
550 » PyType_IsSubtype(w->ob_type, v->ob_type) && | 553 PyType_IsSubtype(w->ob_type, v->ob_type) && |
551 » (f = w->ob_type->tp_richcompare) != NULL) { | 554 (f = w->ob_type->tp_richcompare) != NULL) { |
552 » » checked_reverse_op = 1; | 555 checked_reverse_op = 1; |
553 » » res = (*f)(w, v, _Py_SwappedOp[op]); | 556 res = (*f)(w, v, _Py_SwappedOp[op]); |
554 » » if (res != Py_NotImplemented) | 557 if (res != Py_NotImplemented) |
555 » » » return res; | 558 return res; |
556 » » Py_DECREF(res); | 559 Py_DECREF(res); |
557 » } | 560 } |
558 » if ((f = v->ob_type->tp_richcompare) != NULL) { | 561 if ((f = v->ob_type->tp_richcompare) != NULL) { |
559 » » res = (*f)(v, w, op); | 562 res = (*f)(v, w, op); |
560 » » if (res != Py_NotImplemented) | 563 if (res != Py_NotImplemented) |
561 » » » return res; | 564 return res; |
562 » » Py_DECREF(res); | 565 Py_DECREF(res); |
563 » } | 566 } |
564 » if (!checked_reverse_op && (f = w->ob_type->tp_richcompare) != NULL) { | 567 if (!checked_reverse_op && (f = w->ob_type->tp_richcompare) != NULL) { |
565 » » res = (*f)(w, v, _Py_SwappedOp[op]); | 568 res = (*f)(w, v, _Py_SwappedOp[op]); |
566 » » if (res != Py_NotImplemented) | 569 if (res != Py_NotImplemented) |
567 » » » return res; | 570 return res; |
568 » » Py_DECREF(res); | 571 Py_DECREF(res); |
569 » } | 572 } |
570 » /* If neither object implements it, provide a sensible default | 573 /* If neither object implements it, provide a sensible default |
571 » for == and !=, but raise an exception for ordering. */ | 574 for == and !=, but raise an exception for ordering. */ |
572 » switch (op) { | 575 switch (op) { |
573 » case Py_EQ: | 576 case Py_EQ: |
574 » » res = (v == w) ? Py_True : Py_False; | 577 res = (v == w) ? Py_True : Py_False; |
575 » » break; | 578 break; |
576 » case Py_NE: | 579 case Py_NE: |
577 » » res = (v != w) ? Py_True : Py_False; | 580 res = (v != w) ? Py_True : Py_False; |
578 » » break; | 581 break; |
579 » default: | 582 default: |
580 » » /* XXX Special-case None so it doesn't show as NoneType() */ | 583 /* XXX Special-case None so it doesn't show as NoneType() */ |
581 » » PyErr_Format(PyExc_TypeError, | 584 PyErr_Format(PyExc_TypeError, |
582 » » » "unorderable types: %.100s() %s %.100s()", | 585 "unorderable types: %.100s() %s %.100s()", |
583 » » » v->ob_type->tp_name, | 586 v->ob_type->tp_name, |
584 » » » opstrings[op], | 587 opstrings[op], |
585 » » » w->ob_type->tp_name); | 588 w->ob_type->tp_name); |
586 » » return NULL; | 589 return NULL; |
587 » } | 590 } |
588 » Py_INCREF(res); | 591 Py_INCREF(res); |
589 » return res; | 592 return res; |
590 } | 593 } |
591 | 594 |
592 /* Perform a rich comparison with object result. This wraps do_richcompare() | 595 /* Perform a rich comparison with object result. This wraps do_richcompare() |
593 with a check for NULL arguments and a recursion check. */ | 596 with a check for NULL arguments and a recursion check. */ |
594 | 597 |
595 PyObject * | 598 PyObject * |
596 PyObject_RichCompare(PyObject *v, PyObject *w, int op) | 599 PyObject_RichCompare(PyObject *v, PyObject *w, int op) |
597 { | 600 { |
598 » PyObject *res; | 601 PyObject *res; |
599 | 602 |
600 » assert(Py_LT <= op && op <= Py_GE); | 603 assert(Py_LT <= op && op <= Py_GE); |
601 » if (v == NULL || w == NULL) { | 604 if (v == NULL || w == NULL) { |
602 » » if (!PyErr_Occurred()) | 605 if (!PyErr_Occurred()) |
603 » » » PyErr_BadInternalCall(); | 606 PyErr_BadInternalCall(); |
604 » » return NULL; | 607 return NULL; |
605 » } | 608 } |
606 » if (Py_EnterRecursiveCall(" in comparison")) | 609 if (Py_EnterRecursiveCall(" in comparison")) |
607 » » return NULL; | 610 return NULL; |
608 » res = do_richcompare(v, w, op); | 611 res = do_richcompare(v, w, op); |
609 » Py_LeaveRecursiveCall(); | 612 Py_LeaveRecursiveCall(); |
610 » return res; | 613 return res; |
611 } | 614 } |
612 | 615 |
613 /* Perform a rich comparison with integer result. This wraps | 616 /* Perform a rich comparison with integer result. This wraps |
614 PyObject_RichCompare(), returning -1 for error, 0 for false, 1 for true. */ | 617 PyObject_RichCompare(), returning -1 for error, 0 for false, 1 for true. */ |
615 int | 618 int |
616 PyObject_RichCompareBool(PyObject *v, PyObject *w, int op) | 619 PyObject_RichCompareBool(PyObject *v, PyObject *w, int op) |
617 { | 620 { |
618 » PyObject *res; | 621 PyObject *res; |
619 » int ok; | 622 int ok; |
620 | 623 |
621 » /* Quick result when objects are the same. | 624 /* Quick result when objects are the same. |
622 » Guarantees that identity implies equality. */ | 625 Guarantees that identity implies equality. */ |
623 » if (v == w) { | 626 if (v == w) { |
624 » » if (op == Py_EQ) | 627 if (op == Py_EQ) |
625 » » » return 1; | 628 return 1; |
626 » » else if (op == Py_NE) | 629 else if (op == Py_NE) |
627 » » » return 0; | 630 return 0; |
628 » } | 631 } |
629 | 632 |
630 » res = PyObject_RichCompare(v, w, op); | 633 res = PyObject_RichCompare(v, w, op); |
631 » if (res == NULL) | 634 if (res == NULL) |
632 » » return -1; | 635 return -1; |
633 » if (PyBool_Check(res)) | 636 if (PyBool_Check(res)) |
634 » » ok = (res == Py_True); | 637 ok = (res == Py_True); |
635 » else | 638 else |
636 » » ok = PyObject_IsTrue(res); | 639 ok = PyObject_IsTrue(res); |
637 » Py_DECREF(res); | 640 Py_DECREF(res); |
638 » return ok; | 641 return ok; |
639 } | 642 } |
640 | 643 |
641 /* Set of hash utility functions to help maintaining the invariant that | 644 /* Set of hash utility functions to help maintaining the invariant that |
642 » if a==b then hash(a)==hash(b) | 645 if a==b then hash(a)==hash(b) |
643 | 646 |
644 All the utility functions (_Py_Hash*()) return "-1" to signify an error. | 647 All the utility functions (_Py_Hash*()) return "-1" to signify an error. |
645 */ | 648 */ |
646 | 649 |
647 /* For numeric types, the hash of a number x is based on the reduction | 650 /* For numeric types, the hash of a number x is based on the reduction |
648 of x modulo the prime P = 2**_PyHASH_BITS - 1. It's designed so that | 651 of x modulo the prime P = 2**_PyHASH_BITS - 1. It's designed so that |
649 hash(x) == hash(y) whenever x and y are numerically equal, even if | 652 hash(x) == hash(y) whenever x and y are numerically equal, even if |
650 x and y have different types. | 653 x and y have different types. |
651 | 654 |
652 A quick summary of the hashing strategy: | 655 A quick summary of the hashing strategy: |
653 | 656 |
654 (1) First define the 'reduction of x modulo P' for any rational | 657 (1) First define the 'reduction of x modulo P' for any rational |
655 number x; this is a standard extension of the usual notion of | 658 number x; this is a standard extension of the usual notion of |
656 reduction modulo P for integers. If x == p/q (written in lowest | 659 reduction modulo P for integers. If x == p/q (written in lowest |
657 terms), the reduction is interpreted as the reduction of p times | 660 terms), the reduction is interpreted as the reduction of p times |
658 the inverse of the reduction of q, all modulo P; if q is exactly | 661 the inverse of the reduction of q, all modulo P; if q is exactly |
659 divisible by P then define the reduction to be infinity. So we've | 662 divisible by P then define the reduction to be infinity. So we've |
660 got a well-defined map | 663 got a well-defined map |
661 | 664 |
662 reduce : { rational numbers } -> { 0, 1, 2, ..., P-1, infinity }. | 665 reduce : { rational numbers } -> { 0, 1, 2, ..., P-1, infinity }. |
663 | 666 |
664 (2) Now for a rational number x, define hash(x) by: | 667 (2) Now for a rational number x, define hash(x) by: |
665 | 668 |
666 0 if x == 0 | 669 reduce(x) if x >= 0 |
667 1 + reduce(x-1) if x > 0 | 670 -reduce(-x) if x < 0 |
668 -hash(-x) if x < 0 | 671 |
669 | 672 If the result of the reduction is infinity (this is impossible for |
670 If reduce(x-1) is infinity then use the predefined hash value | 673 integers, floats and Decimals) then use the predefined hash value |
671 _PyHASH_INF instead. _PyHASH_INF, _PyHASH_NINF and _PyHASH_NAN are | 674 _PyHASH_INF for x >= 0, or -_PyHASH_INF for x < 0, instead. |
672 also used for the hashes of float and Decimal infinities and nans. | 675 _PyHASH_INF, -_PyHASH_INF and _PyHASH_NAN are also used for the |
| 676 hashes of float and Decimal infinities and nans. |
673 | 677 |
674 A selling point for the above strategy is that it makes it possible | 678 A selling point for the above strategy is that it makes it possible |
675 to compute hashes of decimal and binary floating-point numbers | 679 to compute hashes of decimal and binary floating-point numbers |
676 efficiently, even if the exponent of the binary or decimal number | 680 efficiently, even if the exponent of the binary or decimal number |
677 is large. The key point is that | 681 is large. The key point is that |
678 | 682 |
679 reduce(x * y) == reduce(reduce(x) * reduce(y)) | 683 reduce(x * y) == reduce(x) * reduce(y) (modulo _PyHASH_MODULUS) |
680 | 684 |
681 provided that {reduce(x), reduce(y)} != {0, infinity}. For binary | 685 provided that {reduce(x), reduce(y)} != {0, infinity}. The reduction of a |
682 and decimal floats the reduction is never infinity, since the | 686 binary or decimal float is never infinity, since the denominator is a power |
683 denominator is a power of 2 (for binary) or a divisor of a power of | 687 of 2 (for binary) or a divisor of a power of 10 (for decimal). So we have, |
684 10 (for decimal). So we have: | 688 for nonnegative x, |
685 | 689 |
686 reduce(x * 2**e) == reduce(reduce(x) * reduce(2**e)) | 690 reduce(x * 2**e) == reduce(x) * reduce(2**e) % _PyHASH_MODULUS |
687 | 691 |
688 reduce(x * 10**e) == reduce(reduce(x) * reduce(10**e)) | 692 reduce(x * 10**e) == reduce(x) * reduce(10**e) % _PyHASH_MODULUS |
689 | 693 |
690 and reduce(10**e) can be computed efficiently by the usual modular | 694 and reduce(10**e) can be computed efficiently by the usual modular |
691 exponentiation algorithm. For reduce(2**e) it's even better: since | 695 exponentiation algorithm. For reduce(2**e) it's even better: since |
692 P is of the form 2**n-1, reduce(2**e) is 2**(e mod n), and multiplication | 696 P is of the form 2**n-1, reduce(2**e) is 2**(e mod n), and multiplication |
693 by 2**(e mod n) modulo 2**n-1 just amounts to a rotation of bits. | 697 by 2**(e mod n) modulo 2**n-1 just amounts to a rotation of bits. |
694 | 698 |
695 */ | 699 */ |
696 | 700 |
697 long | 701 long |
698 _Py_HashDouble(double v) | 702 _Py_HashDouble(double v) |
699 { | 703 { |
700 » int e, sign; | 704 int e, sign; |
701 » double m; | 705 double m; |
702 » unsigned long x, y; | 706 unsigned long x, y; |
703 | 707 |
704 » if (!Py_IS_FINITE(v)) { | 708 if (!Py_IS_FINITE(v)) { |
705 » » if (Py_IS_INFINITY(v)) | 709 if (Py_IS_INFINITY(v)) |
706 » » » return v > 0 ? _PyHASH_INF : _PyHASH_NINF; | 710 return v > 0 ? _PyHASH_INF : -_PyHASH_INF; |
707 » » else | 711 else |
708 » » » return _PyHASH_NAN; | 712 return _PyHASH_NAN; |
709 » } | 713 } |
710 | 714 |
711 » m = frexp(v, &e); | 715 m = frexp(v, &e); |
712 | 716 |
713 » sign = 1; | 717 sign = 1; |
714 » if (m < 0) { | 718 if (m < 0) { |
715 » » sign = -1; | 719 sign = -1; |
716 » » m = -m; | 720 m = -m; |
717 » } | 721 } |
718 | 722 |
719 » /* process 28 bits at a time; this should work well both for binary | 723 /* process 28 bits at a time; this should work well both for binary |
720 » and hexadecimal floating point. */ | 724 and hexadecimal floating point. */ |
721 » x = 0; | 725 x = 0; |
722 » while (m) { | 726 while (m) { |
723 » » x = ((x << 28) & _PyHASH_MASK) | x >> (_PyHASH_BITS - 28); | 727 x = ((x << 28) & _PyHASH_MODULUS) | x >> (_PyHASH_BITS - 28); |
724 » » m *= 268435456.0; /* 2**28 */ | 728 m *= 268435456.0; /* 2**28 */ |
725 » » e -= 28; | 729 e -= 28; |
726 » » y = (unsigned long)m; /* pull out integer part */ | 730 y = (unsigned long)m; /* pull out integer part */ |
727 » » m -= y; | 731 m -= y; |
728 » » x += y; | 732 x += y; |
729 » » if (x > _PyHASH_MASK) | 733 if (x >= _PyHASH_MODULUS) |
730 » » » x -= _PyHASH_MASK; | 734 x -= _PyHASH_MODULUS; |
731 » } | 735 } |
732 | 736 |
733 » /* adjust for the exponent; first reduce it modulo _PyHASH_BITS */ | 737 /* adjust for the exponent; first reduce it modulo _PyHASH_BITS */ |
734 » e = e >= 0 ? e % _PyHASH_BITS : _PyHASH_BITS-1-((-1-e) % _PyHASH_BITS); | 738 e = e >= 0 ? e % _PyHASH_BITS : _PyHASH_BITS-1-((-1-e) % _PyHASH_BITS); |
735 » x = ((x << e) & _PyHASH_MASK) | x >> (_PyHASH_BITS - e); | 739 x = ((x << e) & _PyHASH_MODULUS) | x >> (_PyHASH_BITS - e); |
736 | 740 |
737 » x = x * sign; | 741 x = x * sign; |
738 » if (x == (unsigned long)-1) | 742 if (x == (unsigned long)-1) |
739 » » x = (unsigned long)-2; | 743 x = (unsigned long)-2; |
740 » return (long)x; | 744 return (long)x; |
741 } | 745 } |
742 | 746 |
743 long | 747 long |
744 _Py_HashPointer(void *p) | 748 _Py_HashPointer(void *p) |
745 { | 749 { |
746 » long x; | 750 long x; |
747 » size_t y = (size_t)p; | 751 size_t y = (size_t)p; |
748 » /* bottom 3 or 4 bits are likely to be 0; rotate y by 4 to avoid | 752 /* bottom 3 or 4 bits are likely to be 0; rotate y by 4 to avoid |
749 » excessive hash collisions for dicts and sets */ | 753 excessive hash collisions for dicts and sets */ |
750 » y = (y >> 4) | (y << (8 * SIZEOF_VOID_P - 4)); | 754 y = (y >> 4) | (y << (8 * SIZEOF_VOID_P - 4)); |
751 » x = (long)y; | 755 x = (long)y; |
752 » if (x == -1) | 756 if (x == -1) |
753 » » x = -2; | 757 x = -2; |
754 » return x; | 758 return x; |
755 } | 759 } |
756 | 760 |
757 long | 761 long |
758 PyObject_HashNotImplemented(PyObject *v) | 762 PyObject_HashNotImplemented(PyObject *v) |
759 { | 763 { |
760 » PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'", | 764 PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'", |
761 » » Py_TYPE(v)->tp_name); | 765 Py_TYPE(v)->tp_name); |
762 » return -1; | 766 return -1; |
763 } | 767 } |
764 | 768 |
765 long | 769 long |
766 PyObject_Hash(PyObject *v) | 770 PyObject_Hash(PyObject *v) |
767 { | 771 { |
768 » PyTypeObject *tp = Py_TYPE(v); | 772 PyTypeObject *tp = Py_TYPE(v); |
769 » if (tp->tp_hash != NULL) | 773 if (tp->tp_hash != NULL) |
770 » » return (*tp->tp_hash)(v); | 774 return (*tp->tp_hash)(v); |
771 » /* To keep to the general practice that inheriting | 775 /* To keep to the general practice that inheriting |
772 » * solely from object in C code should work without | 776 * solely from object in C code should work without |
773 » * an explicit call to PyType_Ready, we implicitly call | 777 * an explicit call to PyType_Ready, we implicitly call |
774 » * PyType_Ready here and then check the tp_hash slot again | 778 * PyType_Ready here and then check the tp_hash slot again |
775 » */ | 779 */ |
776 » if (tp->tp_dict == NULL) { | 780 if (tp->tp_dict == NULL) { |
777 » » if (PyType_Ready(tp) < 0) | 781 if (PyType_Ready(tp) < 0) |
778 » » » return -1; | 782 return -1; |
779 » » if (tp->tp_hash != NULL) | 783 if (tp->tp_hash != NULL) |
780 » » » return (*tp->tp_hash)(v); | 784 return (*tp->tp_hash)(v); |
781 » } | 785 } |
782 » /* Otherwise, the object can't be hashed */ | 786 /* Otherwise, the object can't be hashed */ |
783 » return PyObject_HashNotImplemented(v); | 787 return PyObject_HashNotImplemented(v); |
784 } | 788 } |
785 | 789 |
786 PyObject * | 790 PyObject * |
787 PyObject_GetAttrString(PyObject *v, const char *name) | 791 PyObject_GetAttrString(PyObject *v, const char *name) |
788 { | 792 { |
789 » PyObject *w, *res; | 793 PyObject *w, *res; |
790 | 794 |
791 » if (Py_TYPE(v)->tp_getattr != NULL) | 795 if (Py_TYPE(v)->tp_getattr != NULL) |
792 » » return (*Py_TYPE(v)->tp_getattr)(v, (char*)name); | 796 return (*Py_TYPE(v)->tp_getattr)(v, (char*)name); |
793 » w = PyUnicode_InternFromString(name); | 797 w = PyUnicode_InternFromString(name); |
794 » if (w == NULL) | 798 if (w == NULL) |
795 » » return NULL; | 799 return NULL; |
796 » res = PyObject_GetAttr(v, w); | 800 res = PyObject_GetAttr(v, w); |
797 » Py_XDECREF(w); | 801 Py_XDECREF(w); |
798 » return res; | 802 return res; |
799 } | 803 } |
800 | 804 |
801 int | 805 int |
802 PyObject_HasAttrString(PyObject *v, const char *name) | 806 PyObject_HasAttrString(PyObject *v, const char *name) |
803 { | 807 { |
804 » PyObject *res = PyObject_GetAttrString(v, name); | 808 PyObject *res = PyObject_GetAttrString(v, name); |
805 » if (res != NULL) { | 809 if (res != NULL) { |
806 » » Py_DECREF(res); | 810 Py_DECREF(res); |
807 » » return 1; | 811 return 1; |
808 » } | 812 } |
809 » PyErr_Clear(); | 813 PyErr_Clear(); |
810 » return 0; | 814 return 0; |
811 } | 815 } |
812 | 816 |
813 int | 817 int |
814 PyObject_SetAttrString(PyObject *v, const char *name, PyObject *w) | 818 PyObject_SetAttrString(PyObject *v, const char *name, PyObject *w) |
815 { | 819 { |
816 » PyObject *s; | 820 PyObject *s; |
817 » int res; | 821 int res; |
818 | 822 |
819 » if (Py_TYPE(v)->tp_setattr != NULL) | 823 if (Py_TYPE(v)->tp_setattr != NULL) |
820 » » return (*Py_TYPE(v)->tp_setattr)(v, (char*)name, w); | 824 return (*Py_TYPE(v)->tp_setattr)(v, (char*)name, w); |
821 » s = PyUnicode_InternFromString(name); | 825 s = PyUnicode_InternFromString(name); |
822 » if (s == NULL) | 826 if (s == NULL) |
823 » » return -1; | 827 return -1; |
824 » res = PyObject_SetAttr(v, s, w); | 828 res = PyObject_SetAttr(v, s, w); |
825 » Py_XDECREF(s); | 829 Py_XDECREF(s); |
826 » return res; | 830 return res; |
827 } | 831 } |
828 | 832 |
829 PyObject * | 833 PyObject * |
830 PyObject_GetAttr(PyObject *v, PyObject *name) | 834 PyObject_GetAttr(PyObject *v, PyObject *name) |
831 { | 835 { |
832 » PyTypeObject *tp = Py_TYPE(v); | 836 PyTypeObject *tp = Py_TYPE(v); |
833 | 837 |
834 » if (!PyUnicode_Check(name)) { | 838 if (!PyUnicode_Check(name)) { |
835 » » PyErr_Format(PyExc_TypeError, | 839 PyErr_Format(PyExc_TypeError, |
836 » » » "attribute name must be string, not '%.200s'", | 840 "attribute name must be string, not '%.200s'", |
837 » » » name->ob_type->tp_name); | 841 name->ob_type->tp_name); |
838 » » return NULL; | 842 return NULL; |
839 » } | 843 } |
840 » if (tp->tp_getattro != NULL) | 844 if (tp->tp_getattro != NULL) |
841 » » return (*tp->tp_getattro)(v, name); | 845 return (*tp->tp_getattro)(v, name); |
842 » if (tp->tp_getattr != NULL) { | 846 if (tp->tp_getattr != NULL) { |
843 » » char *name_str = _PyUnicode_AsString(name); | 847 char *name_str = _PyUnicode_AsString(name); |
844 » » if (name_str == NULL) | 848 if (name_str == NULL) |
845 » » » return NULL; | 849 return NULL; |
846 » » return (*tp->tp_getattr)(v, name_str); | 850 return (*tp->tp_getattr)(v, name_str); |
847 » } | 851 } |
848 » PyErr_Format(PyExc_AttributeError, | 852 PyErr_Format(PyExc_AttributeError, |
849 » » "'%.50s' object has no attribute '%U'", | 853 "'%.50s' object has no attribute '%U'", |
850 » » tp->tp_name, name); | 854 tp->tp_name, name); |
851 » return NULL; | 855 return NULL; |
852 } | 856 } |
853 | 857 |
854 int | 858 int |
855 PyObject_HasAttr(PyObject *v, PyObject *name) | 859 PyObject_HasAttr(PyObject *v, PyObject *name) |
856 { | 860 { |
857 » PyObject *res = PyObject_GetAttr(v, name); | 861 PyObject *res = PyObject_GetAttr(v, name); |
858 » if (res != NULL) { | 862 if (res != NULL) { |
859 » » Py_DECREF(res); | 863 Py_DECREF(res); |
860 » » return 1; | 864 return 1; |
861 » } | 865 } |
862 » PyErr_Clear(); | 866 PyErr_Clear(); |
863 » return 0; | 867 return 0; |
864 } | 868 } |
865 | 869 |
866 int | 870 int |
867 PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value) | 871 PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value) |
868 { | 872 { |
869 » PyTypeObject *tp = Py_TYPE(v); | 873 PyTypeObject *tp = Py_TYPE(v); |
870 » int err; | 874 int err; |
871 | 875 |
872 » if (!PyUnicode_Check(name)) { | 876 if (!PyUnicode_Check(name)) { |
873 » » PyErr_Format(PyExc_TypeError, | 877 PyErr_Format(PyExc_TypeError, |
874 » » » "attribute name must be string, not '%.200s'", | 878 "attribute name must be string, not '%.200s'", |
875 » » » name->ob_type->tp_name); | 879 name->ob_type->tp_name); |
876 » » return -1; | 880 return -1; |
877 » } | 881 } |
878 » Py_INCREF(name); | 882 Py_INCREF(name); |
879 | 883 |
880 » PyUnicode_InternInPlace(&name); | 884 PyUnicode_InternInPlace(&name); |
881 » if (tp->tp_setattro != NULL) { | 885 if (tp->tp_setattro != NULL) { |
882 » » err = (*tp->tp_setattro)(v, name, value); | 886 err = (*tp->tp_setattro)(v, name, value); |
883 » » Py_DECREF(name); | 887 Py_DECREF(name); |
884 » » return err; | 888 return err; |
885 » } | 889 } |
886 » if (tp->tp_setattr != NULL) { | 890 if (tp->tp_setattr != NULL) { |
887 » » char *name_str = _PyUnicode_AsString(name); | 891 char *name_str = _PyUnicode_AsString(name); |
888 » » if (name_str == NULL) | 892 if (name_str == NULL) |
889 » » » return -1; | 893 return -1; |
890 » » err = (*tp->tp_setattr)(v, name_str, value); | 894 err = (*tp->tp_setattr)(v, name_str, value); |
891 » » Py_DECREF(name); | 895 Py_DECREF(name); |
892 » » return err; | 896 return err; |
893 » } | 897 } |
894 » Py_DECREF(name); | 898 Py_DECREF(name); |
895 » assert(name->ob_refcnt >= 1); | 899 assert(name->ob_refcnt >= 1); |
896 » if (tp->tp_getattr == NULL && tp->tp_getattro == NULL) | 900 if (tp->tp_getattr == NULL && tp->tp_getattro == NULL) |
897 » » PyErr_Format(PyExc_TypeError, | 901 PyErr_Format(PyExc_TypeError, |
898 » » » "'%.100s' object has no attributes " | 902 "'%.100s' object has no attributes " |
899 » » » "(%s .%U)", | 903 "(%s .%U)", |
900 » » » tp->tp_name, | 904 tp->tp_name, |
901 » » » value==NULL ? "del" : "assign to", | 905 value==NULL ? "del" : "assign to", |
902 » » » name); | 906 name); |
903 » else | 907 else |
904 » » PyErr_Format(PyExc_TypeError, | 908 PyErr_Format(PyExc_TypeError, |
905 » » » "'%.100s' object has only read-only attributes " | 909 "'%.100s' object has only read-only attributes " |
906 » » » "(%s .%U)", | 910 "(%s .%U)", |
907 » » » tp->tp_name, | 911 tp->tp_name, |
908 » » » value==NULL ? "del" : "assign to", | 912 value==NULL ? "del" : "assign to", |
909 » » » name); | 913 name); |
910 » return -1; | 914 return -1; |
911 } | 915 } |
912 | 916 |
913 /* Helper to get a pointer to an object's __dict__ slot, if any */ | 917 /* Helper to get a pointer to an object's __dict__ slot, if any */ |
914 | 918 |
915 PyObject ** | 919 PyObject ** |
916 _PyObject_GetDictPtr(PyObject *obj) | 920 _PyObject_GetDictPtr(PyObject *obj) |
917 { | 921 { |
918 » Py_ssize_t dictoffset; | 922 Py_ssize_t dictoffset; |
919 » PyTypeObject *tp = Py_TYPE(obj); | 923 PyTypeObject *tp = Py_TYPE(obj); |
920 | 924 |
921 » dictoffset = tp->tp_dictoffset; | 925 dictoffset = tp->tp_dictoffset; |
922 » if (dictoffset == 0) | 926 if (dictoffset == 0) |
923 » » return NULL; | 927 return NULL; |
924 » if (dictoffset < 0) { | 928 if (dictoffset < 0) { |
925 » » Py_ssize_t tsize; | 929 Py_ssize_t tsize; |
926 » » size_t size; | 930 size_t size; |
927 | 931 |
928 » » tsize = ((PyVarObject *)obj)->ob_size; | 932 tsize = ((PyVarObject *)obj)->ob_size; |
929 » » if (tsize < 0) | 933 if (tsize < 0) |
930 » » » tsize = -tsize; | 934 tsize = -tsize; |
931 » » size = _PyObject_VAR_SIZE(tp, tsize); | 935 size = _PyObject_VAR_SIZE(tp, tsize); |
932 | 936 |
933 » » dictoffset += (long)size; | 937 dictoffset += (long)size; |
934 » » assert(dictoffset > 0); | 938 assert(dictoffset > 0); |
935 » » assert(dictoffset % SIZEOF_VOID_P == 0); | 939 assert(dictoffset % SIZEOF_VOID_P == 0); |
936 » } | 940 } |
937 » return (PyObject **) ((char *)obj + dictoffset); | 941 return (PyObject **) ((char *)obj + dictoffset); |
938 } | 942 } |
939 | 943 |
940 PyObject * | 944 PyObject * |
941 PyObject_SelfIter(PyObject *obj) | 945 PyObject_SelfIter(PyObject *obj) |
942 { | 946 { |
943 » Py_INCREF(obj); | 947 Py_INCREF(obj); |
944 » return obj; | 948 return obj; |
945 } | 949 } |
946 | 950 |
947 /* Helper used when the __next__ method is removed from a type: | 951 /* Helper used when the __next__ method is removed from a type: |
948 tp_iternext is never NULL and can be safely called without checking | 952 tp_iternext is never NULL and can be safely called without checking |
949 on every iteration. | 953 on every iteration. |
950 */ | 954 */ |
951 | 955 |
952 PyObject * | 956 PyObject * |
953 _PyObject_NextNotImplemented(PyObject *self) | 957 _PyObject_NextNotImplemented(PyObject *self) |
954 { | 958 { |
955 » PyErr_Format(PyExc_TypeError, | 959 PyErr_Format(PyExc_TypeError, |
956 » » "'%.200s' object is not iterable", | 960 "'%.200s' object is not iterable", |
957 » » Py_TYPE(self)->tp_name); | 961 Py_TYPE(self)->tp_name); |
958 » return NULL; | 962 return NULL; |
959 } | 963 } |
960 | 964 |
961 /* Generic GetAttr functions - put these in your tp_[gs]etattro slot */ | 965 /* Generic GetAttr functions - put these in your tp_[gs]etattro slot */ |
962 | 966 |
963 PyObject * | 967 PyObject * |
964 PyObject_GenericGetAttr(PyObject *obj, PyObject *name) | 968 PyObject_GenericGetAttr(PyObject *obj, PyObject *name) |
965 { | 969 { |
966 » PyTypeObject *tp = Py_TYPE(obj); | 970 PyTypeObject *tp = Py_TYPE(obj); |
967 » PyObject *descr = NULL; | 971 PyObject *descr = NULL; |
968 » PyObject *res = NULL; | 972 PyObject *res = NULL; |
969 » descrgetfunc f; | 973 descrgetfunc f; |
970 » Py_ssize_t dictoffset; | 974 Py_ssize_t dictoffset; |
971 » PyObject **dictptr; | 975 PyObject **dictptr; |
972 | 976 |
973 » if (!PyUnicode_Check(name)){ | 977 if (!PyUnicode_Check(name)){ |
974 » » PyErr_Format(PyExc_TypeError, | 978 PyErr_Format(PyExc_TypeError, |
975 » » » "attribute name must be string, not '%.200s'", | 979 "attribute name must be string, not '%.200s'", |
976 » » » name->ob_type->tp_name); | 980 name->ob_type->tp_name); |
977 » » return NULL; | 981 return NULL; |
978 » } | 982 } |
979 » else | 983 else |
980 » » Py_INCREF(name); | 984 Py_INCREF(name); |
981 | 985 |
982 » if (tp->tp_dict == NULL) { | 986 if (tp->tp_dict == NULL) { |
983 » » if (PyType_Ready(tp) < 0) | 987 if (PyType_Ready(tp) < 0) |
984 » » » goto done; | 988 goto done; |
985 » } | 989 } |
986 | 990 |
987 #if 0 /* XXX this is not quite _PyType_Lookup anymore */ | 991 #if 0 /* XXX this is not quite _PyType_Lookup anymore */ |
988 » /* Inline _PyType_Lookup */ | 992 /* Inline _PyType_Lookup */ |
989 » { | 993 { |
990 » » Py_ssize_t i, n; | 994 Py_ssize_t i, n; |
991 » » PyObject *mro, *base, *dict; | 995 PyObject *mro, *base, *dict; |
992 | 996 |
993 » » /* Look in tp_dict of types in MRO */ | 997 /* Look in tp_dict of types in MRO */ |
994 » » mro = tp->tp_mro; | 998 mro = tp->tp_mro; |
995 » » assert(mro != NULL); | 999 assert(mro != NULL); |
996 » » assert(PyTuple_Check(mro)); | 1000 assert(PyTuple_Check(mro)); |
997 » » n = PyTuple_GET_SIZE(mro); | 1001 n = PyTuple_GET_SIZE(mro); |
998 » » for (i = 0; i < n; i++) { | 1002 for (i = 0; i < n; i++) { |
999 » » » base = PyTuple_GET_ITEM(mro, i); | 1003 base = PyTuple_GET_ITEM(mro, i); |
1000 » » » assert(PyType_Check(base)); | 1004 assert(PyType_Check(base)); |
1001 » » » dict = ((PyTypeObject *)base)->tp_dict; | 1005 dict = ((PyTypeObject *)base)->tp_dict; |
1002 » » » assert(dict && PyDict_Check(dict)); | 1006 assert(dict && PyDict_Check(dict)); |
1003 » » » descr = PyDict_GetItem(dict, name); | 1007 descr = PyDict_GetItem(dict, name); |
1004 » » » if (descr != NULL) | 1008 if (descr != NULL) |
1005 » » » » break; | 1009 break; |
1006 » » } | 1010 } |
1007 » } | 1011 } |
1008 #else | 1012 #else |
1009 » descr = _PyType_Lookup(tp, name); | 1013 descr = _PyType_Lookup(tp, name); |
1010 #endif | 1014 #endif |
1011 | 1015 |
1012 » Py_XINCREF(descr); | 1016 Py_XINCREF(descr); |
1013 | 1017 |
1014 » f = NULL; | 1018 f = NULL; |
1015 » if (descr != NULL) { | 1019 if (descr != NULL) { |
1016 » » f = descr->ob_type->tp_descr_get; | 1020 f = descr->ob_type->tp_descr_get; |
1017 » » if (f != NULL && PyDescr_IsData(descr)) { | 1021 if (f != NULL && PyDescr_IsData(descr)) { |
1018 » » » res = f(descr, obj, (PyObject *)obj->ob_type); | 1022 res = f(descr, obj, (PyObject *)obj->ob_type); |
1019 » » » Py_DECREF(descr); | 1023 Py_DECREF(descr); |
1020 » » » goto done; | 1024 goto done; |
1021 » » } | 1025 } |
1022 » } | 1026 } |
1023 | 1027 |
1024 » /* Inline _PyObject_GetDictPtr */ | 1028 /* Inline _PyObject_GetDictPtr */ |
1025 » dictoffset = tp->tp_dictoffset; | 1029 dictoffset = tp->tp_dictoffset; |
1026 » if (dictoffset != 0) { | 1030 if (dictoffset != 0) { |
1027 » » PyObject *dict; | 1031 PyObject *dict; |
1028 » » if (dictoffset < 0) { | 1032 if (dictoffset < 0) { |
1029 » » » Py_ssize_t tsize; | 1033 Py_ssize_t tsize; |
1030 » » » size_t size; | 1034 size_t size; |
1031 | 1035 |
1032 » » » tsize = ((PyVarObject *)obj)->ob_size; | 1036 tsize = ((PyVarObject *)obj)->ob_size; |
1033 » » » if (tsize < 0) | 1037 if (tsize < 0) |
1034 » » » » tsize = -tsize; | 1038 tsize = -tsize; |
1035 » » » size = _PyObject_VAR_SIZE(tp, tsize); | 1039 size = _PyObject_VAR_SIZE(tp, tsize); |
1036 | 1040 |
1037 » » » dictoffset += (long)size; | 1041 dictoffset += (long)size; |
1038 » » » assert(dictoffset > 0); | 1042 assert(dictoffset > 0); |
1039 » » » assert(dictoffset % SIZEOF_VOID_P == 0); | 1043 assert(dictoffset % SIZEOF_VOID_P == 0); |
1040 » » } | 1044 } |
1041 » » dictptr = (PyObject **) ((char *)obj + dictoffset); | 1045 dictptr = (PyObject **) ((char *)obj + dictoffset); |
1042 » » dict = *dictptr; | 1046 dict = *dictptr; |
1043 » » if (dict != NULL) { | 1047 if (dict != NULL) { |
1044 » » » Py_INCREF(dict); | 1048 Py_INCREF(dict); |
1045 » » » res = PyDict_GetItem(dict, name); | 1049 res = PyDict_GetItem(dict, name); |
1046 » » » if (res != NULL) { | 1050 if (res != NULL) { |
1047 » » » » Py_INCREF(res); | 1051 Py_INCREF(res); |
1048 » » » » Py_XDECREF(descr); | 1052 Py_XDECREF(descr); |
1049 Py_DECREF(dict); | 1053 Py_DECREF(dict); |
1050 » » » » goto done; | 1054 goto done; |
1051 » » » } | 1055 } |
1052 Py_DECREF(dict); | 1056 Py_DECREF(dict); |
1053 » » } | 1057 } |
1054 » } | 1058 } |
1055 | 1059 |
1056 » if (f != NULL) { | 1060 if (f != NULL) { |
1057 » » res = f(descr, obj, (PyObject *)Py_TYPE(obj)); | 1061 res = f(descr, obj, (PyObject *)Py_TYPE(obj)); |
1058 » » Py_DECREF(descr); | 1062 Py_DECREF(descr); |
1059 » » goto done; | 1063 goto done; |
1060 » } | 1064 } |
1061 | 1065 |
1062 » if (descr != NULL) { | 1066 if (descr != NULL) { |
1063 » » res = descr; | 1067 res = descr; |
1064 » » /* descr was already increfed above */ | 1068 /* descr was already increfed above */ |
1065 » » goto done; | 1069 goto done; |
1066 » } | 1070 } |
1067 | 1071 |
1068 » PyErr_Format(PyExc_AttributeError, | 1072 PyErr_Format(PyExc_AttributeError, |
1069 » » "'%.50s' object has no attribute '%U'", | 1073 "'%.50s' object has no attribute '%U'", |
1070 » » tp->tp_name, name); | 1074 tp->tp_name, name); |
1071 done: | 1075 done: |
1072 » Py_DECREF(name); | 1076 Py_DECREF(name); |
1073 » return res; | 1077 return res; |
1074 } | 1078 } |
1075 | 1079 |
1076 int | 1080 int |
1077 PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value) | 1081 PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value) |
1078 { | 1082 { |
1079 » PyTypeObject *tp = Py_TYPE(obj); | 1083 PyTypeObject *tp = Py_TYPE(obj); |
1080 » PyObject *descr; | 1084 PyObject *descr; |
1081 » descrsetfunc f; | 1085 descrsetfunc f; |
1082 » PyObject **dictptr; | 1086 PyObject **dictptr; |
1083 » int res = -1; | 1087 int res = -1; |
1084 | 1088 |
1085 » if (!PyUnicode_Check(name)){ | 1089 if (!PyUnicode_Check(name)){ |
1086 » » PyErr_Format(PyExc_TypeError, | 1090 PyErr_Format(PyExc_TypeError, |
1087 » » » "attribute name must be string, not '%.200s'", | 1091 "attribute name must be string, not '%.200s'", |
1088 » » » name->ob_type->tp_name); | 1092 name->ob_type->tp_name); |
1089 » » return -1; | 1093 return -1; |
1090 » } | 1094 } |
1091 » else | 1095 else |
1092 » » Py_INCREF(name); | 1096 Py_INCREF(name); |
1093 | 1097 |
1094 » if (tp->tp_dict == NULL) { | 1098 if (tp->tp_dict == NULL) { |
1095 » » if (PyType_Ready(tp) < 0) | 1099 if (PyType_Ready(tp) < 0) |
1096 » » » goto done; | 1100 goto done; |
1097 » } | 1101 } |
1098 | 1102 |
1099 » descr = _PyType_Lookup(tp, name); | 1103 descr = _PyType_Lookup(tp, name); |
1100 » f = NULL; | 1104 f = NULL; |
1101 » if (descr != NULL) { | 1105 if (descr != NULL) { |
1102 » » f = descr->ob_type->tp_descr_set; | 1106 f = descr->ob_type->tp_descr_set; |
1103 » » if (f != NULL && PyDescr_IsData(descr)) { | 1107 if (f != NULL && PyDescr_IsData(descr)) { |
1104 » » » res = f(descr, obj, value); | 1108 res = f(descr, obj, value); |
1105 » » » goto done; | 1109 goto done; |
1106 » » } | 1110 } |
1107 » } | 1111 } |
1108 | 1112 |
1109 » dictptr = _PyObject_GetDictPtr(obj); | 1113 dictptr = _PyObject_GetDictPtr(obj); |
1110 » if (dictptr != NULL) { | 1114 if (dictptr != NULL) { |
1111 » » PyObject *dict = *dictptr; | 1115 PyObject *dict = *dictptr; |
1112 » » if (dict == NULL && value != NULL) { | 1116 if (dict == NULL && value != NULL) { |
1113 » » » dict = PyDict_New(); | 1117 dict = PyDict_New(); |
1114 » » » if (dict == NULL) | 1118 if (dict == NULL) |
1115 » » » » goto done; | 1119 goto done; |
1116 » » » *dictptr = dict; | 1120 *dictptr = dict; |
1117 » » } | 1121 } |
1118 » » if (dict != NULL) { | 1122 if (dict != NULL) { |
1119 » » » Py_INCREF(dict); | 1123 Py_INCREF(dict); |
1120 » » » if (value == NULL) | 1124 if (value == NULL) |
1121 » » » » res = PyDict_DelItem(dict, name); | 1125 res = PyDict_DelItem(dict, name); |
1122 » » » else | 1126 else |
1123 » » » » res = PyDict_SetItem(dict, name, value); | 1127 res = PyDict_SetItem(dict, name, value); |
1124 » » » if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError)) | 1128 if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError)) |
1125 » » » » PyErr_SetObject(PyExc_AttributeError, name); | 1129 PyErr_SetObject(PyExc_AttributeError, name); |
1126 » » » Py_DECREF(dict); | 1130 Py_DECREF(dict); |
1127 » » » goto done; | 1131 goto done; |
1128 » » } | 1132 } |
1129 » } | 1133 } |
1130 | 1134 |
1131 » if (f != NULL) { | 1135 if (f != NULL) { |
1132 » » res = f(descr, obj, value); | 1136 res = f(descr, obj, value); |
1133 » » goto done; | 1137 goto done; |
1134 » } | 1138 } |
1135 | 1139 |
1136 » if (descr == NULL) { | 1140 if (descr == NULL) { |
1137 » » PyErr_Format(PyExc_AttributeError, | 1141 PyErr_Format(PyExc_AttributeError, |
1138 » » » "'%.100s' object has no attribute '%U'", | 1142 "'%.100s' object has no attribute '%U'", |
1139 » » » tp->tp_name, name); | 1143 tp->tp_name, name); |
1140 » » goto done; | 1144 goto done; |
1141 » } | 1145 } |
1142 | 1146 |
1143 » PyErr_Format(PyExc_AttributeError, | 1147 PyErr_Format(PyExc_AttributeError, |
1144 » » "'%.50s' object attribute '%U' is read-only", | 1148 "'%.50s' object attribute '%U' is read-only", |
1145 » » tp->tp_name, name); | 1149 tp->tp_name, name); |
1146 done: | 1150 done: |
1147 » Py_DECREF(name); | 1151 Py_DECREF(name); |
1148 » return res; | 1152 return res; |
1149 } | 1153 } |
1150 | 1154 |
1151 /* Test a value used as condition, e.g., in a for or if statement. | 1155 /* Test a value used as condition, e.g., in a for or if statement. |
1152 Return -1 if an error occurred */ | 1156 Return -1 if an error occurred */ |
1153 | 1157 |
1154 int | 1158 int |
1155 PyObject_IsTrue(PyObject *v) | 1159 PyObject_IsTrue(PyObject *v) |
1156 { | 1160 { |
1157 » Py_ssize_t res; | 1161 Py_ssize_t res; |
1158 » if (v == Py_True) | 1162 if (v == Py_True) |
1159 » » return 1; | 1163 return 1; |
1160 » if (v == Py_False) | 1164 if (v == Py_False) |
1161 » » return 0; | 1165 return 0; |
1162 » if (v == Py_None) | 1166 if (v == Py_None) |
1163 » » return 0; | 1167 return 0; |
1164 » else if (v->ob_type->tp_as_number != NULL && | 1168 else if (v->ob_type->tp_as_number != NULL && |
1165 » » v->ob_type->tp_as_number->nb_bool != NULL) | 1169 v->ob_type->tp_as_number->nb_bool != NULL) |
1166 » » res = (*v->ob_type->tp_as_number->nb_bool)(v); | 1170 res = (*v->ob_type->tp_as_number->nb_bool)(v); |
1167 » else if (v->ob_type->tp_as_mapping != NULL && | 1171 else if (v->ob_type->tp_as_mapping != NULL && |
1168 » » v->ob_type->tp_as_mapping->mp_length != NULL) | 1172 v->ob_type->tp_as_mapping->mp_length != NULL) |
1169 » » res = (*v->ob_type->tp_as_mapping->mp_length)(v); | 1173 res = (*v->ob_type->tp_as_mapping->mp_length)(v); |
1170 » else if (v->ob_type->tp_as_sequence != NULL && | 1174 else if (v->ob_type->tp_as_sequence != NULL && |
1171 » » v->ob_type->tp_as_sequence->sq_length != NULL) | 1175 v->ob_type->tp_as_sequence->sq_length != NULL) |
1172 » » res = (*v->ob_type->tp_as_sequence->sq_length)(v); | 1176 res = (*v->ob_type->tp_as_sequence->sq_length)(v); |
1173 » else | 1177 else |
1174 » » return 1; | 1178 return 1; |
1175 » /* if it is negative, it should be either -1 or -2 */ | 1179 /* if it is negative, it should be either -1 or -2 */ |
1176 » return (res > 0) ? 1 : Py_SAFE_DOWNCAST(res, Py_ssize_t, int); | 1180 return (res > 0) ? 1 : Py_SAFE_DOWNCAST(res, Py_ssize_t, int); |
1177 } | 1181 } |
1178 | 1182 |
1179 /* equivalent of 'not v' | 1183 /* equivalent of 'not v' |
1180 Return -1 if an error occurred */ | 1184 Return -1 if an error occurred */ |
1181 | 1185 |
1182 int | 1186 int |
1183 PyObject_Not(PyObject *v) | 1187 PyObject_Not(PyObject *v) |
1184 { | 1188 { |
1185 » int res; | 1189 int res; |
1186 » res = PyObject_IsTrue(v); | 1190 res = PyObject_IsTrue(v); |
1187 » if (res < 0) | 1191 if (res < 0) |
1188 » » return res; | 1192 return res; |
1189 » return res == 0; | 1193 return res == 0; |
1190 } | 1194 } |
1191 | 1195 |
1192 /* Test whether an object can be called */ | 1196 /* Test whether an object can be called */ |
1193 | 1197 |
1194 int | 1198 int |
1195 PyCallable_Check(PyObject *x) | 1199 PyCallable_Check(PyObject *x) |
1196 { | 1200 { |
1197 » if (x == NULL) | 1201 if (x == NULL) |
1198 » » return 0; | 1202 return 0; |
1199 » return x->ob_type->tp_call != NULL; | 1203 return x->ob_type->tp_call != NULL; |
1200 } | 1204 } |
1201 | 1205 |
1202 /* ------------------------- PyObject_Dir() helpers ------------------------- */ | 1206 /* ------------------------- PyObject_Dir() helpers ------------------------- */ |
1203 | 1207 |
1204 /* Helper for PyObject_Dir. | 1208 /* Helper for PyObject_Dir. |
1205 Merge the __dict__ of aclass into dict, and recursively also all | 1209 Merge the __dict__ of aclass into dict, and recursively also all |
1206 the __dict__s of aclass's base classes. The order of merging isn't | 1210 the __dict__s of aclass's base classes. The order of merging isn't |
1207 defined, as it's expected that only the final set of dict keys is | 1211 defined, as it's expected that only the final set of dict keys is |
1208 interesting. | 1212 interesting. |
1209 Return 0 on success, -1 on error. | 1213 Return 0 on success, -1 on error. |
1210 */ | 1214 */ |
1211 | 1215 |
1212 static int | 1216 static int |
1213 merge_class_dict(PyObject* dict, PyObject* aclass) | 1217 merge_class_dict(PyObject* dict, PyObject* aclass) |
1214 { | 1218 { |
1215 » PyObject *classdict; | 1219 PyObject *classdict; |
1216 » PyObject *bases; | 1220 PyObject *bases; |
1217 | 1221 |
1218 » assert(PyDict_Check(dict)); | 1222 assert(PyDict_Check(dict)); |
1219 » assert(aclass); | 1223 assert(aclass); |
1220 | 1224 |
1221 » /* Merge in the type's dict (if any). */ | 1225 /* Merge in the type's dict (if any). */ |
1222 » classdict = PyObject_GetAttrString(aclass, "__dict__"); | 1226 classdict = PyObject_GetAttrString(aclass, "__dict__"); |
1223 » if (classdict == NULL) | 1227 if (classdict == NULL) |
1224 » » PyErr_Clear(); | 1228 PyErr_Clear(); |
1225 » else { | 1229 else { |
1226 » » int status = PyDict_Update(dict, classdict); | 1230 int status = PyDict_Update(dict, classdict); |
1227 » » Py_DECREF(classdict); | 1231 Py_DECREF(classdict); |
1228 » » if (status < 0) | 1232 if (status < 0) |
1229 » » » return -1; | 1233 return -1; |
1230 » } | 1234 } |
1231 | 1235 |
1232 » /* Recursively merge in the base types' (if any) dicts. */ | 1236 /* Recursively merge in the base types' (if any) dicts. */ |
1233 » bases = PyObject_GetAttrString(aclass, "__bases__"); | 1237 bases = PyObject_GetAttrString(aclass, "__bases__"); |
1234 » if (bases == NULL) | 1238 if (bases == NULL) |
1235 » » PyErr_Clear(); | 1239 PyErr_Clear(); |
1236 » else { | 1240 else { |
1237 » » /* We have no guarantee that bases is a real tuple */ | 1241 /* We have no guarantee that bases is a real tuple */ |
1238 » » Py_ssize_t i, n; | 1242 Py_ssize_t i, n; |
1239 » » n = PySequence_Size(bases); /* This better be right */ | 1243 n = PySequence_Size(bases); /* This better be right */ |
1240 » » if (n < 0) | 1244 if (n < 0) |
1241 » » » PyErr_Clear(); | 1245 PyErr_Clear(); |
1242 » » else { | 1246 else { |
1243 » » » for (i = 0; i < n; i++) { | 1247 for (i = 0; i < n; i++) { |
1244 » » » » int status; | 1248 int status; |
1245 » » » » PyObject *base = PySequence_GetItem(bases, i); | 1249 PyObject *base = PySequence_GetItem(bases, i); |
1246 » » » » if (base == NULL) { | 1250 if (base == NULL) { |
1247 » » » » » Py_DECREF(bases); | 1251 Py_DECREF(bases); |
1248 » » » » » return -1; | 1252 return -1; |
1249 » » » » } | 1253 } |
1250 » » » » status = merge_class_dict(dict, base); | 1254 status = merge_class_dict(dict, base); |
1251 » » » » Py_DECREF(base); | 1255 Py_DECREF(base); |
1252 » » » » if (status < 0) { | 1256 if (status < 0) { |
1253 » » » » » Py_DECREF(bases); | 1257 Py_DECREF(bases); |
1254 » » » » » return -1; | 1258 return -1; |
1255 » » » » } | 1259 } |
1256 » » » } | 1260 } |
1257 » » } | 1261 } |
1258 » » Py_DECREF(bases); | 1262 Py_DECREF(bases); |
1259 » } | 1263 } |
1260 » return 0; | 1264 return 0; |
1261 } | 1265 } |
1262 | 1266 |
1263 /* Helper for PyObject_Dir without arguments: returns the local scope. */ | 1267 /* Helper for PyObject_Dir without arguments: returns the local scope. */ |
1264 static PyObject * | 1268 static PyObject * |
1265 _dir_locals(void) | 1269 _dir_locals(void) |
1266 { | 1270 { |
1267 » PyObject *names; | 1271 PyObject *names; |
1268 » PyObject *locals = PyEval_GetLocals(); | 1272 PyObject *locals = PyEval_GetLocals(); |
1269 | 1273 |
1270 » if (locals == NULL) { | 1274 if (locals == NULL) { |
1271 » » PyErr_SetString(PyExc_SystemError, "frame does not exist"); | 1275 PyErr_SetString(PyExc_SystemError, "frame does not exist"); |
1272 » » return NULL; | 1276 return NULL; |
1273 » } | 1277 } |
1274 | 1278 |
1275 » names = PyMapping_Keys(locals); | 1279 names = PyMapping_Keys(locals); |
1276 » if (!names) | 1280 if (!names) |
1277 » » return NULL; | 1281 return NULL; |
1278 » if (!PyList_Check(names)) { | 1282 if (!PyList_Check(names)) { |
1279 » » PyErr_Format(PyExc_TypeError, | 1283 PyErr_Format(PyExc_TypeError, |
1280 » » » "dir(): expected keys() of locals to be a list, " | 1284 "dir(): expected keys() of locals to be a list, " |
1281 » » » "not '%.200s'", Py_TYPE(names)->tp_name); | 1285 "not '%.200s'", Py_TYPE(names)->tp_name); |
1282 » » Py_DECREF(names); | 1286 Py_DECREF(names); |
1283 » » return NULL; | 1287 return NULL; |
1284 » } | 1288 } |
1285 » /* the locals don't need to be DECREF'd */ | 1289 /* the locals don't need to be DECREF'd */ |
1286 » return names; | 1290 return names; |
1287 } | 1291 } |
1288 | 1292 |
1289 /* Helper for PyObject_Dir of type objects: returns __dict__ and __bases__. | 1293 /* Helper for PyObject_Dir of type objects: returns __dict__ and __bases__. |
1290 We deliberately don't suck up its __class__, as methods belonging to the | 1294 We deliberately don't suck up its __class__, as methods belonging to the |
1291 metaclass would probably be more confusing than helpful. | 1295 metaclass would probably be more confusing than helpful. |
1292 */ | 1296 */ |
1293 static PyObject * | 1297 static PyObject * |
1294 _specialized_dir_type(PyObject *obj) | 1298 _specialized_dir_type(PyObject *obj) |
1295 { | 1299 { |
1296 » PyObject *result = NULL; | 1300 PyObject *result = NULL; |
1297 » PyObject *dict = PyDict_New(); | 1301 PyObject *dict = PyDict_New(); |
1298 | 1302 |
1299 » if (dict != NULL && merge_class_dict(dict, obj) == 0) | 1303 if (dict != NULL && merge_class_dict(dict, obj) == 0) |
1300 » » result = PyDict_Keys(dict); | 1304 result = PyDict_Keys(dict); |
1301 | 1305 |
1302 » Py_XDECREF(dict); | 1306 Py_XDECREF(dict); |
1303 » return result; | 1307 return result; |
1304 } | 1308 } |
1305 | 1309 |
1306 /* Helper for PyObject_Dir of module objects: returns the module's __dict__. */ | 1310 /* Helper for PyObject_Dir of module objects: returns the module's __dict__. */ |
1307 static PyObject * | 1311 static PyObject * |
1308 _specialized_dir_module(PyObject *obj) | 1312 _specialized_dir_module(PyObject *obj) |
1309 { | 1313 { |
1310 » PyObject *result = NULL; | 1314 PyObject *result = NULL; |
1311 » PyObject *dict = PyObject_GetAttrString(obj, "__dict__"); | 1315 PyObject *dict = PyObject_GetAttrString(obj, "__dict__"); |
1312 | 1316 |
1313 » if (dict != NULL) { | 1317 if (dict != NULL) { |
1314 » » if (PyDict_Check(dict)) | 1318 if (PyDict_Check(dict)) |
1315 » » » result = PyDict_Keys(dict); | 1319 result = PyDict_Keys(dict); |
1316 » » else { | 1320 else { |
1317 » » » const char *name = PyModule_GetName(obj); | 1321 const char *name = PyModule_GetName(obj); |
1318 » » » if (name) | 1322 if (name) |
1319 » » » » PyErr_Format(PyExc_TypeError, | 1323 PyErr_Format(PyExc_TypeError, |
1320 » » » » » "%.200s.__dict__ is not a dictionar
y", | 1324 "%.200s.__dict__ is not a dictionary", |
1321 » » » » » name); | 1325 name); |
1322 » » } | 1326 } |
1323 » } | 1327 } |
1324 | 1328 |
1325 » Py_XDECREF(dict); | 1329 Py_XDECREF(dict); |
1326 » return result; | 1330 return result; |
1327 } | 1331 } |
1328 | 1332 |
1329 /* Helper for PyObject_Dir of generic objects: returns __dict__, __class__, | 1333 /* Helper for PyObject_Dir of generic objects: returns __dict__, __class__, |
1330 and recursively up the __class__.__bases__ chain. | 1334 and recursively up the __class__.__bases__ chain. |
1331 */ | 1335 */ |
1332 static PyObject * | 1336 static PyObject * |
1333 _generic_dir(PyObject *obj) | 1337 _generic_dir(PyObject *obj) |
1334 { | 1338 { |
1335 » PyObject *result = NULL; | 1339 PyObject *result = NULL; |
1336 » PyObject *dict = NULL; | 1340 PyObject *dict = NULL; |
1337 » PyObject *itsclass = NULL; | 1341 PyObject *itsclass = NULL; |
1338 | 1342 |
1339 » /* Get __dict__ (which may or may not be a real dict...) */ | 1343 /* Get __dict__ (which may or may not be a real dict...) */ |
1340 » dict = PyObject_GetAttrString(obj, "__dict__"); | 1344 dict = PyObject_GetAttrString(obj, "__dict__"); |
1341 » if (dict == NULL) { | 1345 if (dict == NULL) { |
1342 » » PyErr_Clear(); | 1346 PyErr_Clear(); |
1343 » » dict = PyDict_New(); | 1347 dict = PyDict_New(); |
1344 » } | 1348 } |
1345 » else if (!PyDict_Check(dict)) { | 1349 else if (!PyDict_Check(dict)) { |
1346 » » Py_DECREF(dict); | 1350 Py_DECREF(dict); |
1347 » » dict = PyDict_New(); | 1351 dict = PyDict_New(); |
1348 » } | 1352 } |
1349 » else { | 1353 else { |
1350 » » /* Copy __dict__ to avoid mutating it. */ | 1354 /* Copy __dict__ to avoid mutating it. */ |
1351 » » PyObject *temp = PyDict_Copy(dict); | 1355 PyObject *temp = PyDict_Copy(dict); |
1352 » » Py_DECREF(dict); | 1356 Py_DECREF(dict); |
1353 » » dict = temp; | 1357 dict = temp; |
1354 » } | 1358 } |
1355 | 1359 |
1356 » if (dict == NULL) | 1360 if (dict == NULL) |
1357 » » goto error; | 1361 goto error; |
1358 | 1362 |
1359 » /* Merge in attrs reachable from its class. */ | 1363 /* Merge in attrs reachable from its class. */ |
1360 » itsclass = PyObject_GetAttrString(obj, "__class__"); | 1364 itsclass = PyObject_GetAttrString(obj, "__class__"); |
1361 » if (itsclass == NULL) | 1365 if (itsclass == NULL) |
1362 » » /* XXX(tomer): Perhaps fall back to obj->ob_type if no | 1366 /* XXX(tomer): Perhaps fall back to obj->ob_type if no |
1363 » » __class__ exists? */ | 1367 __class__ exists? */ |
1364 » » PyErr_Clear(); | 1368 PyErr_Clear(); |
1365 » else { | 1369 else { |
1366 » » if (merge_class_dict(dict, itsclass) != 0) | 1370 if (merge_class_dict(dict, itsclass) != 0) |
1367 » » » goto error; | 1371 goto error; |
1368 » } | 1372 } |
1369 | 1373 |
1370 » result = PyDict_Keys(dict); | 1374 result = PyDict_Keys(dict); |
1371 » /* fall through */ | 1375 /* fall through */ |
1372 error: | 1376 error: |
1373 » Py_XDECREF(itsclass); | 1377 Py_XDECREF(itsclass); |
1374 » Py_XDECREF(dict); | 1378 Py_XDECREF(dict); |
1375 » return result; | 1379 return result; |
1376 } | 1380 } |
1377 | 1381 |
1378 /* Helper for PyObject_Dir: object introspection. | 1382 /* Helper for PyObject_Dir: object introspection. |
1379 This calls one of the above specialized versions if no __dir__ method | 1383 This calls one of the above specialized versions if no __dir__ method |
1380 exists. */ | 1384 exists. */ |
1381 static PyObject * | 1385 static PyObject * |
1382 _dir_object(PyObject *obj) | 1386 _dir_object(PyObject *obj) |
1383 { | 1387 { |
1384 » PyObject * result = NULL; | 1388 PyObject * result = NULL; |
1385 » PyObject * dirfunc = PyObject_GetAttrString((PyObject*)obj->ob_type, | 1389 PyObject * dirfunc = PyObject_GetAttrString((PyObject*)obj->ob_type, |
1386 » » » » » » "__dir__"); | 1390 "__dir__"); |
1387 | 1391 |
1388 » assert(obj); | 1392 assert(obj); |
1389 » if (dirfunc == NULL) { | 1393 if (dirfunc == NULL) { |
1390 » » /* use default implementation */ | 1394 /* use default implementation */ |
1391 » » PyErr_Clear(); | 1395 PyErr_Clear(); |
1392 » » if (PyModule_Check(obj)) | 1396 if (PyModule_Check(obj)) |
1393 » » » result = _specialized_dir_module(obj); | 1397 result = _specialized_dir_module(obj); |
1394 » » else if (PyType_Check(obj)) | 1398 else if (PyType_Check(obj)) |
1395 » » » result = _specialized_dir_type(obj); | 1399 result = _specialized_dir_type(obj); |
1396 » » else | 1400 else |
1397 » » » result = _generic_dir(obj); | 1401 result = _generic_dir(obj); |
1398 » } | 1402 } |
1399 » else { | 1403 else { |
1400 » » /* use __dir__ */ | 1404 /* use __dir__ */ |
1401 » » result = PyObject_CallFunctionObjArgs(dirfunc, obj, NULL); | 1405 result = PyObject_CallFunctionObjArgs(dirfunc, obj, NULL); |
1402 » » Py_DECREF(dirfunc); | 1406 Py_DECREF(dirfunc); |
1403 » » if (result == NULL) | 1407 if (result == NULL) |
1404 » » » return NULL; | 1408 return NULL; |
1405 | 1409 |
1406 » » /* result must be a list */ | 1410 /* result must be a list */ |
1407 » » /* XXX(gbrandl): could also check if all items are strings */ | 1411 /* XXX(gbrandl): could also check if all items are strings */ |
1408 » » if (!PyList_Check(result)) { | 1412 if (!PyList_Check(result)) { |
1409 » » » PyErr_Format(PyExc_TypeError, | 1413 PyErr_Format(PyExc_TypeError, |
1410 » » » » "__dir__() must return a list, not %.200s", | 1414 "__dir__() must return a list, not %.200s", |
1411 » » » » Py_TYPE(result)->tp_name); | 1415 Py_TYPE(result)->tp_name); |
1412 » » » Py_DECREF(result); | 1416 Py_DECREF(result); |
1413 » » » result = NULL; | 1417 result = NULL; |
1414 » » } | 1418 } |
1415 » } | 1419 } |
1416 | 1420 |
1417 » return result; | 1421 return result; |
1418 } | 1422 } |
1419 | 1423 |
1420 /* Implementation of dir() -- if obj is NULL, returns the names in the current | 1424 /* Implementation of dir() -- if obj is NULL, returns the names in the current |
1421 (local) scope. Otherwise, performs introspection of the object: returns a | 1425 (local) scope. Otherwise, performs introspection of the object: returns a |
1422 sorted list of attribute names (supposedly) accessible from the object | 1426 sorted list of attribute names (supposedly) accessible from the object |
1423 */ | 1427 */ |
1424 PyObject * | 1428 PyObject * |
1425 PyObject_Dir(PyObject *obj) | 1429 PyObject_Dir(PyObject *obj) |
1426 { | 1430 { |
1427 » PyObject * result; | 1431 PyObject * result; |
1428 | 1432 |
1429 » if (obj == NULL) | 1433 if (obj == NULL) |
1430 » » /* no object -- introspect the locals */ | 1434 /* no object -- introspect the locals */ |
1431 » » result = _dir_locals(); | 1435 result = _dir_locals(); |
1432 » else | 1436 else |
1433 » » /* object -- introspect the object */ | 1437 /* object -- introspect the object */ |
1434 » » result = _dir_object(obj); | 1438 result = _dir_object(obj); |
1435 | 1439 |
1436 » assert(result == NULL || PyList_Check(result)); | 1440 assert(result == NULL || PyList_Check(result)); |
1437 | 1441 |
1438 » if (result != NULL && PyList_Sort(result) != 0) { | 1442 if (result != NULL && PyList_Sort(result) != 0) { |
1439 » » /* sorting the list failed */ | 1443 /* sorting the list failed */ |
1440 » » Py_DECREF(result); | 1444 Py_DECREF(result); |
1441 » » result = NULL; | 1445 result = NULL; |
1442 » } | 1446 } |
1443 | 1447 |
1444 » return result; | 1448 return result; |
1445 } | 1449 } |
1446 | 1450 |
1447 /* | 1451 /* |
1448 NoObject is usable as a non-NULL undefined value, used by the macro None. | 1452 NoObject is usable as a non-NULL undefined value, used by the macro None. |
1449 There is (and should be!) no way to create other objects of this type, | 1453 There is (and should be!) no way to create other objects of this type, |
1450 so there is exactly one (which is indestructible, by the way). | 1454 so there is exactly one (which is indestructible, by the way). |
1451 (XXX This type and the type of NotImplemented below should be unified.) | 1455 (XXX This type and the type of NotImplemented below should be unified.) |
1452 */ | 1456 */ |
1453 | 1457 |
1454 /* ARGSUSED */ | 1458 /* ARGSUSED */ |
1455 static PyObject * | 1459 static PyObject * |
1456 none_repr(PyObject *op) | 1460 none_repr(PyObject *op) |
1457 { | 1461 { |
1458 » return PyUnicode_FromString("None"); | 1462 return PyUnicode_FromString("None"); |
1459 } | 1463 } |
1460 | 1464 |
1461 /* ARGUSED */ | 1465 /* ARGUSED */ |
1462 static void | 1466 static void |
1463 none_dealloc(PyObject* ignore) | 1467 none_dealloc(PyObject* ignore) |
1464 { | 1468 { |
1465 » /* This should never get called, but we also don't want to SEGV if | 1469 /* This should never get called, but we also don't want to SEGV if |
1466 » * we accidentally decref None out of existence. | 1470 * we accidentally decref None out of existence. |
1467 » */ | 1471 */ |
1468 » Py_FatalError("deallocating None"); | 1472 Py_FatalError("deallocating None"); |
1469 } | 1473 } |
1470 | 1474 |
1471 | 1475 |
1472 static PyTypeObject PyNone_Type = { | 1476 static PyTypeObject PyNone_Type = { |
1473 » PyVarObject_HEAD_INIT(&PyType_Type, 0) | 1477 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
1474 » "NoneType", | 1478 "NoneType", |
1475 » 0, | 1479 0, |
1476 » 0, | 1480 0, |
1477 » none_dealloc,» /*tp_dealloc*/ /*never called*/ | 1481 none_dealloc, /*tp_dealloc*/ /*never called*/ |
1478 » 0,» » /*tp_print*/ | 1482 0, /*tp_print*/ |
1479 » 0,» » /*tp_getattr*/ | 1483 0, /*tp_getattr*/ |
1480 » 0,» » /*tp_setattr*/ | 1484 0, /*tp_setattr*/ |
1481 » 0,» » /*tp_reserved*/ | 1485 0, /*tp_reserved*/ |
1482 » none_repr,» /*tp_repr*/ | 1486 none_repr, /*tp_repr*/ |
1483 » 0,» » /*tp_as_number*/ | 1487 0, /*tp_as_number*/ |
1484 » 0,» » /*tp_as_sequence*/ | 1488 0, /*tp_as_sequence*/ |
1485 » 0,» » /*tp_as_mapping*/ | 1489 0, /*tp_as_mapping*/ |
1486 » 0,» » /*tp_hash */ | 1490 0, /*tp_hash */ |
1487 }; | 1491 }; |
1488 | 1492 |
1489 PyObject _Py_NoneStruct = { | 1493 PyObject _Py_NoneStruct = { |
1490 _PyObject_EXTRA_INIT | 1494 _PyObject_EXTRA_INIT |
1491 1, &PyNone_Type | 1495 1, &PyNone_Type |
1492 }; | 1496 }; |
1493 | 1497 |
1494 /* NotImplemented is an object that can be used to signal that an | 1498 /* NotImplemented is an object that can be used to signal that an |
1495 operation is not implemented for the given type combination. */ | 1499 operation is not implemented for the given type combination. */ |
1496 | 1500 |
1497 static PyObject * | 1501 static PyObject * |
1498 NotImplemented_repr(PyObject *op) | 1502 NotImplemented_repr(PyObject *op) |
1499 { | 1503 { |
1500 » return PyUnicode_FromString("NotImplemented"); | 1504 return PyUnicode_FromString("NotImplemented"); |
1501 } | 1505 } |
1502 | 1506 |
1503 static PyTypeObject PyNotImplemented_Type = { | 1507 static PyTypeObject PyNotImplemented_Type = { |
1504 » PyVarObject_HEAD_INIT(&PyType_Type, 0) | 1508 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
1505 » "NotImplementedType", | 1509 "NotImplementedType", |
1506 » 0, | 1510 0, |
1507 » 0, | 1511 0, |
1508 » none_dealloc,» /*tp_dealloc*/ /*never called*/ | 1512 none_dealloc, /*tp_dealloc*/ /*never called*/ |
1509 » 0,» » /*tp_print*/ | 1513 0, /*tp_print*/ |
1510 » 0,» » /*tp_getattr*/ | 1514 0, /*tp_getattr*/ |
1511 » 0,» » /*tp_setattr*/ | 1515 0, /*tp_setattr*/ |
1512 » 0,» » /*tp_reserved*/ | 1516 0, /*tp_reserved*/ |
1513 » NotImplemented_repr, /*tp_repr*/ | 1517 NotImplemented_repr, /*tp_repr*/ |
1514 » 0,» » /*tp_as_number*/ | 1518 0, /*tp_as_number*/ |
1515 » 0,» » /*tp_as_sequence*/ | 1519 0, /*tp_as_sequence*/ |
1516 » 0,» » /*tp_as_mapping*/ | 1520 0, /*tp_as_mapping*/ |
1517 » 0,» » /*tp_hash */ | 1521 0, /*tp_hash */ |
1518 }; | 1522 }; |
1519 | 1523 |
1520 PyObject _Py_NotImplementedStruct = { | 1524 PyObject _Py_NotImplementedStruct = { |
1521 » _PyObject_EXTRA_INIT | 1525 _PyObject_EXTRA_INIT |
1522 » 1, &PyNotImplemented_Type | 1526 1, &PyNotImplemented_Type |
1523 }; | 1527 }; |
1524 | 1528 |
1525 void | 1529 void |
1526 _Py_ReadyTypes(void) | 1530 _Py_ReadyTypes(void) |
1527 { | 1531 { |
1528 » if (PyType_Ready(&PyType_Type) < 0) | 1532 if (PyType_Ready(&PyType_Type) < 0) |
1529 » » Py_FatalError("Can't initialize type type"); | 1533 Py_FatalError("Can't initialize type type"); |
1530 | 1534 |
1531 » if (PyType_Ready(&_PyWeakref_RefType) < 0) | 1535 if (PyType_Ready(&_PyWeakref_RefType) < 0) |
1532 » » Py_FatalError("Can't initialize weakref type"); | 1536 Py_FatalError("Can't initialize weakref type"); |
1533 | 1537 |
1534 » if (PyType_Ready(&_PyWeakref_CallableProxyType) < 0) | 1538 if (PyType_Ready(&_PyWeakref_CallableProxyType) < 0) |
1535 » » Py_FatalError("Can't initialize callable weakref proxy type"); | 1539 Py_FatalError("Can't initialize callable weakref proxy type"); |
1536 | 1540 |
1537 » if (PyType_Ready(&_PyWeakref_ProxyType) < 0) | 1541 if (PyType_Ready(&_PyWeakref_ProxyType) < 0) |
1538 » » Py_FatalError("Can't initialize weakref proxy type"); | 1542 Py_FatalError("Can't initialize weakref proxy type"); |
1539 | 1543 |
1540 » if (PyType_Ready(&PyBool_Type) < 0) | 1544 if (PyType_Ready(&PyBool_Type) < 0) |
1541 » » Py_FatalError("Can't initialize bool type"); | 1545 Py_FatalError("Can't initialize bool type"); |
1542 | 1546 |
1543 » if (PyType_Ready(&PyByteArray_Type) < 0) | 1547 if (PyType_Ready(&PyByteArray_Type) < 0) |
1544 » » Py_FatalError("Can't initialize bytearray type"); | 1548 Py_FatalError("Can't initialize bytearray type"); |
1545 | 1549 |
1546 » if (PyType_Ready(&PyBytes_Type) < 0) | 1550 if (PyType_Ready(&PyBytes_Type) < 0) |
1547 » » Py_FatalError("Can't initialize 'str'"); | 1551 Py_FatalError("Can't initialize 'str'"); |
1548 | 1552 |
1549 » if (PyType_Ready(&PyList_Type) < 0) | 1553 if (PyType_Ready(&PyList_Type) < 0) |
1550 » » Py_FatalError("Can't initialize list type"); | 1554 Py_FatalError("Can't initialize list type"); |
1551 | 1555 |
1552 » if (PyType_Ready(&PyNone_Type) < 0) | 1556 if (PyType_Ready(&PyNone_Type) < 0) |
1553 » » Py_FatalError("Can't initialize None type"); | 1557 Py_FatalError("Can't initialize None type"); |
1554 | 1558 |
1555 » if (PyType_Ready(Py_Ellipsis->ob_type) < 0) | 1559 if (PyType_Ready(Py_Ellipsis->ob_type) < 0) |
1556 » » Py_FatalError("Can't initialize type(Ellipsis)"); | 1560 Py_FatalError("Can't initialize type(Ellipsis)"); |
1557 | 1561 |
1558 » if (PyType_Ready(&PyNotImplemented_Type) < 0) | 1562 if (PyType_Ready(&PyNotImplemented_Type) < 0) |
1559 » » Py_FatalError("Can't initialize NotImplemented type"); | 1563 Py_FatalError("Can't initialize NotImplemented type"); |
1560 | 1564 |
1561 » if (PyType_Ready(&PyTraceBack_Type) < 0) | 1565 if (PyType_Ready(&PyTraceBack_Type) < 0) |
1562 » » Py_FatalError("Can't initialize traceback type"); | 1566 Py_FatalError("Can't initialize traceback type"); |
1563 | 1567 |
1564 » if (PyType_Ready(&PySuper_Type) < 0) | 1568 if (PyType_Ready(&PySuper_Type) < 0) |
1565 » » Py_FatalError("Can't initialize super type"); | 1569 Py_FatalError("Can't initialize super type"); |
1566 | 1570 |
1567 » if (PyType_Ready(&PyBaseObject_Type) < 0) | 1571 if (PyType_Ready(&PyBaseObject_Type) < 0) |
1568 » » Py_FatalError("Can't initialize object type"); | 1572 Py_FatalError("Can't initialize object type"); |
1569 | 1573 |
1570 » if (PyType_Ready(&PyRange_Type) < 0) | 1574 if (PyType_Ready(&PyRange_Type) < 0) |
1571 » » Py_FatalError("Can't initialize range type"); | 1575 Py_FatalError("Can't initialize range type"); |
1572 | 1576 |
1573 » if (PyType_Ready(&PyDict_Type) < 0) | 1577 if (PyType_Ready(&PyDict_Type) < 0) |
1574 » » Py_FatalError("Can't initialize dict type"); | 1578 Py_FatalError("Can't initialize dict type"); |
1575 | 1579 |
1576 » if (PyType_Ready(&PySet_Type) < 0) | 1580 if (PyType_Ready(&PySet_Type) < 0) |
1577 » » Py_FatalError("Can't initialize set type"); | 1581 Py_FatalError("Can't initialize set type"); |
1578 | 1582 |
1579 » if (PyType_Ready(&PyUnicode_Type) < 0) | 1583 if (PyType_Ready(&PyUnicode_Type) < 0) |
1580 » » Py_FatalError("Can't initialize str type"); | 1584 Py_FatalError("Can't initialize str type"); |
1581 | 1585 |
1582 » if (PyType_Ready(&PySlice_Type) < 0) | 1586 if (PyType_Ready(&PySlice_Type) < 0) |
1583 » » Py_FatalError("Can't initialize slice type"); | 1587 Py_FatalError("Can't initialize slice type"); |
1584 | 1588 |
1585 » if (PyType_Ready(&PyStaticMethod_Type) < 0) | 1589 if (PyType_Ready(&PyStaticMethod_Type) < 0) |
1586 » » Py_FatalError("Can't initialize static method type"); | 1590 Py_FatalError("Can't initialize static method type"); |
1587 | 1591 |
1588 » if (PyType_Ready(&PyComplex_Type) < 0) | 1592 if (PyType_Ready(&PyComplex_Type) < 0) |
1589 » » Py_FatalError("Can't initialize complex type"); | 1593 Py_FatalError("Can't initialize complex type"); |
1590 | 1594 |
1591 » if (PyType_Ready(&PyFloat_Type) < 0) | 1595 if (PyType_Ready(&PyFloat_Type) < 0) |
1592 » » Py_FatalError("Can't initialize float type"); | 1596 Py_FatalError("Can't initialize float type"); |
1593 | 1597 |
1594 » if (PyType_Ready(&PyLong_Type) < 0) | 1598 if (PyType_Ready(&PyLong_Type) < 0) |
1595 » » Py_FatalError("Can't initialize int type"); | 1599 Py_FatalError("Can't initialize int type"); |
1596 | 1600 |
1597 » if (PyType_Ready(&PyFrozenSet_Type) < 0) | 1601 if (PyType_Ready(&PyFrozenSet_Type) < 0) |
1598 » » Py_FatalError("Can't initialize frozenset type"); | 1602 Py_FatalError("Can't initialize frozenset type"); |
1599 | 1603 |
1600 » if (PyType_Ready(&PyProperty_Type) < 0) | 1604 if (PyType_Ready(&PyProperty_Type) < 0) |
1601 » » Py_FatalError("Can't initialize property type"); | 1605 Py_FatalError("Can't initialize property type"); |
1602 | 1606 |
1603 » if (PyType_Ready(&PyMemoryView_Type) < 0) | 1607 if (PyType_Ready(&PyMemoryView_Type) < 0) |
1604 » » Py_FatalError("Can't initialize memoryview type"); | 1608 Py_FatalError("Can't initialize memoryview type"); |
1605 | 1609 |
1606 » if (PyType_Ready(&PyTuple_Type) < 0) | 1610 if (PyType_Ready(&PyTuple_Type) < 0) |
1607 » » Py_FatalError("Can't initialize tuple type"); | 1611 Py_FatalError("Can't initialize tuple type"); |
1608 | 1612 |
1609 » if (PyType_Ready(&PyEnum_Type) < 0) | 1613 if (PyType_Ready(&PyEnum_Type) < 0) |
1610 » » Py_FatalError("Can't initialize enumerate type"); | 1614 Py_FatalError("Can't initialize enumerate type"); |
1611 | 1615 |
1612 » if (PyType_Ready(&PyReversed_Type) < 0) | 1616 if (PyType_Ready(&PyReversed_Type) < 0) |
1613 » » Py_FatalError("Can't initialize reversed type"); | 1617 Py_FatalError("Can't initialize reversed type"); |
1614 | 1618 |
1615 » if (PyType_Ready(&PyStdPrinter_Type) < 0) | 1619 if (PyType_Ready(&PyStdPrinter_Type) < 0) |
1616 » » Py_FatalError("Can't initialize StdPrinter"); | 1620 Py_FatalError("Can't initialize StdPrinter"); |
1617 | 1621 |
1618 » if (PyType_Ready(&PyCode_Type) < 0) | 1622 if (PyType_Ready(&PyCode_Type) < 0) |
1619 » » Py_FatalError("Can't initialize code type"); | 1623 Py_FatalError("Can't initialize code type"); |
1620 | 1624 |
1621 » if (PyType_Ready(&PyFrame_Type) < 0) | 1625 if (PyType_Ready(&PyFrame_Type) < 0) |
1622 » » Py_FatalError("Can't initialize frame type"); | 1626 Py_FatalError("Can't initialize frame type"); |
1623 | 1627 |
1624 » if (PyType_Ready(&PyCFunction_Type) < 0) | 1628 if (PyType_Ready(&PyCFunction_Type) < 0) |
1625 » » Py_FatalError("Can't initialize builtin function type"); | 1629 Py_FatalError("Can't initialize builtin function type"); |
1626 | 1630 |
1627 » if (PyType_Ready(&PyMethod_Type) < 0) | 1631 if (PyType_Ready(&PyMethod_Type) < 0) |
1628 » » Py_FatalError("Can't initialize method type"); | 1632 Py_FatalError("Can't initialize method type"); |
1629 | 1633 |
1630 » if (PyType_Ready(&PyFunction_Type) < 0) | 1634 if (PyType_Ready(&PyFunction_Type) < 0) |
1631 » » Py_FatalError("Can't initialize function type"); | 1635 Py_FatalError("Can't initialize function type"); |
1632 | 1636 |
1633 » if (PyType_Ready(&PyDictProxy_Type) < 0) | 1637 if (PyType_Ready(&PyDictProxy_Type) < 0) |
1634 » » Py_FatalError("Can't initialize dict proxy type"); | 1638 Py_FatalError("Can't initialize dict proxy type"); |
1635 | 1639 |
1636 » if (PyType_Ready(&PyGen_Type) < 0) | 1640 if (PyType_Ready(&PyGen_Type) < 0) |
1637 » » Py_FatalError("Can't initialize generator type"); | 1641 Py_FatalError("Can't initialize generator type"); |
1638 | 1642 |
1639 » if (PyType_Ready(&PyGetSetDescr_Type) < 0) | 1643 if (PyType_Ready(&PyGetSetDescr_Type) < 0) |
1640 » » Py_FatalError("Can't initialize get-set descriptor type"); | 1644 Py_FatalError("Can't initialize get-set descriptor type"); |
1641 | 1645 |
1642 » if (PyType_Ready(&PyWrapperDescr_Type) < 0) | 1646 if (PyType_Ready(&PyWrapperDescr_Type) < 0) |
1643 » » Py_FatalError("Can't initialize wrapper type"); | 1647 Py_FatalError("Can't initialize wrapper type"); |
1644 | 1648 |
1645 » if (PyType_Ready(&PyEllipsis_Type) < 0) | 1649 if (PyType_Ready(&PyEllipsis_Type) < 0) |
1646 » » Py_FatalError("Can't initialize ellipsis type"); | 1650 Py_FatalError("Can't initialize ellipsis type"); |
1647 | 1651 |
1648 » if (PyType_Ready(&PyMemberDescr_Type) < 0) | 1652 if (PyType_Ready(&PyMemberDescr_Type) < 0) |
1649 » » Py_FatalError("Can't initialize member descriptor type"); | 1653 Py_FatalError("Can't initialize member descriptor type"); |
1650 | 1654 |
1651 » if (PyType_Ready(&PyFilter_Type) < 0) | 1655 if (PyType_Ready(&PyFilter_Type) < 0) |
1652 » » Py_FatalError("Can't initialize filter type"); | 1656 Py_FatalError("Can't initialize filter type"); |
1653 | 1657 |
1654 » if (PyType_Ready(&PyMap_Type) < 0) | 1658 if (PyType_Ready(&PyMap_Type) < 0) |
1655 » » Py_FatalError("Can't initialize map type"); | 1659 Py_FatalError("Can't initialize map type"); |
1656 | 1660 |
1657 » if (PyType_Ready(&PyZip_Type) < 0) | 1661 if (PyType_Ready(&PyZip_Type) < 0) |
1658 » » Py_FatalError("Can't initialize zip type"); | 1662 Py_FatalError("Can't initialize zip type"); |
1659 } | 1663 } |
1660 | 1664 |
1661 | 1665 |
1662 #ifdef Py_TRACE_REFS | 1666 #ifdef Py_TRACE_REFS |
1663 | 1667 |
1664 void | 1668 void |
1665 _Py_NewReference(PyObject *op) | 1669 _Py_NewReference(PyObject *op) |
1666 { | 1670 { |
1667 » _Py_INC_REFTOTAL; | 1671 _Py_INC_REFTOTAL; |
1668 » op->ob_refcnt = 1; | 1672 op->ob_refcnt = 1; |
1669 » _Py_AddToAllObjects(op, 1); | 1673 _Py_AddToAllObjects(op, 1); |
1670 » _Py_INC_TPALLOCS(op); | 1674 _Py_INC_TPALLOCS(op); |
1671 } | 1675 } |
1672 | 1676 |
1673 void | 1677 void |
1674 _Py_ForgetReference(register PyObject *op) | 1678 _Py_ForgetReference(register PyObject *op) |
1675 { | 1679 { |
1676 #ifdef SLOW_UNREF_CHECK | 1680 #ifdef SLOW_UNREF_CHECK |
1677 register PyObject *p; | 1681 register PyObject *p; |
1678 #endif | 1682 #endif |
1679 » if (op->ob_refcnt < 0) | 1683 if (op->ob_refcnt < 0) |
1680 » » Py_FatalError("UNREF negative refcnt"); | 1684 Py_FatalError("UNREF negative refcnt"); |
1681 » if (op == &refchain || | 1685 if (op == &refchain || |
1682 » op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op) { | 1686 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op) { |
1683 » » fprintf(stderr, "* ob\n"); | 1687 fprintf(stderr, "* ob\n"); |
1684 » » _PyObject_Dump(op); | 1688 _PyObject_Dump(op); |
1685 » » fprintf(stderr, "* op->_ob_prev->_ob_next\n"); | 1689 fprintf(stderr, "* op->_ob_prev->_ob_next\n"); |
1686 » » _PyObject_Dump(op->_ob_prev->_ob_next); | 1690 _PyObject_Dump(op->_ob_prev->_ob_next); |
1687 » » fprintf(stderr, "* op->_ob_next->_ob_prev\n"); | 1691 fprintf(stderr, "* op->_ob_next->_ob_prev\n"); |
1688 » » _PyObject_Dump(op->_ob_next->_ob_prev); | 1692 _PyObject_Dump(op->_ob_next->_ob_prev); |
1689 » » Py_FatalError("UNREF invalid object"); | 1693 Py_FatalError("UNREF invalid object"); |
1690 » } | 1694 } |
1691 #ifdef SLOW_UNREF_CHECK | 1695 #ifdef SLOW_UNREF_CHECK |
1692 » for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) { | 1696 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) { |
1693 » » if (p == op) | 1697 if (p == op) |
1694 » » » break; | 1698 break; |
1695 » } | 1699 } |
1696 » if (p == &refchain) /* Not found */ | 1700 if (p == &refchain) /* Not found */ |
1697 » » Py_FatalError("UNREF unknown object"); | 1701 Py_FatalError("UNREF unknown object"); |
1698 #endif | 1702 #endif |
1699 » op->_ob_next->_ob_prev = op->_ob_prev; | 1703 op->_ob_next->_ob_prev = op->_ob_prev; |
1700 » op->_ob_prev->_ob_next = op->_ob_next; | 1704 op->_ob_prev->_ob_next = op->_ob_next; |
1701 » op->_ob_next = op->_ob_prev = NULL; | 1705 op->_ob_next = op->_ob_prev = NULL; |
1702 » _Py_INC_TPFREES(op); | 1706 _Py_INC_TPFREES(op); |
1703 } | 1707 } |
1704 | 1708 |
1705 void | 1709 void |
1706 _Py_Dealloc(PyObject *op) | 1710 _Py_Dealloc(PyObject *op) |
1707 { | 1711 { |
1708 » destructor dealloc = Py_TYPE(op)->tp_dealloc; | 1712 destructor dealloc = Py_TYPE(op)->tp_dealloc; |
1709 » _Py_ForgetReference(op); | 1713 _Py_ForgetReference(op); |
1710 » (*dealloc)(op); | 1714 (*dealloc)(op); |
1711 } | 1715 } |
1712 | 1716 |
1713 /* Print all live objects. Because PyObject_Print is called, the | 1717 /* Print all live objects. Because PyObject_Print is called, the |
1714 * interpreter must be in a healthy state. | 1718 * interpreter must be in a healthy state. |
1715 */ | 1719 */ |
1716 void | 1720 void |
1717 _Py_PrintReferences(FILE *fp) | 1721 _Py_PrintReferences(FILE *fp) |
1718 { | 1722 { |
1719 » PyObject *op; | 1723 PyObject *op; |
1720 » fprintf(fp, "Remaining objects:\n"); | 1724 fprintf(fp, "Remaining objects:\n"); |
1721 » for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) { | 1725 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) { |
1722 » » fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] ", op, op->ob_refcnt); | 1726 fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] ", op, op->ob_refcnt); |
1723 » » if (PyObject_Print(op, fp, 0) != 0) | 1727 if (PyObject_Print(op, fp, 0) != 0) |
1724 » » » PyErr_Clear(); | 1728 PyErr_Clear(); |
1725 » » putc('\n', fp); | 1729 putc('\n', fp); |
1726 » } | 1730 } |
1727 } | 1731 } |
1728 | 1732 |
1729 /* Print the addresses of all live objects. Unlike _Py_PrintReferences, this | 1733 /* Print the addresses of all live objects. Unlike _Py_PrintReferences, this |
1730 * doesn't make any calls to the Python C API, so is always safe to call. | 1734 * doesn't make any calls to the Python C API, so is always safe to call. |
1731 */ | 1735 */ |
1732 void | 1736 void |
1733 _Py_PrintReferenceAddresses(FILE *fp) | 1737 _Py_PrintReferenceAddresses(FILE *fp) |
1734 { | 1738 { |
1735 » PyObject *op; | 1739 PyObject *op; |
1736 » fprintf(fp, "Remaining object addresses:\n"); | 1740 fprintf(fp, "Remaining object addresses:\n"); |
1737 » for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) | 1741 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) |
1738 » » fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] %s\n", op, | 1742 fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] %s\n", op, |
1739 » » » op->ob_refcnt, Py_TYPE(op)->tp_name); | 1743 op->ob_refcnt, Py_TYPE(op)->tp_name); |
1740 } | 1744 } |
1741 | 1745 |
1742 PyObject * | 1746 PyObject * |
1743 _Py_GetObjects(PyObject *self, PyObject *args) | 1747 _Py_GetObjects(PyObject *self, PyObject *args) |
1744 { | 1748 { |
1745 » int i, n; | 1749 int i, n; |
1746 » PyObject *t = NULL; | 1750 PyObject *t = NULL; |
1747 » PyObject *res, *op; | 1751 PyObject *res, *op; |
1748 | 1752 |
1749 » if (!PyArg_ParseTuple(args, "i|O", &n, &t)) | 1753 if (!PyArg_ParseTuple(args, "i|O", &n, &t)) |
1750 » » return NULL; | 1754 return NULL; |
1751 » op = refchain._ob_next; | 1755 op = refchain._ob_next; |
1752 » res = PyList_New(0); | 1756 res = PyList_New(0); |
1753 » if (res == NULL) | 1757 if (res == NULL) |
1754 » » return NULL; | 1758 return NULL; |
1755 » for (i = 0; (n == 0 || i < n) && op != &refchain; i++) { | 1759 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) { |
1756 » » while (op == self || op == args || op == res || op == t || | 1760 while (op == self || op == args || op == res || op == t || |
1757 » » (t != NULL && Py_TYPE(op) != (PyTypeObject *) t)) { | 1761 (t != NULL && Py_TYPE(op) != (PyTypeObject *) t)) { |
1758 » » » op = op->_ob_next; | 1762 op = op->_ob_next; |
1759 » » » if (op == &refchain) | 1763 if (op == &refchain) |
1760 » » » » return res; | 1764 return res; |
1761 » » } | 1765 } |
1762 » » if (PyList_Append(res, op) < 0) { | 1766 if (PyList_Append(res, op) < 0) { |
1763 » » » Py_DECREF(res); | 1767 Py_DECREF(res); |
1764 » » » return NULL; | 1768 return NULL; |
1765 » » } | 1769 } |
1766 » » op = op->_ob_next; | 1770 op = op->_ob_next; |
1767 » } | 1771 } |
1768 » return res; | 1772 return res; |
1769 } | 1773 } |
1770 | 1774 |
1771 #endif | 1775 #endif |
1772 | 1776 |
1773 | 1777 |
1774 /* Hack to force loading of pycapsule.o */ | 1778 /* Hack to force loading of pycapsule.o */ |
1775 PyTypeObject *_PyCapsule_hack = &PyCapsule_Type; | 1779 PyTypeObject *_PyCapsule_hack = &PyCapsule_Type; |
1776 | 1780 |
1777 | 1781 |
1778 /* Hack to force loading of abstract.o */ | 1782 /* Hack to force loading of abstract.o */ |
1779 Py_ssize_t (*_Py_abstract_hack)(PyObject *) = PyObject_Size; | 1783 Py_ssize_t (*_Py_abstract_hack)(PyObject *) = PyObject_Size; |
1780 | 1784 |
1781 | 1785 |
1782 /* Python's malloc wrappers (see pymem.h) */ | 1786 /* Python's malloc wrappers (see pymem.h) */ |
1783 | 1787 |
1784 void * | 1788 void * |
1785 PyMem_Malloc(size_t nbytes) | 1789 PyMem_Malloc(size_t nbytes) |
1786 { | 1790 { |
1787 » return PyMem_MALLOC(nbytes); | 1791 return PyMem_MALLOC(nbytes); |
1788 } | 1792 } |
1789 | 1793 |
1790 void * | 1794 void * |
1791 PyMem_Realloc(void *p, size_t nbytes) | 1795 PyMem_Realloc(void *p, size_t nbytes) |
1792 { | 1796 { |
1793 » return PyMem_REALLOC(p, nbytes); | 1797 return PyMem_REALLOC(p, nbytes); |
1794 } | 1798 } |
1795 | 1799 |
1796 void | 1800 void |
1797 PyMem_Free(void *p) | 1801 PyMem_Free(void *p) |
1798 { | 1802 { |
1799 » PyMem_FREE(p); | 1803 PyMem_FREE(p); |
1800 } | 1804 } |
1801 | 1805 |
1802 | 1806 |
1803 /* These methods are used to control infinite recursion in repr, str, print, | 1807 /* These methods are used to control infinite recursion in repr, str, print, |
1804 etc. Container objects that may recursively contain themselves, | 1808 etc. Container objects that may recursively contain themselves, |
1805 e.g. builtin dictionaries and lists, should used Py_ReprEnter() and | 1809 e.g. builtin dictionaries and lists, should used Py_ReprEnter() and |
1806 Py_ReprLeave() to avoid infinite recursion. | 1810 Py_ReprLeave() to avoid infinite recursion. |
1807 | 1811 |
1808 Py_ReprEnter() returns 0 the first time it is called for a particular | 1812 Py_ReprEnter() returns 0 the first time it is called for a particular |
1809 object and 1 every time thereafter. It returns -1 if an exception | 1813 object and 1 every time thereafter. It returns -1 if an exception |
1810 occurred. Py_ReprLeave() has no return value. | 1814 occurred. Py_ReprLeave() has no return value. |
1811 | 1815 |
1812 See dictobject.c and listobject.c for examples of use. | 1816 See dictobject.c and listobject.c for examples of use. |
1813 */ | 1817 */ |
1814 | 1818 |
1815 #define KEY "Py_Repr" | 1819 #define KEY "Py_Repr" |
1816 | 1820 |
1817 int | 1821 int |
1818 Py_ReprEnter(PyObject *obj) | 1822 Py_ReprEnter(PyObject *obj) |
1819 { | 1823 { |
1820 » PyObject *dict; | 1824 PyObject *dict; |
1821 » PyObject *list; | 1825 PyObject *list; |
1822 » Py_ssize_t i; | 1826 Py_ssize_t i; |
1823 | 1827 |
1824 » dict = PyThreadState_GetDict(); | 1828 dict = PyThreadState_GetDict(); |
1825 » if (dict == NULL) | 1829 if (dict == NULL) |
1826 » » return 0; | 1830 return 0; |
1827 » list = PyDict_GetItemString(dict, KEY); | 1831 list = PyDict_GetItemString(dict, KEY); |
1828 » if (list == NULL) { | 1832 if (list == NULL) { |
1829 » » list = PyList_New(0); | 1833 list = PyList_New(0); |
1830 » » if (list == NULL) | 1834 if (list == NULL) |
1831 » » » return -1; | 1835 return -1; |
1832 » » if (PyDict_SetItemString(dict, KEY, list) < 0) | 1836 if (PyDict_SetItemString(dict, KEY, list) < 0) |
1833 » » » return -1; | 1837 return -1; |
1834 » » Py_DECREF(list); | 1838 Py_DECREF(list); |
1835 » } | 1839 } |
1836 » i = PyList_GET_SIZE(list); | 1840 i = PyList_GET_SIZE(list); |
1837 » while (--i >= 0) { | 1841 while (--i >= 0) { |
1838 » » if (PyList_GET_ITEM(list, i) == obj) | 1842 if (PyList_GET_ITEM(list, i) == obj) |
1839 » » » return 1; | 1843 return 1; |
1840 » } | 1844 } |
1841 » PyList_Append(list, obj); | 1845 PyList_Append(list, obj); |
1842 » return 0; | 1846 return 0; |
1843 } | 1847 } |
1844 | 1848 |
1845 void | 1849 void |
1846 Py_ReprLeave(PyObject *obj) | 1850 Py_ReprLeave(PyObject *obj) |
1847 { | 1851 { |
1848 » PyObject *dict; | 1852 PyObject *dict; |
1849 » PyObject *list; | 1853 PyObject *list; |
1850 » Py_ssize_t i; | 1854 Py_ssize_t i; |
1851 | 1855 |
1852 » dict = PyThreadState_GetDict(); | 1856 dict = PyThreadState_GetDict(); |
1853 » if (dict == NULL) | 1857 if (dict == NULL) |
1854 » » return; | 1858 return; |
1855 » list = PyDict_GetItemString(dict, KEY); | 1859 list = PyDict_GetItemString(dict, KEY); |
1856 » if (list == NULL || !PyList_Check(list)) | 1860 if (list == NULL || !PyList_Check(list)) |
1857 » » return; | 1861 return; |
1858 » i = PyList_GET_SIZE(list); | 1862 i = PyList_GET_SIZE(list); |
1859 » /* Count backwards because we always expect obj to be list[-1] */ | 1863 /* Count backwards because we always expect obj to be list[-1] */ |
1860 » while (--i >= 0) { | 1864 while (--i >= 0) { |
1861 » » if (PyList_GET_ITEM(list, i) == obj) { | 1865 if (PyList_GET_ITEM(list, i) == obj) { |
1862 » » » PyList_SetSlice(list, i, i + 1, NULL); | 1866 PyList_SetSlice(list, i, i + 1, NULL); |
1863 » » » break; | 1867 break; |
1864 » » } | 1868 } |
1865 » } | 1869 } |
1866 } | 1870 } |
1867 | 1871 |
1868 /* Trashcan support. */ | 1872 /* Trashcan support. */ |
1869 | 1873 |
1870 /* Current call-stack depth of tp_dealloc calls. */ | 1874 /* Current call-stack depth of tp_dealloc calls. */ |
1871 int _PyTrash_delete_nesting = 0; | 1875 int _PyTrash_delete_nesting = 0; |
1872 | 1876 |
1873 /* List of objects that still need to be cleaned up, singly linked via their | 1877 /* List of objects that still need to be cleaned up, singly linked via their |
1874 * gc headers' gc_prev pointers. | 1878 * gc headers' gc_prev pointers. |
1875 */ | 1879 */ |
1876 PyObject *_PyTrash_delete_later = NULL; | 1880 PyObject *_PyTrash_delete_later = NULL; |
1877 | 1881 |
1878 /* Add op to the _PyTrash_delete_later list. Called when the current | 1882 /* Add op to the _PyTrash_delete_later list. Called when the current |
1879 * call-stack depth gets large. op must be a currently untracked gc'ed | 1883 * call-stack depth gets large. op must be a currently untracked gc'ed |
1880 * object, with refcount 0. Py_DECREF must already have been called on it. | 1884 * object, with refcount 0. Py_DECREF must already have been called on it. |
1881 */ | 1885 */ |
1882 void | 1886 void |
1883 _PyTrash_deposit_object(PyObject *op) | 1887 _PyTrash_deposit_object(PyObject *op) |
1884 { | 1888 { |
1885 » assert(PyObject_IS_GC(op)); | 1889 assert(PyObject_IS_GC(op)); |
1886 » assert(_Py_AS_GC(op)->gc.gc_refs == _PyGC_REFS_UNTRACKED); | 1890 assert(_Py_AS_GC(op)->gc.gc_refs == _PyGC_REFS_UNTRACKED); |
1887 » assert(op->ob_refcnt == 0); | 1891 assert(op->ob_refcnt == 0); |
1888 » _Py_AS_GC(op)->gc.gc_prev = (PyGC_Head *)_PyTrash_delete_later; | 1892 _Py_AS_GC(op)->gc.gc_prev = (PyGC_Head *)_PyTrash_delete_later; |
1889 » _PyTrash_delete_later = op; | 1893 _PyTrash_delete_later = op; |
1890 } | 1894 } |
1891 | 1895 |
1892 /* Dealloccate all the objects in the _PyTrash_delete_later list. Called when | 1896 /* Dealloccate all the objects in the _PyTrash_delete_later list. Called when |
1893 * the call-stack unwinds again. | 1897 * the call-stack unwinds again. |
1894 */ | 1898 */ |
1895 void | 1899 void |
1896 _PyTrash_destroy_chain(void) | 1900 _PyTrash_destroy_chain(void) |
1897 { | 1901 { |
1898 » while (_PyTrash_delete_later) { | 1902 while (_PyTrash_delete_later) { |
1899 » » PyObject *op = _PyTrash_delete_later; | 1903 PyObject *op = _PyTrash_delete_later; |
1900 » » destructor dealloc = Py_TYPE(op)->tp_dealloc; | 1904 destructor dealloc = Py_TYPE(op)->tp_dealloc; |
1901 | 1905 |
1902 » » _PyTrash_delete_later = | 1906 _PyTrash_delete_later = |
1903 » » » (PyObject*) _Py_AS_GC(op)->gc.gc_prev; | 1907 (PyObject*) _Py_AS_GC(op)->gc.gc_prev; |
1904 | 1908 |
1905 » » /* Call the deallocator directly. This used to try to | 1909 /* Call the deallocator directly. This used to try to |
1906 » » * fool Py_DECREF into calling it indirectly, but | 1910 * fool Py_DECREF into calling it indirectly, but |
1907 » » * Py_DECREF was already called on this object, and in | 1911 * Py_DECREF was already called on this object, and in |
1908 » » * assorted non-release builds calling Py_DECREF again ends | 1912 * assorted non-release builds calling Py_DECREF again ends |
1909 » » * up distorting allocation statistics. | 1913 * up distorting allocation statistics. |
1910 » » */ | 1914 */ |
1911 » » assert(op->ob_refcnt == 0); | 1915 assert(op->ob_refcnt == 0); |
1912 » » ++_PyTrash_delete_nesting; | 1916 ++_PyTrash_delete_nesting; |
1913 » » (*dealloc)(op); | 1917 (*dealloc)(op); |
1914 » » --_PyTrash_delete_nesting; | 1918 --_PyTrash_delete_nesting; |
1915 » } | 1919 } |
1916 } | 1920 } |
1917 | 1921 |
1918 #ifdef __cplusplus | 1922 #ifdef __cplusplus |
1919 } | 1923 } |
1920 #endif | 1924 #endif |
LEFT | RIGHT |