Skip to content

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion Lib/test/test_capi/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,31 @@ def test_py_fopen(self):
# CRASHES py_fopen(NULL, 'rb')
# CRASHES py_fopen(__file__, NULL)

# TODO: Test Py_UniversalNewlineFgets()
def test_py_universalnewlinefgets(self):
py_universalnewlinefgets = _testcapi.py_universalnewlinefgets
filename = os_helper.TESTFN
self.addCleanup(os_helper.unlink, filename)

with open(filename, "wb") as fp:
fp.write(b"line1\nline2")

with open(filename, "rb") as fp:
line = py_universalnewlinefgets(fp, 1000)
self.assertEqual(line, b"line1\n")

with open(filename, "wb") as fp:
fp.write(b"line2\r\nline3")

with open(filename, "rb") as fp:
line = py_universalnewlinefgets(fp, 1000)
self.assertEqual(line, b"line2\n")

with open(filename, "wb") as fp:
fp.write(b"line3\rline4")

with open(filename, "rb") as fp:
line = py_universalnewlinefgets(fp, 1000)
self.assertEqual(line, b"line3\n")

# PyFile_SetOpenCodeHook() and PyFile_OpenCode() are tested by
# test_embed.test_open_code_hook()
Expand Down
36 changes: 35 additions & 1 deletion Modules/_testcapi/clinic/file.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 54 additions & 0 deletions Modules/_testcapi/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "parts.h"
#include "util.h"
#include "clinic/file.c.h"
#include <stdio.h>
#include <Python.h>


/*[clinic input]
Expand Down Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should return NULL

return NULL;
}

char *buf = (char *)PyMem_Malloc(size);
if (buf == NULL) {
fclose(fp);
return PyErr_NoMemory();
Copy link
Member

@picnixz picnixz Apr 12, 2025

Choose a reason for hiding this comment

The 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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fp can now be closed

Copy link
Contributor Author

@alex-semenyuk alex-semenyuk Apr 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on fclose(fp); it gives me

----------------------------------------------------------------------
Traceback (most recent call last):
  File ".../cpython/Lib/test/test_capi/test_file.py", line 305, in test_py_universalnewlinefgets
    with open(filename, "rb") as fp:
         ~~~~^^^^^^^^^^^^^^^^
OSError: [Errno 9] Bad file descriptor

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},
};

Expand Down
Loading