mirror of
https://github.com/python/cpython.git
synced 2025-08-04 17:08:35 +00:00
gh-127350: Add Py_fopen() and Py_fclose() functions (#127821)
This commit is contained in:
parent
7e8c571604
commit
f89e5e20cb
18 changed files with 270 additions and 53 deletions
48
Modules/_testcapi/clinic/file.c.h
generated
Normal file
48
Modules/_testcapi/clinic/file.c.h
generated
Normal file
|
@ -0,0 +1,48 @@
|
|||
/*[clinic input]
|
||||
preserve
|
||||
[clinic start generated code]*/
|
||||
|
||||
#include "pycore_modsupport.h" // _PyArg_CheckPositional()
|
||||
|
||||
PyDoc_STRVAR(_testcapi_py_fopen__doc__,
|
||||
"py_fopen($module, path, mode, /)\n"
|
||||
"--\n"
|
||||
"\n"
|
||||
"Call Py_fopen(), fread(256) and Py_fclose(). Return read bytes.");
|
||||
|
||||
#define _TESTCAPI_PY_FOPEN_METHODDEF \
|
||||
{"py_fopen", _PyCFunction_CAST(_testcapi_py_fopen), METH_FASTCALL, _testcapi_py_fopen__doc__},
|
||||
|
||||
static PyObject *
|
||||
_testcapi_py_fopen_impl(PyObject *module, PyObject *path, const char *mode);
|
||||
|
||||
static PyObject *
|
||||
_testcapi_py_fopen(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
|
||||
{
|
||||
PyObject *return_value = NULL;
|
||||
PyObject *path;
|
||||
const char *mode;
|
||||
|
||||
if (!_PyArg_CheckPositional("py_fopen", nargs, 2, 2)) {
|
||||
goto exit;
|
||||
}
|
||||
path = args[0];
|
||||
if (!PyUnicode_Check(args[1])) {
|
||||
_PyArg_BadArgument("py_fopen", "argument 2", "str", args[1]);
|
||||
goto exit;
|
||||
}
|
||||
Py_ssize_t mode_length;
|
||||
mode = PyUnicode_AsUTF8AndSize(args[1], &mode_length);
|
||||
if (mode == NULL) {
|
||||
goto exit;
|
||||
}
|
||||
if (strlen(mode) != (size_t)mode_length) {
|
||||
PyErr_SetString(PyExc_ValueError, "embedded null character");
|
||||
goto exit;
|
||||
}
|
||||
return_value = _testcapi_py_fopen_impl(module, path, mode);
|
||||
|
||||
exit:
|
||||
return return_value;
|
||||
}
|
||||
/*[clinic end generated code: output=c9fe964c3e5a0c32 input=a9049054013a1b77]*/
|
|
@ -1,8 +1,43 @@
|
|||
// clinic/file.c.h uses internal pycore_modsupport.h API
|
||||
#define PYTESTCAPI_NEED_INTERNAL_API
|
||||
|
||||
#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]*/
|
||||
|
||||
/*[clinic input]
|
||||
_testcapi.py_fopen
|
||||
|
||||
path: object
|
||||
mode: str
|
||||
/
|
||||
|
||||
Call Py_fopen(), fread(256) and Py_fclose(). Return read bytes.
|
||||
[clinic start generated code]*/
|
||||
|
||||
static PyObject *
|
||||
_testcapi_py_fopen_impl(PyObject *module, PyObject *path, const char *mode)
|
||||
/*[clinic end generated code: output=5a900af000f759de input=d7e7b8f0fd151953]*/
|
||||
{
|
||||
FILE *fp = Py_fopen(path, mode);
|
||||
if (fp == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char buffer[256];
|
||||
size_t size = fread(buffer, 1, Py_ARRAY_LENGTH(buffer), fp);
|
||||
Py_fclose(fp);
|
||||
|
||||
return PyBytes_FromStringAndSize(buffer, size);
|
||||
}
|
||||
|
||||
static PyMethodDef test_methods[] = {
|
||||
_TESTCAPI_PY_FOPEN_METHODDEF
|
||||
{NULL},
|
||||
};
|
||||
|
||||
|
|
|
@ -15,7 +15,7 @@ call_pyobject_print(PyObject *self, PyObject * args)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
fp = _Py_fopen_obj(filename, "w+");
|
||||
fp = Py_fopen(filename, "w+");
|
||||
|
||||
if (Py_IsTrue(print_raw)) {
|
||||
flags = Py_PRINT_RAW;
|
||||
|
@ -41,7 +41,7 @@ pyobject_print_null(PyObject *self, PyObject *args)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
fp = _Py_fopen_obj(filename, "w+");
|
||||
fp = Py_fopen(filename, "w+");
|
||||
|
||||
if (PyObject_Print(NULL, fp, 0) < 0) {
|
||||
fclose(fp);
|
||||
|
@ -72,7 +72,7 @@ pyobject_print_noref_object(PyObject *self, PyObject *args)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
fp = _Py_fopen_obj(filename, "w+");
|
||||
fp = Py_fopen(filename, "w+");
|
||||
|
||||
if (PyObject_Print(test_string, fp, 0) < 0){
|
||||
fclose(fp);
|
||||
|
@ -103,7 +103,7 @@ pyobject_print_os_error(PyObject *self, PyObject *args)
|
|||
}
|
||||
|
||||
// open file in read mode to induce OSError
|
||||
fp = _Py_fopen_obj(filename, "r");
|
||||
fp = Py_fopen(filename, "r");
|
||||
|
||||
if (PyObject_Print(test_string, fp, 0) < 0) {
|
||||
fclose(fp);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue