| 1 |
|
| 2 |
#define PYTESTCAPI_NEED_INTERNAL_API |
| 3 |
|
| 4 |
#include "parts.h" |
| 5 |
#include "util.h" |
| 6 |
#include "clinic/file.c.h" |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
static PyObject * |
| 24 |
_testcapi_pyfile_newstdprinter_impl(PyObject *module, int fd) |
| 25 |
|
| 26 |
{ |
| 27 |
return PyFile_NewStdPrinter(fd); |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
static PyObject * |
| 42 |
_testcapi_py_fopen_impl(PyObject *module, PyObject *path, const char *mode, |
| 43 |
Py_ssize_t mode_length) |
| 44 |
|
| 45 |
{ |
| 46 |
NULLABLE(path); |
| 47 |
FILE *fp = Py_fopen(path, mode); |
| 48 |
if (fp == NULL) { |
| 49 |
return NULL; |
| 50 |
} |
| 51 |
|
| 52 |
char buffer[256]; |
| 53 |
size_t size = fread(buffer, 1, Py_ARRAY_LENGTH(buffer), fp); |
| 54 |
Py_fclose(fp); |
| 55 |
|
| 56 |
return PyBytes_FromStringAndSize(buffer, size); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
static PyObject * |
| 71 |
_testcapi_py_universalnewlinefgets_impl(PyObject *module, PyObject *file, |
| 72 |
int size) |
| 73 |
|
| 74 |
{ |
| 75 |
FILE *fp = Py_fopen(file, "rb"); |
| 76 |
if (fp == NULL) { |
| 77 |
return NULL; |
| 78 |
} |
| 79 |
|
| 80 |
char *buf = (char *)PyMem_Malloc(size); |
| 81 |
if (buf == NULL) { |
| 82 |
Py_fclose(fp); |
| 83 |
return PyErr_NoMemory(); |
| 84 |
} |
| 85 |
|
| 86 |
char *result = Py_UniversalNewlineFgets(buf, size, fp, NULL); |
| 87 |
if (result == NULL) { |
| 88 |
PyMem_Free(buf); |
| 89 |
Py_fclose(fp); |
| 90 |
Py_RETURN_NONE; |
| 91 |
} |
| 92 |
|
| 93 |
PyObject *line = PyBytes_FromString(result); |
| 94 |
PyMem_Free(buf); |
| 95 |
Py_fclose(fp); |
| 96 |
|
| 97 |
return line; |
| 98 |
} |
| 99 |
|
| 100 |
static PyMethodDef test_methods[] = { |
| 101 |
_TESTCAPI_PYFILE_NEWSTDPRINTER_METHODDEF |
| 102 |
_TESTCAPI_PY_FOPEN_METHODDEF |
| 103 |
_TESTCAPI_PY_UNIVERSALNEWLINEFGETS_METHODDEF |
| 104 |
{NULL}, |
| 105 |
}; |
| 106 |
|
| 107 |
int |
| 108 |
_PyTestCapi_Init_File(PyObject *m) |
| 109 |
{ |
| 110 |
return PyModule_AddFunctions(m, test_methods); |
| 111 |
} |