Debian Codegraph @

Modules/_testlimitedcapi/file.c

raw
1 #include "pyconfig.h" // Py_GIL_DISABLED
2 #ifndef Py_GIL_DISABLED
3 // Need limited C API 3.13 for PyLong_AsInt()
4 # define Py_LIMITED_API 0x030d0000
5 #endif
6
7 #include "parts.h"
8 #include "util.h"
9 #include "clinic/file.c.h"
10
11
12 /*[clinic input]
13 module _testcapi
14 [clinic start generated code]*/
15 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=6361033e795369fc]*/
16
17
18 static PyObject *
19 pyfile_fromfd(PyObject *module, PyObject *args)
20 {
21 int fd;
22 const char *name;
23 Py_ssize_t size;
24 const char *mode;
25 int buffering;
26 const char *encoding;
27 const char *errors;
28 const char *newline;
29 int closefd;
30 if (!PyArg_ParseTuple(args,
31 "iz#z#"
32 "iz#z#"
33 "z#i",
34 &fd, &name, &size, &mode, &size,
35 &buffering, &encoding, &size, &errors, &size,
36 &newline, &size, &closefd)) {
37 return NULL;
38 }
39
40 return PyFile_FromFd(fd, name, mode, buffering,
41 encoding, errors, newline, closefd);
42 }
43
44
45 /*[clinic input]
46 _testcapi.pyfile_getline
47
48 file: object
49 n: int
50 /
51
52 [clinic start generated code]*/
53
54 static PyObject *
55 _testcapi_pyfile_getline_impl(PyObject *module, PyObject *file, int n)
56 /*[clinic end generated code: output=137fde2774563266 input=df26686148b3657e]*/
57 {
58 return PyFile_GetLine(file, n);
59 }
60
61
62 /*[clinic input]
63 _testcapi.pyfile_writeobject
64
65 obj: object
66 file: object
67 flags: int
68 /
69
70 [clinic start generated code]*/
71
72 static PyObject *
73 _testcapi_pyfile_writeobject_impl(PyObject *module, PyObject *obj,
74 PyObject *file, int flags)
75 /*[clinic end generated code: output=ebb4d802e3db489c input=64a34a3e75b9935a]*/
76 {
77 NULLABLE(obj);
78 NULLABLE(file);
79 RETURN_INT(PyFile_WriteObject(obj, file, flags));
80 }
81
82
83 static PyObject *
84 pyfile_writestring(PyObject *module, PyObject *args)
85 {
86 const char *str;
87 Py_ssize_t size;
88 PyObject *file;
89 if (!PyArg_ParseTuple(args, "z#O", &str, &size, &file)) {
90 return NULL;
91 }
92 NULLABLE(file);
93
94 RETURN_INT(PyFile_WriteString(str, file));
95 }
96
97
98 /*[clinic input]
99 _testcapi.pyobject_asfiledescriptor
100
101 obj: object
102 /
103
104 [clinic start generated code]*/
105
106 static PyObject *
107 _testcapi_pyobject_asfiledescriptor(PyObject *module, PyObject *obj)
108 /*[clinic end generated code: output=2d640c6a1970c721 input=45fa1171d62b18d7]*/
109 {
110 NULLABLE(obj);
111 RETURN_INT(PyObject_AsFileDescriptor(obj));
112 }
113
114
115 static PyMethodDef test_methods[] = {
116 {"pyfile_fromfd", pyfile_fromfd, METH_VARARGS},
117 _TESTCAPI_PYFILE_GETLINE_METHODDEF
118 _TESTCAPI_PYFILE_WRITEOBJECT_METHODDEF
119 {"pyfile_writestring", pyfile_writestring, METH_VARARGS},
120 _TESTCAPI_PYOBJECT_ASFILEDESCRIPTOR_METHODDEF
121 {NULL},
122 };
123
124 int
125 _PyTestLimitedCAPI_Init_File(PyObject *m)
126 {
127 return PyModule_AddFunctions(m, test_methods);
128 }