From 7cb30ceb070f8bc8682e245b4b9446f244664c20 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Fri, 19 Dec 2025 21:57:36 -0800 Subject: [PATCH] Add comment explaining error_scope usage Clarify why error_scope is used: to preserve any existing Python error state that might be cleared or modified by dict_getitemstringref. --- include/pybind11/gil_safe_call_once.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/include/pybind11/gil_safe_call_once.h b/include/pybind11/gil_safe_call_once.h index 6ac6af4af..083018f95 100644 --- a/include/pybind11/gil_safe_call_once.h +++ b/include/pybind11/gil_safe_call_once.h @@ -236,6 +236,9 @@ private: } static call_once_storage_map_type *get_or_create_call_once_storage_map() { + // Preserve any existing Python error state. dict_getitemstringref may clear + // errors or set new ones when the key is not found; we restore the original + // error state when this scope exits. error_scope err_scope; dict state_dict = detail::get_python_state_dict(); auto storage_map_obj = reinterpret_steal(