Debian Codegraph @

Modules/_testcapi/object.c

raw
1 #include "parts.h"
2 #include "util.h"
3
4 static PyObject *
5 call_pyobject_print(PyObject *self, PyObject * args)
6 {
7 PyObject *object;
8 PyObject *filename;
9 PyObject *print_raw;
10 FILE *fp;
11 int flags = 0;
12
13 if (!PyArg_UnpackTuple(args, "call_pyobject_print", 3, 3,
14 &object, &filename, &print_raw)) {
15 return NULL;
16 }
17
18 fp = Py_fopen(filename, "w+");
19
20 if (Py_IsTrue(print_raw)) {
21 flags = Py_PRINT_RAW;
22 }
23
24 if (PyObject_Print(object, fp, flags) < 0) {
25 fclose(fp);
26 return NULL;
27 }
28
29 fclose(fp);
30
31 Py_RETURN_NONE;
32 }
33
34 static PyObject *
35 pyobject_print_null(PyObject *self, PyObject *args)
36 {
37 PyObject *filename;
38 FILE *fp;
39
40 if (!PyArg_UnpackTuple(args, "call_pyobject_print", 1, 1, &filename)) {
41 return NULL;
42 }
43
44 fp = Py_fopen(filename, "w+");
45
46 if (PyObject_Print(NULL, fp, 0) < 0) {
47 fclose(fp);
48 return NULL;
49 }
50
51 fclose(fp);
52
53 Py_RETURN_NONE;
54 }
55
56 static PyObject *
57 pyobject_print_noref_object(PyObject *self, PyObject *args)
58 {
59 PyObject *test_string;
60 PyObject *filename;
61 FILE *fp;
62 char correct_string[100];
63
64 test_string = PyUnicode_FromString("Spam spam spam");
65
66 Py_SET_REFCNT(test_string, 0);
67
68 PyOS_snprintf(correct_string, 100, "<refcnt %zd at %p>",
69 Py_REFCNT(test_string), (void *)test_string);
70
71 if (!PyArg_UnpackTuple(args, "call_pyobject_print", 1, 1, &filename)) {
72 return NULL;
73 }
74
75 fp = Py_fopen(filename, "w+");
76
77 if (PyObject_Print(test_string, fp, 0) < 0){
78 fclose(fp);
79 Py_SET_REFCNT(test_string, 1);
80 Py_DECREF(test_string);
81 return NULL;
82 }
83
84 fclose(fp);
85
86 Py_SET_REFCNT(test_string, 1);
87 Py_DECREF(test_string);
88
89 return PyUnicode_FromString(correct_string);
90 }
91
92 static PyObject *
93 pyobject_print_os_error(PyObject *self, PyObject *args)
94 {
95 PyObject *test_string;
96 PyObject *filename;
97 FILE *fp;
98
99 test_string = PyUnicode_FromString("Spam spam spam");
100
101 if (!PyArg_UnpackTuple(args, "call_pyobject_print", 1, 1, &filename)) {
102 return NULL;
103 }
104
105 // open file in read mode to induce OSError
106 fp = Py_fopen(filename, "r");
107
108 if (PyObject_Print(test_string, fp, 0) < 0) {
109 fclose(fp);
110 Py_DECREF(test_string);
111 return NULL;
112 }
113
114 fclose(fp);
115 Py_DECREF(test_string);
116
117 Py_RETURN_NONE;
118 }
119
120 static PyObject *
121 pyobject_clear_weakrefs_no_callbacks(PyObject *self, PyObject *obj)
122 {
123 PyUnstable_Object_ClearWeakRefsNoCallbacks(obj);
124 Py_RETURN_NONE;
125 }
126
127 static PyObject *
128 pyobject_enable_deferred_refcount(PyObject *self, PyObject *obj)
129 {
130 int result = PyUnstable_Object_EnableDeferredRefcount(obj);
131 return PyLong_FromLong(result);
132 }
133
134 static PyObject *
135 pyobject_is_unique_temporary(PyObject *self, PyObject *obj)
136 {
137 int result = PyUnstable_Object_IsUniqueReferencedTemporary(obj);
138 return PyLong_FromLong(result);
139 }
140
141 static PyObject *
142 pyobject_is_unique_temporary_new_object(PyObject *self, PyObject *unused)
143 {
144 PyObject *obj = PyList_New(0);
145 int result = PyUnstable_Object_IsUniqueReferencedTemporary(obj);
146 Py_DECREF(obj);
147 return PyLong_FromLong(result);
148 }
149
150 static int MyObject_dealloc_called = 0;
151
152 static void
153 MyObject_dealloc(PyObject *op)
154 {
155 // PyUnstable_TryIncRef should return 0 if object is being deallocated
156 assert(Py_REFCNT(op) == 0);
157 assert(!PyUnstable_TryIncRef(op));
158 assert(Py_REFCNT(op) == 0);
159
160 MyObject_dealloc_called++;
161 Py_TYPE(op)->tp_free(op);
162 }
163
164 static PyTypeObject MyType = {
165 PyVarObject_HEAD_INIT(NULL, 0)
166 .tp_name = "MyType",
167 .tp_basicsize = sizeof(PyObject),
168 .tp_dealloc = MyObject_dealloc,
169 };
170
171 static PyObject *
172 test_py_try_inc_ref(PyObject *self, PyObject *unused)
173 {
174 if (PyType_Ready(&MyType) < 0) {
175 return NULL;
176 }
177
178 MyObject_dealloc_called = 0;
179
180 PyObject *op = PyObject_New(PyObject, &MyType);
181 if (op == NULL) {
182 return NULL;
183 }
184
185 PyUnstable_EnableTryIncRef(op);
186 #ifdef Py_GIL_DISABLED
187 // PyUnstable_EnableTryIncRef sets the shared flags to
188 // `_Py_REF_MAYBE_WEAKREF` if the flags are currently zero to ensure that
189 // the shared reference count is merged on deallocation.
190 assert((op->ob_ref_shared & _Py_REF_SHARED_FLAG_MASK) >= _Py_REF_MAYBE_WEAKREF);
191 #endif
192
193 if (!PyUnstable_TryIncRef(op)) {
194 PyErr_SetString(PyExc_AssertionError, "PyUnstable_TryIncRef failed");
195 Py_DECREF(op);
196 return NULL;
197 }
198 Py_DECREF(op); // undo try-incref
199 Py_DECREF(op); // dealloc
200 assert(MyObject_dealloc_called == 1);
201 Py_RETURN_NONE;
202 }
203
204
205 static PyObject *
206 _test_incref(PyObject *ob)
207 {
208 return Py_NewRef(ob);
209 }
210
211 static PyObject *
212 test_xincref_doesnt_leak(PyObject *ob, PyObject *Py_UNUSED(ignored))
213 {
214 PyObject *obj = PyLong_FromLong(0);
215 Py_XINCREF(_test_incref(obj));
216 Py_DECREF(obj);
217 Py_DECREF(obj);
218 Py_DECREF(obj);
219 Py_RETURN_NONE;
220 }
221
222
223 static PyObject *
224 test_incref_doesnt_leak(PyObject *ob, PyObject *Py_UNUSED(ignored))
225 {
226 PyObject *obj = PyLong_FromLong(0);
227 Py_INCREF(_test_incref(obj));
228 Py_DECREF(obj);
229 Py_DECREF(obj);
230 Py_DECREF(obj);
231 Py_RETURN_NONE;
232 }
233
234
235 static PyObject *
236 test_xdecref_doesnt_leak(PyObject *ob, PyObject *Py_UNUSED(ignored))
237 {
238 Py_XDECREF(PyLong_FromLong(0));
239 Py_RETURN_NONE;
240 }
241
242
243 static PyObject *
244 test_decref_doesnt_leak(PyObject *ob, PyObject *Py_UNUSED(ignored))
245 {
246 Py_DECREF(PyLong_FromLong(0));
247 Py_RETURN_NONE;
248 }
249
250
251 static PyObject *
252 test_incref_decref_API(PyObject *ob, PyObject *Py_UNUSED(ignored))
253 {
254 PyObject *obj = PyLong_FromLong(0);
255 Py_IncRef(obj);
256 Py_DecRef(obj);
257 Py_DecRef(obj);
258 Py_RETURN_NONE;
259 }
260
261
262 #ifdef Py_REF_DEBUG
263 static PyObject *
264 negative_refcount(PyObject *self, PyObject *Py_UNUSED(args))
265 {
266 PyObject *obj = PyUnicode_FromString("negative_refcount");
267 if (obj == NULL) {
268 return NULL;
269 }
270 assert(Py_REFCNT(obj) == 1);
271
272 Py_SET_REFCNT(obj, 0);
273 /* Py_DECREF() must call _Py_NegativeRefcount() and abort Python */
274 Py_DECREF(obj);
275
276 Py_RETURN_NONE;
277 }
278
279
280 static PyObject *
281 decref_freed_object(PyObject *self, PyObject *Py_UNUSED(args))
282 {
283 PyObject *obj = PyUnicode_FromString("decref_freed_object");
284 if (obj == NULL) {
285 return NULL;
286 }
287 assert(Py_REFCNT(obj) == 1);
288
289 // Deallocate the memory
290 Py_DECREF(obj);
291 // obj is a now a dangling pointer
292
293 // gh-109496: If Python is built in debug mode, Py_DECREF() must call
294 // _Py_NegativeRefcount() and abort Python.
295 Py_DECREF(obj);
296
297 Py_RETURN_NONE;
298 }
299 #endif
300
301
302 // Test Py_CLEAR() macro
303 static PyObject*
304 test_py_clear(PyObject *self, PyObject *Py_UNUSED(ignored))
305 {
306 // simple case with a variable
307 PyObject *obj = PyList_New(0);
308 if (obj == NULL) {
309 return NULL;
310 }
311 Py_CLEAR(obj);
312 assert(obj == NULL);
313
314 // gh-98724: complex case, Py_CLEAR() argument has a side effect
315 PyObject* array[1];
316 array[0] = PyList_New(0);
317 if (array[0] == NULL) {
318 return NULL;
319 }
320
321 PyObject **p = array;
322 Py_CLEAR(*p++);
323 assert(array[0] == NULL);
324 assert(p == array + 1);
325
326 Py_RETURN_NONE;
327 }
328
329
330 // Test Py_SETREF() and Py_XSETREF() macros, similar to test_py_clear()
331 static PyObject*
332 test_py_setref(PyObject *self, PyObject *Py_UNUSED(ignored))
333 {
334 // Py_SETREF() simple case with a variable
335 PyObject *obj = PyList_New(0);
336 if (obj == NULL) {
337 return NULL;
338 }
339 Py_SETREF(obj, NULL);
340 assert(obj == NULL);
341
342 // Py_XSETREF() simple case with a variable
343 PyObject *obj2 = PyList_New(0);
344 if (obj2 == NULL) {
345 return NULL;
346 }
347 Py_XSETREF(obj2, NULL);
348 assert(obj2 == NULL);
349 // test Py_XSETREF() when the argument is NULL
350 Py_XSETREF(obj2, NULL);
351 assert(obj2 == NULL);
352
353 // gh-98724: complex case, Py_SETREF() argument has a side effect
354 PyObject* array[1];
355 array[0] = PyList_New(0);
356 if (array[0] == NULL) {
357 return NULL;
358 }
359
360 PyObject **p = array;
361 Py_SETREF(*p++, NULL);
362 assert(array[0] == NULL);
363 assert(p == array + 1);
364
365 // gh-98724: complex case, Py_XSETREF() argument has a side effect
366 PyObject* array2[1];
367 array2[0] = PyList_New(0);
368 if (array2[0] == NULL) {
369 return NULL;
370 }
371
372 PyObject **p2 = array2;
373 Py_XSETREF(*p2++, NULL);
374 assert(array2[0] == NULL);
375 assert(p2 == array2 + 1);
376
377 // test Py_XSETREF() when the argument is NULL
378 p2 = array2;
379 Py_XSETREF(*p2++, NULL);
380 assert(array2[0] == NULL);
381 assert(p2 == array2 + 1);
382
383 Py_RETURN_NONE;
384 }
385
386
387 #define TEST_REFCOUNT() \
388 do { \
389 PyObject *obj = PyList_New(0); \
390 if (obj == NULL) { \
391 return NULL; \
392 } \
393 assert(Py_REFCNT(obj) == 1); \
394 \
395 /* test Py_NewRef() */ \
396 PyObject *ref = Py_NewRef(obj); \
397 assert(ref == obj); \
398 assert(Py_REFCNT(obj) == 2); \
399 Py_DECREF(ref); \
400 \
401 /* test Py_XNewRef() */ \
402 PyObject *xref = Py_XNewRef(obj); \
403 assert(xref == obj); \
404 assert(Py_REFCNT(obj) == 2); \
405 Py_DECREF(xref); \
406 \
407 assert(Py_XNewRef(NULL) == NULL); \
408 \
409 Py_DECREF(obj); \
410 Py_RETURN_NONE; \
411 } while (0)
412
413
414 // Test Py_NewRef() and Py_XNewRef() macros
415 static PyObject*
416 test_refcount_macros(PyObject *self, PyObject *Py_UNUSED(ignored))
417 {
418 TEST_REFCOUNT();
419 }
420
421 #undef Py_NewRef
422 #undef Py_XNewRef
423
424 // Test Py_NewRef() and Py_XNewRef() functions, after undefining macros.
425 static PyObject*
426 test_refcount_funcs(PyObject *self, PyObject *Py_UNUSED(ignored))
427 {
428 TEST_REFCOUNT();
429 }
430
431
432 // Test Py_Is() function
433 #define TEST_PY_IS() \
434 do { \
435 PyObject *o_none = Py_None; \
436 PyObject *o_true = Py_True; \
437 PyObject *o_false = Py_False; \
438 PyObject *obj = PyList_New(0); \
439 if (obj == NULL) { \
440 return NULL; \
441 } \
442 \
443 /* test Py_Is() */ \
444 assert(Py_Is(obj, obj)); \
445 assert(!Py_Is(obj, o_none)); \
446 \
447 /* test Py_None */ \
448 assert(Py_Is(o_none, o_none)); \
449 assert(!Py_Is(obj, o_none)); \
450 \
451 /* test Py_True */ \
452 assert(Py_Is(o_true, o_true)); \
453 assert(!Py_Is(o_false, o_true)); \
454 assert(!Py_Is(obj, o_true)); \
455 \
456 /* test Py_False */ \
457 assert(Py_Is(o_false, o_false)); \
458 assert(!Py_Is(o_true, o_false)); \
459 assert(!Py_Is(obj, o_false)); \
460 \
461 Py_DECREF(obj); \
462 Py_RETURN_NONE; \
463 } while (0)
464
465 // Test Py_Is() macro
466 static PyObject*
467 test_py_is_macros(PyObject *self, PyObject *Py_UNUSED(ignored))
468 {
469 TEST_PY_IS();
470 }
471
472 #undef Py_Is
473
474 // Test Py_Is() function, after undefining its macro.
475 static PyObject*
476 test_py_is_funcs(PyObject *self, PyObject *Py_UNUSED(ignored))
477 {
478 TEST_PY_IS();
479 }
480
481
482 static PyObject *
483 clear_managed_dict(PyObject *self, PyObject *obj)
484 {
485 PyObject_ClearManagedDict(obj);
486 Py_RETURN_NONE;
487 }
488
489
490 static PyObject *
491 is_uniquely_referenced(PyObject *self, PyObject *op)
492 {
493 return PyBool_FromLong(PyUnstable_Object_IsUniquelyReferenced(op));
494 }
495
496
497 static PyMethodDef test_methods[] = {
498 {"call_pyobject_print", call_pyobject_print, METH_VARARGS},
499 {"pyobject_print_null", pyobject_print_null, METH_VARARGS},
500 {"pyobject_print_noref_object", pyobject_print_noref_object, METH_VARARGS},
501 {"pyobject_print_os_error", pyobject_print_os_error, METH_VARARGS},
502 {"pyobject_clear_weakrefs_no_callbacks", pyobject_clear_weakrefs_no_callbacks, METH_O},
503 {"pyobject_enable_deferred_refcount", pyobject_enable_deferred_refcount, METH_O},
504 {"pyobject_is_unique_temporary", pyobject_is_unique_temporary, METH_O},
505 {"pyobject_is_unique_temporary_new_object", pyobject_is_unique_temporary_new_object, METH_NOARGS},
506 {"test_py_try_inc_ref", test_py_try_inc_ref, METH_NOARGS},
507 {"test_xincref_doesnt_leak",test_xincref_doesnt_leak, METH_NOARGS},
508 {"test_incref_doesnt_leak", test_incref_doesnt_leak, METH_NOARGS},
509 {"test_xdecref_doesnt_leak",test_xdecref_doesnt_leak, METH_NOARGS},
510 {"test_decref_doesnt_leak", test_decref_doesnt_leak, METH_NOARGS},
511 {"test_incref_decref_API", test_incref_decref_API, METH_NOARGS},
512 #ifdef Py_REF_DEBUG
513 {"negative_refcount", negative_refcount, METH_NOARGS},
514 {"decref_freed_object", decref_freed_object, METH_NOARGS},
515 #endif
516 {"test_py_clear", test_py_clear, METH_NOARGS},
517 {"test_py_setref", test_py_setref, METH_NOARGS},
518 {"test_refcount_macros", test_refcount_macros, METH_NOARGS},
519 {"test_refcount_funcs", test_refcount_funcs, METH_NOARGS},
520 {"test_py_is_macros", test_py_is_macros, METH_NOARGS},
521 {"test_py_is_funcs", test_py_is_funcs, METH_NOARGS},
522 {"clear_managed_dict", clear_managed_dict, METH_O, NULL},
523 {"is_uniquely_referenced", is_uniquely_referenced, METH_O},
524 {NULL},
525 };
526
527 int
528 _PyTestCapi_Init_Object(PyObject *m)
529 {
530 return PyModule_AddFunctions(m, test_methods);
531 }