[Python-checkins] [3.14] gh-138756: Fix memory leak in PyInitConfig_Free() (GH-138759) (#138785)

2025-10-11 Thread vstinner
https://github.com/python/cpython/commit/0accda746823e4baa30b1cf10d25317a70e556c0
commit: 0accda746823e4baa30b1cf10d25317a70e556c0
branch: 3.14
author: Miss Islington (bot) <[email protected]>
committer: vstinner 
date: 2025-10-07T18:42:50+02:00
summary:

[3.14] gh-138756: Fix memory leak in PyInitConfig_Free() (GH-138759) (#138785)

gh-138756: Fix memory leak in PyInitConfig_Free() (GH-138759)

Clear also memory of PyConfig members.
(cherry picked from commit 96dee64c73531325daa9e048b3c18212f5eadd98)

Co-authored-by: Victor Stinner 

files:
M Python/initconfig.c

diff --git a/Python/initconfig.c b/Python/initconfig.c
index 62bcb727d1b9bf..a7e0fcb42f6b33 100644
--- a/Python/initconfig.c
+++ b/Python/initconfig.c
@@ -239,9 +239,11 @@ static const PyConfigSpec PYPRECONFIG_SPEC[] = {
 
 
 // Forward declarations
-static PyObject*
-config_get(const PyConfig *config, const PyConfigSpec *spec,
-   int use_sys);
+static PyObject* config_get(const PyConfig *config, const PyConfigSpec *spec,
+int use_sys);
+static void initconfig_free_wstr(wchar_t *member);
+static void initconfig_free_wstr_list(PyWideStringList *list);
+static void initconfig_free_config(const PyConfig *config);
 
 
 /* --- Command line options --- */
@@ -3725,6 +3727,8 @@ PyInitConfig_Free(PyInitConfig *config)
 if (config == NULL) {
 return;
 }
+
+initconfig_free_config(&config->config);
 free(config->err_msg);
 free(config);
 }
@@ -4093,13 +4097,51 @@ PyInitConfig_SetStr(PyInitConfig *config, const char 
*name, const char* value)
 }
 
 
+static void
+initconfig_free_wstr(wchar_t *member)
+{
+if (member) {
+free(member);
+}
+}
+
+
+static void
+initconfig_free_wstr_list(PyWideStringList *list)
+{
+for (Py_ssize_t i = 0; i < list->length; i++) {
+free(list->items[i]);
+}
+free(list->items);
+}
+
+
+static void
+initconfig_free_config(const PyConfig *config)
+{
+const PyConfigSpec *spec = PYCONFIG_SPEC;
+for (; spec->name != NULL; spec++) {
+void *member = config_get_spec_member(config, spec);
+if (spec->type == PyConfig_MEMBER_WSTR
+|| spec->type == PyConfig_MEMBER_WSTR_OPT)
+{
+wchar_t *wstr = *(wchar_t **)member;
+initconfig_free_wstr(wstr);
+}
+else if (spec->type == PyConfig_MEMBER_WSTR_LIST) {
+initconfig_free_wstr_list(member);
+}
+}
+}
+
+
 static int
-_PyWideStringList_FromUTF8(PyInitConfig *config, PyWideStringList *list,
-   Py_ssize_t length, char * const *items)
+initconfig_set_str_list(PyInitConfig *config, PyWideStringList *list,
+Py_ssize_t length, char * const *items)
 {
 PyWideStringList wlist = _PyWideStringList_INIT;
 size_t size = sizeof(wchar_t*) * length;
-wlist.items = (wchar_t **)PyMem_RawMalloc(size);
+wlist.items = (wchar_t **)malloc(size);
 if (wlist.items == NULL) {
 config->status = _PyStatus_NO_MEMORY();
 return -1;
@@ -4108,14 +4150,14 @@ _PyWideStringList_FromUTF8(PyInitConfig *config, 
PyWideStringList *list,
 for (Py_ssize_t i = 0; i < length; i++) {
 wchar_t *arg = utf8_to_wstr(config, items[i]);
 if (arg == NULL) {
-_PyWideStringList_Clear(&wlist);
+initconfig_free_wstr_list(&wlist);
 return -1;
 }
 wlist.items[i] = arg;
 wlist.length++;
 }
 
-_PyWideStringList_Clear(list);
+initconfig_free_wstr_list(list);
 *list = wlist;
 return 0;
 }
@@ -4136,7 +4178,7 @@ PyInitConfig_SetStrList(PyInitConfig *config, const char 
*name,
 return -1;
 }
 PyWideStringList *list = raw_member;
-if (_PyWideStringList_FromUTF8(config, list, length, items) < 0) {
+if (initconfig_set_str_list(config, list, length, items) < 0) {
 return -1;
 }
 

___
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]


[Python-checkins] gh-139845: do not print twice in default asyncio REPL (#139846)

2025-10-11 Thread kumaraditya303
https://github.com/python/cpython/commit/a310b3a99d05e107963023a5736b67afe4ae1968
commit: a310b3a99d05e107963023a5736b67afe4ae1968
branch: main
author: yihong 
committer: kumaraditya303 
date: 2025-10-09T15:24:52Z
summary:

gh-139845: do not print twice in default asyncio REPL (#139846)

Co-authored-by: Kumar Aditya 

files:
A Misc/NEWS.d/next/Library/2025-10-09-21-37-20.gh-issue-139845.dzx5UP.rst
M Lib/asyncio/__main__.py

diff --git a/Lib/asyncio/__main__.py b/Lib/asyncio/__main__.py
index 12d6f7714ee4f4..10bfca3cf96b3e 100644
--- a/Lib/asyncio/__main__.py
+++ b/Lib/asyncio/__main__.py
@@ -74,7 +74,8 @@ def callback():
 return
 except BaseException:
 if keyboard_interrupted:
-self.write("\nKeyboardInterrupt\n")
+if not CAN_USE_PYREPL:
+self.write("\nKeyboardInterrupt\n")
 else:
 self.showtraceback()
 return self.STATEMENT_FAILED
diff --git 
a/Misc/NEWS.d/next/Library/2025-10-09-21-37-20.gh-issue-139845.dzx5UP.rst 
b/Misc/NEWS.d/next/Library/2025-10-09-21-37-20.gh-issue-139845.dzx5UP.rst
new file mode 100644
index 00..3cd294e49cdda3
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2025-10-09-21-37-20.gh-issue-139845.dzx5UP.rst
@@ -0,0 +1 @@
+Fix to not print KeyboardInterrupt twice in default asyncio REPL.

___
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]


[Python-checkins] gh-139929: fix incorrect OpenSSL version-based guard in `_ssl.c` (GH-139945)

2025-10-11 Thread gpshead
https://github.com/python/cpython/commit/cdd3eee7fc26538c8365dcbf2dd844ec7cdf7fb7
commit: cdd3eee7fc26538c8365dcbf2dd844ec7cdf7fb7
branch: main
author: Bénédikt Tran <[email protected]>
committer: gpshead <[email protected]>
date: 2025-10-11T10:34:08-07:00
summary:

gh-139929: fix incorrect OpenSSL version-based guard in `_ssl.c` (GH-139945)

fix OpenSSL version-based guards

files:
M Modules/_ssl.c

diff --git a/Modules/_ssl.c b/Modules/_ssl.c
index 1fa44ef1de4e09..4b75e455f402ff 100644
--- a/Modules/_ssl.c
+++ b/Modules/_ssl.c
@@ -937,7 +937,7 @@ newPySSLSocket(PySSLContext *sslctx, PySocketSockObject 
*sock,
 }
 
 /* bpo43522 and OpenSSL < 1.1.1l: copy hostflags manually */
-#if OPENSSL_VERSION < 0x101010cf
+#if OPENSSL_VERSION_NUMBER < 0x101010cf
 X509_VERIFY_PARAM *ssl_verification_params = SSL_get0_param(self->ssl);
 X509_VERIFY_PARAM *ssl_ctx_verification_params = SSL_CTX_get0_param(ctx);
 

___
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]


[Python-checkins] gh-139935: do not skip test on real errors in `os.getlogin` (#139953)

2025-10-11 Thread picnixz
https://github.com/python/cpython/commit/2eb32add92d553b320850975442fa2e55addc377
commit: 2eb32add92d553b320850975442fa2e55addc377
branch: main
author: Bénédikt Tran <[email protected]>
committer: picnixz <[email protected]>
date: 2025-10-11T16:31:34+02:00
summary:

gh-139935: do not skip test on real errors in `os.getlogin` (#139953)

files:
M Lib/test/test_os/test_os.py

diff --git a/Lib/test/test_os/test_os.py b/Lib/test/test_os/test_os.py
index 95b175db6fcdcb..e074858fe2ad99 100644
--- a/Lib/test/test_os/test_os.py
+++ b/Lib/test/test_os/test_os.py
@@ -3204,13 +3204,7 @@ def test_getlogin(self):
 user_name = os.getlogin()
 except OSError as exc:
 # See https://man7.org/linux/man-pages/man3/getlogin.3.html#ERRORS.
-allowed_errors = (
-# defined by POSIX
-errno.EMFILE, errno.ENFILE, errno.ENXIO, errno.ERANGE,
-# defined by Linux/glibc
-errno.ENOENT, errno.ENOMEM, errno.ENOTTY,
-)
-if exc.errno in allowed_errors:
+if exc.errno in (errno.ENXIO, errno.ENOENT, errno.ENOTTY):
 self.skipTest(str(exc))
 else:
 raise

___
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]


[Python-checkins] [3.13] gh-139905: Provide suggestion in error message if `Generic.__init_subclass__` was not called (GH-139943) (#139956)

2025-10-11 Thread AlexWaygood
https://github.com/python/cpython/commit/cbb415e992df78e92244caa0611d2d7952f1719d
commit: cbb415e992df78e92244caa0611d2d7952f1719d
branch: 3.13
author: Miss Islington (bot) <[email protected]>
committer: AlexWaygood 
date: 2025-10-11T15:36:44Z
summary:

[3.13] gh-139905: Provide suggestion in error message if 
`Generic.__init_subclass__` was not called (GH-139943) (#139956)

gh-139905: Provide suggestion in error message if `Generic.__init_subclass__` 
was not called (GH-139943)
(cherry picked from commit 5776d0d2e08f4d93dcd62d875bae8c1396a04ba4)

Co-authored-by: Stan Ulbrych <[email protected]>

files:
A Misc/NEWS.d/next/Library/2025-10-11-10-02-56.gh-issue-139905.UyJIR_.rst
M Lib/test/test_typing.py
M Lib/typing.py

diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index 5f49bf757096f0..fb4cf26982d3b5 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -4582,6 +4582,34 @@ class D(Generic[T]): pass
 with self.assertRaises(TypeError):
 D[()]
 
+def test_generic_init_subclass_not_called_error(self):
+notes = ["Note: this exception may have been caused by "
+ 
r"'GenericTests.test_generic_init_subclass_not_called_error..Base.__init_subclass__'
 "
+ "(or the '__init_subclass__' method on a superclass) not 
calling 'super().__init_subclass__()'"]
+
+class Base:
+def __init_subclass__(cls) -> None:
+# Oops, I forgot super().__init_subclass__()!
+pass
+
+with self.subTest():
+class Sub(Base, Generic[T]):
+pass
+
+with self.assertRaises(AttributeError) as cm:
+Sub[int]
+
+self.assertEqual(cm.exception.__notes__, notes)
+
+with self.subTest():
+class Sub[U](Base):
+pass
+
+with self.assertRaises(AttributeError) as cm:
+Sub[int]
+
+self.assertEqual(cm.exception.__notes__, notes)
+
 def test_generic_subclass_checks(self):
 for typ in [list[int], List[int],
 tuple[int, str], Tuple[int, str],
diff --git a/Lib/typing.py b/Lib/typing.py
index f9141640997933..95e469c4fff74a 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -1236,14 +1236,26 @@ def _generic_class_getitem(cls, args):
 f"Parameters to {cls.__name__}[...] must all be unique")
 else:
 # Subscripting a regular Generic subclass.
-for param in cls.__parameters__:
+try:
+parameters = cls.__parameters__
+except AttributeError as e:
+init_subclass = getattr(cls, '__init_subclass__', None)
+if init_subclass not in {None, Generic.__init_subclass__}:
+e.add_note(
+f"Note: this exception may have been caused by "
+f"{init_subclass.__qualname__!r} (or the "
+f"'__init_subclass__' method on a superclass) not "
+f"calling 'super().__init_subclass__()'"
+)
+raise
+for param in parameters:
 prepare = getattr(param, '__typing_prepare_subst__', None)
 if prepare is not None:
 args = prepare(cls, args)
 _check_generic_specialization(cls, args)
 
 new_args = []
-for param, new_arg in zip(cls.__parameters__, args):
+for param, new_arg in zip(parameters, args):
 if isinstance(param, TypeVarTuple):
 new_args.extend(new_arg)
 else:
diff --git 
a/Misc/NEWS.d/next/Library/2025-10-11-10-02-56.gh-issue-139905.UyJIR_.rst 
b/Misc/NEWS.d/next/Library/2025-10-11-10-02-56.gh-issue-139905.UyJIR_.rst
new file mode 100644
index 00..a6876ca2df8299
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2025-10-11-10-02-56.gh-issue-139905.UyJIR_.rst
@@ -0,0 +1,3 @@
+Add suggestion to error message for :class:`typing.Generic` subclasses when
+``cls.__parameters__`` is missing due to a parent class failing to call
+:meth:`super().__init_subclass__() ` in its 
``__init_subclass__``.

___
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]