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