1 | n/a | |
---|
2 | n/a | /* Generic object operations; and implementation of None */ |
---|
3 | n/a | |
---|
4 | n/a | #include "Python.h" |
---|
5 | n/a | #include "frameobject.h" |
---|
6 | n/a | |
---|
7 | n/a | #ifdef __cplusplus |
---|
8 | n/a | extern "C" { |
---|
9 | n/a | #endif |
---|
10 | n/a | |
---|
11 | n/a | _Py_IDENTIFIER(Py_Repr); |
---|
12 | n/a | _Py_IDENTIFIER(__bytes__); |
---|
13 | n/a | _Py_IDENTIFIER(__dir__); |
---|
14 | n/a | _Py_IDENTIFIER(__isabstractmethod__); |
---|
15 | n/a | _Py_IDENTIFIER(builtins); |
---|
16 | n/a | |
---|
17 | n/a | #ifdef Py_REF_DEBUG |
---|
18 | n/a | Py_ssize_t _Py_RefTotal; |
---|
19 | n/a | |
---|
20 | n/a | Py_ssize_t |
---|
21 | n/a | _Py_GetRefTotal(void) |
---|
22 | n/a | { |
---|
23 | n/a | PyObject *o; |
---|
24 | n/a | Py_ssize_t total = _Py_RefTotal; |
---|
25 | n/a | o = _PySet_Dummy; |
---|
26 | n/a | if (o != NULL) |
---|
27 | n/a | total -= o->ob_refcnt; |
---|
28 | n/a | return total; |
---|
29 | n/a | } |
---|
30 | n/a | |
---|
31 | n/a | void |
---|
32 | n/a | _PyDebug_PrintTotalRefs(void) { |
---|
33 | n/a | PyObject *xoptions, *value; |
---|
34 | n/a | _Py_IDENTIFIER(showrefcount); |
---|
35 | n/a | |
---|
36 | n/a | xoptions = PySys_GetXOptions(); |
---|
37 | n/a | if (xoptions == NULL) |
---|
38 | n/a | return; |
---|
39 | n/a | value = _PyDict_GetItemId(xoptions, &PyId_showrefcount); |
---|
40 | n/a | if (value == Py_True) |
---|
41 | n/a | fprintf(stderr, |
---|
42 | n/a | "[%" PY_FORMAT_SIZE_T "d refs, " |
---|
43 | n/a | "%" PY_FORMAT_SIZE_T "d blocks]\n", |
---|
44 | n/a | _Py_GetRefTotal(), _Py_GetAllocatedBlocks()); |
---|
45 | n/a | } |
---|
46 | n/a | #endif /* Py_REF_DEBUG */ |
---|
47 | n/a | |
---|
48 | n/a | /* Object allocation routines used by NEWOBJ and NEWVAROBJ macros. |
---|
49 | n/a | These are used by the individual routines for object creation. |
---|
50 | n/a | Do not call them otherwise, they do not initialize the object! */ |
---|
51 | n/a | |
---|
52 | n/a | #ifdef Py_TRACE_REFS |
---|
53 | n/a | /* Head of circular doubly-linked list of all objects. These are linked |
---|
54 | n/a | * together via the _ob_prev and _ob_next members of a PyObject, which |
---|
55 | n/a | * exist only in a Py_TRACE_REFS build. |
---|
56 | n/a | */ |
---|
57 | n/a | static PyObject refchain = {&refchain, &refchain}; |
---|
58 | n/a | |
---|
59 | n/a | /* Insert op at the front of the list of all objects. If force is true, |
---|
60 | n/a | * op is added even if _ob_prev and _ob_next are non-NULL already. If |
---|
61 | n/a | * force is false amd _ob_prev or _ob_next are non-NULL, do nothing. |
---|
62 | n/a | * force should be true if and only if op points to freshly allocated, |
---|
63 | n/a | * uninitialized memory, or you've unlinked op from the list and are |
---|
64 | n/a | * relinking it into the front. |
---|
65 | n/a | * Note that objects are normally added to the list via _Py_NewReference, |
---|
66 | n/a | * which is called by PyObject_Init. Not all objects are initialized that |
---|
67 | n/a | * way, though; exceptions include statically allocated type objects, and |
---|
68 | n/a | * statically allocated singletons (like Py_True and Py_None). |
---|
69 | n/a | */ |
---|
70 | n/a | void |
---|
71 | n/a | _Py_AddToAllObjects(PyObject *op, int force) |
---|
72 | n/a | { |
---|
73 | n/a | #ifdef Py_DEBUG |
---|
74 | n/a | if (!force) { |
---|
75 | n/a | /* If it's initialized memory, op must be in or out of |
---|
76 | n/a | * the list unambiguously. |
---|
77 | n/a | */ |
---|
78 | n/a | assert((op->_ob_prev == NULL) == (op->_ob_next == NULL)); |
---|
79 | n/a | } |
---|
80 | n/a | #endif |
---|
81 | n/a | if (force || op->_ob_prev == NULL) { |
---|
82 | n/a | op->_ob_next = refchain._ob_next; |
---|
83 | n/a | op->_ob_prev = &refchain; |
---|
84 | n/a | refchain._ob_next->_ob_prev = op; |
---|
85 | n/a | refchain._ob_next = op; |
---|
86 | n/a | } |
---|
87 | n/a | } |
---|
88 | n/a | #endif /* Py_TRACE_REFS */ |
---|
89 | n/a | |
---|
90 | n/a | #ifdef COUNT_ALLOCS |
---|
91 | n/a | static PyTypeObject *type_list; |
---|
92 | n/a | /* All types are added to type_list, at least when |
---|
93 | n/a | they get one object created. That makes them |
---|
94 | n/a | immortal, which unfortunately contributes to |
---|
95 | n/a | garbage itself. If unlist_types_without_objects |
---|
96 | n/a | is set, they will be removed from the type_list |
---|
97 | n/a | once the last object is deallocated. */ |
---|
98 | n/a | static int unlist_types_without_objects; |
---|
99 | n/a | extern Py_ssize_t tuple_zero_allocs, fast_tuple_allocs; |
---|
100 | n/a | extern Py_ssize_t quick_int_allocs, quick_neg_int_allocs; |
---|
101 | n/a | extern Py_ssize_t null_strings, one_strings; |
---|
102 | n/a | void |
---|
103 | n/a | dump_counts(FILE* f) |
---|
104 | n/a | { |
---|
105 | n/a | PyTypeObject *tp; |
---|
106 | n/a | PyObject *xoptions, *value; |
---|
107 | n/a | _Py_IDENTIFIER(showalloccount); |
---|
108 | n/a | |
---|
109 | n/a | xoptions = PySys_GetXOptions(); |
---|
110 | n/a | if (xoptions == NULL) |
---|
111 | n/a | return; |
---|
112 | n/a | value = _PyDict_GetItemId(xoptions, &PyId_showalloccount); |
---|
113 | n/a | if (value != Py_True) |
---|
114 | n/a | return; |
---|
115 | n/a | |
---|
116 | n/a | for (tp = type_list; tp; tp = tp->tp_next) |
---|
117 | n/a | fprintf(f, "%s alloc'd: %" PY_FORMAT_SIZE_T "d, " |
---|
118 | n/a | "freed: %" PY_FORMAT_SIZE_T "d, " |
---|
119 | n/a | "max in use: %" PY_FORMAT_SIZE_T "d\n", |
---|
120 | n/a | tp->tp_name, tp->tp_allocs, tp->tp_frees, |
---|
121 | n/a | tp->tp_maxalloc); |
---|
122 | n/a | fprintf(f, "fast tuple allocs: %" PY_FORMAT_SIZE_T "d, " |
---|
123 | n/a | "empty: %" PY_FORMAT_SIZE_T "d\n", |
---|
124 | n/a | fast_tuple_allocs, tuple_zero_allocs); |
---|
125 | n/a | fprintf(f, "fast int allocs: pos: %" PY_FORMAT_SIZE_T "d, " |
---|
126 | n/a | "neg: %" PY_FORMAT_SIZE_T "d\n", |
---|
127 | n/a | quick_int_allocs, quick_neg_int_allocs); |
---|
128 | n/a | fprintf(f, "null strings: %" PY_FORMAT_SIZE_T "d, " |
---|
129 | n/a | "1-strings: %" PY_FORMAT_SIZE_T "d\n", |
---|
130 | n/a | null_strings, one_strings); |
---|
131 | n/a | } |
---|
132 | n/a | |
---|
133 | n/a | PyObject * |
---|
134 | n/a | get_counts(void) |
---|
135 | n/a | { |
---|
136 | n/a | PyTypeObject *tp; |
---|
137 | n/a | PyObject *result; |
---|
138 | n/a | PyObject *v; |
---|
139 | n/a | |
---|
140 | n/a | result = PyList_New(0); |
---|
141 | n/a | if (result == NULL) |
---|
142 | n/a | return NULL; |
---|
143 | n/a | for (tp = type_list; tp; tp = tp->tp_next) { |
---|
144 | n/a | v = Py_BuildValue("(snnn)", tp->tp_name, tp->tp_allocs, |
---|
145 | n/a | tp->tp_frees, tp->tp_maxalloc); |
---|
146 | n/a | if (v == NULL) { |
---|
147 | n/a | Py_DECREF(result); |
---|
148 | n/a | return NULL; |
---|
149 | n/a | } |
---|
150 | n/a | if (PyList_Append(result, v) < 0) { |
---|
151 | n/a | Py_DECREF(v); |
---|
152 | n/a | Py_DECREF(result); |
---|
153 | n/a | return NULL; |
---|
154 | n/a | } |
---|
155 | n/a | Py_DECREF(v); |
---|
156 | n/a | } |
---|
157 | n/a | return result; |
---|
158 | n/a | } |
---|
159 | n/a | |
---|
160 | n/a | void |
---|
161 | n/a | inc_count(PyTypeObject *tp) |
---|
162 | n/a | { |
---|
163 | n/a | if (tp->tp_next == NULL && tp->tp_prev == NULL) { |
---|
164 | n/a | /* first time; insert in linked list */ |
---|
165 | n/a | if (tp->tp_next != NULL) /* sanity check */ |
---|
166 | n/a | Py_FatalError("XXX inc_count sanity check"); |
---|
167 | n/a | if (type_list) |
---|
168 | n/a | type_list->tp_prev = tp; |
---|
169 | n/a | tp->tp_next = type_list; |
---|
170 | n/a | /* Note that as of Python 2.2, heap-allocated type objects |
---|
171 | n/a | * can go away, but this code requires that they stay alive |
---|
172 | n/a | * until program exit. That's why we're careful with |
---|
173 | n/a | * refcounts here. type_list gets a new reference to tp, |
---|
174 | n/a | * while ownership of the reference type_list used to hold |
---|
175 | n/a | * (if any) was transferred to tp->tp_next in the line above. |
---|
176 | n/a | * tp is thus effectively immortal after this. |
---|
177 | n/a | */ |
---|
178 | n/a | Py_INCREF(tp); |
---|
179 | n/a | type_list = tp; |
---|
180 | n/a | #ifdef Py_TRACE_REFS |
---|
181 | n/a | /* Also insert in the doubly-linked list of all objects, |
---|
182 | n/a | * if not already there. |
---|
183 | n/a | */ |
---|
184 | n/a | _Py_AddToAllObjects((PyObject *)tp, 0); |
---|
185 | n/a | #endif |
---|
186 | n/a | } |
---|
187 | n/a | tp->tp_allocs++; |
---|
188 | n/a | if (tp->tp_allocs - tp->tp_frees > tp->tp_maxalloc) |
---|
189 | n/a | tp->tp_maxalloc = tp->tp_allocs - tp->tp_frees; |
---|
190 | n/a | } |
---|
191 | n/a | |
---|
192 | n/a | void dec_count(PyTypeObject *tp) |
---|
193 | n/a | { |
---|
194 | n/a | tp->tp_frees++; |
---|
195 | n/a | if (unlist_types_without_objects && |
---|
196 | n/a | tp->tp_allocs == tp->tp_frees) { |
---|
197 | n/a | /* unlink the type from type_list */ |
---|
198 | n/a | if (tp->tp_prev) |
---|
199 | n/a | tp->tp_prev->tp_next = tp->tp_next; |
---|
200 | n/a | else |
---|
201 | n/a | type_list = tp->tp_next; |
---|
202 | n/a | if (tp->tp_next) |
---|
203 | n/a | tp->tp_next->tp_prev = tp->tp_prev; |
---|
204 | n/a | tp->tp_next = tp->tp_prev = NULL; |
---|
205 | n/a | Py_DECREF(tp); |
---|
206 | n/a | } |
---|
207 | n/a | } |
---|
208 | n/a | |
---|
209 | n/a | #endif |
---|
210 | n/a | |
---|
211 | n/a | #ifdef Py_REF_DEBUG |
---|
212 | n/a | /* Log a fatal error; doesn't return. */ |
---|
213 | n/a | void |
---|
214 | n/a | _Py_NegativeRefcount(const char *fname, int lineno, PyObject *op) |
---|
215 | n/a | { |
---|
216 | n/a | char buf[300]; |
---|
217 | n/a | |
---|
218 | n/a | PyOS_snprintf(buf, sizeof(buf), |
---|
219 | n/a | "%s:%i object at %p has negative ref count " |
---|
220 | n/a | "%" PY_FORMAT_SIZE_T "d", |
---|
221 | n/a | fname, lineno, op, op->ob_refcnt); |
---|
222 | n/a | Py_FatalError(buf); |
---|
223 | n/a | } |
---|
224 | n/a | |
---|
225 | n/a | #endif /* Py_REF_DEBUG */ |
---|
226 | n/a | |
---|
227 | n/a | void |
---|
228 | n/a | Py_IncRef(PyObject *o) |
---|
229 | n/a | { |
---|
230 | n/a | Py_XINCREF(o); |
---|
231 | n/a | } |
---|
232 | n/a | |
---|
233 | n/a | void |
---|
234 | n/a | Py_DecRef(PyObject *o) |
---|
235 | n/a | { |
---|
236 | n/a | Py_XDECREF(o); |
---|
237 | n/a | } |
---|
238 | n/a | |
---|
239 | n/a | PyObject * |
---|
240 | n/a | PyObject_Init(PyObject *op, PyTypeObject *tp) |
---|
241 | n/a | { |
---|
242 | n/a | if (op == NULL) |
---|
243 | n/a | return PyErr_NoMemory(); |
---|
244 | n/a | /* Any changes should be reflected in PyObject_INIT (objimpl.h) */ |
---|
245 | n/a | Py_TYPE(op) = tp; |
---|
246 | n/a | _Py_NewReference(op); |
---|
247 | n/a | return op; |
---|
248 | n/a | } |
---|
249 | n/a | |
---|
250 | n/a | PyVarObject * |
---|
251 | n/a | PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, Py_ssize_t size) |
---|
252 | n/a | { |
---|
253 | n/a | if (op == NULL) |
---|
254 | n/a | return (PyVarObject *) PyErr_NoMemory(); |
---|
255 | n/a | /* Any changes should be reflected in PyObject_INIT_VAR */ |
---|
256 | n/a | op->ob_size = size; |
---|
257 | n/a | Py_TYPE(op) = tp; |
---|
258 | n/a | _Py_NewReference((PyObject *)op); |
---|
259 | n/a | return op; |
---|
260 | n/a | } |
---|
261 | n/a | |
---|
262 | n/a | PyObject * |
---|
263 | n/a | _PyObject_New(PyTypeObject *tp) |
---|
264 | n/a | { |
---|
265 | n/a | PyObject *op; |
---|
266 | n/a | op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp)); |
---|
267 | n/a | if (op == NULL) |
---|
268 | n/a | return PyErr_NoMemory(); |
---|
269 | n/a | return PyObject_INIT(op, tp); |
---|
270 | n/a | } |
---|
271 | n/a | |
---|
272 | n/a | PyVarObject * |
---|
273 | n/a | _PyObject_NewVar(PyTypeObject *tp, Py_ssize_t nitems) |
---|
274 | n/a | { |
---|
275 | n/a | PyVarObject *op; |
---|
276 | n/a | const size_t size = _PyObject_VAR_SIZE(tp, nitems); |
---|
277 | n/a | op = (PyVarObject *) PyObject_MALLOC(size); |
---|
278 | n/a | if (op == NULL) |
---|
279 | n/a | return (PyVarObject *)PyErr_NoMemory(); |
---|
280 | n/a | return PyObject_INIT_VAR(op, tp, nitems); |
---|
281 | n/a | } |
---|
282 | n/a | |
---|
283 | n/a | void |
---|
284 | n/a | PyObject_CallFinalizer(PyObject *self) |
---|
285 | n/a | { |
---|
286 | n/a | PyTypeObject *tp = Py_TYPE(self); |
---|
287 | n/a | |
---|
288 | n/a | /* The former could happen on heaptypes created from the C API, e.g. |
---|
289 | n/a | PyType_FromSpec(). */ |
---|
290 | n/a | if (!PyType_HasFeature(tp, Py_TPFLAGS_HAVE_FINALIZE) || |
---|
291 | n/a | tp->tp_finalize == NULL) |
---|
292 | n/a | return; |
---|
293 | n/a | /* tp_finalize should only be called once. */ |
---|
294 | n/a | if (PyType_IS_GC(tp) && _PyGC_FINALIZED(self)) |
---|
295 | n/a | return; |
---|
296 | n/a | |
---|
297 | n/a | tp->tp_finalize(self); |
---|
298 | n/a | if (PyType_IS_GC(tp)) |
---|
299 | n/a | _PyGC_SET_FINALIZED(self, 1); |
---|
300 | n/a | } |
---|
301 | n/a | |
---|
302 | n/a | int |
---|
303 | n/a | PyObject_CallFinalizerFromDealloc(PyObject *self) |
---|
304 | n/a | { |
---|
305 | n/a | Py_ssize_t refcnt; |
---|
306 | n/a | |
---|
307 | n/a | /* Temporarily resurrect the object. */ |
---|
308 | n/a | if (self->ob_refcnt != 0) { |
---|
309 | n/a | Py_FatalError("PyObject_CallFinalizerFromDealloc called on " |
---|
310 | n/a | "object with a non-zero refcount"); |
---|
311 | n/a | } |
---|
312 | n/a | self->ob_refcnt = 1; |
---|
313 | n/a | |
---|
314 | n/a | PyObject_CallFinalizer(self); |
---|
315 | n/a | |
---|
316 | n/a | /* Undo the temporary resurrection; can't use DECREF here, it would |
---|
317 | n/a | * cause a recursive call. |
---|
318 | n/a | */ |
---|
319 | n/a | assert(self->ob_refcnt > 0); |
---|
320 | n/a | if (--self->ob_refcnt == 0) |
---|
321 | n/a | return 0; /* this is the normal path out */ |
---|
322 | n/a | |
---|
323 | n/a | /* tp_finalize resurrected it! Make it look like the original Py_DECREF |
---|
324 | n/a | * never happened. |
---|
325 | n/a | */ |
---|
326 | n/a | refcnt = self->ob_refcnt; |
---|
327 | n/a | _Py_NewReference(self); |
---|
328 | n/a | self->ob_refcnt = refcnt; |
---|
329 | n/a | |
---|
330 | n/a | if (PyType_IS_GC(Py_TYPE(self))) { |
---|
331 | n/a | assert(_PyGC_REFS(self) != _PyGC_REFS_UNTRACKED); |
---|
332 | n/a | } |
---|
333 | n/a | /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so |
---|
334 | n/a | * we need to undo that. */ |
---|
335 | n/a | _Py_DEC_REFTOTAL; |
---|
336 | n/a | /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object |
---|
337 | n/a | * chain, so no more to do there. |
---|
338 | n/a | * If COUNT_ALLOCS, the original decref bumped tp_frees, and |
---|
339 | n/a | * _Py_NewReference bumped tp_allocs: both of those need to be |
---|
340 | n/a | * undone. |
---|
341 | n/a | */ |
---|
342 | n/a | #ifdef COUNT_ALLOCS |
---|
343 | n/a | --Py_TYPE(self)->tp_frees; |
---|
344 | n/a | --Py_TYPE(self)->tp_allocs; |
---|
345 | n/a | #endif |
---|
346 | n/a | return -1; |
---|
347 | n/a | } |
---|
348 | n/a | |
---|
349 | n/a | int |
---|
350 | n/a | PyObject_Print(PyObject *op, FILE *fp, int flags) |
---|
351 | n/a | { |
---|
352 | n/a | int ret = 0; |
---|
353 | n/a | if (PyErr_CheckSignals()) |
---|
354 | n/a | return -1; |
---|
355 | n/a | #ifdef USE_STACKCHECK |
---|
356 | n/a | if (PyOS_CheckStack()) { |
---|
357 | n/a | PyErr_SetString(PyExc_MemoryError, "stack overflow"); |
---|
358 | n/a | return -1; |
---|
359 | n/a | } |
---|
360 | n/a | #endif |
---|
361 | n/a | clearerr(fp); /* Clear any previous error condition */ |
---|
362 | n/a | if (op == NULL) { |
---|
363 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
364 | n/a | fprintf(fp, "<nil>"); |
---|
365 | n/a | Py_END_ALLOW_THREADS |
---|
366 | n/a | } |
---|
367 | n/a | else { |
---|
368 | n/a | if (op->ob_refcnt <= 0) |
---|
369 | n/a | /* XXX(twouters) cast refcount to long until %zd is |
---|
370 | n/a | universally available */ |
---|
371 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
372 | n/a | fprintf(fp, "<refcnt %ld at %p>", |
---|
373 | n/a | (long)op->ob_refcnt, op); |
---|
374 | n/a | Py_END_ALLOW_THREADS |
---|
375 | n/a | else { |
---|
376 | n/a | PyObject *s; |
---|
377 | n/a | if (flags & Py_PRINT_RAW) |
---|
378 | n/a | s = PyObject_Str(op); |
---|
379 | n/a | else |
---|
380 | n/a | s = PyObject_Repr(op); |
---|
381 | n/a | if (s == NULL) |
---|
382 | n/a | ret = -1; |
---|
383 | n/a | else if (PyBytes_Check(s)) { |
---|
384 | n/a | fwrite(PyBytes_AS_STRING(s), 1, |
---|
385 | n/a | PyBytes_GET_SIZE(s), fp); |
---|
386 | n/a | } |
---|
387 | n/a | else if (PyUnicode_Check(s)) { |
---|
388 | n/a | PyObject *t; |
---|
389 | n/a | t = PyUnicode_AsEncodedString(s, "utf-8", "backslashreplace"); |
---|
390 | n/a | if (t == NULL) |
---|
391 | n/a | ret = 0; |
---|
392 | n/a | else { |
---|
393 | n/a | fwrite(PyBytes_AS_STRING(t), 1, |
---|
394 | n/a | PyBytes_GET_SIZE(t), fp); |
---|
395 | n/a | Py_DECREF(t); |
---|
396 | n/a | } |
---|
397 | n/a | } |
---|
398 | n/a | else { |
---|
399 | n/a | PyErr_Format(PyExc_TypeError, |
---|
400 | n/a | "str() or repr() returned '%.100s'", |
---|
401 | n/a | s->ob_type->tp_name); |
---|
402 | n/a | ret = -1; |
---|
403 | n/a | } |
---|
404 | n/a | Py_XDECREF(s); |
---|
405 | n/a | } |
---|
406 | n/a | } |
---|
407 | n/a | if (ret == 0) { |
---|
408 | n/a | if (ferror(fp)) { |
---|
409 | n/a | PyErr_SetFromErrno(PyExc_IOError); |
---|
410 | n/a | clearerr(fp); |
---|
411 | n/a | ret = -1; |
---|
412 | n/a | } |
---|
413 | n/a | } |
---|
414 | n/a | return ret; |
---|
415 | n/a | } |
---|
416 | n/a | |
---|
417 | n/a | /* For debugging convenience. Set a breakpoint here and call it from your DLL */ |
---|
418 | n/a | void |
---|
419 | n/a | _Py_BreakPoint(void) |
---|
420 | n/a | { |
---|
421 | n/a | } |
---|
422 | n/a | |
---|
423 | n/a | |
---|
424 | n/a | /* For debugging convenience. See Misc/gdbinit for some useful gdb hooks */ |
---|
425 | n/a | void |
---|
426 | n/a | _PyObject_Dump(PyObject* op) |
---|
427 | n/a | { |
---|
428 | n/a | if (op == NULL) |
---|
429 | n/a | fprintf(stderr, "NULL\n"); |
---|
430 | n/a | else { |
---|
431 | n/a | #ifdef WITH_THREAD |
---|
432 | n/a | PyGILState_STATE gil; |
---|
433 | n/a | #endif |
---|
434 | n/a | PyObject *error_type, *error_value, *error_traceback; |
---|
435 | n/a | |
---|
436 | n/a | fprintf(stderr, "object : "); |
---|
437 | n/a | #ifdef WITH_THREAD |
---|
438 | n/a | gil = PyGILState_Ensure(); |
---|
439 | n/a | #endif |
---|
440 | n/a | |
---|
441 | n/a | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
---|
442 | n/a | (void)PyObject_Print(op, stderr, 0); |
---|
443 | n/a | PyErr_Restore(error_type, error_value, error_traceback); |
---|
444 | n/a | |
---|
445 | n/a | #ifdef WITH_THREAD |
---|
446 | n/a | PyGILState_Release(gil); |
---|
447 | n/a | #endif |
---|
448 | n/a | /* XXX(twouters) cast refcount to long until %zd is |
---|
449 | n/a | universally available */ |
---|
450 | n/a | fprintf(stderr, "\n" |
---|
451 | n/a | "type : %s\n" |
---|
452 | n/a | "refcount: %ld\n" |
---|
453 | n/a | "address : %p\n", |
---|
454 | n/a | Py_TYPE(op)==NULL ? "NULL" : Py_TYPE(op)->tp_name, |
---|
455 | n/a | (long)op->ob_refcnt, |
---|
456 | n/a | op); |
---|
457 | n/a | } |
---|
458 | n/a | } |
---|
459 | n/a | |
---|
460 | n/a | PyObject * |
---|
461 | n/a | PyObject_Repr(PyObject *v) |
---|
462 | n/a | { |
---|
463 | n/a | PyObject *res; |
---|
464 | n/a | if (PyErr_CheckSignals()) |
---|
465 | n/a | return NULL; |
---|
466 | n/a | #ifdef USE_STACKCHECK |
---|
467 | n/a | if (PyOS_CheckStack()) { |
---|
468 | n/a | PyErr_SetString(PyExc_MemoryError, "stack overflow"); |
---|
469 | n/a | return NULL; |
---|
470 | n/a | } |
---|
471 | n/a | #endif |
---|
472 | n/a | if (v == NULL) |
---|
473 | n/a | return PyUnicode_FromString("<NULL>"); |
---|
474 | n/a | if (Py_TYPE(v)->tp_repr == NULL) |
---|
475 | n/a | return PyUnicode_FromFormat("<%s object at %p>", |
---|
476 | n/a | v->ob_type->tp_name, v); |
---|
477 | n/a | |
---|
478 | n/a | #ifdef Py_DEBUG |
---|
479 | n/a | /* PyObject_Repr() must not be called with an exception set, |
---|
480 | n/a | because it can clear it (directly or indirectly) and so the |
---|
481 | n/a | caller loses its exception */ |
---|
482 | n/a | assert(!PyErr_Occurred()); |
---|
483 | n/a | #endif |
---|
484 | n/a | |
---|
485 | n/a | res = (*v->ob_type->tp_repr)(v); |
---|
486 | n/a | if (res == NULL) |
---|
487 | n/a | return NULL; |
---|
488 | n/a | if (!PyUnicode_Check(res)) { |
---|
489 | n/a | PyErr_Format(PyExc_TypeError, |
---|
490 | n/a | "__repr__ returned non-string (type %.200s)", |
---|
491 | n/a | res->ob_type->tp_name); |
---|
492 | n/a | Py_DECREF(res); |
---|
493 | n/a | return NULL; |
---|
494 | n/a | } |
---|
495 | n/a | #ifndef Py_DEBUG |
---|
496 | n/a | if (PyUnicode_READY(res) < 0) |
---|
497 | n/a | return NULL; |
---|
498 | n/a | #endif |
---|
499 | n/a | return res; |
---|
500 | n/a | } |
---|
501 | n/a | |
---|
502 | n/a | PyObject * |
---|
503 | n/a | PyObject_Str(PyObject *v) |
---|
504 | n/a | { |
---|
505 | n/a | PyObject *res; |
---|
506 | n/a | if (PyErr_CheckSignals()) |
---|
507 | n/a | return NULL; |
---|
508 | n/a | #ifdef USE_STACKCHECK |
---|
509 | n/a | if (PyOS_CheckStack()) { |
---|
510 | n/a | PyErr_SetString(PyExc_MemoryError, "stack overflow"); |
---|
511 | n/a | return NULL; |
---|
512 | n/a | } |
---|
513 | n/a | #endif |
---|
514 | n/a | if (v == NULL) |
---|
515 | n/a | return PyUnicode_FromString("<NULL>"); |
---|
516 | n/a | if (PyUnicode_CheckExact(v)) { |
---|
517 | n/a | #ifndef Py_DEBUG |
---|
518 | n/a | if (PyUnicode_READY(v) < 0) |
---|
519 | n/a | return NULL; |
---|
520 | n/a | #endif |
---|
521 | n/a | Py_INCREF(v); |
---|
522 | n/a | return v; |
---|
523 | n/a | } |
---|
524 | n/a | if (Py_TYPE(v)->tp_str == NULL) |
---|
525 | n/a | return PyObject_Repr(v); |
---|
526 | n/a | |
---|
527 | n/a | #ifdef Py_DEBUG |
---|
528 | n/a | /* PyObject_Str() must not be called with an exception set, |
---|
529 | n/a | because it can clear it (directly or indirectly) and so the |
---|
530 | n/a | caller loses its exception */ |
---|
531 | n/a | assert(!PyErr_Occurred()); |
---|
532 | n/a | #endif |
---|
533 | n/a | |
---|
534 | n/a | /* It is possible for a type to have a tp_str representation that loops |
---|
535 | n/a | infinitely. */ |
---|
536 | n/a | if (Py_EnterRecursiveCall(" while getting the str of an object")) |
---|
537 | n/a | return NULL; |
---|
538 | n/a | res = (*Py_TYPE(v)->tp_str)(v); |
---|
539 | n/a | Py_LeaveRecursiveCall(); |
---|
540 | n/a | if (res == NULL) |
---|
541 | n/a | return NULL; |
---|
542 | n/a | if (!PyUnicode_Check(res)) { |
---|
543 | n/a | PyErr_Format(PyExc_TypeError, |
---|
544 | n/a | "__str__ returned non-string (type %.200s)", |
---|
545 | n/a | Py_TYPE(res)->tp_name); |
---|
546 | n/a | Py_DECREF(res); |
---|
547 | n/a | return NULL; |
---|
548 | n/a | } |
---|
549 | n/a | #ifndef Py_DEBUG |
---|
550 | n/a | if (PyUnicode_READY(res) < 0) |
---|
551 | n/a | return NULL; |
---|
552 | n/a | #endif |
---|
553 | n/a | assert(_PyUnicode_CheckConsistency(res, 1)); |
---|
554 | n/a | return res; |
---|
555 | n/a | } |
---|
556 | n/a | |
---|
557 | n/a | PyObject * |
---|
558 | n/a | PyObject_ASCII(PyObject *v) |
---|
559 | n/a | { |
---|
560 | n/a | PyObject *repr, *ascii, *res; |
---|
561 | n/a | |
---|
562 | n/a | repr = PyObject_Repr(v); |
---|
563 | n/a | if (repr == NULL) |
---|
564 | n/a | return NULL; |
---|
565 | n/a | |
---|
566 | n/a | if (PyUnicode_IS_ASCII(repr)) |
---|
567 | n/a | return repr; |
---|
568 | n/a | |
---|
569 | n/a | /* repr is guaranteed to be a PyUnicode object by PyObject_Repr */ |
---|
570 | n/a | ascii = _PyUnicode_AsASCIIString(repr, "backslashreplace"); |
---|
571 | n/a | Py_DECREF(repr); |
---|
572 | n/a | if (ascii == NULL) |
---|
573 | n/a | return NULL; |
---|
574 | n/a | |
---|
575 | n/a | res = PyUnicode_DecodeASCII( |
---|
576 | n/a | PyBytes_AS_STRING(ascii), |
---|
577 | n/a | PyBytes_GET_SIZE(ascii), |
---|
578 | n/a | NULL); |
---|
579 | n/a | |
---|
580 | n/a | Py_DECREF(ascii); |
---|
581 | n/a | return res; |
---|
582 | n/a | } |
---|
583 | n/a | |
---|
584 | n/a | PyObject * |
---|
585 | n/a | PyObject_Bytes(PyObject *v) |
---|
586 | n/a | { |
---|
587 | n/a | PyObject *result, *func; |
---|
588 | n/a | |
---|
589 | n/a | if (v == NULL) |
---|
590 | n/a | return PyBytes_FromString("<NULL>"); |
---|
591 | n/a | |
---|
592 | n/a | if (PyBytes_CheckExact(v)) { |
---|
593 | n/a | Py_INCREF(v); |
---|
594 | n/a | return v; |
---|
595 | n/a | } |
---|
596 | n/a | |
---|
597 | n/a | func = _PyObject_LookupSpecial(v, &PyId___bytes__); |
---|
598 | n/a | if (func != NULL) { |
---|
599 | n/a | result = _PyObject_CallNoArg(func); |
---|
600 | n/a | Py_DECREF(func); |
---|
601 | n/a | if (result == NULL) |
---|
602 | n/a | return NULL; |
---|
603 | n/a | if (!PyBytes_Check(result)) { |
---|
604 | n/a | PyErr_Format(PyExc_TypeError, |
---|
605 | n/a | "__bytes__ returned non-bytes (type %.200s)", |
---|
606 | n/a | Py_TYPE(result)->tp_name); |
---|
607 | n/a | Py_DECREF(result); |
---|
608 | n/a | return NULL; |
---|
609 | n/a | } |
---|
610 | n/a | return result; |
---|
611 | n/a | } |
---|
612 | n/a | else if (PyErr_Occurred()) |
---|
613 | n/a | return NULL; |
---|
614 | n/a | return PyBytes_FromObject(v); |
---|
615 | n/a | } |
---|
616 | n/a | |
---|
617 | n/a | /* For Python 3.0.1 and later, the old three-way comparison has been |
---|
618 | n/a | completely removed in favour of rich comparisons. PyObject_Compare() and |
---|
619 | n/a | PyObject_Cmp() are gone, and the builtin cmp function no longer exists. |
---|
620 | n/a | The old tp_compare slot has been renamed to tp_reserved, and should no |
---|
621 | n/a | longer be used. Use tp_richcompare instead. |
---|
622 | n/a | |
---|
623 | n/a | See (*) below for practical amendments. |
---|
624 | n/a | |
---|
625 | n/a | tp_richcompare gets called with a first argument of the appropriate type |
---|
626 | n/a | and a second object of an arbitrary type. We never do any kind of |
---|
627 | n/a | coercion. |
---|
628 | n/a | |
---|
629 | n/a | The tp_richcompare slot should return an object, as follows: |
---|
630 | n/a | |
---|
631 | n/a | NULL if an exception occurred |
---|
632 | n/a | NotImplemented if the requested comparison is not implemented |
---|
633 | n/a | any other false value if the requested comparison is false |
---|
634 | n/a | any other true value if the requested comparison is true |
---|
635 | n/a | |
---|
636 | n/a | The PyObject_RichCompare[Bool]() wrappers raise TypeError when they get |
---|
637 | n/a | NotImplemented. |
---|
638 | n/a | |
---|
639 | n/a | (*) Practical amendments: |
---|
640 | n/a | |
---|
641 | n/a | - If rich comparison returns NotImplemented, == and != are decided by |
---|
642 | n/a | comparing the object pointer (i.e. falling back to the base object |
---|
643 | n/a | implementation). |
---|
644 | n/a | |
---|
645 | n/a | */ |
---|
646 | n/a | |
---|
647 | n/a | /* Map rich comparison operators to their swapped version, e.g. LT <--> GT */ |
---|
648 | n/a | int _Py_SwappedOp[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE}; |
---|
649 | n/a | |
---|
650 | n/a | static const char * const opstrings[] = {"<", "<=", "==", "!=", ">", ">="}; |
---|
651 | n/a | |
---|
652 | n/a | /* Perform a rich comparison, raising TypeError when the requested comparison |
---|
653 | n/a | operator is not supported. */ |
---|
654 | n/a | static PyObject * |
---|
655 | n/a | do_richcompare(PyObject *v, PyObject *w, int op) |
---|
656 | n/a | { |
---|
657 | n/a | richcmpfunc f; |
---|
658 | n/a | PyObject *res; |
---|
659 | n/a | int checked_reverse_op = 0; |
---|
660 | n/a | |
---|
661 | n/a | if (v->ob_type != w->ob_type && |
---|
662 | n/a | PyType_IsSubtype(w->ob_type, v->ob_type) && |
---|
663 | n/a | (f = w->ob_type->tp_richcompare) != NULL) { |
---|
664 | n/a | checked_reverse_op = 1; |
---|
665 | n/a | res = (*f)(w, v, _Py_SwappedOp[op]); |
---|
666 | n/a | if (res != Py_NotImplemented) |
---|
667 | n/a | return res; |
---|
668 | n/a | Py_DECREF(res); |
---|
669 | n/a | } |
---|
670 | n/a | if ((f = v->ob_type->tp_richcompare) != NULL) { |
---|
671 | n/a | res = (*f)(v, w, op); |
---|
672 | n/a | if (res != Py_NotImplemented) |
---|
673 | n/a | return res; |
---|
674 | n/a | Py_DECREF(res); |
---|
675 | n/a | } |
---|
676 | n/a | if (!checked_reverse_op && (f = w->ob_type->tp_richcompare) != NULL) { |
---|
677 | n/a | res = (*f)(w, v, _Py_SwappedOp[op]); |
---|
678 | n/a | if (res != Py_NotImplemented) |
---|
679 | n/a | return res; |
---|
680 | n/a | Py_DECREF(res); |
---|
681 | n/a | } |
---|
682 | n/a | /* If neither object implements it, provide a sensible default |
---|
683 | n/a | for == and !=, but raise an exception for ordering. */ |
---|
684 | n/a | switch (op) { |
---|
685 | n/a | case Py_EQ: |
---|
686 | n/a | res = (v == w) ? Py_True : Py_False; |
---|
687 | n/a | break; |
---|
688 | n/a | case Py_NE: |
---|
689 | n/a | res = (v != w) ? Py_True : Py_False; |
---|
690 | n/a | break; |
---|
691 | n/a | default: |
---|
692 | n/a | PyErr_Format(PyExc_TypeError, |
---|
693 | n/a | "'%s' not supported between instances of '%.100s' and '%.100s'", |
---|
694 | n/a | opstrings[op], |
---|
695 | n/a | v->ob_type->tp_name, |
---|
696 | n/a | w->ob_type->tp_name); |
---|
697 | n/a | return NULL; |
---|
698 | n/a | } |
---|
699 | n/a | Py_INCREF(res); |
---|
700 | n/a | return res; |
---|
701 | n/a | } |
---|
702 | n/a | |
---|
703 | n/a | /* Perform a rich comparison with object result. This wraps do_richcompare() |
---|
704 | n/a | with a check for NULL arguments and a recursion check. */ |
---|
705 | n/a | |
---|
706 | n/a | PyObject * |
---|
707 | n/a | PyObject_RichCompare(PyObject *v, PyObject *w, int op) |
---|
708 | n/a | { |
---|
709 | n/a | PyObject *res; |
---|
710 | n/a | |
---|
711 | n/a | assert(Py_LT <= op && op <= Py_GE); |
---|
712 | n/a | if (v == NULL || w == NULL) { |
---|
713 | n/a | if (!PyErr_Occurred()) |
---|
714 | n/a | PyErr_BadInternalCall(); |
---|
715 | n/a | return NULL; |
---|
716 | n/a | } |
---|
717 | n/a | if (Py_EnterRecursiveCall(" in comparison")) |
---|
718 | n/a | return NULL; |
---|
719 | n/a | res = do_richcompare(v, w, op); |
---|
720 | n/a | Py_LeaveRecursiveCall(); |
---|
721 | n/a | return res; |
---|
722 | n/a | } |
---|
723 | n/a | |
---|
724 | n/a | /* Perform a rich comparison with integer result. This wraps |
---|
725 | n/a | PyObject_RichCompare(), returning -1 for error, 0 for false, 1 for true. */ |
---|
726 | n/a | int |
---|
727 | n/a | PyObject_RichCompareBool(PyObject *v, PyObject *w, int op) |
---|
728 | n/a | { |
---|
729 | n/a | PyObject *res; |
---|
730 | n/a | int ok; |
---|
731 | n/a | |
---|
732 | n/a | /* Quick result when objects are the same. |
---|
733 | n/a | Guarantees that identity implies equality. */ |
---|
734 | n/a | if (v == w) { |
---|
735 | n/a | if (op == Py_EQ) |
---|
736 | n/a | return 1; |
---|
737 | n/a | else if (op == Py_NE) |
---|
738 | n/a | return 0; |
---|
739 | n/a | } |
---|
740 | n/a | |
---|
741 | n/a | res = PyObject_RichCompare(v, w, op); |
---|
742 | n/a | if (res == NULL) |
---|
743 | n/a | return -1; |
---|
744 | n/a | if (PyBool_Check(res)) |
---|
745 | n/a | ok = (res == Py_True); |
---|
746 | n/a | else |
---|
747 | n/a | ok = PyObject_IsTrue(res); |
---|
748 | n/a | Py_DECREF(res); |
---|
749 | n/a | return ok; |
---|
750 | n/a | } |
---|
751 | n/a | |
---|
752 | n/a | Py_hash_t |
---|
753 | n/a | PyObject_HashNotImplemented(PyObject *v) |
---|
754 | n/a | { |
---|
755 | n/a | PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'", |
---|
756 | n/a | Py_TYPE(v)->tp_name); |
---|
757 | n/a | return -1; |
---|
758 | n/a | } |
---|
759 | n/a | |
---|
760 | n/a | Py_hash_t |
---|
761 | n/a | PyObject_Hash(PyObject *v) |
---|
762 | n/a | { |
---|
763 | n/a | PyTypeObject *tp = Py_TYPE(v); |
---|
764 | n/a | if (tp->tp_hash != NULL) |
---|
765 | n/a | return (*tp->tp_hash)(v); |
---|
766 | n/a | /* To keep to the general practice that inheriting |
---|
767 | n/a | * solely from object in C code should work without |
---|
768 | n/a | * an explicit call to PyType_Ready, we implicitly call |
---|
769 | n/a | * PyType_Ready here and then check the tp_hash slot again |
---|
770 | n/a | */ |
---|
771 | n/a | if (tp->tp_dict == NULL) { |
---|
772 | n/a | if (PyType_Ready(tp) < 0) |
---|
773 | n/a | return -1; |
---|
774 | n/a | if (tp->tp_hash != NULL) |
---|
775 | n/a | return (*tp->tp_hash)(v); |
---|
776 | n/a | } |
---|
777 | n/a | /* Otherwise, the object can't be hashed */ |
---|
778 | n/a | return PyObject_HashNotImplemented(v); |
---|
779 | n/a | } |
---|
780 | n/a | |
---|
781 | n/a | PyObject * |
---|
782 | n/a | PyObject_GetAttrString(PyObject *v, const char *name) |
---|
783 | n/a | { |
---|
784 | n/a | PyObject *w, *res; |
---|
785 | n/a | |
---|
786 | n/a | if (Py_TYPE(v)->tp_getattr != NULL) |
---|
787 | n/a | return (*Py_TYPE(v)->tp_getattr)(v, (char*)name); |
---|
788 | n/a | w = PyUnicode_InternFromString(name); |
---|
789 | n/a | if (w == NULL) |
---|
790 | n/a | return NULL; |
---|
791 | n/a | res = PyObject_GetAttr(v, w); |
---|
792 | n/a | Py_DECREF(w); |
---|
793 | n/a | return res; |
---|
794 | n/a | } |
---|
795 | n/a | |
---|
796 | n/a | int |
---|
797 | n/a | PyObject_HasAttrString(PyObject *v, const char *name) |
---|
798 | n/a | { |
---|
799 | n/a | PyObject *res = PyObject_GetAttrString(v, name); |
---|
800 | n/a | if (res != NULL) { |
---|
801 | n/a | Py_DECREF(res); |
---|
802 | n/a | return 1; |
---|
803 | n/a | } |
---|
804 | n/a | PyErr_Clear(); |
---|
805 | n/a | return 0; |
---|
806 | n/a | } |
---|
807 | n/a | |
---|
808 | n/a | int |
---|
809 | n/a | PyObject_SetAttrString(PyObject *v, const char *name, PyObject *w) |
---|
810 | n/a | { |
---|
811 | n/a | PyObject *s; |
---|
812 | n/a | int res; |
---|
813 | n/a | |
---|
814 | n/a | if (Py_TYPE(v)->tp_setattr != NULL) |
---|
815 | n/a | return (*Py_TYPE(v)->tp_setattr)(v, (char*)name, w); |
---|
816 | n/a | s = PyUnicode_InternFromString(name); |
---|
817 | n/a | if (s == NULL) |
---|
818 | n/a | return -1; |
---|
819 | n/a | res = PyObject_SetAttr(v, s, w); |
---|
820 | n/a | Py_XDECREF(s); |
---|
821 | n/a | return res; |
---|
822 | n/a | } |
---|
823 | n/a | |
---|
824 | n/a | int |
---|
825 | n/a | _PyObject_IsAbstract(PyObject *obj) |
---|
826 | n/a | { |
---|
827 | n/a | int res; |
---|
828 | n/a | PyObject* isabstract; |
---|
829 | n/a | |
---|
830 | n/a | if (obj == NULL) |
---|
831 | n/a | return 0; |
---|
832 | n/a | |
---|
833 | n/a | isabstract = _PyObject_GetAttrId(obj, &PyId___isabstractmethod__); |
---|
834 | n/a | if (isabstract == NULL) { |
---|
835 | n/a | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
---|
836 | n/a | PyErr_Clear(); |
---|
837 | n/a | return 0; |
---|
838 | n/a | } |
---|
839 | n/a | return -1; |
---|
840 | n/a | } |
---|
841 | n/a | res = PyObject_IsTrue(isabstract); |
---|
842 | n/a | Py_DECREF(isabstract); |
---|
843 | n/a | return res; |
---|
844 | n/a | } |
---|
845 | n/a | |
---|
846 | n/a | PyObject * |
---|
847 | n/a | _PyObject_GetAttrId(PyObject *v, _Py_Identifier *name) |
---|
848 | n/a | { |
---|
849 | n/a | PyObject *result; |
---|
850 | n/a | PyObject *oname = _PyUnicode_FromId(name); /* borrowed */ |
---|
851 | n/a | if (!oname) |
---|
852 | n/a | return NULL; |
---|
853 | n/a | result = PyObject_GetAttr(v, oname); |
---|
854 | n/a | return result; |
---|
855 | n/a | } |
---|
856 | n/a | |
---|
857 | n/a | int |
---|
858 | n/a | _PyObject_HasAttrId(PyObject *v, _Py_Identifier *name) |
---|
859 | n/a | { |
---|
860 | n/a | int result; |
---|
861 | n/a | PyObject *oname = _PyUnicode_FromId(name); /* borrowed */ |
---|
862 | n/a | if (!oname) |
---|
863 | n/a | return -1; |
---|
864 | n/a | result = PyObject_HasAttr(v, oname); |
---|
865 | n/a | return result; |
---|
866 | n/a | } |
---|
867 | n/a | |
---|
868 | n/a | int |
---|
869 | n/a | _PyObject_SetAttrId(PyObject *v, _Py_Identifier *name, PyObject *w) |
---|
870 | n/a | { |
---|
871 | n/a | int result; |
---|
872 | n/a | PyObject *oname = _PyUnicode_FromId(name); /* borrowed */ |
---|
873 | n/a | if (!oname) |
---|
874 | n/a | return -1; |
---|
875 | n/a | result = PyObject_SetAttr(v, oname, w); |
---|
876 | n/a | return result; |
---|
877 | n/a | } |
---|
878 | n/a | |
---|
879 | n/a | PyObject * |
---|
880 | n/a | PyObject_GetAttr(PyObject *v, PyObject *name) |
---|
881 | n/a | { |
---|
882 | n/a | PyTypeObject *tp = Py_TYPE(v); |
---|
883 | n/a | |
---|
884 | n/a | if (!PyUnicode_Check(name)) { |
---|
885 | n/a | PyErr_Format(PyExc_TypeError, |
---|
886 | n/a | "attribute name must be string, not '%.200s'", |
---|
887 | n/a | name->ob_type->tp_name); |
---|
888 | n/a | return NULL; |
---|
889 | n/a | } |
---|
890 | n/a | if (tp->tp_getattro != NULL) |
---|
891 | n/a | return (*tp->tp_getattro)(v, name); |
---|
892 | n/a | if (tp->tp_getattr != NULL) { |
---|
893 | n/a | const char *name_str = PyUnicode_AsUTF8(name); |
---|
894 | n/a | if (name_str == NULL) |
---|
895 | n/a | return NULL; |
---|
896 | n/a | return (*tp->tp_getattr)(v, (char *)name_str); |
---|
897 | n/a | } |
---|
898 | n/a | PyErr_Format(PyExc_AttributeError, |
---|
899 | n/a | "'%.50s' object has no attribute '%U'", |
---|
900 | n/a | tp->tp_name, name); |
---|
901 | n/a | return NULL; |
---|
902 | n/a | } |
---|
903 | n/a | |
---|
904 | n/a | int |
---|
905 | n/a | PyObject_HasAttr(PyObject *v, PyObject *name) |
---|
906 | n/a | { |
---|
907 | n/a | PyObject *res = PyObject_GetAttr(v, name); |
---|
908 | n/a | if (res != NULL) { |
---|
909 | n/a | Py_DECREF(res); |
---|
910 | n/a | return 1; |
---|
911 | n/a | } |
---|
912 | n/a | PyErr_Clear(); |
---|
913 | n/a | return 0; |
---|
914 | n/a | } |
---|
915 | n/a | |
---|
916 | n/a | int |
---|
917 | n/a | PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value) |
---|
918 | n/a | { |
---|
919 | n/a | PyTypeObject *tp = Py_TYPE(v); |
---|
920 | n/a | int err; |
---|
921 | n/a | |
---|
922 | n/a | if (!PyUnicode_Check(name)) { |
---|
923 | n/a | PyErr_Format(PyExc_TypeError, |
---|
924 | n/a | "attribute name must be string, not '%.200s'", |
---|
925 | n/a | name->ob_type->tp_name); |
---|
926 | n/a | return -1; |
---|
927 | n/a | } |
---|
928 | n/a | Py_INCREF(name); |
---|
929 | n/a | |
---|
930 | n/a | PyUnicode_InternInPlace(&name); |
---|
931 | n/a | if (tp->tp_setattro != NULL) { |
---|
932 | n/a | err = (*tp->tp_setattro)(v, name, value); |
---|
933 | n/a | Py_DECREF(name); |
---|
934 | n/a | return err; |
---|
935 | n/a | } |
---|
936 | n/a | if (tp->tp_setattr != NULL) { |
---|
937 | n/a | const char *name_str = PyUnicode_AsUTF8(name); |
---|
938 | n/a | if (name_str == NULL) |
---|
939 | n/a | return -1; |
---|
940 | n/a | err = (*tp->tp_setattr)(v, (char *)name_str, value); |
---|
941 | n/a | Py_DECREF(name); |
---|
942 | n/a | return err; |
---|
943 | n/a | } |
---|
944 | n/a | Py_DECREF(name); |
---|
945 | n/a | assert(name->ob_refcnt >= 1); |
---|
946 | n/a | if (tp->tp_getattr == NULL && tp->tp_getattro == NULL) |
---|
947 | n/a | PyErr_Format(PyExc_TypeError, |
---|
948 | n/a | "'%.100s' object has no attributes " |
---|
949 | n/a | "(%s .%U)", |
---|
950 | n/a | tp->tp_name, |
---|
951 | n/a | value==NULL ? "del" : "assign to", |
---|
952 | n/a | name); |
---|
953 | n/a | else |
---|
954 | n/a | PyErr_Format(PyExc_TypeError, |
---|
955 | n/a | "'%.100s' object has only read-only attributes " |
---|
956 | n/a | "(%s .%U)", |
---|
957 | n/a | tp->tp_name, |
---|
958 | n/a | value==NULL ? "del" : "assign to", |
---|
959 | n/a | name); |
---|
960 | n/a | return -1; |
---|
961 | n/a | } |
---|
962 | n/a | |
---|
963 | n/a | /* Helper to get a pointer to an object's __dict__ slot, if any */ |
---|
964 | n/a | |
---|
965 | n/a | PyObject ** |
---|
966 | n/a | _PyObject_GetDictPtr(PyObject *obj) |
---|
967 | n/a | { |
---|
968 | n/a | Py_ssize_t dictoffset; |
---|
969 | n/a | PyTypeObject *tp = Py_TYPE(obj); |
---|
970 | n/a | |
---|
971 | n/a | dictoffset = tp->tp_dictoffset; |
---|
972 | n/a | if (dictoffset == 0) |
---|
973 | n/a | return NULL; |
---|
974 | n/a | if (dictoffset < 0) { |
---|
975 | n/a | Py_ssize_t tsize; |
---|
976 | n/a | size_t size; |
---|
977 | n/a | |
---|
978 | n/a | tsize = ((PyVarObject *)obj)->ob_size; |
---|
979 | n/a | if (tsize < 0) |
---|
980 | n/a | tsize = -tsize; |
---|
981 | n/a | size = _PyObject_VAR_SIZE(tp, tsize); |
---|
982 | n/a | |
---|
983 | n/a | dictoffset += (long)size; |
---|
984 | n/a | assert(dictoffset > 0); |
---|
985 | n/a | assert(dictoffset % SIZEOF_VOID_P == 0); |
---|
986 | n/a | } |
---|
987 | n/a | return (PyObject **) ((char *)obj + dictoffset); |
---|
988 | n/a | } |
---|
989 | n/a | |
---|
990 | n/a | PyObject * |
---|
991 | n/a | PyObject_SelfIter(PyObject *obj) |
---|
992 | n/a | { |
---|
993 | n/a | Py_INCREF(obj); |
---|
994 | n/a | return obj; |
---|
995 | n/a | } |
---|
996 | n/a | |
---|
997 | n/a | /* Convenience function to get a builtin from its name */ |
---|
998 | n/a | PyObject * |
---|
999 | n/a | _PyObject_GetBuiltin(const char *name) |
---|
1000 | n/a | { |
---|
1001 | n/a | PyObject *mod_name, *mod, *attr; |
---|
1002 | n/a | |
---|
1003 | n/a | mod_name = _PyUnicode_FromId(&PyId_builtins); /* borrowed */ |
---|
1004 | n/a | if (mod_name == NULL) |
---|
1005 | n/a | return NULL; |
---|
1006 | n/a | mod = PyImport_Import(mod_name); |
---|
1007 | n/a | if (mod == NULL) |
---|
1008 | n/a | return NULL; |
---|
1009 | n/a | attr = PyObject_GetAttrString(mod, name); |
---|
1010 | n/a | Py_DECREF(mod); |
---|
1011 | n/a | return attr; |
---|
1012 | n/a | } |
---|
1013 | n/a | |
---|
1014 | n/a | /* Helper used when the __next__ method is removed from a type: |
---|
1015 | n/a | tp_iternext is never NULL and can be safely called without checking |
---|
1016 | n/a | on every iteration. |
---|
1017 | n/a | */ |
---|
1018 | n/a | |
---|
1019 | n/a | PyObject * |
---|
1020 | n/a | _PyObject_NextNotImplemented(PyObject *self) |
---|
1021 | n/a | { |
---|
1022 | n/a | PyErr_Format(PyExc_TypeError, |
---|
1023 | n/a | "'%.200s' object is not iterable", |
---|
1024 | n/a | Py_TYPE(self)->tp_name); |
---|
1025 | n/a | return NULL; |
---|
1026 | n/a | } |
---|
1027 | n/a | |
---|
1028 | n/a | |
---|
1029 | n/a | /* Specialized version of _PyObject_GenericGetAttrWithDict |
---|
1030 | n/a | specifically for the LOAD_METHOD opcode. |
---|
1031 | n/a | |
---|
1032 | n/a | Return 1 if a method is found, 0 if it's a regular attribute |
---|
1033 | n/a | from __dict__ or something returned by using a descriptor |
---|
1034 | n/a | protocol. |
---|
1035 | n/a | |
---|
1036 | n/a | `method` will point to the resolved attribute or NULL. In the |
---|
1037 | n/a | latter case, an error will be set. |
---|
1038 | n/a | */ |
---|
1039 | n/a | int |
---|
1040 | n/a | _PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method) |
---|
1041 | n/a | { |
---|
1042 | n/a | PyTypeObject *tp = Py_TYPE(obj); |
---|
1043 | n/a | PyObject *descr; |
---|
1044 | n/a | descrgetfunc f = NULL; |
---|
1045 | n/a | PyObject **dictptr, *dict; |
---|
1046 | n/a | PyObject *attr; |
---|
1047 | n/a | int meth_found = 0; |
---|
1048 | n/a | |
---|
1049 | n/a | assert(*method == NULL); |
---|
1050 | n/a | |
---|
1051 | n/a | if (Py_TYPE(obj)->tp_getattro != PyObject_GenericGetAttr |
---|
1052 | n/a | || !PyUnicode_Check(name)) { |
---|
1053 | n/a | *method = PyObject_GetAttr(obj, name); |
---|
1054 | n/a | return 0; |
---|
1055 | n/a | } |
---|
1056 | n/a | |
---|
1057 | n/a | if (tp->tp_dict == NULL && PyType_Ready(tp) < 0) |
---|
1058 | n/a | return 0; |
---|
1059 | n/a | |
---|
1060 | n/a | descr = _PyType_Lookup(tp, name); |
---|
1061 | n/a | if (descr != NULL) { |
---|
1062 | n/a | Py_INCREF(descr); |
---|
1063 | n/a | if (PyFunction_Check(descr) || |
---|
1064 | n/a | (Py_TYPE(descr) == &PyMethodDescr_Type)) { |
---|
1065 | n/a | meth_found = 1; |
---|
1066 | n/a | } else { |
---|
1067 | n/a | f = descr->ob_type->tp_descr_get; |
---|
1068 | n/a | if (f != NULL && PyDescr_IsData(descr)) { |
---|
1069 | n/a | *method = f(descr, obj, (PyObject *)obj->ob_type); |
---|
1070 | n/a | Py_DECREF(descr); |
---|
1071 | n/a | return 0; |
---|
1072 | n/a | } |
---|
1073 | n/a | } |
---|
1074 | n/a | } |
---|
1075 | n/a | |
---|
1076 | n/a | dictptr = _PyObject_GetDictPtr(obj); |
---|
1077 | n/a | if (dictptr != NULL && (dict = *dictptr) != NULL) { |
---|
1078 | n/a | Py_INCREF(dict); |
---|
1079 | n/a | attr = PyDict_GetItem(dict, name); |
---|
1080 | n/a | if (attr != NULL) { |
---|
1081 | n/a | Py_INCREF(attr); |
---|
1082 | n/a | *method = attr; |
---|
1083 | n/a | Py_DECREF(dict); |
---|
1084 | n/a | Py_XDECREF(descr); |
---|
1085 | n/a | return 0; |
---|
1086 | n/a | } |
---|
1087 | n/a | Py_DECREF(dict); |
---|
1088 | n/a | } |
---|
1089 | n/a | |
---|
1090 | n/a | if (meth_found) { |
---|
1091 | n/a | *method = descr; |
---|
1092 | n/a | return 1; |
---|
1093 | n/a | } |
---|
1094 | n/a | |
---|
1095 | n/a | if (f != NULL) { |
---|
1096 | n/a | *method = f(descr, obj, (PyObject *)Py_TYPE(obj)); |
---|
1097 | n/a | Py_DECREF(descr); |
---|
1098 | n/a | return 0; |
---|
1099 | n/a | } |
---|
1100 | n/a | |
---|
1101 | n/a | if (descr != NULL) { |
---|
1102 | n/a | *method = descr; |
---|
1103 | n/a | return 0; |
---|
1104 | n/a | } |
---|
1105 | n/a | |
---|
1106 | n/a | PyErr_Format(PyExc_AttributeError, |
---|
1107 | n/a | "'%.50s' object has no attribute '%U'", |
---|
1108 | n/a | tp->tp_name, name); |
---|
1109 | n/a | return 0; |
---|
1110 | n/a | } |
---|
1111 | n/a | |
---|
1112 | n/a | /* Generic GetAttr functions - put these in your tp_[gs]etattro slot. */ |
---|
1113 | n/a | |
---|
1114 | n/a | PyObject * |
---|
1115 | n/a | _PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name, PyObject *dict) |
---|
1116 | n/a | { |
---|
1117 | n/a | /* Make sure the logic of _PyObject_GetMethod is in sync with |
---|
1118 | n/a | this method. |
---|
1119 | n/a | */ |
---|
1120 | n/a | |
---|
1121 | n/a | PyTypeObject *tp = Py_TYPE(obj); |
---|
1122 | n/a | PyObject *descr = NULL; |
---|
1123 | n/a | PyObject *res = NULL; |
---|
1124 | n/a | descrgetfunc f; |
---|
1125 | n/a | Py_ssize_t dictoffset; |
---|
1126 | n/a | PyObject **dictptr; |
---|
1127 | n/a | |
---|
1128 | n/a | if (!PyUnicode_Check(name)){ |
---|
1129 | n/a | PyErr_Format(PyExc_TypeError, |
---|
1130 | n/a | "attribute name must be string, not '%.200s'", |
---|
1131 | n/a | name->ob_type->tp_name); |
---|
1132 | n/a | return NULL; |
---|
1133 | n/a | } |
---|
1134 | n/a | Py_INCREF(name); |
---|
1135 | n/a | |
---|
1136 | n/a | if (tp->tp_dict == NULL) { |
---|
1137 | n/a | if (PyType_Ready(tp) < 0) |
---|
1138 | n/a | goto done; |
---|
1139 | n/a | } |
---|
1140 | n/a | |
---|
1141 | n/a | descr = _PyType_Lookup(tp, name); |
---|
1142 | n/a | |
---|
1143 | n/a | f = NULL; |
---|
1144 | n/a | if (descr != NULL) { |
---|
1145 | n/a | Py_INCREF(descr); |
---|
1146 | n/a | f = descr->ob_type->tp_descr_get; |
---|
1147 | n/a | if (f != NULL && PyDescr_IsData(descr)) { |
---|
1148 | n/a | res = f(descr, obj, (PyObject *)obj->ob_type); |
---|
1149 | n/a | goto done; |
---|
1150 | n/a | } |
---|
1151 | n/a | } |
---|
1152 | n/a | |
---|
1153 | n/a | if (dict == NULL) { |
---|
1154 | n/a | /* Inline _PyObject_GetDictPtr */ |
---|
1155 | n/a | dictoffset = tp->tp_dictoffset; |
---|
1156 | n/a | if (dictoffset != 0) { |
---|
1157 | n/a | if (dictoffset < 0) { |
---|
1158 | n/a | Py_ssize_t tsize; |
---|
1159 | n/a | size_t size; |
---|
1160 | n/a | |
---|
1161 | n/a | tsize = ((PyVarObject *)obj)->ob_size; |
---|
1162 | n/a | if (tsize < 0) |
---|
1163 | n/a | tsize = -tsize; |
---|
1164 | n/a | size = _PyObject_VAR_SIZE(tp, tsize); |
---|
1165 | n/a | assert(size <= PY_SSIZE_T_MAX); |
---|
1166 | n/a | |
---|
1167 | n/a | dictoffset += (Py_ssize_t)size; |
---|
1168 | n/a | assert(dictoffset > 0); |
---|
1169 | n/a | assert(dictoffset % SIZEOF_VOID_P == 0); |
---|
1170 | n/a | } |
---|
1171 | n/a | dictptr = (PyObject **) ((char *)obj + dictoffset); |
---|
1172 | n/a | dict = *dictptr; |
---|
1173 | n/a | } |
---|
1174 | n/a | } |
---|
1175 | n/a | if (dict != NULL) { |
---|
1176 | n/a | Py_INCREF(dict); |
---|
1177 | n/a | res = PyDict_GetItem(dict, name); |
---|
1178 | n/a | if (res != NULL) { |
---|
1179 | n/a | Py_INCREF(res); |
---|
1180 | n/a | Py_DECREF(dict); |
---|
1181 | n/a | goto done; |
---|
1182 | n/a | } |
---|
1183 | n/a | Py_DECREF(dict); |
---|
1184 | n/a | } |
---|
1185 | n/a | |
---|
1186 | n/a | if (f != NULL) { |
---|
1187 | n/a | res = f(descr, obj, (PyObject *)Py_TYPE(obj)); |
---|
1188 | n/a | goto done; |
---|
1189 | n/a | } |
---|
1190 | n/a | |
---|
1191 | n/a | if (descr != NULL) { |
---|
1192 | n/a | res = descr; |
---|
1193 | n/a | descr = NULL; |
---|
1194 | n/a | goto done; |
---|
1195 | n/a | } |
---|
1196 | n/a | |
---|
1197 | n/a | PyErr_Format(PyExc_AttributeError, |
---|
1198 | n/a | "'%.50s' object has no attribute '%U'", |
---|
1199 | n/a | tp->tp_name, name); |
---|
1200 | n/a | done: |
---|
1201 | n/a | Py_XDECREF(descr); |
---|
1202 | n/a | Py_DECREF(name); |
---|
1203 | n/a | return res; |
---|
1204 | n/a | } |
---|
1205 | n/a | |
---|
1206 | n/a | PyObject * |
---|
1207 | n/a | PyObject_GenericGetAttr(PyObject *obj, PyObject *name) |
---|
1208 | n/a | { |
---|
1209 | n/a | return _PyObject_GenericGetAttrWithDict(obj, name, NULL); |
---|
1210 | n/a | } |
---|
1211 | n/a | |
---|
1212 | n/a | int |
---|
1213 | n/a | _PyObject_GenericSetAttrWithDict(PyObject *obj, PyObject *name, |
---|
1214 | n/a | PyObject *value, PyObject *dict) |
---|
1215 | n/a | { |
---|
1216 | n/a | PyTypeObject *tp = Py_TYPE(obj); |
---|
1217 | n/a | PyObject *descr; |
---|
1218 | n/a | descrsetfunc f; |
---|
1219 | n/a | PyObject **dictptr; |
---|
1220 | n/a | int res = -1; |
---|
1221 | n/a | |
---|
1222 | n/a | if (!PyUnicode_Check(name)){ |
---|
1223 | n/a | PyErr_Format(PyExc_TypeError, |
---|
1224 | n/a | "attribute name must be string, not '%.200s'", |
---|
1225 | n/a | name->ob_type->tp_name); |
---|
1226 | n/a | return -1; |
---|
1227 | n/a | } |
---|
1228 | n/a | |
---|
1229 | n/a | if (tp->tp_dict == NULL && PyType_Ready(tp) < 0) |
---|
1230 | n/a | return -1; |
---|
1231 | n/a | |
---|
1232 | n/a | Py_INCREF(name); |
---|
1233 | n/a | |
---|
1234 | n/a | descr = _PyType_Lookup(tp, name); |
---|
1235 | n/a | |
---|
1236 | n/a | if (descr != NULL) { |
---|
1237 | n/a | Py_INCREF(descr); |
---|
1238 | n/a | f = descr->ob_type->tp_descr_set; |
---|
1239 | n/a | if (f != NULL) { |
---|
1240 | n/a | res = f(descr, obj, value); |
---|
1241 | n/a | goto done; |
---|
1242 | n/a | } |
---|
1243 | n/a | } |
---|
1244 | n/a | |
---|
1245 | n/a | if (dict == NULL) { |
---|
1246 | n/a | dictptr = _PyObject_GetDictPtr(obj); |
---|
1247 | n/a | if (dictptr == NULL) { |
---|
1248 | n/a | if (descr == NULL) { |
---|
1249 | n/a | PyErr_Format(PyExc_AttributeError, |
---|
1250 | n/a | "'%.100s' object has no attribute '%U'", |
---|
1251 | n/a | tp->tp_name, name); |
---|
1252 | n/a | } |
---|
1253 | n/a | else { |
---|
1254 | n/a | PyErr_Format(PyExc_AttributeError, |
---|
1255 | n/a | "'%.50s' object attribute '%U' is read-only", |
---|
1256 | n/a | tp->tp_name, name); |
---|
1257 | n/a | } |
---|
1258 | n/a | goto done; |
---|
1259 | n/a | } |
---|
1260 | n/a | res = _PyObjectDict_SetItem(tp, dictptr, name, value); |
---|
1261 | n/a | } |
---|
1262 | n/a | else { |
---|
1263 | n/a | Py_INCREF(dict); |
---|
1264 | n/a | if (value == NULL) |
---|
1265 | n/a | res = PyDict_DelItem(dict, name); |
---|
1266 | n/a | else |
---|
1267 | n/a | res = PyDict_SetItem(dict, name, value); |
---|
1268 | n/a | Py_DECREF(dict); |
---|
1269 | n/a | } |
---|
1270 | n/a | if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError)) |
---|
1271 | n/a | PyErr_SetObject(PyExc_AttributeError, name); |
---|
1272 | n/a | |
---|
1273 | n/a | done: |
---|
1274 | n/a | Py_XDECREF(descr); |
---|
1275 | n/a | Py_DECREF(name); |
---|
1276 | n/a | return res; |
---|
1277 | n/a | } |
---|
1278 | n/a | |
---|
1279 | n/a | int |
---|
1280 | n/a | PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value) |
---|
1281 | n/a | { |
---|
1282 | n/a | return _PyObject_GenericSetAttrWithDict(obj, name, value, NULL); |
---|
1283 | n/a | } |
---|
1284 | n/a | |
---|
1285 | n/a | int |
---|
1286 | n/a | PyObject_GenericSetDict(PyObject *obj, PyObject *value, void *context) |
---|
1287 | n/a | { |
---|
1288 | n/a | PyObject **dictptr = _PyObject_GetDictPtr(obj); |
---|
1289 | n/a | if (dictptr == NULL) { |
---|
1290 | n/a | PyErr_SetString(PyExc_AttributeError, |
---|
1291 | n/a | "This object has no __dict__"); |
---|
1292 | n/a | return -1; |
---|
1293 | n/a | } |
---|
1294 | n/a | if (value == NULL) { |
---|
1295 | n/a | PyErr_SetString(PyExc_TypeError, "cannot delete __dict__"); |
---|
1296 | n/a | return -1; |
---|
1297 | n/a | } |
---|
1298 | n/a | if (!PyDict_Check(value)) { |
---|
1299 | n/a | PyErr_Format(PyExc_TypeError, |
---|
1300 | n/a | "__dict__ must be set to a dictionary, " |
---|
1301 | n/a | "not a '%.200s'", Py_TYPE(value)->tp_name); |
---|
1302 | n/a | return -1; |
---|
1303 | n/a | } |
---|
1304 | n/a | Py_INCREF(value); |
---|
1305 | n/a | Py_XSETREF(*dictptr, value); |
---|
1306 | n/a | return 0; |
---|
1307 | n/a | } |
---|
1308 | n/a | |
---|
1309 | n/a | |
---|
1310 | n/a | /* Test a value used as condition, e.g., in a for or if statement. |
---|
1311 | n/a | Return -1 if an error occurred */ |
---|
1312 | n/a | |
---|
1313 | n/a | int |
---|
1314 | n/a | PyObject_IsTrue(PyObject *v) |
---|
1315 | n/a | { |
---|
1316 | n/a | Py_ssize_t res; |
---|
1317 | n/a | if (v == Py_True) |
---|
1318 | n/a | return 1; |
---|
1319 | n/a | if (v == Py_False) |
---|
1320 | n/a | return 0; |
---|
1321 | n/a | if (v == Py_None) |
---|
1322 | n/a | return 0; |
---|
1323 | n/a | else if (v->ob_type->tp_as_number != NULL && |
---|
1324 | n/a | v->ob_type->tp_as_number->nb_bool != NULL) |
---|
1325 | n/a | res = (*v->ob_type->tp_as_number->nb_bool)(v); |
---|
1326 | n/a | else if (v->ob_type->tp_as_mapping != NULL && |
---|
1327 | n/a | v->ob_type->tp_as_mapping->mp_length != NULL) |
---|
1328 | n/a | res = (*v->ob_type->tp_as_mapping->mp_length)(v); |
---|
1329 | n/a | else if (v->ob_type->tp_as_sequence != NULL && |
---|
1330 | n/a | v->ob_type->tp_as_sequence->sq_length != NULL) |
---|
1331 | n/a | res = (*v->ob_type->tp_as_sequence->sq_length)(v); |
---|
1332 | n/a | else |
---|
1333 | n/a | return 1; |
---|
1334 | n/a | /* if it is negative, it should be either -1 or -2 */ |
---|
1335 | n/a | return (res > 0) ? 1 : Py_SAFE_DOWNCAST(res, Py_ssize_t, int); |
---|
1336 | n/a | } |
---|
1337 | n/a | |
---|
1338 | n/a | /* equivalent of 'not v' |
---|
1339 | n/a | Return -1 if an error occurred */ |
---|
1340 | n/a | |
---|
1341 | n/a | int |
---|
1342 | n/a | PyObject_Not(PyObject *v) |
---|
1343 | n/a | { |
---|
1344 | n/a | int res; |
---|
1345 | n/a | res = PyObject_IsTrue(v); |
---|
1346 | n/a | if (res < 0) |
---|
1347 | n/a | return res; |
---|
1348 | n/a | return res == 0; |
---|
1349 | n/a | } |
---|
1350 | n/a | |
---|
1351 | n/a | /* Test whether an object can be called */ |
---|
1352 | n/a | |
---|
1353 | n/a | int |
---|
1354 | n/a | PyCallable_Check(PyObject *x) |
---|
1355 | n/a | { |
---|
1356 | n/a | if (x == NULL) |
---|
1357 | n/a | return 0; |
---|
1358 | n/a | return x->ob_type->tp_call != NULL; |
---|
1359 | n/a | } |
---|
1360 | n/a | |
---|
1361 | n/a | |
---|
1362 | n/a | /* Helper for PyObject_Dir without arguments: returns the local scope. */ |
---|
1363 | n/a | static PyObject * |
---|
1364 | n/a | _dir_locals(void) |
---|
1365 | n/a | { |
---|
1366 | n/a | PyObject *names; |
---|
1367 | n/a | PyObject *locals; |
---|
1368 | n/a | |
---|
1369 | n/a | locals = PyEval_GetLocals(); |
---|
1370 | n/a | if (locals == NULL) |
---|
1371 | n/a | return NULL; |
---|
1372 | n/a | |
---|
1373 | n/a | names = PyMapping_Keys(locals); |
---|
1374 | n/a | if (!names) |
---|
1375 | n/a | return NULL; |
---|
1376 | n/a | if (!PyList_Check(names)) { |
---|
1377 | n/a | PyErr_Format(PyExc_TypeError, |
---|
1378 | n/a | "dir(): expected keys() of locals to be a list, " |
---|
1379 | n/a | "not '%.200s'", Py_TYPE(names)->tp_name); |
---|
1380 | n/a | Py_DECREF(names); |
---|
1381 | n/a | return NULL; |
---|
1382 | n/a | } |
---|
1383 | n/a | if (PyList_Sort(names)) { |
---|
1384 | n/a | Py_DECREF(names); |
---|
1385 | n/a | return NULL; |
---|
1386 | n/a | } |
---|
1387 | n/a | /* the locals don't need to be DECREF'd */ |
---|
1388 | n/a | return names; |
---|
1389 | n/a | } |
---|
1390 | n/a | |
---|
1391 | n/a | /* Helper for PyObject_Dir: object introspection. */ |
---|
1392 | n/a | static PyObject * |
---|
1393 | n/a | _dir_object(PyObject *obj) |
---|
1394 | n/a | { |
---|
1395 | n/a | PyObject *result, *sorted; |
---|
1396 | n/a | PyObject *dirfunc = _PyObject_LookupSpecial(obj, &PyId___dir__); |
---|
1397 | n/a | |
---|
1398 | n/a | assert(obj); |
---|
1399 | n/a | if (dirfunc == NULL) { |
---|
1400 | n/a | if (!PyErr_Occurred()) |
---|
1401 | n/a | PyErr_SetString(PyExc_TypeError, "object does not provide __dir__"); |
---|
1402 | n/a | return NULL; |
---|
1403 | n/a | } |
---|
1404 | n/a | /* use __dir__ */ |
---|
1405 | n/a | result = _PyObject_CallNoArg(dirfunc); |
---|
1406 | n/a | Py_DECREF(dirfunc); |
---|
1407 | n/a | if (result == NULL) |
---|
1408 | n/a | return NULL; |
---|
1409 | n/a | /* return sorted(result) */ |
---|
1410 | n/a | sorted = PySequence_List(result); |
---|
1411 | n/a | Py_DECREF(result); |
---|
1412 | n/a | if (sorted == NULL) |
---|
1413 | n/a | return NULL; |
---|
1414 | n/a | if (PyList_Sort(sorted)) { |
---|
1415 | n/a | Py_DECREF(sorted); |
---|
1416 | n/a | return NULL; |
---|
1417 | n/a | } |
---|
1418 | n/a | return sorted; |
---|
1419 | n/a | } |
---|
1420 | n/a | |
---|
1421 | n/a | /* Implementation of dir() -- if obj is NULL, returns the names in the current |
---|
1422 | n/a | (local) scope. Otherwise, performs introspection of the object: returns a |
---|
1423 | n/a | sorted list of attribute names (supposedly) accessible from the object |
---|
1424 | n/a | */ |
---|
1425 | n/a | PyObject * |
---|
1426 | n/a | PyObject_Dir(PyObject *obj) |
---|
1427 | n/a | { |
---|
1428 | n/a | return (obj == NULL) ? _dir_locals() : _dir_object(obj); |
---|
1429 | n/a | } |
---|
1430 | n/a | |
---|
1431 | n/a | /* |
---|
1432 | n/a | None is a non-NULL undefined value. |
---|
1433 | n/a | There is (and should be!) no way to create other objects of this type, |
---|
1434 | n/a | so there is exactly one (which is indestructible, by the way). |
---|
1435 | n/a | */ |
---|
1436 | n/a | |
---|
1437 | n/a | /* ARGSUSED */ |
---|
1438 | n/a | static PyObject * |
---|
1439 | n/a | none_repr(PyObject *op) |
---|
1440 | n/a | { |
---|
1441 | n/a | return PyUnicode_FromString("None"); |
---|
1442 | n/a | } |
---|
1443 | n/a | |
---|
1444 | n/a | /* ARGUSED */ |
---|
1445 | n/a | static void |
---|
1446 | n/a | none_dealloc(PyObject* ignore) |
---|
1447 | n/a | { |
---|
1448 | n/a | /* This should never get called, but we also don't want to SEGV if |
---|
1449 | n/a | * we accidentally decref None out of existence. |
---|
1450 | n/a | */ |
---|
1451 | n/a | Py_FatalError("deallocating None"); |
---|
1452 | n/a | } |
---|
1453 | n/a | |
---|
1454 | n/a | static PyObject * |
---|
1455 | n/a | none_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
---|
1456 | n/a | { |
---|
1457 | n/a | if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) { |
---|
1458 | n/a | PyErr_SetString(PyExc_TypeError, "NoneType takes no arguments"); |
---|
1459 | n/a | return NULL; |
---|
1460 | n/a | } |
---|
1461 | n/a | Py_RETURN_NONE; |
---|
1462 | n/a | } |
---|
1463 | n/a | |
---|
1464 | n/a | static int |
---|
1465 | n/a | none_bool(PyObject *v) |
---|
1466 | n/a | { |
---|
1467 | n/a | return 0; |
---|
1468 | n/a | } |
---|
1469 | n/a | |
---|
1470 | n/a | static PyNumberMethods none_as_number = { |
---|
1471 | n/a | 0, /* nb_add */ |
---|
1472 | n/a | 0, /* nb_subtract */ |
---|
1473 | n/a | 0, /* nb_multiply */ |
---|
1474 | n/a | 0, /* nb_remainder */ |
---|
1475 | n/a | 0, /* nb_divmod */ |
---|
1476 | n/a | 0, /* nb_power */ |
---|
1477 | n/a | 0, /* nb_negative */ |
---|
1478 | n/a | 0, /* nb_positive */ |
---|
1479 | n/a | 0, /* nb_absolute */ |
---|
1480 | n/a | (inquiry)none_bool, /* nb_bool */ |
---|
1481 | n/a | 0, /* nb_invert */ |
---|
1482 | n/a | 0, /* nb_lshift */ |
---|
1483 | n/a | 0, /* nb_rshift */ |
---|
1484 | n/a | 0, /* nb_and */ |
---|
1485 | n/a | 0, /* nb_xor */ |
---|
1486 | n/a | 0, /* nb_or */ |
---|
1487 | n/a | 0, /* nb_int */ |
---|
1488 | n/a | 0, /* nb_reserved */ |
---|
1489 | n/a | 0, /* nb_float */ |
---|
1490 | n/a | 0, /* nb_inplace_add */ |
---|
1491 | n/a | 0, /* nb_inplace_subtract */ |
---|
1492 | n/a | 0, /* nb_inplace_multiply */ |
---|
1493 | n/a | 0, /* nb_inplace_remainder */ |
---|
1494 | n/a | 0, /* nb_inplace_power */ |
---|
1495 | n/a | 0, /* nb_inplace_lshift */ |
---|
1496 | n/a | 0, /* nb_inplace_rshift */ |
---|
1497 | n/a | 0, /* nb_inplace_and */ |
---|
1498 | n/a | 0, /* nb_inplace_xor */ |
---|
1499 | n/a | 0, /* nb_inplace_or */ |
---|
1500 | n/a | 0, /* nb_floor_divide */ |
---|
1501 | n/a | 0, /* nb_true_divide */ |
---|
1502 | n/a | 0, /* nb_inplace_floor_divide */ |
---|
1503 | n/a | 0, /* nb_inplace_true_divide */ |
---|
1504 | n/a | 0, /* nb_index */ |
---|
1505 | n/a | }; |
---|
1506 | n/a | |
---|
1507 | n/a | PyTypeObject _PyNone_Type = { |
---|
1508 | n/a | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
---|
1509 | n/a | "NoneType", |
---|
1510 | n/a | 0, |
---|
1511 | n/a | 0, |
---|
1512 | n/a | none_dealloc, /*tp_dealloc*/ /*never called*/ |
---|
1513 | n/a | 0, /*tp_print*/ |
---|
1514 | n/a | 0, /*tp_getattr*/ |
---|
1515 | n/a | 0, /*tp_setattr*/ |
---|
1516 | n/a | 0, /*tp_reserved*/ |
---|
1517 | n/a | none_repr, /*tp_repr*/ |
---|
1518 | n/a | &none_as_number, /*tp_as_number*/ |
---|
1519 | n/a | 0, /*tp_as_sequence*/ |
---|
1520 | n/a | 0, /*tp_as_mapping*/ |
---|
1521 | n/a | 0, /*tp_hash */ |
---|
1522 | n/a | 0, /*tp_call */ |
---|
1523 | n/a | 0, /*tp_str */ |
---|
1524 | n/a | 0, /*tp_getattro */ |
---|
1525 | n/a | 0, /*tp_setattro */ |
---|
1526 | n/a | 0, /*tp_as_buffer */ |
---|
1527 | n/a | Py_TPFLAGS_DEFAULT, /*tp_flags */ |
---|
1528 | n/a | 0, /*tp_doc */ |
---|
1529 | n/a | 0, /*tp_traverse */ |
---|
1530 | n/a | 0, /*tp_clear */ |
---|
1531 | n/a | 0, /*tp_richcompare */ |
---|
1532 | n/a | 0, /*tp_weaklistoffset */ |
---|
1533 | n/a | 0, /*tp_iter */ |
---|
1534 | n/a | 0, /*tp_iternext */ |
---|
1535 | n/a | 0, /*tp_methods */ |
---|
1536 | n/a | 0, /*tp_members */ |
---|
1537 | n/a | 0, /*tp_getset */ |
---|
1538 | n/a | 0, /*tp_base */ |
---|
1539 | n/a | 0, /*tp_dict */ |
---|
1540 | n/a | 0, /*tp_descr_get */ |
---|
1541 | n/a | 0, /*tp_descr_set */ |
---|
1542 | n/a | 0, /*tp_dictoffset */ |
---|
1543 | n/a | 0, /*tp_init */ |
---|
1544 | n/a | 0, /*tp_alloc */ |
---|
1545 | n/a | none_new, /*tp_new */ |
---|
1546 | n/a | }; |
---|
1547 | n/a | |
---|
1548 | n/a | PyObject _Py_NoneStruct = { |
---|
1549 | n/a | _PyObject_EXTRA_INIT |
---|
1550 | n/a | 1, &_PyNone_Type |
---|
1551 | n/a | }; |
---|
1552 | n/a | |
---|
1553 | n/a | /* NotImplemented is an object that can be used to signal that an |
---|
1554 | n/a | operation is not implemented for the given type combination. */ |
---|
1555 | n/a | |
---|
1556 | n/a | static PyObject * |
---|
1557 | n/a | NotImplemented_repr(PyObject *op) |
---|
1558 | n/a | { |
---|
1559 | n/a | return PyUnicode_FromString("NotImplemented"); |
---|
1560 | n/a | } |
---|
1561 | n/a | |
---|
1562 | n/a | static PyObject * |
---|
1563 | n/a | NotImplemented_reduce(PyObject *op) |
---|
1564 | n/a | { |
---|
1565 | n/a | return PyUnicode_FromString("NotImplemented"); |
---|
1566 | n/a | } |
---|
1567 | n/a | |
---|
1568 | n/a | static PyMethodDef notimplemented_methods[] = { |
---|
1569 | n/a | {"__reduce__", (PyCFunction)NotImplemented_reduce, METH_NOARGS, NULL}, |
---|
1570 | n/a | {NULL, NULL} |
---|
1571 | n/a | }; |
---|
1572 | n/a | |
---|
1573 | n/a | static PyObject * |
---|
1574 | n/a | notimplemented_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
---|
1575 | n/a | { |
---|
1576 | n/a | if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) { |
---|
1577 | n/a | PyErr_SetString(PyExc_TypeError, "NotImplementedType takes no arguments"); |
---|
1578 | n/a | return NULL; |
---|
1579 | n/a | } |
---|
1580 | n/a | Py_RETURN_NOTIMPLEMENTED; |
---|
1581 | n/a | } |
---|
1582 | n/a | |
---|
1583 | n/a | static void |
---|
1584 | n/a | notimplemented_dealloc(PyObject* ignore) |
---|
1585 | n/a | { |
---|
1586 | n/a | /* This should never get called, but we also don't want to SEGV if |
---|
1587 | n/a | * we accidentally decref NotImplemented out of existence. |
---|
1588 | n/a | */ |
---|
1589 | n/a | Py_FatalError("deallocating NotImplemented"); |
---|
1590 | n/a | } |
---|
1591 | n/a | |
---|
1592 | n/a | PyTypeObject _PyNotImplemented_Type = { |
---|
1593 | n/a | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
---|
1594 | n/a | "NotImplementedType", |
---|
1595 | n/a | 0, |
---|
1596 | n/a | 0, |
---|
1597 | n/a | notimplemented_dealloc, /*tp_dealloc*/ /*never called*/ |
---|
1598 | n/a | 0, /*tp_print*/ |
---|
1599 | n/a | 0, /*tp_getattr*/ |
---|
1600 | n/a | 0, /*tp_setattr*/ |
---|
1601 | n/a | 0, /*tp_reserved*/ |
---|
1602 | n/a | NotImplemented_repr, /*tp_repr*/ |
---|
1603 | n/a | 0, /*tp_as_number*/ |
---|
1604 | n/a | 0, /*tp_as_sequence*/ |
---|
1605 | n/a | 0, /*tp_as_mapping*/ |
---|
1606 | n/a | 0, /*tp_hash */ |
---|
1607 | n/a | 0, /*tp_call */ |
---|
1608 | n/a | 0, /*tp_str */ |
---|
1609 | n/a | 0, /*tp_getattro */ |
---|
1610 | n/a | 0, /*tp_setattro */ |
---|
1611 | n/a | 0, /*tp_as_buffer */ |
---|
1612 | n/a | Py_TPFLAGS_DEFAULT, /*tp_flags */ |
---|
1613 | n/a | 0, /*tp_doc */ |
---|
1614 | n/a | 0, /*tp_traverse */ |
---|
1615 | n/a | 0, /*tp_clear */ |
---|
1616 | n/a | 0, /*tp_richcompare */ |
---|
1617 | n/a | 0, /*tp_weaklistoffset */ |
---|
1618 | n/a | 0, /*tp_iter */ |
---|
1619 | n/a | 0, /*tp_iternext */ |
---|
1620 | n/a | notimplemented_methods, /*tp_methods */ |
---|
1621 | n/a | 0, /*tp_members */ |
---|
1622 | n/a | 0, /*tp_getset */ |
---|
1623 | n/a | 0, /*tp_base */ |
---|
1624 | n/a | 0, /*tp_dict */ |
---|
1625 | n/a | 0, /*tp_descr_get */ |
---|
1626 | n/a | 0, /*tp_descr_set */ |
---|
1627 | n/a | 0, /*tp_dictoffset */ |
---|
1628 | n/a | 0, /*tp_init */ |
---|
1629 | n/a | 0, /*tp_alloc */ |
---|
1630 | n/a | notimplemented_new, /*tp_new */ |
---|
1631 | n/a | }; |
---|
1632 | n/a | |
---|
1633 | n/a | PyObject _Py_NotImplementedStruct = { |
---|
1634 | n/a | _PyObject_EXTRA_INIT |
---|
1635 | n/a | 1, &_PyNotImplemented_Type |
---|
1636 | n/a | }; |
---|
1637 | n/a | |
---|
1638 | n/a | void |
---|
1639 | n/a | _Py_ReadyTypes(void) |
---|
1640 | n/a | { |
---|
1641 | n/a | if (PyType_Ready(&PyBaseObject_Type) < 0) |
---|
1642 | n/a | Py_FatalError("Can't initialize object type"); |
---|
1643 | n/a | |
---|
1644 | n/a | if (PyType_Ready(&PyType_Type) < 0) |
---|
1645 | n/a | Py_FatalError("Can't initialize type type"); |
---|
1646 | n/a | |
---|
1647 | n/a | if (PyType_Ready(&_PyWeakref_RefType) < 0) |
---|
1648 | n/a | Py_FatalError("Can't initialize weakref type"); |
---|
1649 | n/a | |
---|
1650 | n/a | if (PyType_Ready(&_PyWeakref_CallableProxyType) < 0) |
---|
1651 | n/a | Py_FatalError("Can't initialize callable weakref proxy type"); |
---|
1652 | n/a | |
---|
1653 | n/a | if (PyType_Ready(&_PyWeakref_ProxyType) < 0) |
---|
1654 | n/a | Py_FatalError("Can't initialize weakref proxy type"); |
---|
1655 | n/a | |
---|
1656 | n/a | if (PyType_Ready(&PyLong_Type) < 0) |
---|
1657 | n/a | Py_FatalError("Can't initialize int type"); |
---|
1658 | n/a | |
---|
1659 | n/a | if (PyType_Ready(&PyBool_Type) < 0) |
---|
1660 | n/a | Py_FatalError("Can't initialize bool type"); |
---|
1661 | n/a | |
---|
1662 | n/a | if (PyType_Ready(&PyByteArray_Type) < 0) |
---|
1663 | n/a | Py_FatalError("Can't initialize bytearray type"); |
---|
1664 | n/a | |
---|
1665 | n/a | if (PyType_Ready(&PyBytes_Type) < 0) |
---|
1666 | n/a | Py_FatalError("Can't initialize 'str'"); |
---|
1667 | n/a | |
---|
1668 | n/a | if (PyType_Ready(&PyList_Type) < 0) |
---|
1669 | n/a | Py_FatalError("Can't initialize list type"); |
---|
1670 | n/a | |
---|
1671 | n/a | if (PyType_Ready(&_PyNone_Type) < 0) |
---|
1672 | n/a | Py_FatalError("Can't initialize None type"); |
---|
1673 | n/a | |
---|
1674 | n/a | if (PyType_Ready(&_PyNotImplemented_Type) < 0) |
---|
1675 | n/a | Py_FatalError("Can't initialize NotImplemented type"); |
---|
1676 | n/a | |
---|
1677 | n/a | if (PyType_Ready(&PyTraceBack_Type) < 0) |
---|
1678 | n/a | Py_FatalError("Can't initialize traceback type"); |
---|
1679 | n/a | |
---|
1680 | n/a | if (PyType_Ready(&PySuper_Type) < 0) |
---|
1681 | n/a | Py_FatalError("Can't initialize super type"); |
---|
1682 | n/a | |
---|
1683 | n/a | if (PyType_Ready(&PyRange_Type) < 0) |
---|
1684 | n/a | Py_FatalError("Can't initialize range type"); |
---|
1685 | n/a | |
---|
1686 | n/a | if (PyType_Ready(&PyDict_Type) < 0) |
---|
1687 | n/a | Py_FatalError("Can't initialize dict type"); |
---|
1688 | n/a | |
---|
1689 | n/a | if (PyType_Ready(&PyDictKeys_Type) < 0) |
---|
1690 | n/a | Py_FatalError("Can't initialize dict keys type"); |
---|
1691 | n/a | |
---|
1692 | n/a | if (PyType_Ready(&PyDictValues_Type) < 0) |
---|
1693 | n/a | Py_FatalError("Can't initialize dict values type"); |
---|
1694 | n/a | |
---|
1695 | n/a | if (PyType_Ready(&PyDictItems_Type) < 0) |
---|
1696 | n/a | Py_FatalError("Can't initialize dict items type"); |
---|
1697 | n/a | |
---|
1698 | n/a | if (PyType_Ready(&PyODict_Type) < 0) |
---|
1699 | n/a | Py_FatalError("Can't initialize OrderedDict type"); |
---|
1700 | n/a | |
---|
1701 | n/a | if (PyType_Ready(&PyODictKeys_Type) < 0) |
---|
1702 | n/a | Py_FatalError("Can't initialize odict_keys type"); |
---|
1703 | n/a | |
---|
1704 | n/a | if (PyType_Ready(&PyODictItems_Type) < 0) |
---|
1705 | n/a | Py_FatalError("Can't initialize odict_items type"); |
---|
1706 | n/a | |
---|
1707 | n/a | if (PyType_Ready(&PyODictValues_Type) < 0) |
---|
1708 | n/a | Py_FatalError("Can't initialize odict_values type"); |
---|
1709 | n/a | |
---|
1710 | n/a | if (PyType_Ready(&PyODictIter_Type) < 0) |
---|
1711 | n/a | Py_FatalError("Can't initialize odict_keyiterator type"); |
---|
1712 | n/a | |
---|
1713 | n/a | if (PyType_Ready(&PySet_Type) < 0) |
---|
1714 | n/a | Py_FatalError("Can't initialize set type"); |
---|
1715 | n/a | |
---|
1716 | n/a | if (PyType_Ready(&PyUnicode_Type) < 0) |
---|
1717 | n/a | Py_FatalError("Can't initialize str type"); |
---|
1718 | n/a | |
---|
1719 | n/a | if (PyType_Ready(&PySlice_Type) < 0) |
---|
1720 | n/a | Py_FatalError("Can't initialize slice type"); |
---|
1721 | n/a | |
---|
1722 | n/a | if (PyType_Ready(&PyStaticMethod_Type) < 0) |
---|
1723 | n/a | Py_FatalError("Can't initialize static method type"); |
---|
1724 | n/a | |
---|
1725 | n/a | if (PyType_Ready(&PyComplex_Type) < 0) |
---|
1726 | n/a | Py_FatalError("Can't initialize complex type"); |
---|
1727 | n/a | |
---|
1728 | n/a | if (PyType_Ready(&PyFloat_Type) < 0) |
---|
1729 | n/a | Py_FatalError("Can't initialize float type"); |
---|
1730 | n/a | |
---|
1731 | n/a | if (PyType_Ready(&PyFrozenSet_Type) < 0) |
---|
1732 | n/a | Py_FatalError("Can't initialize frozenset type"); |
---|
1733 | n/a | |
---|
1734 | n/a | if (PyType_Ready(&PyProperty_Type) < 0) |
---|
1735 | n/a | Py_FatalError("Can't initialize property type"); |
---|
1736 | n/a | |
---|
1737 | n/a | if (PyType_Ready(&_PyManagedBuffer_Type) < 0) |
---|
1738 | n/a | Py_FatalError("Can't initialize managed buffer type"); |
---|
1739 | n/a | |
---|
1740 | n/a | if (PyType_Ready(&PyMemoryView_Type) < 0) |
---|
1741 | n/a | Py_FatalError("Can't initialize memoryview type"); |
---|
1742 | n/a | |
---|
1743 | n/a | if (PyType_Ready(&PyTuple_Type) < 0) |
---|
1744 | n/a | Py_FatalError("Can't initialize tuple type"); |
---|
1745 | n/a | |
---|
1746 | n/a | if (PyType_Ready(&PyEnum_Type) < 0) |
---|
1747 | n/a | Py_FatalError("Can't initialize enumerate type"); |
---|
1748 | n/a | |
---|
1749 | n/a | if (PyType_Ready(&PyReversed_Type) < 0) |
---|
1750 | n/a | Py_FatalError("Can't initialize reversed type"); |
---|
1751 | n/a | |
---|
1752 | n/a | if (PyType_Ready(&PyStdPrinter_Type) < 0) |
---|
1753 | n/a | Py_FatalError("Can't initialize StdPrinter"); |
---|
1754 | n/a | |
---|
1755 | n/a | if (PyType_Ready(&PyCode_Type) < 0) |
---|
1756 | n/a | Py_FatalError("Can't initialize code type"); |
---|
1757 | n/a | |
---|
1758 | n/a | if (PyType_Ready(&PyFrame_Type) < 0) |
---|
1759 | n/a | Py_FatalError("Can't initialize frame type"); |
---|
1760 | n/a | |
---|
1761 | n/a | if (PyType_Ready(&PyCFunction_Type) < 0) |
---|
1762 | n/a | Py_FatalError("Can't initialize builtin function type"); |
---|
1763 | n/a | |
---|
1764 | n/a | if (PyType_Ready(&PyMethod_Type) < 0) |
---|
1765 | n/a | Py_FatalError("Can't initialize method type"); |
---|
1766 | n/a | |
---|
1767 | n/a | if (PyType_Ready(&PyFunction_Type) < 0) |
---|
1768 | n/a | Py_FatalError("Can't initialize function type"); |
---|
1769 | n/a | |
---|
1770 | n/a | if (PyType_Ready(&PyDictProxy_Type) < 0) |
---|
1771 | n/a | Py_FatalError("Can't initialize dict proxy type"); |
---|
1772 | n/a | |
---|
1773 | n/a | if (PyType_Ready(&PyGen_Type) < 0) |
---|
1774 | n/a | Py_FatalError("Can't initialize generator type"); |
---|
1775 | n/a | |
---|
1776 | n/a | if (PyType_Ready(&PyGetSetDescr_Type) < 0) |
---|
1777 | n/a | Py_FatalError("Can't initialize get-set descriptor type"); |
---|
1778 | n/a | |
---|
1779 | n/a | if (PyType_Ready(&PyWrapperDescr_Type) < 0) |
---|
1780 | n/a | Py_FatalError("Can't initialize wrapper type"); |
---|
1781 | n/a | |
---|
1782 | n/a | if (PyType_Ready(&_PyMethodWrapper_Type) < 0) |
---|
1783 | n/a | Py_FatalError("Can't initialize method wrapper type"); |
---|
1784 | n/a | |
---|
1785 | n/a | if (PyType_Ready(&PyEllipsis_Type) < 0) |
---|
1786 | n/a | Py_FatalError("Can't initialize ellipsis type"); |
---|
1787 | n/a | |
---|
1788 | n/a | if (PyType_Ready(&PyMemberDescr_Type) < 0) |
---|
1789 | n/a | Py_FatalError("Can't initialize member descriptor type"); |
---|
1790 | n/a | |
---|
1791 | n/a | if (PyType_Ready(&_PyNamespace_Type) < 0) |
---|
1792 | n/a | Py_FatalError("Can't initialize namespace type"); |
---|
1793 | n/a | |
---|
1794 | n/a | if (PyType_Ready(&PyCapsule_Type) < 0) |
---|
1795 | n/a | Py_FatalError("Can't initialize capsule type"); |
---|
1796 | n/a | |
---|
1797 | n/a | if (PyType_Ready(&PyLongRangeIter_Type) < 0) |
---|
1798 | n/a | Py_FatalError("Can't initialize long range iterator type"); |
---|
1799 | n/a | |
---|
1800 | n/a | if (PyType_Ready(&PyCell_Type) < 0) |
---|
1801 | n/a | Py_FatalError("Can't initialize cell type"); |
---|
1802 | n/a | |
---|
1803 | n/a | if (PyType_Ready(&PyInstanceMethod_Type) < 0) |
---|
1804 | n/a | Py_FatalError("Can't initialize instance method type"); |
---|
1805 | n/a | |
---|
1806 | n/a | if (PyType_Ready(&PyClassMethodDescr_Type) < 0) |
---|
1807 | n/a | Py_FatalError("Can't initialize class method descr type"); |
---|
1808 | n/a | |
---|
1809 | n/a | if (PyType_Ready(&PyMethodDescr_Type) < 0) |
---|
1810 | n/a | Py_FatalError("Can't initialize method descr type"); |
---|
1811 | n/a | |
---|
1812 | n/a | if (PyType_Ready(&PyCallIter_Type) < 0) |
---|
1813 | n/a | Py_FatalError("Can't initialize call iter type"); |
---|
1814 | n/a | |
---|
1815 | n/a | if (PyType_Ready(&PySeqIter_Type) < 0) |
---|
1816 | n/a | Py_FatalError("Can't initialize sequence iterator type"); |
---|
1817 | n/a | |
---|
1818 | n/a | if (PyType_Ready(&PyCoro_Type) < 0) |
---|
1819 | n/a | Py_FatalError("Can't initialize coroutine type"); |
---|
1820 | n/a | |
---|
1821 | n/a | if (PyType_Ready(&_PyCoroWrapper_Type) < 0) |
---|
1822 | n/a | Py_FatalError("Can't initialize coroutine wrapper type"); |
---|
1823 | n/a | } |
---|
1824 | n/a | |
---|
1825 | n/a | |
---|
1826 | n/a | #ifdef Py_TRACE_REFS |
---|
1827 | n/a | |
---|
1828 | n/a | void |
---|
1829 | n/a | _Py_NewReference(PyObject *op) |
---|
1830 | n/a | { |
---|
1831 | n/a | _Py_INC_REFTOTAL; |
---|
1832 | n/a | op->ob_refcnt = 1; |
---|
1833 | n/a | _Py_AddToAllObjects(op, 1); |
---|
1834 | n/a | _Py_INC_TPALLOCS(op); |
---|
1835 | n/a | } |
---|
1836 | n/a | |
---|
1837 | n/a | void |
---|
1838 | n/a | _Py_ForgetReference(PyObject *op) |
---|
1839 | n/a | { |
---|
1840 | n/a | #ifdef SLOW_UNREF_CHECK |
---|
1841 | n/a | PyObject *p; |
---|
1842 | n/a | #endif |
---|
1843 | n/a | if (op->ob_refcnt < 0) |
---|
1844 | n/a | Py_FatalError("UNREF negative refcnt"); |
---|
1845 | n/a | if (op == &refchain || |
---|
1846 | n/a | op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op) { |
---|
1847 | n/a | fprintf(stderr, "* ob\n"); |
---|
1848 | n/a | _PyObject_Dump(op); |
---|
1849 | n/a | fprintf(stderr, "* op->_ob_prev->_ob_next\n"); |
---|
1850 | n/a | _PyObject_Dump(op->_ob_prev->_ob_next); |
---|
1851 | n/a | fprintf(stderr, "* op->_ob_next->_ob_prev\n"); |
---|
1852 | n/a | _PyObject_Dump(op->_ob_next->_ob_prev); |
---|
1853 | n/a | Py_FatalError("UNREF invalid object"); |
---|
1854 | n/a | } |
---|
1855 | n/a | #ifdef SLOW_UNREF_CHECK |
---|
1856 | n/a | for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) { |
---|
1857 | n/a | if (p == op) |
---|
1858 | n/a | break; |
---|
1859 | n/a | } |
---|
1860 | n/a | if (p == &refchain) /* Not found */ |
---|
1861 | n/a | Py_FatalError("UNREF unknown object"); |
---|
1862 | n/a | #endif |
---|
1863 | n/a | op->_ob_next->_ob_prev = op->_ob_prev; |
---|
1864 | n/a | op->_ob_prev->_ob_next = op->_ob_next; |
---|
1865 | n/a | op->_ob_next = op->_ob_prev = NULL; |
---|
1866 | n/a | _Py_INC_TPFREES(op); |
---|
1867 | n/a | } |
---|
1868 | n/a | |
---|
1869 | n/a | void |
---|
1870 | n/a | _Py_Dealloc(PyObject *op) |
---|
1871 | n/a | { |
---|
1872 | n/a | destructor dealloc = Py_TYPE(op)->tp_dealloc; |
---|
1873 | n/a | _Py_ForgetReference(op); |
---|
1874 | n/a | (*dealloc)(op); |
---|
1875 | n/a | } |
---|
1876 | n/a | |
---|
1877 | n/a | /* Print all live objects. Because PyObject_Print is called, the |
---|
1878 | n/a | * interpreter must be in a healthy state. |
---|
1879 | n/a | */ |
---|
1880 | n/a | void |
---|
1881 | n/a | _Py_PrintReferences(FILE *fp) |
---|
1882 | n/a | { |
---|
1883 | n/a | PyObject *op; |
---|
1884 | n/a | fprintf(fp, "Remaining objects:\n"); |
---|
1885 | n/a | for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) { |
---|
1886 | n/a | fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] ", op, op->ob_refcnt); |
---|
1887 | n/a | if (PyObject_Print(op, fp, 0) != 0) |
---|
1888 | n/a | PyErr_Clear(); |
---|
1889 | n/a | putc('\n', fp); |
---|
1890 | n/a | } |
---|
1891 | n/a | } |
---|
1892 | n/a | |
---|
1893 | n/a | /* Print the addresses of all live objects. Unlike _Py_PrintReferences, this |
---|
1894 | n/a | * doesn't make any calls to the Python C API, so is always safe to call. |
---|
1895 | n/a | */ |
---|
1896 | n/a | void |
---|
1897 | n/a | _Py_PrintReferenceAddresses(FILE *fp) |
---|
1898 | n/a | { |
---|
1899 | n/a | PyObject *op; |
---|
1900 | n/a | fprintf(fp, "Remaining object addresses:\n"); |
---|
1901 | n/a | for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) |
---|
1902 | n/a | fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] %s\n", op, |
---|
1903 | n/a | op->ob_refcnt, Py_TYPE(op)->tp_name); |
---|
1904 | n/a | } |
---|
1905 | n/a | |
---|
1906 | n/a | PyObject * |
---|
1907 | n/a | _Py_GetObjects(PyObject *self, PyObject *args) |
---|
1908 | n/a | { |
---|
1909 | n/a | int i, n; |
---|
1910 | n/a | PyObject *t = NULL; |
---|
1911 | n/a | PyObject *res, *op; |
---|
1912 | n/a | |
---|
1913 | n/a | if (!PyArg_ParseTuple(args, "i|O", &n, &t)) |
---|
1914 | n/a | return NULL; |
---|
1915 | n/a | op = refchain._ob_next; |
---|
1916 | n/a | res = PyList_New(0); |
---|
1917 | n/a | if (res == NULL) |
---|
1918 | n/a | return NULL; |
---|
1919 | n/a | for (i = 0; (n == 0 || i < n) && op != &refchain; i++) { |
---|
1920 | n/a | while (op == self || op == args || op == res || op == t || |
---|
1921 | n/a | (t != NULL && Py_TYPE(op) != (PyTypeObject *) t)) { |
---|
1922 | n/a | op = op->_ob_next; |
---|
1923 | n/a | if (op == &refchain) |
---|
1924 | n/a | return res; |
---|
1925 | n/a | } |
---|
1926 | n/a | if (PyList_Append(res, op) < 0) { |
---|
1927 | n/a | Py_DECREF(res); |
---|
1928 | n/a | return NULL; |
---|
1929 | n/a | } |
---|
1930 | n/a | op = op->_ob_next; |
---|
1931 | n/a | } |
---|
1932 | n/a | return res; |
---|
1933 | n/a | } |
---|
1934 | n/a | |
---|
1935 | n/a | #endif |
---|
1936 | n/a | |
---|
1937 | n/a | |
---|
1938 | n/a | /* Hack to force loading of abstract.o */ |
---|
1939 | n/a | Py_ssize_t (*_Py_abstract_hack)(PyObject *) = PyObject_Size; |
---|
1940 | n/a | |
---|
1941 | n/a | |
---|
1942 | n/a | void |
---|
1943 | n/a | _PyObject_DebugTypeStats(FILE *out) |
---|
1944 | n/a | { |
---|
1945 | n/a | _PyCFunction_DebugMallocStats(out); |
---|
1946 | n/a | _PyDict_DebugMallocStats(out); |
---|
1947 | n/a | _PyFloat_DebugMallocStats(out); |
---|
1948 | n/a | _PyFrame_DebugMallocStats(out); |
---|
1949 | n/a | _PyList_DebugMallocStats(out); |
---|
1950 | n/a | _PyMethod_DebugMallocStats(out); |
---|
1951 | n/a | _PyTuple_DebugMallocStats(out); |
---|
1952 | n/a | } |
---|
1953 | n/a | |
---|
1954 | n/a | /* These methods are used to control infinite recursion in repr, str, print, |
---|
1955 | n/a | etc. Container objects that may recursively contain themselves, |
---|
1956 | n/a | e.g. builtin dictionaries and lists, should use Py_ReprEnter() and |
---|
1957 | n/a | Py_ReprLeave() to avoid infinite recursion. |
---|
1958 | n/a | |
---|
1959 | n/a | Py_ReprEnter() returns 0 the first time it is called for a particular |
---|
1960 | n/a | object and 1 every time thereafter. It returns -1 if an exception |
---|
1961 | n/a | occurred. Py_ReprLeave() has no return value. |
---|
1962 | n/a | |
---|
1963 | n/a | See dictobject.c and listobject.c for examples of use. |
---|
1964 | n/a | */ |
---|
1965 | n/a | |
---|
1966 | n/a | int |
---|
1967 | n/a | Py_ReprEnter(PyObject *obj) |
---|
1968 | n/a | { |
---|
1969 | n/a | PyObject *dict; |
---|
1970 | n/a | PyObject *list; |
---|
1971 | n/a | Py_ssize_t i; |
---|
1972 | n/a | |
---|
1973 | n/a | dict = PyThreadState_GetDict(); |
---|
1974 | n/a | /* Ignore a missing thread-state, so that this function can be called |
---|
1975 | n/a | early on startup. */ |
---|
1976 | n/a | if (dict == NULL) |
---|
1977 | n/a | return 0; |
---|
1978 | n/a | list = _PyDict_GetItemId(dict, &PyId_Py_Repr); |
---|
1979 | n/a | if (list == NULL) { |
---|
1980 | n/a | list = PyList_New(0); |
---|
1981 | n/a | if (list == NULL) |
---|
1982 | n/a | return -1; |
---|
1983 | n/a | if (_PyDict_SetItemId(dict, &PyId_Py_Repr, list) < 0) |
---|
1984 | n/a | return -1; |
---|
1985 | n/a | Py_DECREF(list); |
---|
1986 | n/a | } |
---|
1987 | n/a | i = PyList_GET_SIZE(list); |
---|
1988 | n/a | while (--i >= 0) { |
---|
1989 | n/a | if (PyList_GET_ITEM(list, i) == obj) |
---|
1990 | n/a | return 1; |
---|
1991 | n/a | } |
---|
1992 | n/a | if (PyList_Append(list, obj) < 0) |
---|
1993 | n/a | return -1; |
---|
1994 | n/a | return 0; |
---|
1995 | n/a | } |
---|
1996 | n/a | |
---|
1997 | n/a | void |
---|
1998 | n/a | Py_ReprLeave(PyObject *obj) |
---|
1999 | n/a | { |
---|
2000 | n/a | PyObject *dict; |
---|
2001 | n/a | PyObject *list; |
---|
2002 | n/a | Py_ssize_t i; |
---|
2003 | n/a | PyObject *error_type, *error_value, *error_traceback; |
---|
2004 | n/a | |
---|
2005 | n/a | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
---|
2006 | n/a | |
---|
2007 | n/a | dict = PyThreadState_GetDict(); |
---|
2008 | n/a | if (dict == NULL) |
---|
2009 | n/a | goto finally; |
---|
2010 | n/a | |
---|
2011 | n/a | list = _PyDict_GetItemId(dict, &PyId_Py_Repr); |
---|
2012 | n/a | if (list == NULL || !PyList_Check(list)) |
---|
2013 | n/a | goto finally; |
---|
2014 | n/a | |
---|
2015 | n/a | i = PyList_GET_SIZE(list); |
---|
2016 | n/a | /* Count backwards because we always expect obj to be list[-1] */ |
---|
2017 | n/a | while (--i >= 0) { |
---|
2018 | n/a | if (PyList_GET_ITEM(list, i) == obj) { |
---|
2019 | n/a | PyList_SetSlice(list, i, i + 1, NULL); |
---|
2020 | n/a | break; |
---|
2021 | n/a | } |
---|
2022 | n/a | } |
---|
2023 | n/a | |
---|
2024 | n/a | finally: |
---|
2025 | n/a | /* ignore exceptions because there is no way to report them. */ |
---|
2026 | n/a | PyErr_Restore(error_type, error_value, error_traceback); |
---|
2027 | n/a | } |
---|
2028 | n/a | |
---|
2029 | n/a | /* Trashcan support. */ |
---|
2030 | n/a | |
---|
2031 | n/a | /* Current call-stack depth of tp_dealloc calls. */ |
---|
2032 | n/a | int _PyTrash_delete_nesting = 0; |
---|
2033 | n/a | |
---|
2034 | n/a | /* List of objects that still need to be cleaned up, singly linked via their |
---|
2035 | n/a | * gc headers' gc_prev pointers. |
---|
2036 | n/a | */ |
---|
2037 | n/a | PyObject *_PyTrash_delete_later = NULL; |
---|
2038 | n/a | |
---|
2039 | n/a | /* Add op to the _PyTrash_delete_later list. Called when the current |
---|
2040 | n/a | * call-stack depth gets large. op must be a currently untracked gc'ed |
---|
2041 | n/a | * object, with refcount 0. Py_DECREF must already have been called on it. |
---|
2042 | n/a | */ |
---|
2043 | n/a | void |
---|
2044 | n/a | _PyTrash_deposit_object(PyObject *op) |
---|
2045 | n/a | { |
---|
2046 | n/a | assert(PyObject_IS_GC(op)); |
---|
2047 | n/a | assert(_PyGC_REFS(op) == _PyGC_REFS_UNTRACKED); |
---|
2048 | n/a | assert(op->ob_refcnt == 0); |
---|
2049 | n/a | _Py_AS_GC(op)->gc.gc_prev = (PyGC_Head *)_PyTrash_delete_later; |
---|
2050 | n/a | _PyTrash_delete_later = op; |
---|
2051 | n/a | } |
---|
2052 | n/a | |
---|
2053 | n/a | /* The equivalent API, using per-thread state recursion info */ |
---|
2054 | n/a | void |
---|
2055 | n/a | _PyTrash_thread_deposit_object(PyObject *op) |
---|
2056 | n/a | { |
---|
2057 | n/a | PyThreadState *tstate = PyThreadState_GET(); |
---|
2058 | n/a | assert(PyObject_IS_GC(op)); |
---|
2059 | n/a | assert(_PyGC_REFS(op) == _PyGC_REFS_UNTRACKED); |
---|
2060 | n/a | assert(op->ob_refcnt == 0); |
---|
2061 | n/a | _Py_AS_GC(op)->gc.gc_prev = (PyGC_Head *) tstate->trash_delete_later; |
---|
2062 | n/a | tstate->trash_delete_later = op; |
---|
2063 | n/a | } |
---|
2064 | n/a | |
---|
2065 | n/a | /* Dealloccate all the objects in the _PyTrash_delete_later list. Called when |
---|
2066 | n/a | * the call-stack unwinds again. |
---|
2067 | n/a | */ |
---|
2068 | n/a | void |
---|
2069 | n/a | _PyTrash_destroy_chain(void) |
---|
2070 | n/a | { |
---|
2071 | n/a | while (_PyTrash_delete_later) { |
---|
2072 | n/a | PyObject *op = _PyTrash_delete_later; |
---|
2073 | n/a | destructor dealloc = Py_TYPE(op)->tp_dealloc; |
---|
2074 | n/a | |
---|
2075 | n/a | _PyTrash_delete_later = |
---|
2076 | n/a | (PyObject*) _Py_AS_GC(op)->gc.gc_prev; |
---|
2077 | n/a | |
---|
2078 | n/a | /* Call the deallocator directly. This used to try to |
---|
2079 | n/a | * fool Py_DECREF into calling it indirectly, but |
---|
2080 | n/a | * Py_DECREF was already called on this object, and in |
---|
2081 | n/a | * assorted non-release builds calling Py_DECREF again ends |
---|
2082 | n/a | * up distorting allocation statistics. |
---|
2083 | n/a | */ |
---|
2084 | n/a | assert(op->ob_refcnt == 0); |
---|
2085 | n/a | ++_PyTrash_delete_nesting; |
---|
2086 | n/a | (*dealloc)(op); |
---|
2087 | n/a | --_PyTrash_delete_nesting; |
---|
2088 | n/a | } |
---|
2089 | n/a | } |
---|
2090 | n/a | |
---|
2091 | n/a | /* The equivalent API, using per-thread state recursion info */ |
---|
2092 | n/a | void |
---|
2093 | n/a | _PyTrash_thread_destroy_chain(void) |
---|
2094 | n/a | { |
---|
2095 | n/a | PyThreadState *tstate = PyThreadState_GET(); |
---|
2096 | n/a | while (tstate->trash_delete_later) { |
---|
2097 | n/a | PyObject *op = tstate->trash_delete_later; |
---|
2098 | n/a | destructor dealloc = Py_TYPE(op)->tp_dealloc; |
---|
2099 | n/a | |
---|
2100 | n/a | tstate->trash_delete_later = |
---|
2101 | n/a | (PyObject*) _Py_AS_GC(op)->gc.gc_prev; |
---|
2102 | n/a | |
---|
2103 | n/a | /* Call the deallocator directly. This used to try to |
---|
2104 | n/a | * fool Py_DECREF into calling it indirectly, but |
---|
2105 | n/a | * Py_DECREF was already called on this object, and in |
---|
2106 | n/a | * assorted non-release builds calling Py_DECREF again ends |
---|
2107 | n/a | * up distorting allocation statistics. |
---|
2108 | n/a | */ |
---|
2109 | n/a | assert(op->ob_refcnt == 0); |
---|
2110 | n/a | ++tstate->trash_delete_nesting; |
---|
2111 | n/a | (*dealloc)(op); |
---|
2112 | n/a | --tstate->trash_delete_nesting; |
---|
2113 | n/a | } |
---|
2114 | n/a | } |
---|
2115 | n/a | |
---|
2116 | n/a | #ifndef Py_TRACE_REFS |
---|
2117 | n/a | /* For Py_LIMITED_API, we need an out-of-line version of _Py_Dealloc. |
---|
2118 | n/a | Define this here, so we can undefine the macro. */ |
---|
2119 | n/a | #undef _Py_Dealloc |
---|
2120 | n/a | PyAPI_FUNC(void) _Py_Dealloc(PyObject *); |
---|
2121 | n/a | void |
---|
2122 | n/a | _Py_Dealloc(PyObject *op) |
---|
2123 | n/a | { |
---|
2124 | n/a | _Py_INC_TPFREES(op) _Py_COUNT_ALLOCS_COMMA |
---|
2125 | n/a | (*Py_TYPE(op)->tp_dealloc)(op); |
---|
2126 | n/a | } |
---|
2127 | n/a | #endif |
---|
2128 | n/a | |
---|
2129 | n/a | #ifdef __cplusplus |
---|
2130 | n/a | } |
---|
2131 | n/a | #endif |
---|