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

Merged
merged 1 commit into from
Apr 24, 2025
Merged
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
23 changes: 22 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,28 @@ 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")

line = py_universalnewlinefgets(filename, 1000)
self.assertEqual(line, b"line1\n")

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

line = py_universalnewlinefgets(filename, 1000)
self.assertEqual(line, b"line2\n")

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

line = py_universalnewlinefgets(filename, 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.

41 changes: 41 additions & 0 deletions Modules/_testcapi/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,50 @@ _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]*/
{
FILE *fp = Py_fopen(file, "rb");
if (fp == NULL) {
return NULL;
}

char *buf = (char *)PyMem_Malloc(size);
if (buf == NULL) {
Py_fclose(fp);
return PyErr_NoMemory();
}

char *result = Py_UniversalNewlineFgets(buf, size, fp, NULL);
if (result == NULL) {
PyMem_Free(buf);
Py_fclose(fp);
Py_RETURN_NONE;
}

PyObject *line = PyBytes_FromString(result);
PyMem_Free(buf);
Py_fclose(fp);

return line;
}

static PyMethodDef test_methods[] = {
_TESTCAPI_PYFILE_NEWSTDPRINTER_METHODDEF
_TESTCAPI_PY_FOPEN_METHODDEF
_TESTCAPI_PY_UNIVERSALNEWLINEFGETS_METHODDEF
{NULL},
};

Expand Down
Loading