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