Skip to content

Commit c91aa82

Browse files
committed
Rewrite the Windows implementation
1 parent 8241422 commit c91aa82

File tree

1 file changed

+38
-40
lines changed

1 file changed

+38
-40
lines changed

Python/fileutils.c

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1776,54 +1776,52 @@ Py_fopen(PyObject *path, const char *mode)
17761776
int async_err = 0;
17771777
int saved_errno;
17781778
#ifdef MS_WINDOWS
1779-
PyObject *fspath = PyOS_FSPath(path);
1780-
if (fspath == NULL) {
1779+
PyObject *unicode;
1780+
if (!PyUnicode_FSDecoder(path, &unicode)) {
17811781
return NULL;
17821782
}
1783-
Py_SETREF(path, fspath);
17841783

1785-
if (PyUnicode_Check(path)) {
1786-
wchar_t *wpath = PyUnicode_AsWideCharString(path, NULL);
1787-
if (wpath == NULL) {
1788-
return NULL;
1789-
}
1790-
1791-
wchar_t wmode[10];
1792-
int usize = MultiByteToWideChar(CP_ACP, 0, mode, -1,
1793-
wmode, Py_ARRAY_LENGTH(wmode));
1794-
if (usize == 0) {
1795-
PyErr_SetFromWindowsErr(0);
1796-
PyMem_Free(wpath);
1797-
return NULL;
1798-
}
1784+
wchar_t *wpath = PyUnicode_AsWideCharString(unicode, NULL);
1785+
Py_DECREF(unicode);
1786+
if (wpath == NULL) {
1787+
return NULL;
1788+
}
17991789

1800-
do {
1801-
Py_BEGIN_ALLOW_THREADS
1802-
f = _wfopen(wpath, wmode);
1803-
Py_END_ALLOW_THREADS
1804-
} while (f == NULL
1805-
&& errno == EINTR && !(async_err = PyErr_CheckSignals()));
1806-
saved_errno = errno;
1790+
wchar_t wmode[10];
1791+
int usize = MultiByteToWideChar(CP_ACP, 0, mode, -1,
1792+
wmode, Py_ARRAY_LENGTH(wmode));
1793+
if (usize == 0) {
1794+
PyErr_SetFromWindowsErr(0);
18071795
PyMem_Free(wpath);
1796+
return NULL;
18081797
}
1809-
else
1810-
#endif
1811-
{
1812-
PyObject *bytes;
1813-
if (!PyUnicode_FSConverter(path, &bytes)) {
1814-
return NULL;
1815-
}
1816-
const char *path_bytes = PyBytes_AS_STRING(bytes);
18171798

1818-
do {
1819-
Py_BEGIN_ALLOW_THREADS
1820-
f = fopen(path_bytes, mode);
1821-
Py_END_ALLOW_THREADS
1822-
} while (f == NULL
1823-
&& errno == EINTR && !(async_err = PyErr_CheckSignals()));
1824-
saved_errno = errno;
1825-
Py_DECREF(bytes);
1799+
do {
1800+
Py_BEGIN_ALLOW_THREADS
1801+
_Py_BEGIN_SUPPRESS_IPH
1802+
f = _wfopen(wpath, wmode);
1803+
_Py_END_SUPPRESS_IPH
1804+
Py_END_ALLOW_THREADS
1805+
} while (f == NULL
1806+
&& errno == EINTR && !(async_err = PyErr_CheckSignals()));
1807+
saved_errno = errno;
1808+
PyMem_Free(wpath);
1809+
#else
1810+
PyObject *bytes;
1811+
if (!PyUnicode_FSConverter(path, &bytes)) {
1812+
return NULL;
18261813
}
1814+
const char *path_bytes = PyBytes_AS_STRING(bytes);
1815+
1816+
do {
1817+
Py_BEGIN_ALLOW_THREADS
1818+
f = fopen(path_bytes, mode);
1819+
Py_END_ALLOW_THREADS
1820+
} while (f == NULL
1821+
&& errno == EINTR && !(async_err = PyErr_CheckSignals()));
1822+
saved_errno = errno;
1823+
Py_DECREF(bytes);
1824+
#endif
18271825

18281826
if (async_err) {
18291827
return NULL;

0 commit comments

Comments
 (0)