Skip to content
7 changes: 4 additions & 3 deletions Doc/library/asyncio-queue.rst
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,10 @@ Queue
raise :exc:`QueueShutDown`.

If *immediate* is true, the queue is terminated immediately.
The queue is drained to be completely empty. All callers of
:meth:`~Queue.join` are unblocked regardless of the number
of unfinished tasks. Blocked callers of :meth:`~Queue.get`
The queue is drained to be completely empty and the count
of unfinished tasks is reduced by the number of tasks drained.
If unfinished tasks is zero, callers of :meth:`~Queue.join`
are unblocked. Also, blocked callers of :meth:`~Queue.get`
are unblocked and will raise :exc:`QueueShutDown` because the
queue is empty.

Expand Down
7 changes: 4 additions & 3 deletions Doc/library/queue.rst
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,10 @@ until empty or terminated immediately with a hard shutdown.
raise :exc:`ShutDown`.

If *immediate* is true, the queue is terminated immediately.
The queue is drained to be completely empty. All callers of
:meth:`~Queue.join` are unblocked regardless of the number
of unfinished tasks. Blocked callers of :meth:`~Queue.get`
The queue is drained to be completely empty and the count
of unfinished tasks is reduced by the number of tasks drained.
If unfinished tasks is zero, callers of :meth:`~Queue.join`
are unblocked. Also, blocked callers of :meth:`~Queue.get`
are unblocked and will raise :exc:`ShutDown` because the
queue is empty.

Expand Down
11 changes: 11 additions & 0 deletions Doc/whatsnew/3.15.rst
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,17 @@ difflib
(Contributed by Jiahao Li in :gh:`134580`.)


hashlib
-------

* Ensure that hash functions guaranteed to be always *available* exist as
attributes of :mod:`hashlib` even if they will not work at runtime due to
missing backend implementations. For instance, ``hashlib.md5`` will no
longer raise :exc:`AttributeError` if OpenSSL is not available and Python
has been built without MD5 support.
(Contributed by Bénédikt Tran in :gh:`136929`.)


http.client
-----------

Expand Down
8 changes: 5 additions & 3 deletions Lib/asyncio/queues.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,11 @@ def shutdown(self, immediate=False):
By default, gets will only raise once the queue is empty. Set
'immediate' to True to make gets raise immediately instead.

All blocked callers of put() and get() will be unblocked. If
'immediate', unblock callers of join() regardless of the
number of unfinished tasks.
All blocked callers of put() and get() will be unblocked.

