mirror of
https://github.com/python/cpython.git
synced 2025-08-31 05:58:33 +00:00
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:
parent
4e47e05045
commit
4ca9fc08f8
11 changed files with 500 additions and 72 deletions
|
@ -163,7 +163,7 @@
|
|||
@MODULE__TESTBUFFER_TRUE@_testbuffer _testbuffer.c
|
||||
@MODULE__TESTINTERNALCAPI_TRUE@_testinternalcapi _testinternalcapi.c _testinternalcapi/test_lock.c _testinternalcapi/pytime.c _testinternalcapi/set.c _testinternalcapi/test_critical_sections.c
|
||||
@MODULE__TESTCAPI_TRUE@_testcapi _testcapimodule.c _testcapi/vectorcall.c _testcapi/heaptype.c _testcapi/abstract.c _testcapi/unicode.c _testcapi/dict.c _testcapi/set.c _testcapi/list.c _testcapi/tuple.c _testcapi/getargs.c _testcapi/datetime.c _testcapi/docstring.c _testcapi/mem.c _testcapi/watchers.c _testcapi/long.c _testcapi/float.c _testcapi/complex.c _testcapi/numbers.c _testcapi/structmember.c _testcapi/exceptions.c _testcapi/code.c _testcapi/buffer.c _testcapi/pyatomic.c _testcapi/run.c _testcapi/file.c _testcapi/codec.c _testcapi/immortal.c _testcapi/gc.c _testcapi/hash.c _testcapi/time.c _testcapi/bytes.c _testcapi/object.c _testcapi/monitoring.c _testcapi/config.c _testcapi/import.c
|
||||
@MODULE__TESTLIMITEDCAPI_TRUE@_testlimitedcapi _testlimitedcapi.c _testlimitedcapi/abstract.c _testlimitedcapi/bytearray.c _testlimitedcapi/bytes.c _testlimitedcapi/codec.c _testlimitedcapi/complex.c _testlimitedcapi/dict.c _testlimitedcapi/eval.c _testlimitedcapi/float.c _testlimitedcapi/heaptype_relative.c _testlimitedcapi/import.c _testlimitedcapi/list.c _testlimitedcapi/long.c _testlimitedcapi/object.c _testlimitedcapi/pyos.c _testlimitedcapi/set.c _testlimitedcapi/sys.c _testlimitedcapi/tuple.c _testlimitedcapi/unicode.c _testlimitedcapi/vectorcall_limited.c _testlimitedcapi/version.c
|
||||
@MODULE__TESTLIMITEDCAPI_TRUE@_testlimitedcapi _testlimitedcapi.c _testlimitedcapi/abstract.c _testlimitedcapi/bytearray.c _testlimitedcapi/bytes.c _testlimitedcapi/codec.c _testlimitedcapi/complex.c _testlimitedcapi/dict.c _testlimitedcapi/eval.c _testlimitedcapi/float.c _testlimitedcapi/heaptype_relative.c _testlimitedcapi/import.c _testlimitedcapi/list.c _testlimitedcapi/long.c _testlimitedcapi/object.c _testlimitedcapi/pyos.c _testlimitedcapi/set.c _testlimitedcapi/sys.c _testlimitedcapi/tuple.c _testlimitedcapi/unicode.c _testlimitedcapi/vectorcall_limited.c _testlimitedcapi/version.c _testlimitedcapi/file.c
|
||||
@MODULE__TESTCLINIC_TRUE@_testclinic _testclinic.c
|
||||
@MODULE__TESTCLINIC_LIMITED_TRUE@_testclinic_limited _testclinic_limited.c
|
||||
|
||||
|
|
29
Modules/_testcapi/clinic/file.c.h
generated
29
Modules/_testcapi/clinic/file.c.h
generated
|
@ -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]*/
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -89,5 +89,8 @@ PyInit__testlimitedcapi(void)
|
|||
if (_PyTestLimitedCAPI_Init_Version(mod) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
if (_PyTestLimitedCAPI_Init_File(mod) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
return mod;
|
||||
}
|
||||
|
|
81
Modules/_testlimitedcapi/clinic/file.c.h
generated
Normal file
81
Modules/_testlimitedcapi/clinic/file.c.h
generated
Normal file
|
@ -0,0 +1,81 @@
|
|||
/*[clinic input]
|
||||
preserve
|
||||
[clinic start generated code]*/
|
||||
|
||||
PyDoc_STRVAR(_testcapi_pyfile_getline__doc__,
|
||||
"pyfile_getline($module, file, n, /)\n"
|
||||
"--\n"
|
||||
"\n");
|
||||
|
||||
#define _TESTCAPI_PYFILE_GETLINE_METHODDEF \
|
||||
{"pyfile_getline", (PyCFunction)(void(*)(void))_testcapi_pyfile_getline, METH_FASTCALL, _testcapi_pyfile_getline__doc__},
|
||||
|
||||
static PyObject *
|
||||
_testcapi_pyfile_getline_impl(PyObject *module, PyObject *file, int n);
|
||||
|
||||
static PyObject *
|
||||
_testcapi_pyfile_getline(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
|
||||
{
|
||||
PyObject *return_value = NULL;
|
||||
PyObject *file;
|
||||
int n;
|
||||
|
||||
if (nargs != 2) {
|
||||
PyErr_Format(PyExc_TypeError, "pyfile_getline expected 2 arguments, got %zd", nargs);
|
||||
goto exit;
|
||||
}
|
||||
file = args[0];
|
||||
n = PyLong_AsInt(args[1]);
|
||||
if (n == -1 && PyErr_Occurred()) {
|
||||
goto exit;
|
||||
}
|
||||
return_value = _testcapi_pyfile_getline_impl(module, file, n);
|
||||
|
||||
exit:
|
||||
return return_value;
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(_testcapi_pyfile_writeobject__doc__,
|
||||
"pyfile_writeobject($module, obj, file, flags, /)\n"
|
||||
"--\n"
|
||||
"\n");
|
||||
|
||||
#define _TESTCAPI_PYFILE_WRITEOBJECT_METHODDEF \
|
||||
{"pyfile_writeobject", (PyCFunction)(void(*)(void))_testcapi_pyfile_writeobject, METH_FASTCALL, _testcapi_pyfile_writeobject__doc__},
|
||||
|
||||
static PyObject *
|
||||
_testcapi_pyfile_writeobject_impl(PyObject *module, PyObject *obj,
|
||||
PyObject *file, int flags);
|
||||
|
||||
static PyObject *
|
||||
_testcapi_pyfile_writeobject(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
|
||||
{
|
||||
PyObject *return_value = NULL;
|
||||
PyObject *obj;
|
||||
PyObject *file;
|
||||
int flags;
|
||||
|
||||
if (nargs != 3) {
|
||||
PyErr_Format(PyExc_TypeError, "pyfile_writeobject expected 3 arguments, got %zd", nargs);
|
||||
goto exit;
|
||||
}
|
||||
obj = args[0];
|
||||
file = args[1];
|
||||
flags = PyLong_AsInt(args[2]);
|
||||
if (flags == -1 && PyErr_Occurred()) {
|
||||
goto exit;
|
||||
}
|
||||
return_value = _testcapi_pyfile_writeobject_impl(module, obj, file, flags);
|
||||
|
||||
exit:
|
||||
return return_value;
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(_testcapi_pyobject_asfiledescriptor__doc__,
|
||||
"pyobject_asfiledescriptor($module, obj, /)\n"
|
||||
"--\n"
|
||||
"\n");
|
||||
|
||||
#define _TESTCAPI_PYOBJECT_ASFILEDESCRIPTOR_METHODDEF \
|
||||
{"pyobject_asfiledescriptor", (PyCFunction)_testcapi_pyobject_asfiledescriptor, METH_O, _testcapi_pyobject_asfiledescriptor__doc__},
|
||||
/*[clinic end generated code: output=ea572aaaa01aec7b input=a9049054013a1b77]*/
|
128
Modules/_testlimitedcapi/file.c
Normal file
128
Modules/_testlimitedcapi/file.c
Normal file
|
@ -0,0 +1,128 @@
|
|||
#include "pyconfig.h" // Py_GIL_DISABLED
|
||||
#ifndef Py_GIL_DISABLED
|
||||
// Need limited C API 3.13 for PyLong_AsInt()
|
||||
# define Py_LIMITED_API 0x030d0000
|
||||
#endif
|
||||
|
||||
#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));
|
||||
}
|
||||
|
||||
|
||||
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
|
||||
{NULL},
|
||||
};
|
||||
|
||||
int
|
||||
_PyTestLimitedCAPI_Init_File(PyObject *m)
|
||||
{
|
||||
return PyModule_AddFunctions(m, test_methods);
|
||||
}
|
|
@ -42,5 +42,6 @@ int _PyTestLimitedCAPI_Init_Tuple(PyObject *module);
|
|||
int _PyTestLimitedCAPI_Init_Unicode(PyObject *module);
|
||||
int _PyTestLimitedCAPI_Init_VectorcallLimited(PyObject *module);
|
||||
int _PyTestLimitedCAPI_Init_Version(PyObject *module);
|
||||
int _PyTestLimitedCAPI_Init_File(PyObject *module);
|
||||
|
||||
#endif // Py_TESTLIMITEDCAPI_PARTS_H
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue