gh-111495: Add PyFile tests (#129449)

Add tests for the following functions in test_capi.test_file:

* PyFile_FromFd()
* PyFile_GetLine()
* PyFile_NewStdPrinter()
* PyFile_WriteObject()
* PyFile_WriteString()
* PyObject_AsFileDescriptor()

Add Modules/_testlimitedcapi/file.c file.

Remove test_embed.StdPrinterTests which became redundant.
This commit is contained in:
Victor Stinner 2025-01-30 18:05:32 +01:00 committed by GitHub
parent 4e47e05045
commit 4ca9fc08f8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 500 additions and 72 deletions

View file

@ -4,6 +4,33 @@ preserve
#include "pycore_modsupport.h" // _PyArg_CheckPositional()
PyDoc_STRVAR(_testcapi_pyfile_newstdprinter__doc__,
"pyfile_newstdprinter($module, fd, /)\n"
"--\n"
"\n");
#define _TESTCAPI_PYFILE_NEWSTDPRINTER_METHODDEF \
{"pyfile_newstdprinter", (PyCFunction)_testcapi_pyfile_newstdprinter, METH_O, _testcapi_pyfile_newstdprinter__doc__},
static PyObject *
_testcapi_pyfile_newstdprinter_impl(PyObject *module, int fd);
static PyObject *
_testcapi_pyfile_newstdprinter(PyObject *module, PyObject *arg)
{
PyObject *return_value = NULL;
int fd;
fd = PyLong_AsInt(arg);
if (fd == -1 && PyErr_Occurred()) {
goto exit;
}
return_value = _testcapi_pyfile_newstdprinter_impl(module, fd);
exit:
return return_value;
}
PyDoc_STRVAR(_testcapi_py_fopen__doc__,
"py_fopen($module, path, mode, /)\n"
"--\n"
@ -34,4 +61,4 @@ _testcapi_py_fopen(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
exit:
return return_value;
}
/*[clinic end generated code: output=c4dc92400306c3eb input=a9049054013a1b77]*/
/*[clinic end generated code: output=e943bbd7f181d079 input=a9049054013a1b77]*/

View file

@ -5,11 +5,29 @@
#include "util.h"
#include "clinic/file.c.h"
/*[clinic input]
module _testcapi
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=6361033e795369fc]*/
/*[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);
}
/*[clinic input]
_testcapi.py_fopen
@ -38,7 +56,9 @@ _testcapi_py_fopen_impl(PyObject *module, PyObject *path, const char *mode,
return PyBytes_FromStringAndSize(buffer, size);
}
static PyMethodDef test_methods[] = {
_TESTCAPI_PYFILE_NEWSTDPRINTER_METHODDEF
_TESTCAPI_PY_FOPEN_METHODDEF
{NULL},
};
@ -46,9 +66,5 @@ static PyMethodDef test_methods[] = {
int
_PyTestCapi_Init_File(PyObject *m)
{
if (PyModule_AddFunctions(m, test_methods) < 0){
return -1;
}
return 0;
return PyModule_AddFunctions(m, test_methods);
}