Skip to content

Commit 9578288

Browse files
authored
gh-116012: Preserve GetLastError() across calls to TlsGetValue on Windows (GH-116014)
1 parent 647053f commit 9578288

File tree

3 files changed

+7
-10
lines changed

3 files changed

+7
-10
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Ensure the value of ``GetLastError()`` is preserved across GIL operations.

Python/pystate.c

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2528,16 +2528,7 @@ PyGILState_Check(void)
25282528
return 0;
25292529
}
25302530

2531-
#ifdef MS_WINDOWS
2532-
int err = GetLastError();
2533-
#endif
2534-
25352531
PyThreadState *tcur = gilstate_tss_get(runtime);
2536-
2537-
#ifdef MS_WINDOWS
2538-
SetLastError(err);
2539-
#endif
2540-
25412532
return (tstate == tcur);
25422533
}
25432534

Python/thread_nt.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,5 +513,10 @@ void *
513513
PyThread_tss_get(Py_tss_t *key)
514514
{
515515
assert(key != NULL);
516-
return TlsGetValue(key->_key);
516+
int err = GetLastError();
517+
void *r = TlsGetValue(key->_key);
518+
if (r || !GetLastError()) {
519+
SetLastError(err);
520+
}
521+
return r;
517522
}

0 commit comments

Comments
 (0)