File: | object.c |
Location: | line 1171, column 10 |
Description: | Access to field 'ob_type' results in a dereference of a null pointer (loaded from variable 'name') |
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 | int Py_Py3kWarningFlag; | |||||
34 | ||||||
35 | /* Object allocation routines used by NEWOBJ and NEWVAROBJ macros. | |||||
36 | These are used by the individual routines for object creation. | |||||
37 | Do not call them otherwise, they do not initialize the object! */ | |||||
38 | ||||||
39 | #ifdef Py_TRACE_REFS | |||||
40 | /* Head of circular doubly-linked list of all objects. These are linked | |||||
41 | * together via the _ob_prev and _ob_next members of a PyObject, which | |||||
42 | * exist only in a Py_TRACE_REFS build. | |||||
43 | */ | |||||
44 | static PyObject refchain = {&refchain, &refchain}; | |||||
45 | ||||||
46 | /* Insert op at the front of the list of all objects. If force is true, | |||||
47 | * op is added even if _ob_prev and _ob_next are non-NULL already. If | |||||
48 | * force is false amd _ob_prev or _ob_next are non-NULL, do nothing. | |||||
49 | * force should be true if and only if op points to freshly allocated, | |||||
50 | * uninitialized memory, or you've unlinked op from the list and are | |||||
51 | * relinking it into the front. | |||||
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 | |||||
54 | * way, though; exceptions include statically allocated type objects, and | |||||
55 | * statically allocated singletons (like Py_True and Py_None). | |||||
56 | */ | |||||
57 | void | |||||
58 | _Py_AddToAllObjects(PyObject *op, int force) | |||||
59 | { | |||||
60 | #ifdef Py_DEBUG | |||||
61 | if (!force) { | |||||
62 | /* If it's initialized memory, op must be in or out of | |||||
63 | * the list unambiguously. | |||||
64 | */ | |||||
65 | assert((op->_ob_prev == NULL) == (op->_ob_next == NULL))((void) (0)); | |||||
66 | } | |||||
67 | #endif | |||||
68 | if (force || op->_ob_prev == NULL((void*)0)) { | |||||
69 | op->_ob_next = refchain._ob_next; | |||||
70 | op->_ob_prev = &refchain; | |||||
71 | refchain._ob_next->_ob_prev = op; | |||||
72 | refchain._ob_next = op; | |||||
73 | } | |||||
74 | } | |||||
75 | #endif /* Py_TRACE_REFS */ | |||||
76 | ||||||
77 | #ifdef COUNT_ALLOCS | |||||
78 | static PyTypeObject *type_list; | |||||
79 | /* All types are added to type_list, at least when | |||||
80 | they get one object created. That makes them | |||||
81 | immortal, which unfortunately contributes to | |||||
82 | garbage itself. If unlist_types_without_objects | |||||
83 | is set, they will be removed from the type_list | |||||
84 | once the last object is deallocated. */ | |||||
85 | static int unlist_types_without_objects; | |||||
86 | extern Py_ssize_t tuple_zero_allocs, fast_tuple_allocs; | |||||
87 | extern Py_ssize_t quick_int_allocs, quick_neg_int_allocs; | |||||
88 | extern Py_ssize_t null_strings, one_strings; | |||||
89 | void | |||||
90 | dump_counts(FILE* f) | |||||
91 | { | |||||
92 | PyTypeObject *tp; | |||||
93 | ||||||
94 | for (tp = type_list; tp; tp = tp->tp_next) | |||||
95 | fprintf(f, "%s alloc'd: %" PY_FORMAT_SIZE_T"z" "d, " | |||||
96 | "freed: %" PY_FORMAT_SIZE_T"z" "d, " | |||||
97 | "max in use: %" PY_FORMAT_SIZE_T"z" "d\n", | |||||
98 | tp->tp_name, tp->tp_allocs, tp->tp_frees, | |||||
99 | tp->tp_maxalloc); | |||||
100 | fprintf(f, "fast tuple allocs: %" PY_FORMAT_SIZE_T"z" "d, " | |||||
101 | "empty: %" PY_FORMAT_SIZE_T"z" "d\n", | |||||
102 | fast_tuple_allocs, tuple_zero_allocs); | |||||
103 | fprintf(f, "fast int allocs: pos: %" PY_FORMAT_SIZE_T"z" "d, " | |||||
104 | "neg: %" PY_FORMAT_SIZE_T"z" "d\n", | |||||
105 | quick_int_allocs, quick_neg_int_allocs); | |||||
106 | fprintf(f, "null strings: %" PY_FORMAT_SIZE_T"z" "d, " | |||||
107 | "1-strings: %" PY_FORMAT_SIZE_T"z" "d\n", | |||||
108 | null_strings, one_strings); | |||||
109 | } | |||||
110 | ||||||
111 | PyObject * | |||||
112 | get_counts(void) | |||||
113 | { | |||||
114 | PyTypeObject *tp; | |||||
115 | PyObject *result; | |||||
116 | PyObject *v; | |||||
117 | ||||||
118 | result = PyList_New(0); | |||||
119 | if (result == NULL((void*)0)) | |||||
120 | return NULL((void*)0); | |||||
121 | for (tp = type_list; tp; tp = tp->tp_next) { | |||||
122 | v = Py_BuildValue("(snnn)", tp->tp_name, tp->tp_allocs, | |||||
123 | tp->tp_frees, tp->tp_maxalloc); | |||||
124 | if (v == NULL((void*)0)) { | |||||
125 | Py_DECREF(result)do { if ( --((PyObject*)(result))->ob_refcnt != 0) ; else ( (*(((PyObject*)((PyObject *)(result)))->ob_type)->tp_dealloc )((PyObject *)((PyObject *)(result)))); } while (0); | |||||
126 | return NULL((void*)0); | |||||
127 | } | |||||
128 | if (PyList_Append(result, v) < 0) { | |||||
129 | Py_DECREF(v)do { if ( --((PyObject*)(v))->ob_refcnt != 0) ; else ( (*( ((PyObject*)((PyObject *)(v)))->ob_type)->tp_dealloc)(( PyObject *)((PyObject *)(v)))); } while (0); | |||||
130 | Py_DECREF(result)do { if ( --((PyObject*)(result))->ob_refcnt != 0) ; else ( (*(((PyObject*)((PyObject *)(result)))->ob_type)->tp_dealloc )((PyObject *)((PyObject *)(result)))); } while (0); | |||||
131 | return NULL((void*)0); | |||||
132 | } | |||||
133 | Py_DECREF(v)do { if ( --((PyObject*)(v))->ob_refcnt != 0) ; else ( (*( ((PyObject*)((PyObject *)(v)))->ob_type)->tp_dealloc)(( PyObject *)((PyObject *)(v)))); } while (0); | |||||
134 | } | |||||
135 | return result; | |||||
136 | } | |||||
137 | ||||||
138 | void | |||||
139 | inc_count(PyTypeObject *tp) | |||||
140 | { | |||||
141 | if (tp->tp_next == NULL((void*)0) && tp->tp_prev == NULL((void*)0)) { | |||||
142 | /* first time; insert in linked list */ | |||||
143 | if (tp->tp_next != NULL((void*)0)) /* sanity check */ | |||||
144 | Py_FatalError("XXX inc_count sanity check"); | |||||
145 | if (type_list) | |||||
146 | type_list->tp_prev = tp; | |||||
147 | tp->tp_next = type_list; | |||||
148 | /* Note that as of Python 2.2, heap-allocated type objects | |||||
149 | * can go away, but this code requires that they stay alive | |||||
150 | * until program exit. That's why we're careful with | |||||
151 | * refcounts here. type_list gets a new reference to tp, | |||||
152 | * while ownership of the reference type_list used to hold | |||||
153 | * (if any) was transferred to tp->tp_next in the line above. | |||||
154 | * tp is thus effectively immortal after this. | |||||
155 | */ | |||||
156 | Py_INCREF(tp)( ((PyObject*)(tp))->ob_refcnt++); | |||||
157 | type_list = tp; | |||||
158 | #ifdef Py_TRACE_REFS | |||||
159 | /* Also insert in the doubly-linked list of all objects, | |||||
160 | * if not already there. | |||||
161 | */ | |||||
162 | _Py_AddToAllObjects((PyObject *)tp, 0); | |||||
163 | #endif | |||||
164 | } | |||||
165 | tp->tp_allocs++; | |||||
166 | if (tp->tp_allocs - tp->tp_frees > tp->tp_maxalloc) | |||||
167 | tp->tp_maxalloc = tp->tp_allocs - tp->tp_frees; | |||||
168 | } | |||||
169 | ||||||
170 | void dec_count(PyTypeObject *tp) | |||||
171 | { | |||||
172 | tp->tp_frees++; | |||||
173 | if (unlist_types_without_objects && | |||||
174 | tp->tp_allocs == tp->tp_frees) { | |||||
175 | /* unlink the type from type_list */ | |||||
176 | if (tp->tp_prev) | |||||
177 | tp->tp_prev->tp_next = tp->tp_next; | |||||
178 | else | |||||
179 | type_list = tp->tp_next; | |||||
180 | if (tp->tp_next) | |||||
181 | tp->tp_next->tp_prev = tp->tp_prev; | |||||
182 | tp->tp_next = tp->tp_prev = NULL((void*)0); | |||||
183 | Py_DECREF(tp)do { if ( --((PyObject*)(tp))->ob_refcnt != 0) ; else ( (* (((PyObject*)((PyObject *)(tp)))->ob_type)->tp_dealloc) ((PyObject *)((PyObject *)(tp)))); } while (0); | |||||
184 | } | |||||
185 | } | |||||
186 | ||||||
187 | #endif | |||||
188 | ||||||
189 | #ifdef Py_REF_DEBUG | |||||
190 | /* Log a fatal error; doesn't return. */ | |||||
191 | void | |||||
192 | _Py_NegativeRefcount(const char *fname, int lineno, PyObject *op) | |||||
193 | { | |||||
194 | char buf[300]; | |||||
195 | ||||||
196 | PyOS_snprintf(buf, sizeof(buf), | |||||
197 | "%s:%i object at %p has negative ref count " | |||||
198 | "%" PY_FORMAT_SIZE_T"z" "d", | |||||
199 | fname, lineno, op, op->ob_refcnt); | |||||
200 | Py_FatalError(buf); | |||||
201 | } | |||||
202 | ||||||
203 | #endif /* Py_REF_DEBUG */ | |||||
204 | ||||||
205 | void | |||||
206 | Py_IncRef(PyObject *o) | |||||
207 | { | |||||
208 | Py_XINCREF(o)do { if ((o) == ((void*)0)) ; else ( ((PyObject*)(o))->ob_refcnt ++); } while (0); | |||||
209 | } | |||||
210 | ||||||
211 | void | |||||
212 | Py_DecRef(PyObject *o) | |||||
213 | { | |||||
214 | Py_XDECREF(o)do { if ((o) == ((void*)0)) ; else do { if ( --((PyObject*)(o ))->ob_refcnt != 0) ; else ( (*(((PyObject*)((PyObject *)( o)))->ob_type)->tp_dealloc)((PyObject *)((PyObject *)(o )))); } while (0); } while (0); | |||||
215 | } | |||||
216 | ||||||
217 | PyObject * | |||||
218 | PyObject_Init(PyObject *op, PyTypeObject *tp) | |||||
219 | { | |||||
220 | if (op == NULL((void*)0)) | |||||
221 | return PyErr_NoMemory(); | |||||
222 | /* Any changes should be reflected in PyObject_INIT (objimpl.h) */ | |||||
223 | Py_TYPE(op)(((PyObject*)(op))->ob_type) = tp; | |||||
224 | _Py_NewReference(op)( (((PyObject*)(op))->ob_refcnt) = 1); | |||||
225 | return op; | |||||
226 | } | |||||
227 | ||||||
228 | PyVarObject * | |||||
229 | PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, Py_ssize_t size) | |||||
230 | { | |||||
231 | if (op == NULL((void*)0)) | |||||
232 | return (PyVarObject *) PyErr_NoMemory(); | |||||
233 | /* Any changes should be reflected in PyObject_INIT_VAR */ | |||||
234 | op->ob_size = size; | |||||
235 | Py_TYPE(op)(((PyObject*)(op))->ob_type) = tp; | |||||
236 | _Py_NewReference((PyObject *)op)( (((PyObject*)((PyObject *)op))->ob_refcnt) = 1); | |||||
237 | return op; | |||||
238 | } | |||||
239 | ||||||
240 | PyObject * | |||||
241 | _PyObject_New(PyTypeObject *tp) | |||||
242 | { | |||||
243 | PyObject *op; | |||||
244 | op = (PyObject *) PyObject_MALLOCPyObject_Malloc(_PyObject_SIZE(tp)( (tp)->tp_basicsize )); | |||||
245 | if (op == NULL((void*)0)) | |||||
246 | return PyErr_NoMemory(); | |||||
247 | return PyObject_INIT(op, tp)( (((PyObject*)(op))->ob_type) = (tp), ( (((PyObject*)((PyObject *)(op)))->ob_refcnt) = 1), (op) ); | |||||
248 | } | |||||
249 | ||||||
250 | PyVarObject * | |||||
251 | _PyObject_NewVar(PyTypeObject *tp, Py_ssize_t nitems) | |||||
252 | { | |||||
253 | PyVarObject *op; | |||||
254 | const size_t size = _PyObject_VAR_SIZE(tp, nitems)(size_t) ( ( (tp)->tp_basicsize + (nitems)*(tp)->tp_itemsize + (8 - 1) ) & ~(8 - 1) ); | |||||
255 | op = (PyVarObject *) PyObject_MALLOCPyObject_Malloc(size); | |||||
256 | if (op == NULL((void*)0)) | |||||
257 | return (PyVarObject *)PyErr_NoMemory(); | |||||
258 | return PyObject_INIT_VAR(op, tp, nitems)( (((PyVarObject*)(op))->ob_size) = (nitems), ( (((PyObject *)((op)))->ob_type) = ((tp)), ( (((PyObject*)((PyObject *) ((op))))->ob_refcnt) = 1), ((op)) ) ); | |||||
259 | } | |||||
260 | ||||||
261 | /* for binary compatibility with 2.2 */ | |||||
262 | #undef _PyObject_Del | |||||
263 | void | |||||
264 | _PyObject_Del(PyObject *op) | |||||
265 | { | |||||
266 | PyObject_FREEPyObject_Free(op); | |||||
267 | } | |||||
268 | ||||||
269 | /* Implementation of PyObject_Print with recursion checking */ | |||||
270 | static int | |||||
271 | internal_print(PyObject *op, FILE *fp, int flags, int nesting) | |||||
272 | { | |||||
273 | int ret = 0; | |||||
274 | if (nesting > 10) { | |||||
275 | PyErr_SetString(PyExc_RuntimeError, "print recursion"); | |||||
276 | return -1; | |||||
277 | } | |||||
278 | if (PyErr_CheckSignals()) | |||||
279 | return -1; | |||||
280 | #ifdef USE_STACKCHECK | |||||
281 | if (PyOS_CheckStack()) { | |||||
282 | PyErr_SetString(PyExc_MemoryError, "stack overflow"); | |||||
283 | return -1; | |||||
284 | } | |||||
285 | #endif | |||||
286 | clearerr(fp); /* Clear any previous error condition */ | |||||
287 | if (op == NULL((void*)0)) { | |||||
288 | Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread(); | |||||
289 | fprintf(fp, "<nil>"); | |||||
290 | Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); } | |||||
291 | } | |||||
292 | else { | |||||
293 | if (op->ob_refcnt <= 0) | |||||
294 | /* XXX(twouters) cast refcount to long until %zd is | |||||
295 | universally available */ | |||||
296 | Py_BEGIN_ALLOW_THREADS{ PyThreadState *_save; _save = PyEval_SaveThread(); | |||||
297 | fprintf(fp, "<refcnt %ld at %p>", | |||||
298 | (long)op->ob_refcnt, op); | |||||
299 | Py_END_ALLOW_THREADSPyEval_RestoreThread(_save); } | |||||
300 | else if (Py_TYPE(op)(((PyObject*)(op))->ob_type)->tp_print == NULL((void*)0)) { | |||||
301 | PyObject *s; | |||||
302 | if (flags & Py_PRINT_RAW1) | |||||
303 | s = PyObject_Str(op); | |||||
304 | else | |||||
305 | s = PyObject_Repr(op); | |||||
306 | if (s == NULL((void*)0)) | |||||
307 | ret = -1; | |||||
308 | else { | |||||
309 | ret = internal_print(s, fp, Py_PRINT_RAW1, | |||||
310 | nesting+1); | |||||
311 | } | |||||
312 | Py_XDECREF(s)do { if ((s) == ((void*)0)) ; else do { if ( --((PyObject*)(s ))->ob_refcnt != 0) ; else ( (*(((PyObject*)((PyObject *)( s)))->ob_type)->tp_dealloc)((PyObject *)((PyObject *)(s )))); } while (0); } while (0); | |||||
313 | } | |||||
314 | else | |||||
315 | ret = (*Py_TYPE(op)(((PyObject*)(op))->ob_type)->tp_print)(op, fp, flags); | |||||
316 | } | |||||
317 | if (ret == 0) { | |||||
318 | if (ferror(fp)) { | |||||
319 | PyErr_SetFromErrno(PyExc_IOError); | |||||
320 | clearerr(fp); | |||||
321 | ret = -1; | |||||
322 | } | |||||
323 | } | |||||
324 | return ret; | |||||
325 | } | |||||
326 | ||||||
327 | int | |||||
328 | PyObject_Print(PyObject *op, FILE *fp, int flags) | |||||
329 | { | |||||
330 | return internal_print(op, fp, flags, 0); | |||||
331 | } | |||||
332 | ||||||
333 | ||||||
334 | /* For debugging convenience. See Misc/gdbinit for some useful gdb hooks */ | |||||
335 | void _PyObject_Dump(PyObject* op) | |||||
336 | { | |||||
337 | if (op == NULL((void*)0)) | |||||
338 | fprintf(stderrstderr, "NULL\n"); | |||||
339 | else { | |||||
340 | #ifdef WITH_THREAD1 | |||||
341 | PyGILState_STATE gil; | |||||
342 | #endif | |||||
343 | fprintf(stderrstderr, "object : "); | |||||
344 | #ifdef WITH_THREAD1 | |||||
345 | gil = PyGILState_Ensure(); | |||||
346 | #endif | |||||
347 | (void)PyObject_Print(op, stderrstderr, 0); | |||||
348 | #ifdef WITH_THREAD1 | |||||
349 | PyGILState_Release(gil); | |||||
350 | #endif | |||||
351 | /* XXX(twouters) cast refcount to long until %zd is | |||||
352 | universally available */ | |||||
353 | fprintf(stderrstderr, "\n" | |||||
354 | "type : %s\n" | |||||
355 | "refcount: %ld\n" | |||||
356 | "address : %p\n", | |||||
357 | Py_TYPE(op)(((PyObject*)(op))->ob_type)==NULL((void*)0) ? "NULL" : Py_TYPE(op)(((PyObject*)(op))->ob_type)->tp_name, | |||||
358 | (long)op->ob_refcnt, | |||||
359 | op); | |||||
360 | } | |||||
361 | } | |||||
362 | ||||||
363 | PyObject * | |||||
364 | PyObject_Repr(PyObject *v) | |||||
365 | { | |||||
366 | if (PyErr_CheckSignals()) | |||||
367 | return NULL((void*)0); | |||||
368 | #ifdef USE_STACKCHECK | |||||
369 | if (PyOS_CheckStack()) { | |||||
370 | PyErr_SetString(PyExc_MemoryError, "stack overflow"); | |||||
371 | return NULL((void*)0); | |||||
372 | } | |||||
373 | #endif | |||||
374 | if (v == NULL((void*)0)) | |||||
375 | return PyString_FromString("<NULL>"); | |||||
376 | else if (Py_TYPE(v)(((PyObject*)(v))->ob_type)->tp_repr == NULL((void*)0)) | |||||
377 | return PyString_FromFormat("<%s object at %p>", | |||||
378 | Py_TYPE(v)(((PyObject*)(v))->ob_type)->tp_name, v); | |||||
379 | else { | |||||
380 | PyObject *res; | |||||
381 | res = (*Py_TYPE(v)(((PyObject*)(v))->ob_type)->tp_repr)(v); | |||||
382 | if (res == NULL((void*)0)) | |||||
383 | return NULL((void*)0); | |||||
384 | #ifdef Py_USING_UNICODE1 | |||||
385 | if (PyUnicode_Check(res)((((((PyObject*)(res))->ob_type))->tp_flags & ((1L<< 28))) != 0)) { | |||||
386 | PyObject* str; | |||||
387 | str = PyUnicode_AsEncodedStringPyUnicodeUCS4_AsEncodedString(res, NULL((void*)0), NULL((void*)0)); | |||||
388 | Py_DECREF(res)do { if ( --((PyObject*)(res))->ob_refcnt != 0) ; else ( ( *(((PyObject*)((PyObject *)(res)))->ob_type)->tp_dealloc )((PyObject *)((PyObject *)(res)))); } while (0); | |||||
389 | if (str) | |||||
390 | res = str; | |||||
391 | else | |||||
392 | return NULL((void*)0); | |||||
393 | } | |||||
394 | #endif | |||||
395 | if (!PyString_Check(res)((((((PyObject*)(res))->ob_type))->tp_flags & ((1L<< 27))) != 0)) { | |||||
396 | PyErr_Format(PyExc_TypeError, | |||||
397 | "__repr__ returned non-string (type %.200s)", | |||||
398 | Py_TYPE(res)(((PyObject*)(res))->ob_type)->tp_name); | |||||
399 | Py_DECREF(res)do { if ( --((PyObject*)(res))->ob_refcnt != 0) ; else ( ( *(((PyObject*)((PyObject *)(res)))->ob_type)->tp_dealloc )((PyObject *)((PyObject *)(res)))); } while (0); | |||||
400 | return NULL((void*)0); | |||||
401 | } | |||||
402 | return res; | |||||
403 | } | |||||
404 | } | |||||
405 | ||||||
406 | PyObject * | |||||
407 | _PyObject_Str(PyObject *v) | |||||
408 | { | |||||
409 | PyObject *res; | |||||
410 | int type_ok; | |||||
411 | if (v == NULL((void*)0)) | |||||
412 | return PyString_FromString("<NULL>"); | |||||
413 | if (PyString_CheckExact(v)((((PyObject*)(v))->ob_type) == &PyString_Type)) { | |||||
414 | Py_INCREF(v)( ((PyObject*)(v))->ob_refcnt++); | |||||
415 | return v; | |||||
416 | } | |||||
417 | #ifdef Py_USING_UNICODE1 | |||||
418 | if (PyUnicode_CheckExact(v)((((PyObject*)(v))->ob_type) == &PyUnicode_Type)) { | |||||
419 | Py_INCREF(v)( ((PyObject*)(v))->ob_refcnt++); | |||||
420 | return v; | |||||
421 | } | |||||
422 | #endif | |||||
423 | if (Py_TYPE(v)(((PyObject*)(v))->ob_type)->tp_str == NULL((void*)0)) | |||||
424 | return PyObject_Repr(v); | |||||
425 | ||||||
426 | /* It is possible for a type to have a tp_str representation that loops | |||||
427 | infinitely. */ | |||||
428 | if (Py_EnterRecursiveCall(" while getting the str of an object")((++((_PyThreadState_Current)->recursion_depth) > _Py_CheckRecursionLimit ) && _Py_CheckRecursiveCall(" while getting the str of an object" ))) | |||||
429 | return NULL((void*)0); | |||||
430 | res = (*Py_TYPE(v)(((PyObject*)(v))->ob_type)->tp_str)(v); | |||||
431 | Py_LeaveRecursiveCall()(--(_PyThreadState_Current)->recursion_depth); | |||||
432 | if (res == NULL((void*)0)) | |||||
433 | return NULL((void*)0); | |||||
434 | type_ok = PyString_Check(res)((((((PyObject*)(res))->ob_type))->tp_flags & ((1L<< 27))) != 0); | |||||
435 | #ifdef Py_USING_UNICODE1 | |||||
436 | type_ok = type_ok || PyUnicode_Check(res)((((((PyObject*)(res))->ob_type))->tp_flags & ((1L<< 28))) != 0); | |||||
437 | #endif | |||||
438 | if (!type_ok) { | |||||
439 | PyErr_Format(PyExc_TypeError, | |||||
440 | "__str__ returned non-string (type %.200s)", | |||||
441 | Py_TYPE(res)(((PyObject*)(res))->ob_type)->tp_name); | |||||
442 | Py_DECREF(res)do { if ( --((PyObject*)(res))->ob_refcnt != 0) ; else ( ( *(((PyObject*)((PyObject *)(res)))->ob_type)->tp_dealloc )((PyObject *)((PyObject *)(res)))); } while (0); | |||||
443 | return NULL((void*)0); | |||||
444 | } | |||||
445 | return res; | |||||
446 | } | |||||
447 | ||||||
448 | PyObject * | |||||
449 | PyObject_Str(PyObject *v) | |||||
450 | { | |||||
451 | PyObject *res = _PyObject_Str(v); | |||||
452 | if (res == NULL((void*)0)) | |||||
453 | return NULL((void*)0); | |||||
454 | #ifdef Py_USING_UNICODE1 | |||||
455 | if (PyUnicode_Check(res)((((((PyObject*)(res))->ob_type))->tp_flags & ((1L<< 28))) != 0)) { | |||||
456 | PyObject* str; | |||||
457 | str = PyUnicode_AsEncodedStringPyUnicodeUCS4_AsEncodedString(res, NULL((void*)0), NULL((void*)0)); | |||||
458 | Py_DECREF(res)do { if ( --((PyObject*)(res))->ob_refcnt != 0) ; else ( ( *(((PyObject*)((PyObject *)(res)))->ob_type)->tp_dealloc )((PyObject *)((PyObject *)(res)))); } while (0); | |||||
459 | if (str) | |||||
460 | res = str; | |||||
461 | else | |||||
462 | return NULL((void*)0); | |||||
463 | } | |||||
464 | #endif | |||||
465 | assert(PyString_Check(res))((void) (0)); | |||||
466 | return res; | |||||
467 | } | |||||
468 | ||||||
469 | #ifdef Py_USING_UNICODE1 | |||||
470 | PyObject * | |||||
471 | PyObject_Unicode(PyObject *v) | |||||
472 | { | |||||
473 | PyObject *res; | |||||
474 | PyObject *func; | |||||
475 | PyObject *str; | |||||
476 | int unicode_method_found = 0; | |||||
477 | static PyObject *unicodestr; | |||||
| ||||||
478 | ||||||
479 | if (v == NULL((void*)0)) { | |||||
480 | res = PyString_FromString("<NULL>"); | |||||
481 | if (res == NULL((void*)0)) | |||||
482 | return NULL((void*)0); | |||||
483 | str = PyUnicode_FromEncodedObjectPyUnicodeUCS4_FromEncodedObject(res, NULL((void*)0), "strict"); | |||||
484 | Py_DECREF(res)do { if ( --((PyObject*)(res))->ob_refcnt != 0) ; else ( ( *(((PyObject*)((PyObject *)(res)))->ob_type)->tp_dealloc )((PyObject *)((PyObject *)(res)))); } while (0); | |||||
485 | return str; | |||||
486 | } else if (PyUnicode_CheckExact(v)((((PyObject*)(v))->ob_type) == &PyUnicode_Type)) { | |||||
487 | Py_INCREF(v)( ((PyObject*)(v))->ob_refcnt++); | |||||
488 | return v; | |||||
489 | } | |||||
490 | ||||||
491 | if (PyInstance_Check(v)((v)->ob_type == &PyInstance_Type)) { | |||||
492 | /* We're an instance of a classic class */ | |||||
493 | /* Try __unicode__ from the instance -- alas we have no type */ | |||||
494 | func = PyObject_GetAttr(v, unicodestr); | |||||
495 | if (func != NULL((void*)0)) { | |||||
496 | unicode_method_found = 1; | |||||
497 | res = PyObject_CallFunctionObjArgs(func, NULL((void*)0)); | |||||
498 | Py_DECREF(func)do { if ( --((PyObject*)(func))->ob_refcnt != 0) ; else ( ( *(((PyObject*)((PyObject *)(func)))->ob_type)->tp_dealloc )((PyObject *)((PyObject *)(func)))); } while (0); | |||||
499 | } | |||||
500 | else { | |||||
501 | PyErr_Clear(); | |||||
502 | } | |||||
503 | } | |||||
504 | else { | |||||
505 | /* Not a classic class instance, try __unicode__. */ | |||||
506 | func = _PyObject_LookupSpecial(v, "__unicode__", &unicodestr); | |||||
507 | if (func != NULL((void*)0)) { | |||||
508 | unicode_method_found = 1; | |||||
509 | res = PyObject_CallFunctionObjArgs(func, NULL((void*)0)); | |||||
510 | Py_DECREF(func)do { if ( --((PyObject*)(func))->ob_refcnt != 0) ; else ( ( *(((PyObject*)((PyObject *)(func)))->ob_type)->tp_dealloc )((PyObject *)((PyObject *)(func)))); } while (0); | |||||
511 | } | |||||
512 | else if (PyErr_Occurred()) | |||||
513 | return NULL((void*)0); | |||||
514 | } | |||||
515 | ||||||
516 | /* Didn't find __unicode__ */ | |||||
517 | if (!unicode_method_found) { | |||||
518 | if (PyUnicode_Check(v)((((((PyObject*)(v))->ob_type))->tp_flags & ((1L<< 28))) != 0)) { | |||||
519 | /* For a Unicode subtype that's didn't overwrite __unicode__, | |||||
520 | return a true Unicode object with the same data. */ | |||||
521 | return PyUnicode_FromUnicodePyUnicodeUCS4_FromUnicode(PyUnicode_AS_UNICODE(v)(((PyUnicodeObject *)(v))->str), | |||||
522 | PyUnicode_GET_SIZE(v)(((PyUnicodeObject *)(v))->length)); | |||||
523 | } | |||||
524 | if (PyString_CheckExact(v)((((PyObject*)(v))->ob_type) == &PyString_Type)) { | |||||
525 | Py_INCREF(v)( ((PyObject*)(v))->ob_refcnt++); | |||||
526 | res = v; | |||||
527 | } | |||||
528 | else { | |||||
529 | if (Py_TYPE(v)(((PyObject*)(v))->ob_type)->tp_str != NULL((void*)0)) | |||||
530 | res = (*Py_TYPE(v)(((PyObject*)(v))->ob_type)->tp_str)(v); | |||||
531 | else | |||||
532 | res = PyObject_Repr(v); | |||||
533 | } | |||||
534 | } | |||||
535 | ||||||
536 | if (res == NULL((void*)0)) | |||||
537 | return NULL((void*)0); | |||||
538 | if (!PyUnicode_Check(res)((((((PyObject*)(res))->ob_type))->tp_flags & ((1L<< 28))) != 0)) { | |||||
539 | str = PyUnicode_FromEncodedObjectPyUnicodeUCS4_FromEncodedObject(res, NULL((void*)0), "strict"); | |||||
540 | Py_DECREF(res)do { if ( --((PyObject*)(res))->ob_refcnt != 0) ; else ( ( *(((PyObject*)((PyObject *)(res)))->ob_type)->tp_dealloc )((PyObject *)((PyObject *)(res)))); } while (0); | |||||
541 | res = str; | |||||
542 | } | |||||
543 | return res; | |||||
544 | } | |||||
545 | #endif | |||||
546 | ||||||
547 | ||||||
548 | /* Helper to warn about deprecated tp_compare return values. Return: | |||||
549 | -2 for an exception; | |||||
550 | -1 if v < w; | |||||
551 | 0 if v == w; | |||||
552 | 1 if v > w. | |||||
553 | (This function cannot return 2.) | |||||
554 | */ | |||||
555 | static int | |||||
556 | adjust_tp_compare(int c) | |||||
557 | { | |||||
558 | if (PyErr_Occurred()) { | |||||
559 | if (c != -1 && c != -2) { | |||||
560 | PyObject *t, *v, *tb; | |||||
561 | PyErr_Fetch(&t, &v, &tb); | |||||
562 | if (PyErr_Warn(PyExc_RuntimeWarning,PyErr_WarnEx(PyExc_RuntimeWarning, "tp_compare didn't return -1 or -2 " "for exception", 1) | |||||
563 | "tp_compare didn't return -1 or -2 "PyErr_WarnEx(PyExc_RuntimeWarning, "tp_compare didn't return -1 or -2 " "for exception", 1) | |||||
564 | "for exception")PyErr_WarnEx(PyExc_RuntimeWarning, "tp_compare didn't return -1 or -2 " "for exception", 1) < 0) { | |||||
565 | Py_XDECREF(t)do { if ((t) == ((void*)0)) ; else do { if ( --((PyObject*)(t ))->ob_refcnt != 0) ; else ( (*(((PyObject*)((PyObject *)( t)))->ob_type)->tp_dealloc)((PyObject *)((PyObject *)(t )))); } while (0); } while (0); | |||||
566 | Py_XDECREF(v)do { if ((v) == ((void*)0)) ; else do { if ( --((PyObject*)(v ))->ob_refcnt != 0) ; else ( (*(((PyObject*)((PyObject *)( v)))->ob_type)->tp_dealloc)((PyObject *)((PyObject *)(v )))); } while (0); } while (0); | |||||
567 | Py_XDECREF(tb)do { if ((tb) == ((void*)0)) ; else do { if ( --((PyObject*)( tb))->ob_refcnt != 0) ; else ( (*(((PyObject*)((PyObject * )(tb)))->ob_type)->tp_dealloc)((PyObject *)((PyObject * )(tb)))); } while (0); } while (0); | |||||
568 | } | |||||
569 | else | |||||
570 | PyErr_Restore(t, v, tb); | |||||
571 | } | |||||
572 | return -2; | |||||
573 | } | |||||
574 | else if (c < -1 || c > 1) { | |||||
575 | if (PyErr_Warn(PyExc_RuntimeWarning,PyErr_WarnEx(PyExc_RuntimeWarning, "tp_compare didn't return -1, 0 or 1" , 1) | |||||
576 | "tp_compare didn't return -1, 0 or 1")PyErr_WarnEx(PyExc_RuntimeWarning, "tp_compare didn't return -1, 0 or 1" , 1) < 0) | |||||
577 | return -2; | |||||
578 | else | |||||
579 | return c < -1 ? -1 : 1; | |||||
580 | } | |||||
581 | else { | |||||
582 | assert(c >= -1 && c <= 1)((void) (0)); | |||||
583 | return c; | |||||
584 | } | |||||
585 | } | |||||
586 | ||||||
587 | ||||||
588 | /* Macro to get the tp_richcompare field of a type if defined */ | |||||
589 | #define RICHCOMPARE(t)(((((t))->tp_flags & ((1L<<5))) != 0) ? (t)-> tp_richcompare : ((void*)0)) (PyType_HasFeature((t), Py_TPFLAGS_HAVE_RICHCOMPARE)((((t))->tp_flags & ((1L<<5))) != 0) \ | |||||
590 | ? (t)->tp_richcompare : NULL((void*)0)) | |||||
591 | ||||||
592 | /* Map rich comparison operators to their swapped version, e.g. LT --> GT */ | |||||
593 | int _Py_SwappedOp[] = {Py_GT4, Py_GE5, Py_EQ2, Py_NE3, Py_LT0, Py_LE1}; | |||||
594 | ||||||
595 | /* Try a genuine rich comparison, returning an object. Return: | |||||
596 | NULL for exception; | |||||
597 | NotImplemented if this particular rich comparison is not implemented or | |||||
598 | undefined; | |||||
599 | some object not equal to NotImplemented if it is implemented | |||||
600 | (this latter object may not be a Boolean). | |||||
601 | */ | |||||
602 | static PyObject * | |||||
603 | try_rich_compare(PyObject *v, PyObject *w, int op) | |||||
604 | { | |||||
605 | richcmpfunc f; | |||||
606 | PyObject *res; | |||||
607 | ||||||
608 | if (v->ob_type != w->ob_type && | |||||
609 | PyType_IsSubtype(w->ob_type, v->ob_type) && | |||||
610 | (f = RICHCOMPARE(w->ob_type)(((((w->ob_type))->tp_flags & ((1L<<5))) != 0 ) ? (w->ob_type)->tp_richcompare : ((void*)0))) != NULL((void*)0)) { | |||||
611 | res = (*f)(w, v, _Py_SwappedOp[op]); | |||||
612 | if (res != Py_NotImplemented(&_Py_NotImplementedStruct)) | |||||
613 | return res; | |||||
614 | Py_DECREF(res)do { if ( --((PyObject*)(res))->ob_refcnt != 0) ; else ( ( *(((PyObject*)((PyObject *)(res)))->ob_type)->tp_dealloc )((PyObject *)((PyObject *)(res)))); } while (0); | |||||
615 | } | |||||
616 | if ((f = RICHCOMPARE(v->ob_type)(((((v->ob_type))->tp_flags & ((1L<<5))) != 0 ) ? (v->ob_type)->tp_richcompare : ((void*)0))) != NULL((void*)0)) { | |||||
617 | res = (*f)(v, w, op); | |||||
618 | if (res != Py_NotImplemented(&_Py_NotImplementedStruct)) | |||||
619 | return res; | |||||
620 | Py_DECREF(res)do { if ( --((PyObject*)(res))->ob_refcnt != 0) ; else ( ( *(((PyObject*)((PyObject *)(res)))->ob_type)->tp_dealloc )((PyObject *)((PyObject *)(res)))); } while (0); | |||||
621 | } | |||||
622 | if ((f = RICHCOMPARE(w->ob_type)(((((w->ob_type))->tp_flags & ((1L<<5))) != 0 ) ? (w->ob_type)->tp_richcompare : ((void*)0))) != NULL((void*)0)) { | |||||
623 | return (*f)(w, v, _Py_SwappedOp[op]); | |||||
624 | } | |||||
625 | res = Py_NotImplemented(&_Py_NotImplementedStruct); | |||||
626 | Py_INCREF(res)( ((PyObject*)(res))->ob_refcnt++); | |||||
627 | return res; | |||||
628 | } | |||||
629 | ||||||
630 | /* Try a genuine rich comparison, returning an int. Return: | |||||
631 | -1 for exception (including the case where try_rich_compare() returns an | |||||
632 | object that's not a Boolean); | |||||
633 | 0 if the outcome is false; | |||||
634 | 1 if the outcome is true; | |||||
635 | 2 if this particular rich comparison is not implemented or undefined. | |||||
636 | */ | |||||
637 | static int | |||||
638 | try_rich_compare_bool(PyObject *v, PyObject *w, int op) | |||||
639 | { | |||||
640 | PyObject *res; | |||||
641 | int ok; | |||||
642 | ||||||
643 | if (RICHCOMPARE(v->ob_type)(((((v->ob_type))->tp_flags & ((1L<<5))) != 0 ) ? (v->ob_type)->tp_richcompare : ((void*)0)) == NULL((void*)0) && RICHCOMPARE(w->ob_type)(((((w->ob_type))->tp_flags & ((1L<<5))) != 0 ) ? (w->ob_type)->tp_richcompare : ((void*)0)) == NULL((void*)0)) | |||||
644 | return 2; /* Shortcut, avoid INCREF+DECREF */ | |||||
645 | res = try_rich_compare(v, w, op); | |||||
646 | if (res == NULL((void*)0)) | |||||
647 | return -1; | |||||
648 | if (res == Py_NotImplemented(&_Py_NotImplementedStruct)) { | |||||
649 | Py_DECREF(res)do { if ( --((PyObject*)(res))->ob_refcnt != 0) ; else ( ( *(((PyObject*)((PyObject *)(res)))->ob_type)->tp_dealloc )((PyObject *)((PyObject *)(res)))); } while (0); | |||||
650 | return 2; | |||||
651 | } | |||||
652 | ok = PyObject_IsTrue(res); | |||||
653 | Py_DECREF(res)do { if ( --((PyObject*)(res))->ob_refcnt != 0) ; else ( ( *(((PyObject*)((PyObject *)(res)))->ob_type)->tp_dealloc )((PyObject *)((PyObject *)(res)))); } while (0); | |||||
654 | return ok; | |||||
655 | } | |||||
656 | ||||||
657 | /* Try rich comparisons to determine a 3-way comparison. Return: | |||||
658 | -2 for an exception; | |||||
659 | -1 if v < w; | |||||
660 | 0 if v == w; | |||||
661 | 1 if v > w; | |||||
662 | 2 if this particular rich comparison is not implemented or undefined. | |||||
663 | */ | |||||
664 | static int | |||||
665 | try_rich_to_3way_compare(PyObject *v, PyObject *w) | |||||
666 | { | |||||
667 | static struct { int op; int outcome; } tries[3] = { | |||||
668 | /* Try this operator, and if it is true, use this outcome: */ | |||||
669 | {Py_EQ2, 0}, | |||||
670 | {Py_LT0, -1}, | |||||
671 | {Py_GT4, 1}, | |||||
672 | }; | |||||
673 | int i; | |||||
674 | ||||||
675 | if (RICHCOMPARE(v->ob_type)(((((v->ob_type))->tp_flags & ((1L<<5))) != 0 ) ? (v->ob_type)->tp_richcompare : ((void*)0)) == NULL((void*)0) && RICHCOMPARE(w->ob_type)(((((w->ob_type))->tp_flags & ((1L<<5))) != 0 ) ? (w->ob_type)->tp_richcompare : ((void*)0)) == NULL((void*)0)) | |||||
676 | return 2; /* Shortcut */ | |||||
677 | ||||||
678 | for (i = 0; i < 3; i++) { | |||||
679 | switch (try_rich_compare_bool(v, w, tries[i].op)) { | |||||
680 | case -1: | |||||
681 | return -2; | |||||
682 | case 1: | |||||
683 | return tries[i].outcome; | |||||
684 | } | |||||
685 | } | |||||
686 | ||||||
687 | return 2; | |||||
688 | } | |||||
689 | ||||||
690 | /* Try a 3-way comparison, returning an int. Return: | |||||
691 | -2 for an exception; | |||||
692 | -1 if v < w; | |||||
693 | 0 if v == w; | |||||
694 | 1 if v > w; | |||||
695 | 2 if this particular 3-way comparison is not implemented or undefined. | |||||
696 | */ | |||||
697 | static int | |||||
698 | try_3way_compare(PyObject *v, PyObject *w) | |||||
699 | { | |||||
700 | int c; | |||||
701 | cmpfunc f; | |||||
702 | ||||||
703 | /* Comparisons involving instances are given to instance_compare, | |||||
704 | which has the same return conventions as this function. */ | |||||
705 | ||||||
706 | f = v->ob_type->tp_compare; | |||||
707 | if (PyInstance_Check(v)((v)->ob_type == &PyInstance_Type)) | |||||
708 | return (*f)(v, w); | |||||
709 | if (PyInstance_Check(w)((w)->ob_type == &PyInstance_Type)) | |||||
710 | return (*w->ob_type->tp_compare)(v, w); | |||||
711 | ||||||
712 | /* If both have the same (non-NULL) tp_compare, use it. */ | |||||
713 | if (f != NULL((void*)0) && f == w->ob_type->tp_compare) { | |||||
714 | c = (*f)(v, w); | |||||
715 | return adjust_tp_compare(c); | |||||
716 | } | |||||
717 | ||||||
718 | /* If either tp_compare is _PyObject_SlotCompare, that's safe. */ | |||||
719 | if (f == _PyObject_SlotCompare || | |||||
720 | w->ob_type->tp_compare == _PyObject_SlotCompare) | |||||
721 | return _PyObject_SlotCompare(v, w); | |||||
722 | ||||||
723 | /* If we're here, v and w, | |||||
724 | a) are not instances; | |||||
725 | b) have different types or a type without tp_compare; and | |||||
726 | c) don't have a user-defined tp_compare. | |||||
727 | tp_compare implementations in C assume that both arguments | |||||
728 | have their type, so we give up if the coercion fails or if | |||||
729 | it yields types which are still incompatible (which can | |||||
730 | happen with a user-defined nb_coerce). | |||||
731 | */ | |||||
732 | c = PyNumber_CoerceEx(&v, &w); | |||||
733 | if (c < 0) | |||||
734 | return -2; | |||||
735 | if (c > 0) | |||||
736 | return 2; | |||||
737 | f = v->ob_type->tp_compare; | |||||
738 | if (f != NULL((void*)0) && f == w->ob_type->tp_compare) { | |||||
739 | c = (*f)(v, w); | |||||
740 | Py_DECREF(v)do { if ( --((PyObject*)(v))->ob_refcnt != 0) ; else ( (*( ((PyObject*)((PyObject *)(v)))->ob_type)->tp_dealloc)(( PyObject *)((PyObject *)(v)))); } while (0); | |||||
741 | Py_DECREF(w)do { if ( --((PyObject*)(w))->ob_refcnt != 0) ; else ( (*( ((PyObject*)((PyObject *)(w)))->ob_type)->tp_dealloc)(( PyObject *)((PyObject *)(w)))); } while (0); | |||||
742 | return adjust_tp_compare(c); | |||||
743 | } | |||||
744 | ||||||
745 | /* No comparison defined */ | |||||
746 | Py_DECREF(v)do { if ( --((PyObject*)(v))->ob_refcnt != 0) ; else ( (*( ((PyObject*)((PyObject *)(v)))->ob_type)->tp_dealloc)(( PyObject *)((PyObject *)(v)))); } while (0); | |||||
747 | Py_DECREF(w)do { if ( --((PyObject*)(w))->ob_refcnt != 0) ; else ( (*( ((PyObject*)((PyObject *)(w)))->ob_type)->tp_dealloc)(( PyObject *)((PyObject *)(w)))); } while (0); | |||||
748 | return 2; | |||||
749 | } | |||||
750 | ||||||
751 | /* Final fallback 3-way comparison, returning an int. Return: | |||||
752 | -2 if an error occurred; | |||||
753 | -1 if v < w; | |||||
754 | 0 if v == w; | |||||
755 | 1 if v > w. | |||||
756 | */ | |||||
757 | static int | |||||
758 | default_3way_compare(PyObject *v, PyObject *w) | |||||
759 | { | |||||
760 | int c; | |||||
761 | const char *vname, *wname; | |||||
762 | ||||||
763 | if (v->ob_type == w->ob_type) { | |||||
764 | /* When comparing these pointers, they must be cast to | |||||
765 | * integer types (i.e. Py_uintptr_t, our spelling of C9X's | |||||
766 | * uintptr_t). ANSI specifies that pointer compares other | |||||
767 | * than == and != to non-related structures are undefined. | |||||
768 | */ | |||||
769 | Py_uintptr_t vv = (Py_uintptr_t)v; | |||||
770 | Py_uintptr_t ww = (Py_uintptr_t)w; | |||||
771 | return (vv < ww) ? -1 : (vv > ww) ? 1 : 0; | |||||
772 | } | |||||
773 | ||||||
774 | /* None is smaller than anything */ | |||||
775 | if (v == Py_None(&_Py_NoneStruct)) | |||||
776 | return -1; | |||||
777 | if (w == Py_None(&_Py_NoneStruct)) | |||||
778 | return 1; | |||||
779 | ||||||
780 | /* different type: compare type names; numbers are smaller */ | |||||
781 | if (PyNumber_Check(v)) | |||||
782 | vname = ""; | |||||
783 | else | |||||
784 | vname = v->ob_type->tp_name; | |||||
785 | if (PyNumber_Check(w)) | |||||
786 | wname = ""; | |||||
787 | else | |||||
788 | wname = w->ob_type->tp_name; | |||||
789 | c = strcmp(vname, wname); | |||||
790 | if (c < 0) | |||||
791 | return -1; | |||||
792 | if (c > 0) | |||||
793 | return 1; | |||||
794 | /* Same type name, or (more likely) incomparable numeric types */ | |||||
795 | return ((Py_uintptr_t)(v->ob_type) < ( | |||||
796 | Py_uintptr_t)(w->ob_type)) ? -1 : 1; | |||||
797 | } | |||||
798 | ||||||
799 | /* Do a 3-way comparison, by hook or by crook. Return: | |||||
800 | -2 for an exception (but see below); | |||||
801 | -1 if v < w; | |||||
802 | 0 if v == w; | |||||
803 | 1 if v > w; | |||||
804 | BUT: if the object implements a tp_compare function, it returns | |||||
805 | whatever this function returns (whether with an exception or not). | |||||
806 | */ | |||||
807 | static int | |||||
808 | do_cmp(PyObject *v, PyObject *w) | |||||
809 | { | |||||
810 | int c; | |||||
811 | cmpfunc f; | |||||
812 | ||||||
813 | if (v->ob_type == w->ob_type | |||||
814 | && (f = v->ob_type->tp_compare) != NULL((void*)0)) { | |||||
815 | c = (*f)(v, w); | |||||
816 | if (PyInstance_Check(v)((v)->ob_type == &PyInstance_Type)) { | |||||
817 | /* Instance tp_compare has a different signature. | |||||
818 | But if it returns undefined we fall through. */ | |||||
819 | if (c != 2) | |||||
820 | return c; | |||||
821 | /* Else fall through to try_rich_to_3way_compare() */ | |||||
822 | } | |||||
823 | else | |||||
824 | return adjust_tp_compare(c); | |||||
825 | } | |||||
826 | /* We only get here if one of the following is true: | |||||
827 | a) v and w have different types | |||||
828 | b) v and w have the same type, which doesn't have tp_compare | |||||
829 | c) v and w are instances, and either __cmp__ is not defined or | |||||
830 | __cmp__ returns NotImplemented | |||||
831 | */ | |||||
832 | c = try_rich_to_3way_compare(v, w); | |||||
833 | if (c < 2) | |||||
834 | return c; | |||||
835 | c = try_3way_compare(v, w); | |||||
836 | if (c < 2) | |||||
837 | return c; | |||||
838 | return default_3way_compare(v, w); | |||||
839 | } | |||||
840 | ||||||
841 | /* Compare v to w. Return | |||||
842 | -1 if v < w or exception (PyErr_Occurred() true in latter case). | |||||
843 | 0 if v == w. | |||||
844 | 1 if v > w. | |||||
845 | XXX The docs (C API manual) say the return value is undefined in case | |||||
846 | XXX of error. | |||||
847 | */ | |||||
848 | int | |||||
849 | PyObject_Compare(PyObject *v, PyObject *w) | |||||
850 | { | |||||
851 | int result; | |||||
852 | ||||||
853 | if (v == NULL((void*)0) || w == NULL((void*)0)) { | |||||
854 | PyErr_BadInternalCall()_PyErr_BadInternalCall("/builddir/build/BUILD/Python-2.7.3/Objects/object.c" , 854); | |||||
855 | return -1; | |||||
856 | } | |||||
857 | if (v == w) | |||||
858 | return 0; | |||||
859 | if (Py_EnterRecursiveCall(" in cmp")((++((_PyThreadState_Current)->recursion_depth) > _Py_CheckRecursionLimit ) && _Py_CheckRecursiveCall(" in cmp"))) | |||||
860 | return -1; | |||||
861 | result = do_cmp(v, w); | |||||
862 | Py_LeaveRecursiveCall()(--(_PyThreadState_Current)->recursion_depth); | |||||
863 | return result < 0 ? -1 : result; | |||||
864 | } | |||||
865 | ||||||
866 | /* Return (new reference to) Py_True or Py_False. */ | |||||
867 | static PyObject * | |||||
868 | convert_3way_to_object(int op, int c) | |||||
869 | { | |||||
870 | PyObject *result; | |||||
871 | switch (op) { | |||||
872 | case Py_LT0: c = c < 0; break; | |||||
873 | case Py_LE1: c = c <= 0; break; | |||||
874 | case Py_EQ2: c = c == 0; break; | |||||
875 | case Py_NE3: c = c != 0; break; | |||||
876 | case Py_GT4: c = c > 0; break; | |||||
877 | case Py_GE5: c = c >= 0; break; | |||||
878 | } | |||||
879 | result = c ? Py_True((PyObject *) &_Py_TrueStruct) : Py_False((PyObject *) &_Py_ZeroStruct); | |||||
880 | Py_INCREF(result)( ((PyObject*)(result))->ob_refcnt++); | |||||
881 | return result; | |||||
882 | } | |||||
883 | ||||||
884 | /* We want a rich comparison but don't have one. Try a 3-way cmp instead. | |||||
885 | Return | |||||
886 | NULL if error | |||||
887 | Py_True if v op w | |||||
888 | Py_False if not (v op w) | |||||
889 | */ | |||||
890 | static PyObject * | |||||
891 | try_3way_to_rich_compare(PyObject *v, PyObject *w, int op) | |||||
892 | { | |||||
893 | int c; | |||||
894 | ||||||
895 | c = try_3way_compare(v, w); | |||||
896 | if (c >= 2) { | |||||
897 | ||||||
898 | /* Py3K warning if types are not equal and comparison isn't == or != */ | |||||
899 | if (Py_Py3kWarningFlag && | |||||
900 | v->ob_type != w->ob_type && op != Py_EQ2 && op != Py_NE3 && | |||||
901 | PyErr_WarnEx(PyExc_DeprecationWarning, | |||||
902 | "comparing unequal types not supported " | |||||
903 | "in 3.x", 1) < 0) { | |||||
904 | return NULL((void*)0); | |||||
905 | } | |||||
906 | ||||||
907 | c = default_3way_compare(v, w); | |||||
908 | } | |||||
909 | if (c <= -2) | |||||
910 | return NULL((void*)0); | |||||
911 | return convert_3way_to_object(op, c); | |||||
912 | } | |||||
913 | ||||||
914 | /* Do rich comparison on v and w. Return | |||||
915 | NULL if error | |||||
916 | Else a new reference to an object other than Py_NotImplemented, usually(?): | |||||
917 | Py_True if v op w | |||||
918 | Py_False if not (v op w) | |||||
919 | */ | |||||
920 | static PyObject * | |||||
921 | do_richcmp(PyObject *v, PyObject *w, int op) | |||||
922 | { | |||||
923 | PyObject *res; | |||||
924 | ||||||
925 | res = try_rich_compare(v, w, op); | |||||
926 | if (res != Py_NotImplemented(&_Py_NotImplementedStruct)) | |||||
927 | return res; | |||||
928 | Py_DECREF(res)do { if ( --((PyObject*)(res))->ob_refcnt != 0) ; else ( ( *(((PyObject*)((PyObject *)(res)))->ob_type)->tp_dealloc )((PyObject *)((PyObject *)(res)))); } while (0); | |||||
929 | ||||||
930 | return try_3way_to_rich_compare(v, w, op); | |||||
931 | } | |||||
932 | ||||||
933 | /* Return: | |||||
934 | NULL for exception; | |||||
935 | some object not equal to NotImplemented if it is implemented | |||||
936 | (this latter object may not be a Boolean). | |||||
937 | */ | |||||
938 | PyObject * | |||||
939 | PyObject_RichCompare(PyObject *v, PyObject *w, int op) | |||||
940 | { | |||||
941 | PyObject *res; | |||||
942 | ||||||
943 | assert(Py_LT <= op && op <= Py_GE)((void) (0)); | |||||
944 | if (Py_EnterRecursiveCall(" in cmp")((++((_PyThreadState_Current)->recursion_depth) > _Py_CheckRecursionLimit ) && _Py_CheckRecursiveCall(" in cmp"))) | |||||
945 | return NULL((void*)0); | |||||
946 | ||||||
947 | /* If the types are equal, and not old-style instances, try to | |||||
948 | get out cheap (don't bother with coercions etc.). */ | |||||
949 | if (v->ob_type == w->ob_type && !PyInstance_Check(v)((v)->ob_type == &PyInstance_Type)) { | |||||
950 | cmpfunc fcmp; | |||||
951 | richcmpfunc frich = RICHCOMPARE(v->ob_type)(((((v->ob_type))->tp_flags & ((1L<<5))) != 0 ) ? (v->ob_type)->tp_richcompare : ((void*)0)); | |||||
952 | /* If the type has richcmp, try it first. try_rich_compare | |||||
953 | tries it two-sided, which is not needed since we've a | |||||
954 | single type only. */ | |||||
955 | if (frich != NULL((void*)0)) { | |||||
956 | res = (*frich)(v, w, op); | |||||
957 | if (res != Py_NotImplemented(&_Py_NotImplementedStruct)) | |||||
958 | goto Done; | |||||
959 | Py_DECREF(res)do { if ( --((PyObject*)(res))->ob_refcnt != 0) ; else ( ( *(((PyObject*)((PyObject *)(res)))->ob_type)->tp_dealloc )((PyObject *)((PyObject *)(res)))); } while (0); | |||||
960 | } | |||||
961 | /* No richcmp, or this particular richmp not implemented. | |||||
962 | Try 3-way cmp. */ | |||||
963 | fcmp = v->ob_type->tp_compare; | |||||
964 | if (fcmp != NULL((void*)0)) { | |||||
965 | int c = (*fcmp)(v, w); | |||||
966 | c = adjust_tp_compare(c); | |||||
967 | if (c == -2) { | |||||
968 | res = NULL((void*)0); | |||||
969 | goto Done; | |||||
970 | } | |||||
971 | res = convert_3way_to_object(op, c); | |||||
972 | goto Done; | |||||
973 | } | |||||
974 | } | |||||
975 | ||||||
976 | /* Fast path not taken, or couldn't deliver a useful result. */ | |||||
977 | res = do_richcmp(v, w, op); | |||||
978 | Done: | |||||
979 | Py_LeaveRecursiveCall()(--(_PyThreadState_Current)->recursion_depth); | |||||
980 | return res; | |||||
981 | } | |||||
982 | ||||||
983 | /* Return -1 if error; 1 if v op w; 0 if not (v op w). */ | |||||
984 | int | |||||
985 | PyObject_RichCompareBool(PyObject *v, PyObject *w, int op) | |||||
986 | { | |||||
987 | PyObject *res; | |||||
988 | int ok; | |||||
989 | ||||||
990 | /* Quick result when objects are the same. | |||||
991 | Guarantees that identity implies equality. */ | |||||
992 | if (v == w) { | |||||
993 | if (op == Py_EQ2) | |||||
994 | return 1; | |||||
995 | else if (op == Py_NE3) | |||||
996 | return 0; | |||||
997 | } | |||||
998 | ||||||
999 | res = PyObject_RichCompare(v, w, op); | |||||
1000 | if (res == NULL((void*)0)) | |||||
1001 | return -1; | |||||
1002 | if (PyBool_Check(res)((((PyObject*)(res))->ob_type) == &PyBool_Type)) | |||||
1003 | ok = (res == Py_True((PyObject *) &_Py_TrueStruct)); | |||||
1004 | else | |||||
1005 | ok = PyObject_IsTrue(res); | |||||
1006 | Py_DECREF(res)do { if ( --((PyObject*)(res))->ob_refcnt != 0) ; else ( ( *(((PyObject*)((PyObject *)(res)))->ob_type)->tp_dealloc )((PyObject *)((PyObject *)(res)))); } while (0); | |||||
1007 | return ok; | |||||
1008 | } | |||||
1009 | ||||||
1010 | /* Set of hash utility functions to help maintaining the invariant that | |||||
1011 | if a==b then hash(a)==hash(b) | |||||
1012 | ||||||
1013 | All the utility functions (_Py_Hash*()) return "-1" to signify an error. | |||||
1014 | */ | |||||
1015 | ||||||
1016 | long | |||||
1017 | _Py_HashDouble(double v) | |||||
1018 | { | |||||
1019 | double intpart, fractpart; | |||||
1020 | int expo; | |||||
1021 | long hipart; | |||||
1022 | long x; /* the final hash value */ | |||||
1023 | /* This is designed so that Python numbers of different types | |||||
1024 | * that compare equal hash to the same value; otherwise comparisons | |||||
1025 | * of mapping keys will turn out weird. | |||||
1026 | */ | |||||
1027 | ||||||
1028 | if (!Py_IS_FINITE(v)(sizeof (v) == sizeof (float) ? __finitef (v) : sizeof (v) == sizeof (double) ? __finite (v) : __finitel (v))) { | |||||
1029 | if (Py_IS_INFINITY(v)(sizeof (v) == sizeof (float) ? __isinff (v) : sizeof (v) == sizeof (double) ? __isinf (v) : __isinfl (v))) | |||||
1030 | return v < 0 ? -271828 : 314159; | |||||
1031 | else | |||||
1032 | return 0; | |||||
1033 | } | |||||
1034 | fractpart = modf(v, &intpart); | |||||
1035 | if (fractpart == 0.0) { | |||||
1036 | /* This must return the same hash as an equal int or long. */ | |||||
1037 | if (intpart > LONG_MAX9223372036854775807L/2 || -intpart > LONG_MAX9223372036854775807L/2) { | |||||
1038 | /* Convert to long and use its hash. */ | |||||
1039 | PyObject *plong; /* converted to Python long */ | |||||
1040 | plong = PyLong_FromDouble(v); | |||||
1041 | if (plong == NULL((void*)0)) | |||||
1042 | return -1; | |||||
1043 | x = PyObject_Hash(plong); | |||||
1044 | Py_DECREF(plong)do { if ( --((PyObject*)(plong))->ob_refcnt != 0) ; else ( (*(((PyObject*)((PyObject *)(plong)))->ob_type)->tp_dealloc )((PyObject *)((PyObject *)(plong)))); } while (0); | |||||
1045 | return x; | |||||
1046 | } | |||||
1047 | /* Fits in a C long == a Python int, so is its own hash. */ | |||||
1048 | x = (long)intpart; | |||||
1049 | if (x == -1) | |||||
1050 | x = -2; | |||||
1051 | return x; | |||||
1052 | } | |||||
1053 | /* The fractional part is non-zero, so we don't have to worry about | |||||
1054 | * making this match the hash of some other type. | |||||
1055 | * Use frexp to get at the bits in the double. | |||||
1056 | * Since the VAX D double format has 56 mantissa bits, which is the | |||||
1057 | * most of any double format in use, each of these parts may have as | |||||
1058 | * many as (but no more than) 56 significant bits. | |||||
1059 | * So, assuming sizeof(long) >= 4, each part can be broken into two | |||||
1060 | * longs; frexp and multiplication are used to do that. | |||||
1061 | * Also, since the Cray double format has 15 exponent bits, which is | |||||
1062 | * the most of any double format in use, shifting the exponent field | |||||
1063 | * left by 15 won't overflow a long (again assuming sizeof(long) >= 4). | |||||
1064 | */ | |||||
1065 | v = frexp(v, &expo); | |||||
1066 | v *= 2147483648.0; /* 2**31 */ | |||||
1067 | hipart = (long)v; /* take the top 32 bits */ | |||||
1068 | v = (v - (double)hipart) * 2147483648.0; /* get the next 32 bits */ | |||||
1069 | x = hipart + (long)v + (expo << 15); | |||||
1070 | if (x == -1) | |||||
1071 | x = -2; | |||||
1072 | return x; | |||||
1073 | } | |||||
1074 | ||||||
1075 | long | |||||
1076 | _Py_HashPointer(void *p) | |||||
1077 | { | |||||
1078 | long x; | |||||
1079 | size_t y = (size_t)p; | |||||
1080 | /* bottom 3 or 4 bits are likely to be 0; rotate y by 4 to avoid | |||||
1081 | excessive hash collisions for dicts and sets */ | |||||
1082 | y = (y >> 4) | (y << (8 * SIZEOF_VOID_P8 - 4)); | |||||
1083 | x = (long)y; | |||||
1084 | if (x == -1) | |||||
1085 | x = -2; | |||||
1086 | return x; | |||||
1087 | } | |||||
1088 | ||||||
1089 | long | |||||
1090 | PyObject_HashNotImplemented(PyObject *self) | |||||
1091 | { | |||||
1092 | PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'", | |||||
1093 | self->ob_type->tp_name); | |||||
1094 | return -1; | |||||
1095 | } | |||||
1096 | ||||||
1097 | _Py_HashSecret_t _Py_HashSecret; | |||||
1098 | ||||||
1099 | long | |||||
1100 | PyObject_Hash(PyObject *v) | |||||
1101 | { | |||||
1102 | PyTypeObject *tp = v->ob_type; | |||||
1103 | if (tp->tp_hash != NULL((void*)0)) | |||||
1104 | return (*tp->tp_hash)(v); | |||||
1105 | /* To keep to the general practice that inheriting | |||||
1106 | * solely from object in C code should work without | |||||
1107 | * an explicit call to PyType_Ready, we implicitly call | |||||
1108 | * PyType_Ready here and then check the tp_hash slot again | |||||
1109 | */ | |||||
1110 | if (tp->tp_dict == NULL((void*)0)) { | |||||
1111 | if (PyType_Ready(tp) < 0) | |||||
1112 | return -1; | |||||
1113 | if (tp->tp_hash != NULL((void*)0)) | |||||
1114 | return (*tp->tp_hash)(v); | |||||
1115 | } | |||||
1116 | if (tp->tp_compare == NULL((void*)0) && RICHCOMPARE(tp)(((((tp))->tp_flags & ((1L<<5))) != 0) ? (tp)-> tp_richcompare : ((void*)0)) == NULL((void*)0)) { | |||||
1117 | return _Py_HashPointer(v); /* Use address as hash value */ | |||||
1118 | } | |||||
1119 | /* If there's a cmp but no hash defined, the object can't be hashed */ | |||||
1120 | return PyObject_HashNotImplemented(v); | |||||
1121 | } | |||||
1122 | ||||||
1123 | PyObject * | |||||
1124 | PyObject_GetAttrString(PyObject *v, const char *name) | |||||
1125 | { | |||||
1126 | PyObject *w, *res; | |||||
1127 | ||||||
1128 | if (Py_TYPE(v)(((PyObject*)(v))->ob_type)->tp_getattr != NULL((void*)0)) | |||||
1129 | return (*Py_TYPE(v)(((PyObject*)(v))->ob_type)->tp_getattr)(v, (char*)name); | |||||
1130 | w = PyString_InternFromString(name); | |||||
1131 | if (w == NULL((void*)0)) | |||||
1132 | return NULL((void*)0); | |||||
1133 | res = PyObject_GetAttr(v, w); | |||||
1134 | Py_XDECREF(w)do { if ((w) == ((void*)0)) ; else do { if ( --((PyObject*)(w ))->ob_refcnt != 0) ; else ( (*(((PyObject*)((PyObject *)( w)))->ob_type)->tp_dealloc)((PyObject *)((PyObject *)(w )))); } while (0); } while (0); | |||||
1135 | return res; | |||||
1136 | } | |||||
1137 | ||||||
1138 | int | |||||
1139 | PyObject_HasAttrString(PyObject *v, const char *name) | |||||
1140 | { | |||||
1141 | PyObject *res = PyObject_GetAttrString(v, name); | |||||
1142 | if (res != NULL((void*)0)) { | |||||
1143 | Py_DECREF(res)do { if ( --((PyObject*)(res))->ob_refcnt != 0) ; else ( ( *(((PyObject*)((PyObject *)(res)))->ob_type)->tp_dealloc )((PyObject *)((PyObject *)(res)))); } while (0); | |||||
1144 | return 1; | |||||
1145 | } | |||||
1146 | PyErr_Clear(); | |||||
1147 | return 0; | |||||
1148 | } | |||||
1149 | ||||||
1150 | int | |||||
1151 | PyObject_SetAttrString(PyObject *v, const char *name, PyObject *w) | |||||
1152 | { | |||||
1153 | PyObject *s; | |||||
1154 | int res; | |||||
1155 | ||||||
1156 | if (Py_TYPE(v)(((PyObject*)(v))->ob_type)->tp_setattr != NULL((void*)0)) | |||||
1157 | return (*Py_TYPE(v)(((PyObject*)(v))->ob_type)->tp_setattr)(v, (char*)name, w); | |||||
1158 | s = PyString_InternFromString(name); | |||||
1159 | if (s == NULL((void*)0)) | |||||
1160 | return -1; | |||||
1161 | res = PyObject_SetAttr(v, s, w); | |||||
1162 | Py_XDECREF(s)do { if ((s) == ((void*)0)) ; else do { if ( --((PyObject*)(s ))->ob_refcnt != 0) ; else ( (*(((PyObject*)((PyObject *)( s)))->ob_type)->tp_dealloc)((PyObject *)((PyObject *)(s )))); } while (0); } while (0); | |||||
1163 | return res; | |||||
1164 | } | |||||
1165 | ||||||
1166 | PyObject * | |||||
1167 | PyObject_GetAttr(PyObject *v, PyObject *name) | |||||
1168 | { | |||||
1169 | PyTypeObject *tp = Py_TYPE(v)(((PyObject*)(v))->ob_type); | |||||
1170 | ||||||
1171 | if (!PyString_Check(name)((((((PyObject*)(name))->ob_type))->tp_flags & ((1L <<27))) != 0)) { | |||||
| ||||||
1172 | #ifdef Py_USING_UNICODE1 | |||||
1173 | /* The Unicode to string conversion is done here because the | |||||
1174 | existing tp_getattro slots expect a string object as name | |||||
1175 | and we wouldn't want to break those. */ | |||||
1176 | if (PyUnicode_Check(name)((((((PyObject*)(name))->ob_type))->tp_flags & ((1L <<28))) != 0)) { | |||||
1177 | name = _PyUnicode_AsDefaultEncodedString_PyUnicodeUCS4_AsDefaultEncodedString(name, NULL((void*)0)); | |||||
1178 | if (name == NULL((void*)0)) | |||||
1179 | return NULL((void*)0); | |||||
1180 | } | |||||
1181 | else | |||||
1182 | #endif | |||||
1183 | { | |||||
1184 | PyErr_Format(PyExc_TypeError, | |||||
1185 | "attribute name must be string, not '%.200s'", | |||||
1186 | Py_TYPE(name)(((PyObject*)(name))->ob_type)->tp_name); | |||||
1187 | return NULL((void*)0); | |||||
1188 | } | |||||
1189 | } | |||||
1190 | if (tp->tp_getattro != NULL((void*)0)) | |||||
1191 | return (*tp->tp_getattro)(v, name); | |||||
1192 | if (tp->tp_getattr != NULL((void*)0)) | |||||
1193 | return (*tp->tp_getattr)(v, PyString_AS_STRING(name)(((PyStringObject *)(name))->ob_sval)); | |||||
1194 | PyErr_Format(PyExc_AttributeError, | |||||
1195 | "'%.50s' object has no attribute '%.400s'", | |||||
1196 | tp->tp_name, PyString_AS_STRING(name)(((PyStringObject *)(name))->ob_sval)); | |||||
1197 | return NULL((void*)0); | |||||
1198 | } | |||||
1199 | ||||||
1200 | int | |||||
1201 | PyObject_HasAttr(PyObject *v, PyObject *name) | |||||
1202 | { | |||||
1203 | PyObject *res = PyObject_GetAttr(v, name); | |||||
1204 | if (res != NULL((void*)0)) { | |||||
1205 | Py_DECREF(res)do { if ( --((PyObject*)(res))->ob_refcnt != 0) ; else ( ( *(((PyObject*)((PyObject *)(res)))->ob_type)->tp_dealloc )((PyObject *)((PyObject *)(res)))); } while (0); | |||||
1206 | return 1; | |||||
1207 | } | |||||
1208 | PyErr_Clear(); | |||||
1209 | return 0; | |||||
1210 | } | |||||
1211 | ||||||
1212 | int | |||||
1213 | PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value) | |||||
1214 | { | |||||
1215 | PyTypeObject *tp = Py_TYPE(v)(((PyObject*)(v))->ob_type); | |||||
1216 | int err; | |||||
1217 | ||||||
1218 | if (!PyString_Check(name)((((((PyObject*)(name))->ob_type))->tp_flags & ((1L <<27))) != 0)){ | |||||
1219 | #ifdef Py_USING_UNICODE1 | |||||
1220 | /* The Unicode to string conversion is done here because the | |||||
1221 | existing tp_setattro slots expect a string object as name | |||||
1222 | and we wouldn't want to break those. */ | |||||
1223 | if (PyUnicode_Check(name)((((((PyObject*)(name))->ob_type))->tp_flags & ((1L <<28))) != 0)) { | |||||
1224 | name = PyUnicode_AsEncodedStringPyUnicodeUCS4_AsEncodedString(name, NULL((void*)0), NULL((void*)0)); | |||||
1225 | if (name == NULL((void*)0)) | |||||
1226 | return -1; | |||||
1227 | } | |||||
1228 | else | |||||
1229 | #endif | |||||
1230 | { | |||||
1231 | PyErr_Format(PyExc_TypeError, | |||||
1232 | "attribute name must be string, not '%.200s'", | |||||
1233 | Py_TYPE(name)(((PyObject*)(name))->ob_type)->tp_name); | |||||
1234 | return -1; | |||||
1235 | } | |||||
1236 | } | |||||
1237 | else | |||||
1238 | Py_INCREF(name)( ((PyObject*)(name))->ob_refcnt++); | |||||
1239 | ||||||
1240 | PyString_InternInPlace(&name); | |||||
1241 | if (tp->tp_setattro != NULL((void*)0)) { | |||||
1242 | err = (*tp->tp_setattro)(v, name, value); | |||||
1243 | Py_DECREF(name)do { if ( --((PyObject*)(name))->ob_refcnt != 0) ; else ( ( *(((PyObject*)((PyObject *)(name)))->ob_type)->tp_dealloc )((PyObject *)((PyObject *)(name)))); } while (0); | |||||
1244 | return err; | |||||
1245 | } | |||||
1246 | if (tp->tp_setattr != NULL((void*)0)) { | |||||
1247 | err = (*tp->tp_setattr)(v, PyString_AS_STRING(name)(((PyStringObject *)(name))->ob_sval), value); | |||||
1248 | Py_DECREF(name)do { if ( --((PyObject*)(name))->ob_refcnt != 0) ; else ( ( *(((PyObject*)((PyObject *)(name)))->ob_type)->tp_dealloc )((PyObject *)((PyObject *)(name)))); } while (0); | |||||
1249 | return err; | |||||
1250 | } | |||||
1251 | Py_DECREF(name)do { if ( --((PyObject*)(name))->ob_refcnt != 0) ; else ( ( *(((PyObject*)((PyObject *)(name)))->ob_type)->tp_dealloc )((PyObject *)((PyObject *)(name)))); } while (0); | |||||
1252 | if (tp->tp_getattr == NULL((void*)0) && tp->tp_getattro == NULL((void*)0)) | |||||
1253 | PyErr_Format(PyExc_TypeError, | |||||
1254 | "'%.100s' object has no attributes " | |||||
1255 | "(%s .%.100s)", | |||||
1256 | tp->tp_name, | |||||
1257 | value==NULL((void*)0) ? "del" : "assign to", | |||||
1258 | PyString_AS_STRING(name)(((PyStringObject *)(name))->ob_sval)); | |||||
1259 | else | |||||
1260 | PyErr_Format(PyExc_TypeError, | |||||
1261 | "'%.100s' object has only read-only attributes " | |||||
1262 | "(%s .%.100s)", | |||||
1263 | tp->tp_name, | |||||
1264 | value==NULL((void*)0) ? "del" : "assign to", | |||||
1265 | PyString_AS_STRING(name)(((PyStringObject *)(name))->ob_sval)); | |||||
1266 | return -1; | |||||
1267 | } | |||||
1268 | ||||||
1269 | /* Helper to get a pointer to an object's __dict__ slot, if any */ | |||||
1270 | ||||||
1271 | PyObject ** | |||||
1272 | _PyObject_GetDictPtr(PyObject *obj) | |||||
1273 | { | |||||
1274 | Py_ssize_t dictoffset; | |||||
1275 | PyTypeObject *tp = Py_TYPE(obj)(((PyObject*)(obj))->ob_type); | |||||
1276 | ||||||
1277 | if (!(tp->tp_flags & Py_TPFLAGS_HAVE_CLASS(1L<<8))) | |||||
1278 | return NULL((void*)0); | |||||
1279 | dictoffset = tp->tp_dictoffset; | |||||
1280 | if (dictoffset == 0) | |||||
1281 | return NULL((void*)0); | |||||
1282 | if (dictoffset < 0) { | |||||
1283 | Py_ssize_t tsize; | |||||
1284 | size_t size; | |||||
1285 | ||||||
1286 | tsize = ((PyVarObject *)obj)->ob_size; | |||||
1287 | if (tsize < 0) | |||||
1288 | tsize = -tsize; | |||||
1289 | size = _PyObject_VAR_SIZE(tp, tsize)(size_t) ( ( (tp)->tp_basicsize + (tsize)*(tp)->tp_itemsize + (8 - 1) ) & ~(8 - 1) ); | |||||
1290 | ||||||
1291 | dictoffset += (long)size; | |||||
1292 | assert(dictoffset > 0)((void) (0)); | |||||
1293 | assert(dictoffset % SIZEOF_VOID_P == 0)((void) (0)); | |||||
1294 | } | |||||
1295 | return (PyObject **) ((char *)obj + dictoffset); | |||||
1296 | } | |||||
1297 | ||||||
1298 | PyObject * | |||||
1299 | PyObject_SelfIter(PyObject *obj) | |||||
1300 | { | |||||
1301 | Py_INCREF(obj)( ((PyObject*)(obj))->ob_refcnt++); | |||||
1302 | return obj; | |||||
1303 | } | |||||
1304 | ||||||
1305 | /* Helper used when the __next__ method is removed from a type: | |||||
1306 | tp_iternext is never NULL and can be safely called without checking | |||||
1307 | on every iteration. | |||||
1308 | */ | |||||
1309 | ||||||
1310 | PyObject * | |||||
1311 | _PyObject_NextNotImplemented(PyObject *self) | |||||
1312 | { | |||||
1313 | PyErr_Format(PyExc_TypeError, | |||||
1314 | "'%.200s' object is not iterable", | |||||
1315 | Py_TYPE(self)(((PyObject*)(self))->ob_type)->tp_name); | |||||
1316 | return NULL((void*)0); | |||||
1317 | } | |||||
1318 | ||||||
1319 | /* Generic GetAttr functions - put these in your tp_[gs]etattro slot */ | |||||
1320 | ||||||
1321 | PyObject * | |||||
1322 | _PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name, PyObject *dict) | |||||
1323 | { | |||||
1324 | PyTypeObject *tp = Py_TYPE(obj)(((PyObject*)(obj))->ob_type); | |||||
1325 | PyObject *descr = NULL((void*)0); | |||||
1326 | PyObject *res = NULL((void*)0); | |||||
1327 | descrgetfunc f; | |||||
1328 | Py_ssize_t dictoffset; | |||||
1329 | PyObject **dictptr; | |||||
1330 | ||||||
1331 | if (!PyString_Check(name)((((((PyObject*)(name))->ob_type))->tp_flags & ((1L <<27))) != 0)){ | |||||
1332 | #ifdef Py_USING_UNICODE1 | |||||
1333 | /* The Unicode to string conversion is done here because the | |||||
1334 | existing tp_setattro slots expect a string object as name | |||||
1335 | and we wouldn't want to break those. */ | |||||
1336 | if (PyUnicode_Check(name)((((((PyObject*)(name))->ob_type))->tp_flags & ((1L <<28))) != 0)) { | |||||
1337 | name = PyUnicode_AsEncodedStringPyUnicodeUCS4_AsEncodedString(name, NULL((void*)0), NULL((void*)0)); | |||||
1338 | if (name == NULL((void*)0)) | |||||
1339 | return NULL((void*)0); | |||||
1340 | } | |||||
1341 | else | |||||
1342 | #endif | |||||
1343 | { | |||||
1344 | PyErr_Format(PyExc_TypeError, | |||||
1345 | "attribute name must be string, not '%.200s'", | |||||
1346 | Py_TYPE(name)(((PyObject*)(name))->ob_type)->tp_name); | |||||
1347 | return NULL((void*)0); | |||||
1348 | } | |||||
1349 | } | |||||
1350 | else | |||||
1351 | Py_INCREF(name)( ((PyObject*)(name))->ob_refcnt++); | |||||
1352 | ||||||
1353 | if (tp->tp_dict == NULL((void*)0)) { | |||||
1354 | if (PyType_Ready(tp) < 0) | |||||
1355 | goto done; | |||||
1356 | } | |||||
1357 | ||||||
1358 | #if 0 /* XXX this is not quite _PyType_Lookup anymore */ | |||||
1359 | /* Inline _PyType_Lookup */ | |||||
1360 | { | |||||
1361 | Py_ssize_t i, n; | |||||
1362 | PyObject *mro, *base, *dict; | |||||
1363 | ||||||
1364 | /* Look in tp_dict of types in MRO */ | |||||
1365 | mro = tp->tp_mro; | |||||
1366 | assert(mro != NULL)((void) (0)); | |||||
1367 | assert(PyTuple_Check(mro))((void) (0)); | |||||
1368 | n = PyTuple_GET_SIZE(mro)(((PyVarObject*)(mro))->ob_size); | |||||
1369 | for (i = 0; i < n; i++) { | |||||
1370 | base = PyTuple_GET_ITEM(mro, i)(((PyTupleObject *)(mro))->ob_item[i]); | |||||
1371 | if (PyClass_Check(base)((base)->ob_type == &PyClass_Type)) | |||||
1372 | dict = ((PyClassObject *)base)->cl_dict; | |||||
1373 | else { | |||||
1374 | assert(PyType_Check(base))((void) (0)); | |||||
1375 | dict = ((PyTypeObject *)base)->tp_dict; | |||||
1376 | } | |||||
1377 | assert(dict && PyDict_Check(dict))((void) (0)); | |||||
1378 | descr = PyDict_GetItem(dict, name); | |||||
1379 | if (descr != NULL((void*)0)) | |||||
1380 | break; | |||||
1381 | } | |||||
1382 | } | |||||
1383 | #else | |||||
1384 | descr = _PyType_Lookup(tp, name); | |||||
1385 | #endif | |||||
1386 | ||||||
1387 | Py_XINCREF(descr)do { if ((descr) == ((void*)0)) ; else ( ((PyObject*)(descr)) ->ob_refcnt++); } while (0); | |||||
1388 | ||||||
1389 | f = NULL((void*)0); | |||||
1390 | if (descr != NULL((void*)0) && | |||||
1391 | PyType_HasFeature(descr->ob_type, Py_TPFLAGS_HAVE_CLASS)(((descr->ob_type)->tp_flags & ((1L<<8))) != 0 )) { | |||||
1392 | f = descr->ob_type->tp_descr_get; | |||||
1393 | if (f != NULL((void*)0) && PyDescr_IsData(descr)((((PyObject*)(descr))->ob_type)->tp_descr_set != ((void *)0))) { | |||||
1394 | res = f(descr, obj, (PyObject *)obj->ob_type); | |||||
1395 | Py_DECREF(descr)do { if ( --((PyObject*)(descr))->ob_refcnt != 0) ; else ( (*(((PyObject*)((PyObject *)(descr)))->ob_type)->tp_dealloc )((PyObject *)((PyObject *)(descr)))); } while (0); | |||||
1396 | goto done; | |||||
1397 | } | |||||
1398 | } | |||||
1399 | ||||||
1400 | if (dict == NULL((void*)0)) { | |||||
1401 | /* Inline _PyObject_GetDictPtr */ | |||||
1402 | dictoffset = tp->tp_dictoffset; | |||||
1403 | if (dictoffset != 0) { | |||||
1404 | if (dictoffset < 0) { | |||||
1405 | Py_ssize_t tsize; | |||||
1406 | size_t size; | |||||
1407 | ||||||
1408 | tsize = ((PyVarObject *)obj)->ob_size; | |||||
1409 | if (tsize < 0) | |||||
1410 | tsize = -tsize; | |||||
1411 | size = _PyObject_VAR_SIZE(tp, tsize)(size_t) ( ( (tp)->tp_basicsize + (tsize)*(tp)->tp_itemsize + (8 - 1) ) & ~(8 - 1) ); | |||||
1412 | ||||||
1413 | dictoffset += (long)size; | |||||
1414 | assert(dictoffset > 0)((void) (0)); | |||||
1415 | assert(dictoffset % SIZEOF_VOID_P == 0)((void) (0)); | |||||
1416 | } | |||||
1417 | dictptr = (PyObject **) ((char *)obj + dictoffset); | |||||
1418 | dict = *dictptr; | |||||
1419 | } | |||||
1420 | } | |||||
1421 | if (dict != NULL((void*)0)) { | |||||
1422 | Py_INCREF(dict)( ((PyObject*)(dict))->ob_refcnt++); | |||||
1423 | res = PyDict_GetItem(dict, name); | |||||
1424 | if (res != NULL((void*)0)) { | |||||
1425 | Py_INCREF(res)( ((PyObject*)(res))->ob_refcnt++); | |||||
1426 | Py_XDECREF(descr)do { if ((descr) == ((void*)0)) ; else do { if ( --((PyObject *)(descr))->ob_refcnt != 0) ; else ( (*(((PyObject*)((PyObject *)(descr)))->ob_type)->tp_dealloc)((PyObject *)((PyObject *)(descr)))); } while (0); } while (0); | |||||
1427 | Py_DECREF(dict)do { if ( --((PyObject*)(dict))->ob_refcnt != 0) ; else ( ( *(((PyObject*)((PyObject *)(dict)))->ob_type)->tp_dealloc )((PyObject *)((PyObject *)(dict)))); } while (0); | |||||
1428 | goto done; | |||||
1429 | } | |||||
1430 | Py_DECREF(dict)do { if ( --((PyObject*)(dict))->ob_refcnt != 0) ; else ( ( *(((PyObject*)((PyObject *)(dict)))->ob_type)->tp_dealloc )((PyObject *)((PyObject *)(dict)))); } while (0); | |||||
1431 | } | |||||
1432 | ||||||
1433 | if (f != NULL((void*)0)) { | |||||
1434 | res = f(descr, obj, (PyObject *)Py_TYPE(obj)(((PyObject*)(obj))->ob_type)); | |||||
1435 | Py_DECREF(descr)do { if ( --((PyObject*)(descr))->ob_refcnt != 0) ; else ( (*(((PyObject*)((PyObject *)(descr)))->ob_type)->tp_dealloc )((PyObject *)((PyObject *)(descr)))); } while (0); | |||||
1436 | goto done; | |||||
1437 | } | |||||
1438 | ||||||
1439 | if (descr != NULL((void*)0)) { | |||||
1440 | res = descr; | |||||
1441 | /* descr was already increfed above */ | |||||
1442 | goto done; | |||||
1443 | } | |||||
1444 | ||||||
1445 | PyErr_Format(PyExc_AttributeError, | |||||
1446 | "'%.50s' object has no attribute '%.400s'", | |||||
1447 | tp->tp_name, PyString_AS_STRING(name)(((PyStringObject *)(name))->ob_sval)); | |||||
1448 | done: | |||||
1449 | Py_DECREF(name)do { if ( --((PyObject*)(name))->ob_refcnt != 0) ; else ( ( *(((PyObject*)((PyObject *)(name)))->ob_type)->tp_dealloc )((PyObject *)((PyObject *)(name)))); } while (0); | |||||
1450 | return res; | |||||
1451 | } | |||||
1452 | ||||||
1453 | PyObject * | |||||
1454 | PyObject_GenericGetAttr(PyObject *obj, PyObject *name) | |||||
1455 | { | |||||
1456 | return _PyObject_GenericGetAttrWithDict(obj, name, NULL((void*)0)); | |||||
1457 | } | |||||
1458 | ||||||
1459 | int | |||||
1460 | _PyObject_GenericSetAttrWithDict(PyObject *obj, PyObject *name, | |||||
1461 | PyObject *value, PyObject *dict) | |||||
1462 | { | |||||
1463 | PyTypeObject *tp = Py_TYPE(obj)(((PyObject*)(obj))->ob_type); | |||||
1464 | PyObject *descr; | |||||
1465 | descrsetfunc f; | |||||
1466 | PyObject **dictptr; | |||||
1467 | int res = -1; | |||||
1468 | ||||||
1469 | if (!PyString_Check(name)((((((PyObject*)(name))->ob_type))->tp_flags & ((1L <<27))) != 0)){ | |||||
1470 | #ifdef Py_USING_UNICODE1 | |||||
1471 | /* The Unicode to string conversion is done here because the | |||||
1472 | existing tp_setattro slots expect a string object as name | |||||
1473 | and we wouldn't want to break those. */ | |||||
1474 | if (PyUnicode_Check(name)((((((PyObject*)(name))->ob_type))->tp_flags & ((1L <<28))) != 0)) { | |||||
1475 | name = PyUnicode_AsEncodedStringPyUnicodeUCS4_AsEncodedString(name, NULL((void*)0), NULL((void*)0)); | |||||
1476 | if (name == NULL((void*)0)) | |||||
1477 | return -1; | |||||
1478 | } | |||||
1479 | else | |||||
1480 | #endif | |||||
1481 | { | |||||
1482 | PyErr_Format(PyExc_TypeError, | |||||
1483 | "attribute name must be string, not '%.200s'", | |||||
1484 | Py_TYPE(name)(((PyObject*)(name))->ob_type)->tp_name); | |||||
1485 | return -1; | |||||
1486 | } | |||||
1487 | } | |||||
1488 | else | |||||
1489 | Py_INCREF(name)( ((PyObject*)(name))->ob_refcnt++); | |||||
1490 | ||||||
1491 | if (tp->tp_dict == NULL((void*)0)) { | |||||
1492 | if (PyType_Ready(tp) < 0) | |||||
1493 | goto done; | |||||
1494 | } | |||||
1495 | ||||||
1496 | descr = _PyType_Lookup(tp, name); | |||||
1497 | f = NULL((void*)0); | |||||
1498 | if (descr != NULL((void*)0) && | |||||
1499 | PyType_HasFeature(descr->ob_type, Py_TPFLAGS_HAVE_CLASS)(((descr->ob_type)->tp_flags & ((1L<<8))) != 0 )) { | |||||
1500 | f = descr->ob_type->tp_descr_set; | |||||
1501 | if (f != NULL((void*)0) && PyDescr_IsData(descr)((((PyObject*)(descr))->ob_type)->tp_descr_set != ((void *)0))) { | |||||
1502 | res = f(descr, obj, value); | |||||
1503 | goto done; | |||||
1504 | } | |||||
1505 | } | |||||
1506 | ||||||
1507 | if (dict == NULL((void*)0)) { | |||||
1508 | dictptr = _PyObject_GetDictPtr(obj); | |||||
1509 | if (dictptr != NULL((void*)0)) { | |||||
1510 | dict = *dictptr; | |||||
1511 | if (dict == NULL((void*)0) && value != NULL((void*)0)) { | |||||
1512 | dict = PyDict_New(); | |||||
1513 | if (dict == NULL((void*)0)) | |||||
1514 | goto done; | |||||
1515 | *dictptr = dict; | |||||
1516 | } | |||||
1517 | } | |||||
1518 | } | |||||
1519 | if (dict != NULL((void*)0)) { | |||||
1520 | Py_INCREF(dict)( ((PyObject*)(dict))->ob_refcnt++); | |||||
1521 | if (value == NULL((void*)0)) | |||||
1522 | res = PyDict_DelItem(dict, name); | |||||
1523 | else | |||||
1524 | res = PyDict_SetItem(dict, name, value); | |||||
1525 | if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError)) | |||||
1526 | PyErr_SetObject(PyExc_AttributeError, name); | |||||
1527 | Py_DECREF(dict)do { if ( --((PyObject*)(dict))->ob_refcnt != 0) ; else ( ( *(((PyObject*)((PyObject *)(dict)))->ob_type)->tp_dealloc )((PyObject *)((PyObject *)(dict)))); } while (0); | |||||
1528 | goto done; | |||||
1529 | } | |||||
1530 | ||||||
1531 | if (f != NULL((void*)0)) { | |||||
1532 | res = f(descr, obj, value); | |||||
1533 | goto done; | |||||
1534 | } | |||||
1535 | ||||||
1536 | if (descr == NULL((void*)0)) { | |||||
1537 | PyErr_Format(PyExc_AttributeError, | |||||
1538 | "'%.100s' object has no attribute '%.200s'", | |||||
1539 | tp->tp_name, PyString_AS_STRING(name)(((PyStringObject *)(name))->ob_sval)); | |||||
1540 | goto done; | |||||
1541 | } | |||||
1542 | ||||||
1543 | PyErr_Format(PyExc_AttributeError, | |||||
1544 | "'%.50s' object attribute '%.400s' is read-only", | |||||
1545 | tp->tp_name, PyString_AS_STRING(name)(((PyStringObject *)(name))->ob_sval)); | |||||
1546 | done: | |||||
1547 | Py_DECREF(name)do { if ( --((PyObject*)(name))->ob_refcnt != 0) ; else ( ( *(((PyObject*)((PyObject *)(name)))->ob_type)->tp_dealloc )((PyObject *)((PyObject *)(name)))); } while (0); | |||||
1548 | return res; | |||||
1549 | } | |||||
1550 | ||||||
1551 | int | |||||
1552 | PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value) | |||||
1553 | { | |||||
1554 | return _PyObject_GenericSetAttrWithDict(obj, name, value, NULL((void*)0)); | |||||
1555 | } | |||||
1556 | ||||||
1557 | ||||||
1558 | /* Test a value used as condition, e.g., in a for or if statement. | |||||
1559 | Return -1 if an error occurred */ | |||||
1560 | ||||||
1561 | int | |||||
1562 | PyObject_IsTrue(PyObject *v) | |||||
1563 | { | |||||
1564 | Py_ssize_t res; | |||||
1565 | if (v == Py_True((PyObject *) &_Py_TrueStruct)) | |||||
1566 | return 1; | |||||
1567 | if (v == Py_False((PyObject *) &_Py_ZeroStruct)) | |||||
1568 | return 0; | |||||
1569 | if (v == Py_None(&_Py_NoneStruct)) | |||||
1570 | return 0; | |||||
1571 | else if (v->ob_type->tp_as_number != NULL((void*)0) && | |||||
1572 | v->ob_type->tp_as_number->nb_nonzero != NULL((void*)0)) | |||||
1573 | res = (*v->ob_type->tp_as_number->nb_nonzero)(v); | |||||
1574 | else if (v->ob_type->tp_as_mapping != NULL((void*)0) && | |||||
1575 | v->ob_type->tp_as_mapping->mp_length != NULL((void*)0)) | |||||
1576 | res = (*v->ob_type->tp_as_mapping->mp_length)(v); | |||||
1577 | else if (v->ob_type->tp_as_sequence != NULL((void*)0) && | |||||
1578 | v->ob_type->tp_as_sequence->sq_length != NULL((void*)0)) | |||||
1579 | res = (*v->ob_type->tp_as_sequence->sq_length)(v); | |||||
1580 | else | |||||
1581 | return 1; | |||||
1582 | /* if it is negative, it should be either -1 or -2 */ | |||||
1583 | return (res > 0) ? 1 : Py_SAFE_DOWNCAST(res, Py_ssize_t, int)(int)(res); | |||||
1584 | } | |||||
1585 | ||||||
1586 | /* equivalent of 'not v' | |||||
1587 | Return -1 if an error occurred */ | |||||
1588 | ||||||
1589 | int | |||||
1590 | PyObject_Not(PyObject *v) | |||||
1591 | { | |||||
1592 | int res; | |||||
1593 | res = PyObject_IsTrue(v); | |||||
1594 | if (res < 0) | |||||
1595 | return res; | |||||
1596 | return res == 0; | |||||
1597 | } | |||||
1598 | ||||||
1599 | /* Coerce two numeric types to the "larger" one. | |||||
1600 | Increment the reference count on each argument. | |||||
1601 | Return value: | |||||
1602 | -1 if an error occurred; | |||||
1603 | 0 if the coercion succeeded (and then the reference counts are increased); | |||||
1604 | 1 if no coercion is possible (and no error is raised). | |||||
1605 | */ | |||||
1606 | int | |||||
1607 | PyNumber_CoerceEx(PyObject **pv, PyObject **pw) | |||||
1608 | { | |||||
1609 | register PyObject *v = *pv; | |||||
1610 | register PyObject *w = *pw; | |||||
1611 | int res; | |||||
1612 | ||||||
1613 | /* Shortcut only for old-style types */ | |||||
1614 | if (v->ob_type == w->ob_type && | |||||
1615 | !PyType_HasFeature(v->ob_type, Py_TPFLAGS_CHECKTYPES)(((v->ob_type)->tp_flags & ((1L<<4))) != 0)) | |||||
1616 | { | |||||
1617 | Py_INCREF(v)( ((PyObject*)(v))->ob_refcnt++); | |||||
1618 | Py_INCREF(w)( ((PyObject*)(w))->ob_refcnt++); | |||||
1619 | return 0; | |||||
1620 | } | |||||
1621 | if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) { | |||||
1622 | res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw); | |||||
1623 | if (res <= 0) | |||||
1624 | return res; | |||||
1625 | } | |||||
1626 | if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) { | |||||
1627 | res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv); | |||||
1628 | if (res <= 0) | |||||
1629 | return res; | |||||
1630 | } | |||||
1631 | return 1; | |||||
1632 | } | |||||
1633 | ||||||
1634 | /* Coerce two numeric types to the "larger" one. | |||||
1635 | Increment the reference count on each argument. | |||||
1636 | Return -1 and raise an exception if no coercion is possible | |||||
1637 | (and then no reference count is incremented). | |||||
1638 | */ | |||||
1639 | int | |||||
1640 | PyNumber_Coerce(PyObject **pv, PyObject **pw) | |||||
1641 | { | |||||
1642 | int err = PyNumber_CoerceEx(pv, pw); | |||||
1643 | if (err <= 0) | |||||
1644 | return err; | |||||
1645 | PyErr_SetString(PyExc_TypeError, "number coercion failed"); | |||||
1646 | return -1; | |||||
1647 | } | |||||
1648 | ||||||
1649 | ||||||
1650 | /* Test whether an object can be called */ | |||||
1651 | ||||||
1652 | int | |||||
1653 | PyCallable_Check(PyObject *x) | |||||
1654 | { | |||||
1655 | if (x == NULL((void*)0)) | |||||
1656 | return 0; | |||||
1657 | if (PyInstance_Check(x)((x)->ob_type == &PyInstance_Type)) { | |||||
1658 | PyObject *call = PyObject_GetAttrString(x, "__call__"); | |||||
1659 | if (call == NULL((void*)0)) { | |||||
1660 | PyErr_Clear(); | |||||
1661 | return 0; | |||||
1662 | } | |||||
1663 | /* Could test recursively but don't, for fear of endless | |||||
1664 | recursion if some joker sets self.__call__ = self */ | |||||
1665 | Py_DECREF(call)do { if ( --((PyObject*)(call))->ob_refcnt != 0) ; else ( ( *(((PyObject*)((PyObject *)(call)))->ob_type)->tp_dealloc )((PyObject *)((PyObject *)(call)))); } while (0); | |||||
1666 | return 1; | |||||
1667 | } | |||||
1668 | else { | |||||
1669 | return x->ob_type->tp_call != NULL((void*)0); | |||||
1670 | } | |||||
1671 | } | |||||
1672 | ||||||
1673 | /* ------------------------- PyObject_Dir() helpers ------------------------- */ | |||||
1674 | ||||||
1675 | /* Helper for PyObject_Dir. | |||||
1676 | Merge the __dict__ of aclass into dict, and recursively also all | |||||
1677 | the __dict__s of aclass's base classes. The order of merging isn't | |||||
1678 | defined, as it's expected that only the final set of dict keys is | |||||
1679 | interesting. | |||||
1680 | Return 0 on success, -1 on error. | |||||
1681 | */ | |||||
1682 | ||||||
1683 | static int | |||||
1684 | merge_class_dict(PyObject* dict, PyObject* aclass) | |||||
1685 | { | |||||
1686 | PyObject *classdict; | |||||
1687 | PyObject *bases; | |||||
1688 | ||||||
1689 | assert(PyDict_Check(dict))((void) (0)); | |||||
1690 | assert(aclass)((void) (0)); | |||||
1691 | ||||||
1692 | /* Merge in the type's dict (if any). */ | |||||
1693 | classdict = PyObject_GetAttrString(aclass, "__dict__"); | |||||
1694 | if (classdict == NULL((void*)0)) | |||||
1695 | PyErr_Clear(); | |||||
1696 | else { | |||||
1697 | int status = PyDict_Update(dict, classdict); | |||||
1698 | Py_DECREF(classdict)do { if ( --((PyObject*)(classdict))->ob_refcnt != 0) ; else ( (*(((PyObject*)((PyObject *)(classdict)))->ob_type)-> tp_dealloc)((PyObject *)((PyObject *)(classdict)))); } while ( 0); | |||||
1699 | if (status < 0) | |||||
1700 | return -1; | |||||
1701 | } | |||||
1702 | ||||||
1703 | /* Recursively merge in the base types' (if any) dicts. */ | |||||
1704 | bases = PyObject_GetAttrString(aclass, "__bases__"); | |||||
1705 | if (bases == NULL((void*)0)) | |||||
1706 | PyErr_Clear(); | |||||
1707 | else { | |||||
1708 | /* We have no guarantee that bases is a real tuple */ | |||||
1709 | Py_ssize_t i, n; | |||||
1710 | n = PySequence_Size(bases); /* This better be right */ | |||||
1711 | if (n < 0) | |||||
1712 | PyErr_Clear(); | |||||
1713 | else { | |||||
1714 | for (i = 0; i < n; i++) { | |||||
1715 | int status; | |||||
1716 | PyObject *base = PySequence_GetItem(bases, i); | |||||
1717 | if (base == NULL((void*)0)) { | |||||
1718 | Py_DECREF(bases)do { if ( --((PyObject*)(bases))->ob_refcnt != 0) ; else ( (*(((PyObject*)((PyObject *)(bases)))->ob_type)->tp_dealloc )((PyObject *)((PyObject *)(bases)))); } while (0); | |||||
1719 | return -1; | |||||
1720 | } | |||||
1721 | status = merge_class_dict(dict, base); | |||||
1722 | Py_DECREF(base)do { if ( --((PyObject*)(base))->ob_refcnt != 0) ; else ( ( *(((PyObject*)((PyObject *)(base)))->ob_type)->tp_dealloc )((PyObject *)((PyObject *)(base)))); } while (0); | |||||
1723 | if (status < 0) { | |||||
1724 | Py_DECREF(bases)do { if ( --((PyObject*)(bases))->ob_refcnt != 0) ; else ( (*(((PyObject*)((PyObject *)(bases)))->ob_type)->tp_dealloc )((PyObject *)((PyObject *)(bases)))); } while (0); | |||||
1725 | return -1; | |||||
1726 | } | |||||
1727 | } | |||||
1728 | } | |||||
1729 | Py_DECREF(bases)do { if ( --((PyObject*)(bases))->ob_refcnt != 0) ; else ( (*(((PyObject*)((PyObject *)(bases)))->ob_type)->tp_dealloc )((PyObject *)((PyObject *)(bases)))); } while (0); | |||||
1730 | } | |||||
1731 | return 0; | |||||
1732 | } | |||||
1733 | ||||||
1734 | /* Helper for PyObject_Dir. | |||||
1735 | If obj has an attr named attrname that's a list, merge its string | |||||
1736 | elements into keys of dict. | |||||
1737 | Return 0 on success, -1 on error. Errors due to not finding the attr, | |||||
1738 | or the attr not being a list, are suppressed. | |||||
1739 | */ | |||||
1740 | ||||||
1741 | static int | |||||
1742 | merge_list_attr(PyObject* dict, PyObject* obj, const char *attrname) | |||||
1743 | { | |||||
1744 | PyObject *list; | |||||
1745 | int result = 0; | |||||
1746 | ||||||
1747 | assert(PyDict_Check(dict))((void) (0)); | |||||
1748 | assert(obj)((void) (0)); | |||||
1749 | assert(attrname)((void) (0)); | |||||
1750 | ||||||
1751 | list = PyObject_GetAttrString(obj, attrname); | |||||
1752 | if (list == NULL((void*)0)) | |||||
1753 | PyErr_Clear(); | |||||
1754 | ||||||
1755 | else if (PyList_Check(list)((((((PyObject*)(list))->ob_type))->tp_flags & ((1L <<25))) != 0)) { | |||||
1756 | int i; | |||||
1757 | for (i = 0; i < PyList_GET_SIZE(list)(((PyVarObject*)(list))->ob_size); ++i) { | |||||
1758 | PyObject *item = PyList_GET_ITEM(list, i)(((PyListObject *)(list))->ob_item[i]); | |||||
1759 | if (PyString_Check(item)((((((PyObject*)(item))->ob_type))->tp_flags & ((1L <<27))) != 0)) { | |||||
1760 | result = PyDict_SetItem(dict, item, Py_None(&_Py_NoneStruct)); | |||||
1761 | if (result < 0) | |||||
1762 | break; | |||||
1763 | } | |||||
1764 | } | |||||
1765 | if (Py_Py3kWarningFlag && | |||||
1766 | (strcmp(attrname, "__members__") == 0 || | |||||
1767 | strcmp(attrname, "__methods__") == 0)) { | |||||
1768 | if (PyErr_WarnEx(PyExc_DeprecationWarning, | |||||
1769 | "__members__ and __methods__ not " | |||||
1770 | "supported in 3.x", 1) < 0) { | |||||
1771 | Py_XDECREF(list)do { if ((list) == ((void*)0)) ; else do { if ( --((PyObject* )(list))->ob_refcnt != 0) ; else ( (*(((PyObject*)((PyObject *)(list)))->ob_type)->tp_dealloc)((PyObject *)((PyObject *)(list)))); } while (0); } while (0); | |||||
1772 | return -1; | |||||
1773 | } | |||||
1774 | } | |||||
1775 | } | |||||
1776 | ||||||
1777 | Py_XDECREF(list)do { if ((list) == ((void*)0)) ; else do { if ( --((PyObject* )(list))->ob_refcnt != 0) ; else ( (*(((PyObject*)((PyObject *)(list)))->ob_type)->tp_dealloc)((PyObject *)((PyObject *)(list)))); } while (0); } while (0); | |||||
1778 | return result; | |||||
1779 | } | |||||
1780 | ||||||
1781 | /* Helper for PyObject_Dir without arguments: returns the local scope. */ | |||||
1782 | static PyObject * | |||||
1783 | _dir_locals(void) | |||||
1784 | { | |||||
1785 | PyObject *names; | |||||
1786 | PyObject *locals = PyEval_GetLocals(); | |||||
1787 | ||||||
1788 | if (locals == NULL((void*)0)) { | |||||
1789 | PyErr_SetString(PyExc_SystemError, "frame does not exist"); | |||||
1790 | return NULL((void*)0); | |||||
1791 | } | |||||
1792 | ||||||
1793 | names = PyMapping_Keys(locals)PyObject_CallMethod(locals,"keys",((void*)0)); | |||||
1794 | if (!names) | |||||
1795 | return NULL((void*)0); | |||||
1796 | if (!PyList_Check(names)((((((PyObject*)(names))->ob_type))->tp_flags & ((1L <<25))) != 0)) { | |||||
1797 | PyErr_Format(PyExc_TypeError, | |||||
1798 | "dir(): expected keys() of locals to be a list, " | |||||
1799 | "not '%.200s'", Py_TYPE(names)(((PyObject*)(names))->ob_type)->tp_name); | |||||
1800 | Py_DECREF(names)do { if ( --((PyObject*)(names))->ob_refcnt != 0) ; else ( (*(((PyObject*)((PyObject *)(names)))->ob_type)->tp_dealloc )((PyObject *)((PyObject *)(names)))); } while (0); | |||||
1801 | return NULL((void*)0); | |||||
1802 | } | |||||
1803 | /* the locals don't need to be DECREF'd */ | |||||
1804 | return names; | |||||
1805 | } | |||||
1806 | ||||||
1807 | /* Helper for PyObject_Dir of type objects: returns __dict__ and __bases__. | |||||
1808 | We deliberately don't suck up its __class__, as methods belonging to the | |||||
1809 | metaclass would probably be more confusing than helpful. | |||||
1810 | */ | |||||
1811 | static PyObject * | |||||
1812 | _specialized_dir_type(PyObject *obj) | |||||
1813 | { | |||||
1814 | PyObject *result = NULL((void*)0); | |||||
1815 | PyObject *dict = PyDict_New(); | |||||
1816 | ||||||
1817 | if (dict != NULL((void*)0) && merge_class_dict(dict, obj) == 0) | |||||
1818 | result = PyDict_Keys(dict); | |||||
1819 | ||||||
1820 | Py_XDECREF(dict)do { if ((dict) == ((void*)0)) ; else do { if ( --((PyObject* )(dict))->ob_refcnt != 0) ; else ( (*(((PyObject*)((PyObject *)(dict)))->ob_type)->tp_dealloc)((PyObject *)((PyObject *)(dict)))); } while (0); } while (0); | |||||
1821 | return result; | |||||
1822 | } | |||||
1823 | ||||||
1824 | /* Helper for PyObject_Dir of module objects: returns the module's __dict__. */ | |||||
1825 | static PyObject * | |||||
1826 | _specialized_dir_module(PyObject *obj) | |||||
1827 | { | |||||
1828 | PyObject *result = NULL((void*)0); | |||||
1829 | PyObject *dict = PyObject_GetAttrString(obj, "__dict__"); | |||||
1830 | ||||||
1831 | if (dict != NULL((void*)0)) { | |||||
1832 | if (PyDict_Check(dict)((((((PyObject*)(dict))->ob_type))->tp_flags & ((1L <<29))) != 0)) | |||||
1833 | result = PyDict_Keys(dict); | |||||
1834 | else { | |||||
1835 | char *name = PyModule_GetName(obj); | |||||
1836 | if (name) | |||||
1837 | PyErr_Format(PyExc_TypeError, | |||||
1838 | "%.200s.__dict__ is not a dictionary", | |||||
1839 | name); | |||||
1840 | } | |||||
1841 | } | |||||
1842 | ||||||
1843 | Py_XDECREF(dict)do { if ((dict) == ((void*)0)) ; else do { if ( --((PyObject* )(dict))->ob_refcnt != 0) ; else ( (*(((PyObject*)((PyObject *)(dict)))->ob_type)->tp_dealloc)((PyObject *)((PyObject *)(dict)))); } while (0); } while (0); | |||||
1844 | return result; | |||||
1845 | } | |||||
1846 | ||||||
1847 | /* Helper for PyObject_Dir of generic objects: returns __dict__, __class__, | |||||
1848 | and recursively up the __class__.__bases__ chain. | |||||
1849 | */ | |||||
1850 | static PyObject * | |||||
1851 | _generic_dir(PyObject *obj) | |||||
1852 | { | |||||
1853 | PyObject *result = NULL((void*)0); | |||||
1854 | PyObject *dict = NULL((void*)0); | |||||
1855 | PyObject *itsclass = NULL((void*)0); | |||||
1856 | ||||||
1857 | /* Get __dict__ (which may or may not be a real dict...) */ | |||||
1858 | dict = PyObject_GetAttrString(obj, "__dict__"); | |||||
1859 | if (dict == NULL((void*)0)) { | |||||
1860 | PyErr_Clear(); | |||||
1861 | dict = PyDict_New(); | |||||
1862 | } | |||||
1863 | else if (!PyDict_Check(dict)((((((PyObject*)(dict))->ob_type))->tp_flags & ((1L <<29))) != 0)) { | |||||
1864 | Py_DECREF(dict)do { if ( --((PyObject*)(dict))->ob_refcnt != 0) ; else ( ( *(((PyObject*)((PyObject *)(dict)))->ob_type)->tp_dealloc )((PyObject *)((PyObject *)(dict)))); } while (0); | |||||
1865 | dict = PyDict_New(); | |||||
1866 | } | |||||
1867 | else { | |||||
1868 | /* Copy __dict__ to avoid mutating it. */ | |||||
1869 | PyObject *temp = PyDict_Copy(dict); | |||||
1870 | Py_DECREF(dict)do { if ( --((PyObject*)(dict))->ob_refcnt != 0) ; else ( ( *(((PyObject*)((PyObject *)(dict)))->ob_type)->tp_dealloc )((PyObject *)((PyObject *)(dict)))); } while (0); | |||||
1871 | dict = temp; | |||||
1872 | } | |||||
1873 | ||||||
1874 | if (dict == NULL((void*)0)) | |||||
1875 | goto error; | |||||
1876 | ||||||
1877 | /* Merge in __members__ and __methods__ (if any). | |||||
1878 | * This is removed in Python 3000. */ | |||||
1879 | if (merge_list_attr(dict, obj, "__members__") < 0) | |||||
1880 | goto error; | |||||
1881 | if (merge_list_attr(dict, obj, "__methods__") < 0) | |||||
1882 | goto error; | |||||
1883 | ||||||
1884 | /* Merge in attrs reachable from its class. */ | |||||
1885 | itsclass = PyObject_GetAttrString(obj, "__class__"); | |||||
1886 | if (itsclass == NULL((void*)0)) | |||||
1887 | /* XXX(tomer): Perhaps fall back to obj->ob_type if no | |||||
1888 | __class__ exists? */ | |||||
1889 | PyErr_Clear(); | |||||
1890 | else { | |||||
1891 | if (merge_class_dict(dict, itsclass) != 0) | |||||
1892 | goto error; | |||||
1893 | } | |||||
1894 | ||||||
1895 | result = PyDict_Keys(dict); | |||||
1896 | /* fall through */ | |||||
1897 | error: | |||||
1898 | Py_XDECREF(itsclass)do { if ((itsclass) == ((void*)0)) ; else do { if ( --((PyObject *)(itsclass))->ob_refcnt != 0) ; else ( (*(((PyObject*)((PyObject *)(itsclass)))->ob_type)->tp_dealloc)((PyObject *)((PyObject *)(itsclass)))); } while (0); } while (0); | |||||
1899 | Py_XDECREF(dict)do { if ((dict) == ((void*)0)) ; else do { if ( --((PyObject* )(dict))->ob_refcnt != 0) ; else ( (*(((PyObject*)((PyObject *)(dict)))->ob_type)->tp_dealloc)((PyObject *)((PyObject *)(dict)))); } while (0); } while (0); | |||||
1900 | return result; | |||||
1901 | } | |||||
1902 | ||||||
1903 | /* Helper for PyObject_Dir: object introspection. | |||||
1904 | This calls one of the above specialized versions if no __dir__ method | |||||
1905 | exists. */ | |||||
1906 | static PyObject * | |||||
1907 | _dir_object(PyObject *obj) | |||||
1908 | { | |||||
1909 | PyObject *result = NULL((void*)0); | |||||
1910 | static PyObject *dir_str = NULL((void*)0); | |||||
1911 | PyObject *dirfunc; | |||||
1912 | ||||||
1913 | assert(obj)((void) (0)); | |||||
1914 | if (PyInstance_Check(obj)((obj)->ob_type == &PyInstance_Type)) { | |||||
1915 | dirfunc = PyObject_GetAttrString(obj, "__dir__"); | |||||
1916 | if (dirfunc == NULL((void*)0)) { | |||||
1917 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) | |||||
1918 | PyErr_Clear(); | |||||
1919 | else | |||||
1920 | return NULL((void*)0); | |||||
1921 | } | |||||
1922 | } | |||||
1923 | else { | |||||
1924 | dirfunc = _PyObject_LookupSpecial(obj, "__dir__", &dir_str); | |||||
1925 | if (PyErr_Occurred()) | |||||
1926 | return NULL((void*)0); | |||||
1927 | } | |||||
1928 | if (dirfunc == NULL((void*)0)) { | |||||
1929 | /* use default implementation */ | |||||
1930 | if (PyModule_Check(obj)((((PyObject*)(obj))->ob_type) == (&PyModule_Type) || PyType_IsSubtype ((((PyObject*)(obj))->ob_type), (&PyModule_Type)))) | |||||
1931 | result = _specialized_dir_module(obj); | |||||
1932 | else if (PyType_Check(obj)((((((PyObject*)(obj))->ob_type))->tp_flags & ((1L<< 31))) != 0) || PyClass_Check(obj)((obj)->ob_type == &PyClass_Type)) | |||||
1933 | result = _specialized_dir_type(obj); | |||||
1934 | else | |||||
1935 | result = _generic_dir(obj); | |||||
1936 | } | |||||
1937 | else { | |||||
1938 | /* use __dir__ */ | |||||
1939 | result = PyObject_CallFunctionObjArgs(dirfunc, NULL((void*)0)); | |||||
1940 | Py_DECREF(dirfunc)do { if ( --((PyObject*)(dirfunc))->ob_refcnt != 0) ; else ( (*(((PyObject*)((PyObject *)(dirfunc)))->ob_type)->tp_dealloc )((PyObject *)((PyObject *)(dirfunc)))); } while (0); | |||||
1941 | if (result == NULL((void*)0)) | |||||
1942 | return NULL((void*)0); | |||||
1943 | ||||||
1944 | /* result must be a list */ | |||||
1945 | /* XXX(gbrandl): could also check if all items are strings */ | |||||
1946 | if (!PyList_Check(result)((((((PyObject*)(result))->ob_type))->tp_flags & (( 1L<<25))) != 0)) { | |||||
1947 | PyErr_Format(PyExc_TypeError, | |||||
1948 | "__dir__() must return a list, not %.200s", | |||||
1949 | Py_TYPE(result)(((PyObject*)(result))->ob_type)->tp_name); | |||||
1950 | Py_DECREF(result)do { if ( --((PyObject*)(result))->ob_refcnt != 0) ; else ( (*(((PyObject*)((PyObject *)(result)))->ob_type)->tp_dealloc )((PyObject *)((PyObject *)(result)))); } while (0); | |||||
1951 | result = NULL((void*)0); | |||||
1952 | } | |||||
1953 | } | |||||
1954 | ||||||
1955 | return result; | |||||
1956 | } | |||||
1957 | ||||||
1958 | /* Implementation of dir() -- if obj is NULL, returns the names in the current | |||||
1959 | (local) scope. Otherwise, performs introspection of the object: returns a | |||||
1960 | sorted list of attribute names (supposedly) accessible from the object | |||||
1961 | */ | |||||
1962 | PyObject * | |||||
1963 | PyObject_Dir(PyObject *obj) | |||||
1964 | { | |||||
1965 | PyObject * result; | |||||
1966 | ||||||
1967 | if (obj == NULL((void*)0)) | |||||
1968 | /* no object -- introspect the locals */ | |||||
1969 | result = _dir_locals(); | |||||
1970 | else | |||||
1971 | /* object -- introspect the object */ | |||||
1972 | result = _dir_object(obj); | |||||
1973 | ||||||
1974 | assert(result == NULL || PyList_Check(result))((void) (0)); | |||||
1975 | ||||||
1976 | if (result != NULL((void*)0) && PyList_Sort(result) != 0) { | |||||
1977 | /* sorting the list failed */ | |||||
1978 | Py_DECREF(result)do { if ( --((PyObject*)(result))->ob_refcnt != 0) ; else ( (*(((PyObject*)((PyObject *)(result)))->ob_type)->tp_dealloc )((PyObject *)((PyObject *)(result)))); } while (0); | |||||
1979 | result = NULL((void*)0); | |||||
1980 | } | |||||
1981 | ||||||
1982 | return result; | |||||
1983 | } | |||||
1984 | ||||||
1985 | /* | |||||
1986 | NoObject is usable as a non-NULL undefined value, used by the macro None. | |||||
1987 | There is (and should be!) no way to create other objects of this type, | |||||
1988 | so there is exactly one (which is indestructible, by the way). | |||||
1989 | (XXX This type and the type of NotImplemented below should be unified.) | |||||
1990 | */ | |||||
1991 | ||||||
1992 | /* ARGSUSED */ | |||||
1993 | static PyObject * | |||||
1994 | none_repr(PyObject *op) | |||||
1995 | { | |||||
1996 | return PyString_FromString("None"); | |||||
1997 | } | |||||
1998 | ||||||
1999 | /* ARGUSED */ | |||||
2000 | static void | |||||
2001 | none_dealloc(PyObject* ignore) | |||||
2002 | { | |||||
2003 | /* This should never get called, but we also don't want to SEGV if | |||||
2004 | * we accidentally decref None out of existence. | |||||
2005 | */ | |||||
2006 | Py_FatalError("deallocating None"); | |||||
2007 | } | |||||
2008 | ||||||
2009 | ||||||
2010 | static PyTypeObject PyNone_Type = { | |||||
2011 | PyVarObject_HEAD_INIT(&PyType_Type, 0)1, &PyType_Type, 0, | |||||
2012 | "NoneType", | |||||
2013 | 0, | |||||
2014 | 0, | |||||
2015 | none_dealloc, /*tp_dealloc*/ /*never called*/ | |||||
2016 | 0, /*tp_print*/ | |||||
2017 | 0, /*tp_getattr*/ | |||||
2018 | 0, /*tp_setattr*/ | |||||
2019 | 0, /*tp_compare*/ | |||||
2020 | none_repr, /*tp_repr*/ | |||||
2021 | 0, /*tp_as_number*/ | |||||
2022 | 0, /*tp_as_sequence*/ | |||||
2023 | 0, /*tp_as_mapping*/ | |||||
2024 | (hashfunc)_Py_HashPointer, /*tp_hash */ | |||||
2025 | }; | |||||
2026 | ||||||
2027 | PyObject _Py_NoneStruct = { | |||||
2028 | _PyObject_EXTRA_INIT | |||||
2029 | 1, &PyNone_Type | |||||
2030 | }; | |||||
2031 | ||||||
2032 | /* NotImplemented is an object that can be used to signal that an | |||||
2033 | operation is not implemented for the given type combination. */ | |||||
2034 | ||||||
2035 | static PyObject * | |||||
2036 | NotImplemented_repr(PyObject *op) | |||||
2037 | { | |||||
2038 | return PyString_FromString("NotImplemented"); | |||||
2039 | } | |||||
2040 | ||||||
2041 | static PyTypeObject PyNotImplemented_Type = { | |||||
2042 | PyVarObject_HEAD_INIT(&PyType_Type, 0)1, &PyType_Type, 0, | |||||
2043 | "NotImplementedType", | |||||
2044 | 0, | |||||
2045 | 0, | |||||
2046 | none_dealloc, /*tp_dealloc*/ /*never called*/ | |||||
2047 | 0, /*tp_print*/ | |||||
2048 | 0, /*tp_getattr*/ | |||||
2049 | 0, /*tp_setattr*/ | |||||
2050 | 0, /*tp_compare*/ | |||||
2051 | NotImplemented_repr, /*tp_repr*/ | |||||
2052 | 0, /*tp_as_number*/ | |||||
2053 | 0, /*tp_as_sequence*/ | |||||
2054 | 0, /*tp_as_mapping*/ | |||||
2055 | 0, /*tp_hash */ | |||||
2056 | }; | |||||
2057 | ||||||
2058 | PyObject _Py_NotImplementedStruct = { | |||||
2059 | _PyObject_EXTRA_INIT | |||||
2060 | 1, &PyNotImplemented_Type | |||||
2061 | }; | |||||
2062 | ||||||
2063 | void | |||||
2064 | _Py_ReadyTypes(void) | |||||
2065 | { | |||||
2066 | if (PyType_Ready(&PyType_Type) < 0) | |||||
2067 | Py_FatalError("Can't initialize type type"); | |||||
2068 | ||||||
2069 | if (PyType_Ready(&_PyWeakref_RefType) < 0) | |||||
2070 | Py_FatalError("Can't initialize weakref type"); | |||||
2071 | ||||||
2072 | if (PyType_Ready(&_PyWeakref_CallableProxyType) < 0) | |||||
2073 | Py_FatalError("Can't initialize callable weakref proxy type"); | |||||
2074 | ||||||
2075 | if (PyType_Ready(&_PyWeakref_ProxyType) < 0) | |||||
2076 | Py_FatalError("Can't initialize weakref proxy type"); | |||||
2077 | ||||||
2078 | if (PyType_Ready(&PyBool_Type) < 0) | |||||
2079 | Py_FatalError("Can't initialize bool type"); | |||||
2080 | ||||||
2081 | if (PyType_Ready(&PyString_Type) < 0) | |||||
2082 | Py_FatalError("Can't initialize str type"); | |||||
2083 | ||||||
2084 | if (PyType_Ready(&PyByteArray_Type) < 0) | |||||
2085 | Py_FatalError("Can't initialize bytearray type"); | |||||
2086 | ||||||
2087 | if (PyType_Ready(&PyList_Type) < 0) | |||||
2088 | Py_FatalError("Can't initialize list type"); | |||||
2089 | ||||||
2090 | if (PyType_Ready(&PyNone_Type) < 0) | |||||
2091 | Py_FatalError("Can't initialize None type"); | |||||
2092 | ||||||
2093 | if (PyType_Ready(&PyNotImplemented_Type) < 0) | |||||
2094 | Py_FatalError("Can't initialize NotImplemented type"); | |||||
2095 | ||||||
2096 | if (PyType_Ready(&PyTraceBack_Type) < 0) | |||||
2097 | Py_FatalError("Can't initialize traceback type"); | |||||
2098 | ||||||
2099 | if (PyType_Ready(&PySuper_Type) < 0) | |||||
2100 | Py_FatalError("Can't initialize super type"); | |||||
2101 | ||||||
2102 | if (PyType_Ready(&PyBaseObject_Type) < 0) | |||||
2103 | Py_FatalError("Can't initialize object type"); | |||||
2104 | ||||||
2105 | if (PyType_Ready(&PyRange_Type) < 0) | |||||
2106 | Py_FatalError("Can't initialize xrange type"); | |||||
2107 | ||||||
2108 | if (PyType_Ready(&PyDict_Type) < 0) | |||||
2109 | Py_FatalError("Can't initialize dict type"); | |||||
2110 | ||||||
2111 | if (PyType_Ready(&PySet_Type) < 0) | |||||
2112 | Py_FatalError("Can't initialize set type"); | |||||
2113 | ||||||
2114 | if (PyType_Ready(&PyUnicode_Type) < 0) | |||||
2115 | Py_FatalError("Can't initialize unicode type"); | |||||
2116 | ||||||
2117 | if (PyType_Ready(&PySlice_Type) < 0) | |||||
2118 | Py_FatalError("Can't initialize slice type"); | |||||
2119 | ||||||
2120 | if (PyType_Ready(&PyStaticMethod_Type) < 0) | |||||
2121 | Py_FatalError("Can't initialize static method type"); | |||||
2122 | ||||||
2123 | #ifndef WITHOUT_COMPLEX | |||||
2124 | if (PyType_Ready(&PyComplex_Type) < 0) | |||||
2125 | Py_FatalError("Can't initialize complex type"); | |||||
2126 | #endif | |||||
2127 | ||||||
2128 | if (PyType_Ready(&PyFloat_Type) < 0) | |||||
2129 | Py_FatalError("Can't initialize float type"); | |||||
2130 | ||||||
2131 | if (PyType_Ready(&PyBuffer_Type) < 0) | |||||
2132 | Py_FatalError("Can't initialize buffer type"); | |||||
2133 | ||||||
2134 | if (PyType_Ready(&PyLong_Type) < 0) | |||||
2135 | Py_FatalError("Can't initialize long type"); | |||||
2136 | ||||||
2137 | if (PyType_Ready(&PyInt_Type) < 0) | |||||
2138 | Py_FatalError("Can't initialize int type"); | |||||
2139 | ||||||
2140 | if (PyType_Ready(&PyFrozenSet_Type) < 0) | |||||
2141 | Py_FatalError("Can't initialize frozenset type"); | |||||
2142 | ||||||
2143 | if (PyType_Ready(&PyProperty_Type) < 0) | |||||
2144 | Py_FatalError("Can't initialize property type"); | |||||
2145 | ||||||
2146 | if (PyType_Ready(&PyMemoryView_Type) < 0) | |||||
2147 | Py_FatalError("Can't initialize memoryview type"); | |||||
2148 | ||||||
2149 | if (PyType_Ready(&PyTuple_Type) < 0) | |||||
2150 | Py_FatalError("Can't initialize tuple type"); | |||||
2151 | ||||||
2152 | if (PyType_Ready(&PyEnum_Type) < 0) | |||||
2153 | Py_FatalError("Can't initialize enumerate type"); | |||||
2154 | ||||||
2155 | if (PyType_Ready(&PyReversed_Type) < 0) | |||||
2156 | Py_FatalError("Can't initialize reversed type"); | |||||
2157 | ||||||
2158 | if (PyType_Ready(&PyCode_Type) < 0) | |||||
2159 | Py_FatalError("Can't initialize code type"); | |||||
2160 | ||||||
2161 | if (PyType_Ready(&PyFrame_Type) < 0) | |||||
2162 | Py_FatalError("Can't initialize frame type"); | |||||
2163 | ||||||
2164 | if (PyType_Ready(&PyCFunction_Type) < 0) | |||||
2165 | Py_FatalError("Can't initialize builtin function type"); | |||||
2166 | ||||||
2167 | if (PyType_Ready(&PyMethod_Type) < 0) | |||||
2168 | Py_FatalError("Can't initialize method type"); | |||||
2169 | ||||||
2170 | if (PyType_Ready(&PyFunction_Type) < 0) | |||||
2171 | Py_FatalError("Can't initialize function type"); | |||||
2172 | ||||||
2173 | if (PyType_Ready(&PyClass_Type) < 0) | |||||
2174 | Py_FatalError("Can't initialize class type"); | |||||
2175 | ||||||
2176 | if (PyType_Ready(&PyDictProxy_Type) < 0) | |||||
2177 | Py_FatalError("Can't initialize dict proxy type"); | |||||
2178 | ||||||
2179 | if (PyType_Ready(&PyGen_Type) < 0) | |||||
2180 | Py_FatalError("Can't initialize generator type"); | |||||
2181 | ||||||
2182 | if (PyType_Ready(&PyGetSetDescr_Type) < 0) | |||||
2183 | Py_FatalError("Can't initialize get-set descriptor type"); | |||||
2184 | ||||||
2185 | if (PyType_Ready(&PyWrapperDescr_Type) < 0) | |||||
2186 | Py_FatalError("Can't initialize wrapper type"); | |||||
2187 | ||||||
2188 | if (PyType_Ready(&PyInstance_Type) < 0) | |||||
2189 | Py_FatalError("Can't initialize instance type"); | |||||
2190 | ||||||
2191 | if (PyType_Ready(&PyEllipsis_Type) < 0) | |||||
2192 | Py_FatalError("Can't initialize ellipsis type"); | |||||
2193 | ||||||
2194 | if (PyType_Ready(&PyMemberDescr_Type) < 0) | |||||
2195 | Py_FatalError("Can't initialize member descriptor type"); | |||||
2196 | ||||||
2197 | if (PyType_Ready(&PyFile_Type) < 0) | |||||
2198 | Py_FatalError("Can't initialize file type"); | |||||
2199 | } | |||||
2200 | ||||||
2201 | ||||||
2202 | #ifdef Py_TRACE_REFS | |||||
2203 | ||||||
2204 | void | |||||
2205 | _Py_NewReference(PyObject *op)( (((PyObject*)(PyObject *op))->ob_refcnt) = 1) | |||||
2206 | { | |||||
2207 | _Py_INC_REFTOTAL; | |||||
2208 | op->ob_refcnt = 1; | |||||
2209 | _Py_AddToAllObjects(op, 1); | |||||
2210 | _Py_INC_TPALLOCS(op); | |||||
2211 | } | |||||
2212 | ||||||
2213 | void | |||||
2214 | _Py_ForgetReference(register PyObject *op) | |||||
2215 | { | |||||
2216 | #ifdef SLOW_UNREF_CHECK | |||||
2217 | register PyObject *p; | |||||
2218 | #endif | |||||
2219 | if (op->ob_refcnt < 0) | |||||
2220 | Py_FatalError("UNREF negative refcnt"); | |||||
2221 | if (op == &refchain || | |||||
2222 | op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op) | |||||
2223 | Py_FatalError("UNREF invalid object"); | |||||
2224 | #ifdef SLOW_UNREF_CHECK | |||||
2225 | for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) { | |||||
2226 | if (p == op) | |||||
2227 | break; | |||||
2228 | } | |||||
2229 | if (p == &refchain) /* Not found */ | |||||
2230 | Py_FatalError("UNREF unknown object"); | |||||
2231 | #endif | |||||
2232 | op->_ob_next->_ob_prev = op->_ob_prev; | |||||
2233 | op->_ob_prev->_ob_next = op->_ob_next; | |||||
2234 | op->_ob_next = op->_ob_prev = NULL((void*)0); | |||||
2235 | _Py_INC_TPFREES(op); | |||||
2236 | } | |||||
2237 | ||||||
2238 | void | |||||
2239 | _Py_Dealloc(PyObject *op)( (*(((PyObject*)(PyObject *op))->ob_type)->tp_dealloc) ((PyObject *)(PyObject *op))) | |||||
2240 | { | |||||
2241 | destructor dealloc = Py_TYPE(op)(((PyObject*)(op))->ob_type)->tp_dealloc; | |||||
2242 | _Py_ForgetReference(op); | |||||
2243 | (*dealloc)(op); | |||||
2244 | } | |||||
2245 | ||||||
2246 | /* Print all live objects. Because PyObject_Print is called, the | |||||
2247 | * interpreter must be in a healthy state. | |||||
2248 | */ | |||||
2249 | void | |||||
2250 | _Py_PrintReferences(FILE *fp) | |||||
2251 | { | |||||
2252 | PyObject *op; | |||||
2253 | fprintf(fp, "Remaining objects:\n"); | |||||
2254 | for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) { | |||||
2255 | fprintf(fp, "%p [%" PY_FORMAT_SIZE_T"z" "d] ", op, op->ob_refcnt); | |||||
2256 | if (PyObject_Print(op, fp, 0) != 0) | |||||
2257 | PyErr_Clear(); | |||||
2258 | putc('\n', fp)_IO_putc ('\n', fp); | |||||
2259 | } | |||||
2260 | } | |||||
2261 | ||||||
2262 | /* Print the addresses of all live objects. Unlike _Py_PrintReferences, this | |||||
2263 | * doesn't make any calls to the Python C API, so is always safe to call. | |||||
2264 | */ | |||||
2265 | void | |||||
2266 | _Py_PrintReferenceAddresses(FILE *fp) | |||||
2267 | { | |||||
2268 | PyObject *op; | |||||
2269 | fprintf(fp, "Remaining object addresses:\n"); | |||||
2270 | for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) | |||||
2271 | fprintf(fp, "%p [%" PY_FORMAT_SIZE_T"z" "d] %s\n", op, | |||||
2272 | op->ob_refcnt, Py_TYPE(op)(((PyObject*)(op))->ob_type)->tp_name); | |||||
2273 | } | |||||
2274 | ||||||
2275 | PyObject * | |||||
2276 | _Py_GetObjects(PyObject *self, PyObject *args) | |||||
2277 | { | |||||
2278 | int i, n; | |||||
2279 | PyObject *t = NULL((void*)0); | |||||
2280 | PyObject *res, *op; | |||||
2281 | ||||||
2282 | if (!PyArg_ParseTuple(args, "i|O", &n, &t)) | |||||
2283 | return NULL((void*)0); | |||||
2284 | op = refchain._ob_next; | |||||
2285 | res = PyList_New(0); | |||||
2286 | if (res == NULL((void*)0)) | |||||
2287 | return NULL((void*)0); | |||||
2288 | for (i = 0; (n == 0 || i < n) && op != &refchain; i++) { | |||||
2289 | while (op == self || op == args || op == res || op == t || | |||||
2290 | (t != NULL((void*)0) && Py_TYPE(op)(((PyObject*)(op))->ob_type) != (PyTypeObject *) t)) { | |||||
2291 | op = op->_ob_next; | |||||
2292 | if (op == &refchain) | |||||
2293 | return res; | |||||
2294 | } | |||||
2295 | if (PyList_Append(res, op) < 0) { | |||||
2296 | Py_DECREF(res)do { if ( --((PyObject*)(res))->ob_refcnt != 0) ; else ( ( *(((PyObject*)((PyObject *)(res)))->ob_type)->tp_dealloc )((PyObject *)((PyObject *)(res)))); } while (0); | |||||
2297 | return NULL((void*)0); | |||||
2298 | } | |||||
2299 | op = op->_ob_next; | |||||
2300 | } | |||||
2301 | return res; | |||||
2302 | } | |||||
2303 | ||||||
2304 | #endif | |||||
2305 | ||||||
2306 | ||||||
2307 | /* Hack to force loading of capsule.o */ | |||||
2308 | PyTypeObject *_Py_capsule_hack = &PyCapsule_Type; | |||||
2309 | ||||||
2310 | ||||||
2311 | /* Hack to force loading of cobject.o */ | |||||
2312 | PyTypeObject *_Py_cobject_hack = &PyCObject_Type; | |||||
2313 | ||||||
2314 | ||||||
2315 | /* Hack to force loading of abstract.o */ | |||||
2316 | Py_ssize_t (*_Py_abstract_hack)(PyObject *) = PyObject_Size; | |||||
2317 | ||||||
2318 | ||||||
2319 | /* Python's malloc wrappers (see pymem.h) */ | |||||
2320 | ||||||
2321 | void * | |||||
2322 | PyMem_Malloc(size_t nbytes) | |||||
2323 | { | |||||
2324 | return PyMem_MALLOC(nbytes)((size_t)(nbytes) > (size_t)((Py_ssize_t)(((size_t)-1)>> 1)) ? ((void*)0) : malloc((nbytes) ? (nbytes) : 1)); | |||||
2325 | } | |||||
2326 | ||||||
2327 | void * | |||||
2328 | PyMem_Realloc(void *p, size_t nbytes) | |||||
2329 | { | |||||
2330 | return PyMem_REALLOC(p, nbytes)((size_t)(nbytes) > (size_t)((Py_ssize_t)(((size_t)-1)>> 1)) ? ((void*)0) : realloc((p), (nbytes) ? (nbytes) : 1)); | |||||
2331 | } | |||||
2332 | ||||||
2333 | void | |||||
2334 | PyMem_Free(void *p) | |||||
2335 | { | |||||
2336 | PyMem_FREEfree(p); | |||||
2337 | } | |||||
2338 | ||||||
2339 | void | |||||
2340 | _PyObject_DebugTypeStats(FILE *out) | |||||
2341 | { | |||||
2342 | _PyString_DebugMallocStats(out); | |||||
2343 | _PyCFunction_DebugMallocStats(out); | |||||
2344 | _PyDict_DebugMallocStats(out); | |||||
2345 | _PyFloat_DebugMallocStats(out); | |||||
2346 | _PyFrame_DebugMallocStats(out); | |||||
2347 | _PyInt_DebugMallocStats(out); | |||||
2348 | _PyList_DebugMallocStats(out); | |||||
2349 | _PyMethod_DebugMallocStats(out); | |||||
2350 | _PySet_DebugMallocStats(out); | |||||
2351 | _PyTuple_DebugMallocStats(out); | |||||
2352 | #if Py_USING_UNICODE1 | |||||
2353 | _PyUnicode_DebugMallocStats(out); | |||||
2354 | #endif | |||||
2355 | } | |||||
2356 | ||||||
2357 | /* These methods are used to control infinite recursion in repr, str, print, | |||||
2358 | etc. Container objects that may recursively contain themselves, | |||||
2359 | e.g. builtin dictionaries and lists, should used Py_ReprEnter() and | |||||
2360 | Py_ReprLeave() to avoid infinite recursion. | |||||
2361 | ||||||
2362 | Py_ReprEnter() returns 0 the first time it is called for a particular | |||||
2363 | object and 1 every time thereafter. It returns -1 if an exception | |||||
2364 | occurred. Py_ReprLeave() has no return value. | |||||
2365 | ||||||
2366 | See dictobject.c and listobject.c for examples of use. | |||||
2367 | */ | |||||
2368 | ||||||
2369 | #define KEY"Py_Repr" "Py_Repr" | |||||
2370 | ||||||
2371 | int | |||||
2372 | Py_ReprEnter(PyObject *obj) | |||||
2373 | { | |||||
2374 | PyObject *dict; | |||||
2375 | PyObject *list; | |||||
2376 | Py_ssize_t i; | |||||
2377 | ||||||
2378 | dict = PyThreadState_GetDict(); | |||||
2379 | if (dict == NULL((void*)0)) | |||||
2380 | return 0; | |||||
2381 | list = PyDict_GetItemString(dict, KEY"Py_Repr"); | |||||
2382 | if (list == NULL((void*)0)) { | |||||
2383 | list = PyList_New(0); | |||||
2384 | if (list == NULL((void*)0)) | |||||
2385 | return -1; | |||||
2386 | if (PyDict_SetItemString(dict, KEY"Py_Repr", list) < 0) | |||||
2387 | return -1; | |||||
2388 | Py_DECREF(list)do { if ( --((PyObject*)(list))->ob_refcnt != 0) ; else ( ( *(((PyObject*)((PyObject *)(list)))->ob_type)->tp_dealloc )((PyObject *)((PyObject *)(list)))); } while (0); | |||||
2389 | } | |||||
2390 | i = PyList_GET_SIZE(list)(((PyVarObject*)(list))->ob_size); | |||||
2391 | while (--i >= 0) { | |||||
2392 | if (PyList_GET_ITEM(list, i)(((PyListObject *)(list))->ob_item[i]) == obj) | |||||
2393 | return 1; | |||||
2394 | } | |||||
2395 | PyList_Append(list, obj); | |||||
2396 | return 0; | |||||
2397 | } | |||||
2398 | ||||||
2399 | void | |||||
2400 | Py_ReprLeave(PyObject *obj) | |||||
2401 | { | |||||
2402 | PyObject *dict; | |||||
2403 | PyObject *list; | |||||
2404 | Py_ssize_t i; | |||||
2405 | ||||||
2406 | dict = PyThreadState_GetDict(); | |||||
2407 | if (dict == NULL((void*)0)) | |||||
2408 | return; | |||||
2409 | list = PyDict_GetItemString(dict, KEY"Py_Repr"); | |||||
2410 | if (list == NULL((void*)0) || !PyList_Check(list)((((((PyObject*)(list))->ob_type))->tp_flags & ((1L <<25))) != 0)) | |||||
2411 | return; | |||||
2412 | i = PyList_GET_SIZE(list)(((PyVarObject*)(list))->ob_size); | |||||
2413 | /* Count backwards because we always expect obj to be list[-1] */ | |||||
2414 | while (--i >= 0) { | |||||
2415 | if (PyList_GET_ITEM(list, i)(((PyListObject *)(list))->ob_item[i]) == obj) { | |||||
2416 | PyList_SetSlice(list, i, i + 1, NULL((void*)0)); | |||||
2417 | break; | |||||
2418 | } | |||||
2419 | } | |||||
2420 | } | |||||
2421 | ||||||
2422 | /* Trashcan support. */ | |||||
2423 | ||||||
2424 | /* Current call-stack depth of tp_dealloc calls. */ | |||||
2425 | int _PyTrash_delete_nesting = 0; | |||||
2426 | ||||||
2427 | /* List of objects that still need to be cleaned up, singly linked via their | |||||
2428 | * gc headers' gc_prev pointers. | |||||
2429 | */ | |||||
2430 | PyObject *_PyTrash_delete_later = NULL((void*)0); | |||||
2431 | ||||||
2432 | /* Add op to the _PyTrash_delete_later list. Called when the current | |||||
2433 | * call-stack depth gets large. op must be a currently untracked gc'ed | |||||
2434 | * object, with refcount 0. Py_DECREF must already have been called on it. | |||||
2435 | */ | |||||
2436 | void | |||||
2437 | _PyTrash_deposit_object(PyObject *op) | |||||
2438 | { | |||||
2439 | assert(PyObject_IS_GC(op))((void) (0)); | |||||
2440 | assert(_Py_AS_GC(op)->gc.gc_refs == _PyGC_REFS_UNTRACKED)((void) (0)); | |||||
2441 | assert(op->ob_refcnt == 0)((void) (0)); | |||||
2442 | _Py_AS_GC(op)((PyGC_Head *)(op)-1)->gc.gc_prev = (PyGC_Head *)_PyTrash_delete_later; | |||||
2443 | _PyTrash_delete_later = op; | |||||
2444 | } | |||||
2445 | ||||||
2446 | /* Dealloccate all the objects in the _PyTrash_delete_later list. Called when | |||||
2447 | * the call-stack unwinds again. | |||||
2448 | */ | |||||
2449 | void | |||||
2450 | _PyTrash_destroy_chain(void) | |||||
2451 | { | |||||
2452 | while (_PyTrash_delete_later) { | |||||
2453 | PyObject *op = _PyTrash_delete_later; | |||||
2454 | destructor dealloc = Py_TYPE(op)(((PyObject*)(op))->ob_type)->tp_dealloc; | |||||
2455 | ||||||
2456 | _PyTrash_delete_later = | |||||
2457 | (PyObject*) _Py_AS_GC(op)((PyGC_Head *)(op)-1)->gc.gc_prev; | |||||
2458 | ||||||
2459 | /* Call the deallocator directly. This used to try to | |||||
2460 | * fool Py_DECREF into calling it indirectly, but | |||||
2461 | * Py_DECREF was already called on this object, and in | |||||
2462 | * assorted non-release builds calling Py_DECREF again ends | |||||
2463 | * up distorting allocation statistics. | |||||
2464 | */ | |||||
2465 | assert(op->ob_refcnt == 0)((void) (0)); | |||||
2466 | ++_PyTrash_delete_nesting; | |||||
2467 | (*dealloc)(op); | |||||
2468 | --_PyTrash_delete_nesting; | |||||
2469 | } | |||||
2470 | } | |||||
2471 | ||||||
2472 | #ifdef __cplusplus | |||||
2473 | } | |||||
2474 | #endif |