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