-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
gh-132162: tests for py_universalnewlinefgets #132164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,8 @@ | |
#include "parts.h" | ||
#include "util.h" | ||
#include "clinic/file.c.h" | ||
#include <stdio.h> | ||
#include <Python.h> | ||
|
||
|
||
/*[clinic input] | ||
|
@@ -57,9 +59,61 @@ _testcapi_py_fopen_impl(PyObject *module, PyObject *path, const char *mode, | |
} | ||
|
||
|
||
/*[clinic input] | ||
_testcapi.py_universalnewlinefgets | ||
|
||
file: object | ||
size: int | ||
/ | ||
|
||
Read a line from a file using Py_UniversalNewlineFgets. | ||
[clinic start generated code]*/ | ||
|
||
static PyObject * | ||
_testcapi_py_universalnewlinefgets_impl(PyObject *module, PyObject *file, | ||
int size) | ||
/*[clinic end generated code: output=2ce1bc76c9dc871c input=02c236049d18569a]*/ | ||
{ | ||
int fd = PyObject_AsFileDescriptor(file); | ||
if (fd == -1) { | ||
return NULL; | ||
} | ||
|
||
FILE *fp; | ||
#ifdef MS_WINDOWS | ||
fp = _fdopen(fd, "rb"); | ||
#else | ||
fp = fdopen(fd, "rb"); | ||
#endif | ||
|
||
if (fp == NULL) { | ||
PyErr_SetFromErrno(PyExc_OSError); | ||
return NULL; | ||
} | ||
|
||
char *buf = (char *)PyMem_Malloc(size); | ||
if (buf == NULL) { | ||
fclose(fp); | ||
return PyErr_NoMemory(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fp needs to be released here: #ifdef MS_WINDOWS
_close(fd);
#else
close(fd)
#endif |
||
} | ||
|
||
char *result = Py_UniversalNewlineFgets(buf, size, fp, NULL); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fp can now be closed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. on
|
||
if (result == NULL) { | ||
PyMem_Free(buf); | ||
fclose(fp); | ||
Py_RETURN_NONE; | ||
} | ||
|
||
PyObject *line = PyBytes_FromString(result); | ||
PyMem_Free(buf); | ||
|
||
return line; | ||
} | ||
|
||
static PyMethodDef test_methods[] = { | ||
_TESTCAPI_PYFILE_NEWSTDPRINTER_METHODDEF | ||
_TESTCAPI_PY_FOPEN_METHODDEF | ||
_TESTCAPI_PY_UNIVERSALNEWLINEFGETS_METHODDEF | ||
{NULL}, | ||
}; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should return NULL