summaryrefslogtreecommitdiffstats
path: root/Modules/_testcapi/file.c
blob: 8763dbba62c0a830f4e02127024b625947cf820c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#define PY_SSIZE_T_CLEAN
#include "parts.h"
#include "util.h"
#include "clinic/file.c.h"


/*[clinic input]
module _testcapi
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=6361033e795369fc]*/


static PyObject *
pyfile_fromfd(PyObject *module, PyObject *args)
{
    int fd;
    const char *name;
    Py_ssize_t size;
    const char *mode;
    int buffering;
    const char *encoding;
    const char *errors;
    const char *newline;
    int closefd;
    if (!PyArg_ParseTuple(args,
                          "iz#z#"
                          "iz#z#"
                          "z#i",
                          &fd, &name, &size, &mode, &size,
                          &buffering, &encoding, &size, &errors, &size,
                          &newline, &size, &closefd)) {
        return NULL;
    }

    return PyFile_FromFd(fd, name, mode, buffering,
                         encoding, errors, newline, closefd);
}


/*[clinic input]
_testcapi.pyfile_getline

    file: object
    n: int
    /

[clinic start generated code]*/

static PyObject *
_testcapi_pyfile_getline_impl(PyObject *module, PyObject *file, int n)
/*[clinic end generated code: output=137fde2774563266 input=df26686148b3657e]*/
{
    return PyFile_GetLine(file, n);
}


/*[clinic input]
_testcapi.pyfile_writeobject

    obj: object
    file: object
    flags: int
    /

[clinic start generated code]*/

static PyObject *
_testcapi_pyfile_writeobject_impl(PyObject *module, PyObject *obj,
                                  PyObject *file, int flags)
/*[clinic end generated code: output=ebb4d802e3db489c input=64a34a3e75b9935a]*/
{
    NULLABLE(obj);
    NULLABLE(file);
    RETURN_INT(PyFile_WriteObject(obj, file, flags));
}


static PyObject *
pyfile_writestring(PyObject *module, PyObject *args)
{
    const char *str;
    Py_ssize_t size;
    PyObject *file;
    if (!PyArg_ParseTuple(args, "z#O", &str, &size, &file)) {
        return NULL;
    }
    NULLABLE(file);

    RETURN_INT(PyFile_WriteString(str, file));
}


/*[clinic input]
_testcapi.pyobject_asfiledescriptor

    obj: object
    /

[clinic start generated code]*/

static PyObject *
_testcapi_pyobject_asfiledescriptor(PyObject *module, PyObject *obj)
/*[clinic end generated code: output=2d640c6a1970c721 input=45fa1171d62b18d7]*/
{
    NULLABLE(obj);
    RETURN_INT(PyObject_AsFileDescriptor(obj));
}


/*[clinic input]
_testcapi.pyfile_newstdprinter

    fd: int
    /

[clinic start generated code]*/

static PyObject *
_testcapi_pyfile_newstdprinter_impl(PyObject *module, int fd)
/*[clinic end generated code: output=8a2d1c57b6892db3 input=442f1824142262ea]*/
{
    return PyFile_NewStdPrinter(fd);
}


static PyMethodDef test_methods[] = {
    {"pyfile_fromfd", pyfile_fromfd, METH_VARARGS},
    _TESTCAPI_PYFILE_GETLINE_METHODDEF
    _TESTCAPI_PYFILE_WRITEOBJECT_METHODDEF
    {"pyfile_writestring", pyfile_writestring, METH_VARARGS},
    _TESTCAPI_PYOBJECT_ASFILEDESCRIPTOR_METHODDEF
    _TESTCAPI_PYFILE_NEWSTDPRINTER_METHODDEF
    {NULL},
};

int
_PyTestCapi_Init_File(PyObject *m)
{
    return PyModule_AddFunctions(m, test_methods);
}