If 'immediate', the queue is drained and unfinished tasks
is reduced by the number of drained tasks. If unfinished tasks
is reduced to zero, callers of Queue.join are unblocked.
"""
self._is_shutdown = True
if immediate:
Expand Down
5 changes: 4 additions & 1 deletion Lib/doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def _test():

import __future__
import difflib
import functools
import inspect
import linecache
import os
Expand Down Expand Up @@ -1141,7 +1142,9 @@ def _find_lineno(self, obj, source_lines):
if inspect.ismethod(obj): obj = obj.__func__
if isinstance(obj, property):
obj = obj.fget
if inspect.isfunction(obj) and getattr(obj, '__doc__', None):
if isinstance(obj, functools.cached_property):
obj = obj.func
if inspect.isroutine(obj) and getattr(obj, '__doc__', None):
# We don't use `docstring` var here, because `obj` can be changed.
obj = inspect.unwrap(obj)
try:
Expand Down
31 changes: 27 additions & 4 deletions Lib/hashlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,16 +261,39 @@ def file_digest(fileobj, digest, /, *, _bufsize=2**18):
return digestobj


__logging = None
for __func_name in __always_supported:
# try them all, some may not work due to the OpenSSL
# version not supporting that algorithm.
try:
globals()[__func_name] = __get_hash(__func_name)
except ValueError:
import logging
logging.exception('code for hash %s was not found.', __func_name)

except ValueError as __exc:
import logging as __logging
__logging.error('hash algorithm %s will not be supported at runtime '
'[reason: %s]', __func_name, __exc)
# The following code can be simplified in Python 3.19
# once "string" is removed from the signature.
__code = f'''\
def {__func_name}(data=__UNSET, *, usedforsecurity=True, string=__UNSET):
if data is __UNSET and string is not __UNSET:
import warnings
warnings.warn(
"the 'string' keyword parameter is deprecated since "
"Python 3.15 and slated for removal in Python 3.19; "
"use the 'data' keyword parameter or pass the data "
"to hash as a positional argument instead",
DeprecationWarning, stacklevel=2)
if data is not __UNSET and string is not __UNSET:
raise TypeError("'data' and 'string' are mutually exclusive "
"and support for 'string' keyword parameter "
"is slated for removal in a future version.")
raise ValueError("unsupported hash algorithm {__func_name}")
'''
exec(__code, {"__UNSET": object()}, __locals := {})
globals()[__func_name] = __locals[__func_name]
del __exc, __code, __locals

# Cleanup locals()
del __always_supported, __func_name, __get_hash
del __py_new, __hash_new, __get_openssl_constructor
del __logging
8 changes: 5 additions & 3 deletions Lib/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,11 @@ def shutdown(self, immediate=False):
By default, gets will only raise once the queue is empty. Set
'immediate' to True to make gets raise immediately instead.

All blocked callers of put() and get() will be unblocked. If
'immediate', callers of join() are unblocked regardless of
the number of unfinished tasks.
All blocked callers of put() and get() will be unblocked.

If 'immediate', the queue is drained and unfinished tasks
is reduced by the number of drained tasks. If unfinished tasks
is reduced to zero, callers of Queue.join are unblocked.
'''
with self.mutex:
self.is_shutdown = True
Expand Down
29 changes: 29 additions & 0 deletions Lib/test/test_doctest/doctest_lineno.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,32 @@ def property_with_doctest(self):
@decorator
def func_with_docstring_wrapped():
"""Some unrelated info."""


# https://github.com/python/cpython/issues/136914
import functools


@functools.cache
def cached_func_with_doctest(value):
"""
>>> cached_func_with_doctest(1)
-1
"""
return -value


@functools.cache
def cached_func_without_docstring(value):
return value + 1


class ClassWithACachedProperty:

@functools.cached_property
def cached(self):
"""
>>> X().cached
-1
"""
return 0
4 changes: 4 additions & 0 deletions Lib/test/test_doctest/test_doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,8 @@ def basics(): r"""
>>> for t in tests:
... print('%5s %s' % (t.lineno, t.name))
None test.test_doctest.doctest_lineno
None test.test_doctest.doctest_lineno.ClassWithACachedProperty
102 test.test_doctest.doctest_lineno.ClassWithACachedProperty.cached
22 test.test_doctest.doctest_lineno.ClassWithDocstring
30 test.test_doctest.doctest_lineno.ClassWithDoctest
None test.test_doctest.doctest_lineno.ClassWithoutDocstring
Expand All @@ -687,6 +689,8 @@ def basics(): r"""
45 test.test_doctest.doctest_lineno.MethodWrapper.method_with_doctest
None test.test_doctest.doctest_lineno.MethodWrapper.method_without_docstring
61 test.test_doctest.doctest_lineno.MethodWrapper.property_with_doctest
86 test.test_doctest.doctest_lineno.cached_func_with_doctest
None test.test_doctest.doctest_lineno.cached_func_without_docstring
4 test.test_doctest.doctest_lineno.func_with_docstring
77 test.test_doctest.doctest_lineno.func_with_docstring_wrapped
12 test.test_doctest.doctest_lineno.func_with_doctest
Expand Down
19 changes: 19 additions & 0 deletions Lib/test/test_ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1239,6 +1239,25 @@ def getpass(self):
# Make sure the password function isn't called if it isn't needed
ctx.load_cert_chain(CERTFILE, password=getpass_exception)

@threading_helper.requires_working_threading()
def test_load_cert_chain_thread_safety(self):
# gh-134698: _ssl detaches the thread state (and as such,
# releases the GIL and critical sections) around expensive
# OpenSSL calls. Unfortunately, OpenSSL structures aren't
# thread-safe, so executing these calls concurrently led
# to crashes.
ctx = ssl.create_default_context()

def race():
ctx.load_cert_chain(CERTFILE)

threads = [threading.Thread(target=race) for _ in range(8)]
with threading_helper.catch_threading_exception() as cm:
with threading_helper.start_threads(threads):
pass

self.assertIsNone(cm.exc_value)

def test_load_verify_locations(self):
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
ctx.load_verify_locations(CERTFILE)
Expand Down
17 changes: 17 additions & 0 deletions Lib/test/test_threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -2042,6 +2042,23 @@ def modify_file():
t.start()
t.join()

def test_dummy_thread_on_interpreter_shutdown(self):
# GH-130522: When `threading` held a reference to itself and then a
# _DummyThread() object was created, destruction of the dummy thread
# would emit an unraisable exception at shutdown, due to a lock being
# destroyed.
code = """if True:
import sys
import threading

threading.x = sys.modules[__name__]
x = threading._DummyThread()
"""
rc, out, err = assert_python_ok("-c", code)
self.assertEqual(rc, 0)
self.assertEqual(out, b"")
self.assertEqual(err, b"")


class ThreadRunFail(threading.Thread):
def run(self):
Expand Down
2 changes: 1 addition & 1 deletion Lib/threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -1414,7 +1414,7 @@ def __init__(self, dummy_thread):
# the related _DummyThread will be kept forever!
_thread_local_info._track_dummy_thread_ref = self

def __del__(self):
def __del__(self, _active_limbo_lock=_active_limbo_lock, _active=_active):
with _active_limbo_lock:
if _active.get(self._tident) is self._dummy_thread:
_active.pop(self._tident, None)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a crash when calling methods of :class:`ssl.SSLContext` or
:class:`ssl.SSLSocket` across multiple threads.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix retrieval of :attr:`doctest.DocTest.lineno` for objects decorated with
:func:`functools.cache` or :class:`functools.cached_property`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Ensure that hash functions guaranteed to be always *available* exist as
attributes of :mod:`hashlib` even if they will not work at runtime due to
missing backend implementations. For instance, ``hashlib.md5`` will no
longer raise :exc:`AttributeError` if OpenSSL is not available and Python
has been built without MD5 support. Patch by Bénédikt Tran.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix unraisable :exc:`TypeError` raised during :term:`interpreter shutdown`
in the :mod:`threading` module.
Loading
Loading