summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2023-08-21 12:33:46 (GMT)
committerGitHub <noreply@github.com>2023-08-21 12:33:46 (GMT)
commit238c8d236bca2fab43453d09af69eba2bab1fcb0 (patch)
treeef1251a15bcc6294991ed60538295560523c1b57
parent1ce8b92ce92e9a44ac804ea4e4c61d08d7e89a2e (diff)
downloadcpython-238c8d236bca2fab43453d09af69eba2bab1fcb0.zip
cpython-238c8d236bca2fab43453d09af69eba2bab1fcb0.tar.gz
cpython-238c8d236bca2fab43453d09af69eba2bab1fcb0.tar.bz2
[3.12] gh-107916: Save the error code before decoding the filename in PyErr_SetFromErrnoWithFilename() etc (GH-107929) (#108205)
gh-107916: Save the error code before decoding the filename in PyErr_SetFromErrnoWithFilename() etc (GH-107929) (cherry picked from commit 80bdebdd8593f007a2232ec04a7729bba6ebf12c) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
-rw-r--r--Misc/NEWS.d/next/C API/2023-08-14-10-59-03.gh-issue-107916.KH4Muo.rst4
-rw-r--r--Python/errors.c8
2 files changed, 12 insertions, 0 deletions
diff --git a/Misc/NEWS.d/next/C API/2023-08-14-10-59-03.gh-issue-107916.KH4Muo.rst b/Misc/NEWS.d/next/C API/2023-08-14-10-59-03.gh-issue-107916.KH4Muo.rst
new file mode 100644
index 0000000..f1f1660
--- /dev/null
+++ b/Misc/NEWS.d/next/C API/2023-08-14-10-59-03.gh-issue-107916.KH4Muo.rst
@@ -0,0 +1,4 @@
+C API functions :c:func:`PyErr_SetFromErrnoWithFilename`,
+:c:func:`PyErr_SetExcFromWindowsErrWithFilename` and
+:c:func:`PyErr_SetFromWindowsErrWithFilename` save now the error code before
+calling :c:func:`PyUnicode_DecodeFSDefault`.
diff --git a/Python/errors.c b/Python/errors.c
index f1d3007..6c46d1f 100644
--- a/Python/errors.c
+++ b/Python/errors.c
@@ -919,10 +919,12 @@ PyErr_SetFromErrnoWithFilename(PyObject *exc, const char *filename)
{
PyObject *name = NULL;
if (filename) {
+ int i = errno;
name = PyUnicode_DecodeFSDefault(filename);
if (name == NULL) {
return NULL;
}
+ errno = i;
}
PyObject *result = PyErr_SetFromErrnoWithFilenameObjects(exc, name, NULL);
Py_XDECREF(name);
@@ -1022,6 +1024,9 @@ PyObject *PyErr_SetExcFromWindowsErrWithFilename(
{
PyObject *name = NULL;
if (filename) {
+ if ((DWORD)ierr == 0) {
+ ierr = (int)GetLastError();
+ }
name = PyUnicode_DecodeFSDefault(filename);
if (name == NULL) {
return NULL;
@@ -1052,6 +1057,9 @@ PyObject *PyErr_SetFromWindowsErrWithFilename(
{
PyObject *name = NULL;
if (filename) {
+ if ((DWORD)ierr == 0) {
+ ierr = (int)GetLastError();
+ }
name = PyUnicode_DecodeFSDefault(filename);
if (name == NULL) {
return NULL;