https://github.com/python/cpython/commit/9f13436ef002038aa9a3007462ad01045fafa48b
commit: 9f13436ef002038aa9a3007462ad01045fafa48b
branch: 3.15
author: Miss Islington (bot) <[email protected]>
committer: gpshead <[email protected]>
date: 2026-10-20T00:18:08Z
summary:

[3.15] gh-159739: Release replaced PyInitConfig strings (GH-159769) (#159809)

gh-159739: Release replaced PyInitConfig strings (GH-159769)

* gh-159739: Release replaced PyInitConfig strings

Backport to 3.15 and 3.14.

---------
(cherry picked from commit 395d766d4f44e45ca4e9053b4761f115de122317)

Co-authored-by: Jelle Zijlstra <[email protected]>
Co-authored-by: Jelle Zijlstra <[email protected]>

files:
A Misc/NEWS.d/next/C_API/2026-10-19-20-38-17.gh-issue-159739.eb9cdc.rst
M Python/initconfig.c

diff --git 
a/Misc/NEWS.d/next/C_API/2026-10-19-20-38-17.gh-issue-159739.eb9cdc.rst 
b/Misc/NEWS.d/next/C_API/2026-10-19-20-38-17.gh-issue-159739.eb9cdc.rst
new file mode 100644
index 00000000000000..e3ca598d37fdce
--- /dev/null
+++ b/Misc/NEWS.d/next/C_API/2026-10-19-20-38-17.gh-issue-159739.eb9cdc.rst
@@ -0,0 +1,2 @@
+:c:func:`PyInitConfig_SetStr` now releases a previously configured value and
+preserves it if conversion of the replacement fails.
diff --git a/Python/initconfig.c b/Python/initconfig.c
index c95df5b6ef2158..e21cbaaca5b89b 100644
--- a/Python/initconfig.c
+++ b/Python/initconfig.c
@@ -4357,14 +4357,17 @@ PyInitConfig_SetStr(PyInitConfig *config, const char 
*name, const char* value)
 
     if (value == NULL && spec->type != PyConfig_MEMBER_WSTR_OPT) {
         initconfig_set_error(config, "config option string cannot be NULL");
+        return -1;
     }
 
     wchar_t **member = raw_member;
 
-    *member = utf8_to_wstr(config, value);
-    if (*member == NULL) {
+    wchar_t *wstr = utf8_to_wstr(config, value);
+    if (wstr == NULL) {
         return -1;
     }
+    free(*member);
+    *member = wstr;
     return 0;
 }
 

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to