Issue #23908: os functions now reject paths with embedded null character
on Windows instead of silently truncate them.
Removed no longer used _PyUnicode_HasNULChars().
diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c
index 145d93a..0894ca4 100644
--- a/Modules/_io/fileio.c
+++ b/Modules/_io/fileio.c
@@ -281,15 +281,14 @@
#ifdef MS_WINDOWS
if (PyUnicode_Check(nameobj)) {
- int rv = _PyUnicode_HasNULChars(nameobj);
- if (rv) {
- if (rv != -1)
- PyErr_SetString(PyExc_ValueError, "embedded null character");
- return -1;
- }
- widename = PyUnicode_AsUnicode(nameobj);
+ Py_ssize_t length;
+ widename = PyUnicode_AsUnicodeAndSize(nameobj, &length);
if (widename == NULL)
return -1;
+ if (wcslen(widename) != length) {
+ PyErr_SetString(PyExc_ValueError, "embedded null character");
+ return -1;
+ }
} else
#endif
if (fd < 0)