https://github.com/python/cpython/commit/9578288a3e5a7f42d1f3bec139c0c85b87775c90
commit: 9578288a3e5a7f42d1f3bec139c0c85b87775c90
branch: main
author: Steve Dower <steve.do...@python.org>
committer: zooba <steve.do...@microsoft.com>
date: 2024-02-28T13:58:25Z
summary:

gh-116012: Preserve GetLastError() across calls to TlsGetValue on Windows 
(GH-116014)

files:
A Misc/NEWS.d/next/Windows/2024-02-27-23-21-55.gh-issue-116012.B9_IwM.rst
M Python/pystate.c
M Python/thread_nt.h

diff --git 
a/Misc/NEWS.d/next/Windows/2024-02-27-23-21-55.gh-issue-116012.B9_IwM.rst 
b/Misc/NEWS.d/next/Windows/2024-02-27-23-21-55.gh-issue-116012.B9_IwM.rst
new file mode 100644
index 00000000000000..a55e5b1c7b566d
--- /dev/null
+++ b/Misc/NEWS.d/next/Windows/2024-02-27-23-21-55.gh-issue-116012.B9_IwM.rst
@@ -0,0 +1 @@
+Ensure the value of ``GetLastError()`` is preserved across GIL operations.
diff --git a/Python/pystate.c b/Python/pystate.c
index a80c1b7fb9c866..a370fff857af85 100644
--- a/Python/pystate.c
+++ b/Python/pystate.c
@@ -2528,16 +2528,7 @@ PyGILState_Check(void)
         return 0;
     }
 
-#ifdef MS_WINDOWS
-    int err = GetLastError();
-#endif
-
     PyThreadState *tcur = gilstate_tss_get(runtime);
-
-#ifdef MS_WINDOWS
-    SetLastError(err);
-#endif
-
     return (tstate == tcur);
 }
 
diff --git a/Python/thread_nt.h b/Python/thread_nt.h
index 7922b2d7e84845..9dca833ff203ca 100644
--- a/Python/thread_nt.h
+++ b/Python/thread_nt.h
@@ -513,5 +513,10 @@ void *
 PyThread_tss_get(Py_tss_t *key)
 {
     assert(key != NULL);
-    return TlsGetValue(key->_key);
+    int err = GetLastError();
+    void *r = TlsGetValue(key->_key);
+    if (r || !GetLastError()) {
+        SetLastError(err);
+    }
+    return r;
 }

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: arch...@mail-archive.com

Reply via email to