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_obj(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_obj(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_obj(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_obj(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 PyMethodDef test_methods[] = {
128 {"call_pyobject_print", call_pyobject_print, METH_VARARGS},
129 {"pyobject_print_null", pyobject_print_null, METH_VARARGS},
130 {"pyobject_print_noref_object", pyobject_print_noref_object, METH_VARARGS},
131 {"pyobject_print_os_error", pyobject_print_os_error, METH_VARARGS},
132 {"pyobject_clear_weakrefs_no_callbacks", pyobject_clear_weakrefs_no_callbacks, METH_O},
133
134 {NULL},
135 };
136
137 int
138 _PyTestCapi_Init_Object(PyObject *m)
139 {
140 if (PyModule_AddFunctions(m, test_methods) < 0) {
141 return -1;
142 }
143
144 return 0;
145 }