Skip to content

Commit 9006b44

Browse files
committed
Merge remote-tracking branch 'upstream/3.10' into 3.10
2 parents 4d37dc6 + 9e1bfd8 commit 9006b44

File tree

5 files changed

+40
-48
lines changed

5 files changed

+40
-48
lines changed

Lib/test/test_asyncio/test_threads.py

Lines changed: 18 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,87 +5,58 @@
55

66
from contextvars import ContextVar
77
from unittest import mock
8-
from test.test_asyncio import utils as test_utils
98

109

1110
def tearDownModule():
1211
asyncio.set_event_loop_policy(None)
1312

1413

15-
class ToThreadTests(test_utils.TestCase):
16-
def setUp(self):
17-
super().setUp()
18-
self.loop = asyncio.new_event_loop()
19-
asyncio.set_event_loop(self.loop)
20-
21-
def tearDown(self):
22-
self.loop.run_until_complete(
23-
self.loop.shutdown_default_executor())
24-
self.loop.close()
25-
asyncio.set_event_loop(None)
26-
self.loop = None
27-
super().tearDown()
28-
29-
def test_to_thread(self):
30-
async def main():
31-
return await asyncio.to_thread(sum, [40, 2])
32-
33-
result = self.loop.run_until_complete(main())
14+
class ToThreadTests(unittest.IsolatedAsyncioTestCase):
15+
async def test_to_thread(self):
16+
result = await asyncio.to_thread(sum, [40, 2])
3417
self.assertEqual(result, 42)
3518

36-
def test_to_thread_exception(self):
19+
async def test_to_thread_exception(self):
3720
def raise_runtime():
3821
raise RuntimeError("test")
3922

40-
async def main():
41-
await asyncio.to_thread(raise_runtime)
42-
4323
with self.assertRaisesRegex(RuntimeError, "test"):
44-
self.loop.run_until_complete(main())
24+
await asyncio.to_thread(raise_runtime)
4525

46-
def test_to_thread_once(self):
26+
async def test_to_thread_once(self):
4727
func = mock.Mock()
4828

49-
async def main():
50-
await asyncio.to_thread(func)
51-
52-
self.loop.run_until_complete(main())
29+
await asyncio.to_thread(func)
5330
func.assert_called_once()
5431

55-
def test_to_thread_concurrent(self):
32+
async def test_to_thread_concurrent(self):
5633
func = mock.Mock()
5734

58-
async def main():
59-
futs = []
60-
for _ in range(10):
61-
fut = asyncio.to_thread(func)
62-
futs.append(fut)
63-
await asyncio.gather(*futs)
35+
futs = []
36+
for _ in range(10):
37+
fut = asyncio.to_thread(func)
38+
futs.append(fut)
39+
await asyncio.gather(*futs)
6440

65-
self.loop.run_until_complete(main())
6641
self.assertEqual(func.call_count, 10)
6742

68-
def test_to_thread_args_kwargs(self):
43+
async def test_to_thread_args_kwargs(self):
6944
# Unlike run_in_executor(), to_thread() should directly accept kwargs.
7045
func = mock.Mock()
7146

72-
async def main():
73-
await asyncio.to_thread(func, 'test', something=True)
47+
await asyncio.to_thread(func, 'test', something=True)
7448

75-
self.loop.run_until_complete(main())
7649
func.assert_called_once_with('test', something=True)
7750

78-
def test_to_thread_contextvars(self):
51+
async def test_to_thread_contextvars(self):
7952
test_ctx = ContextVar('test_ctx')
8053

8154
def get_ctx():
8255
return test_ctx.get()
8356

84-
async def main():
85-
test_ctx.set('parrot')
86-
return await asyncio.to_thread(get_ctx)
57+
test_ctx.set('parrot')
58+
result = await asyncio.to_thread(get_ctx)
8759

88-
result = self.loop.run_until_complete(main())
8960
self.assertEqual(result, 'parrot')
9061

9162

Lib/test/test_hashlib.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,10 @@ def test_algorithms_guaranteed(self):
221221
def test_algorithms_available(self):
222222
self.assertTrue(set(hashlib.algorithms_guaranteed).
223223
issubset(hashlib.algorithms_available))
224+
# all available algorithms must be loadable, bpo-47101
225+
self.assertNotIn("undefined", hashlib.algorithms_available)
226+
for name in hashlib.algorithms_available:
227+
digest = hashlib.new(name, usedforsecurity=False)
224228

225229
def test_usedforsecurity_true(self):
226230
hashlib.new("sha256", usedforsecurity=True)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
:const:`hashlib.algorithms_available` now lists only algorithms that are
2+
provided by activated crypto providers on OpenSSL 3.0. Legacy algorithms are
3+
not listed unless the legacy provider has been loaded into the default
4+
OSSL context.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Rewrite :func:`asyncio.to_thread` tests to use
2+
:class:`unittest.IsolatedAsyncioTestCase`.

Modules/_hashopenssl.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1836,15 +1836,21 @@ typedef struct _internal_name_mapper_state {
18361836

18371837
/* A callback function to pass to OpenSSL's OBJ_NAME_do_all(...) */
18381838
static void
1839+
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
1840+
_openssl_hash_name_mapper(EVP_MD *md, void *arg)
1841+
#else
18391842
_openssl_hash_name_mapper(const EVP_MD *md, const char *from,
18401843
const char *to, void *arg)
1844+
#endif
18411845
{
18421846
_InternalNameMapperState *state = (_InternalNameMapperState *)arg;
18431847
PyObject *py_name;
18441848

18451849
assert(state != NULL);
1846-
if (md == NULL)
1850+
// ignore all undefined providers
1851+
if ((md == NULL) || (EVP_MD_nid(md) == NID_undef)) {
18471852
return;
1853+
}
18481854

18491855
py_name = py_digest_name(md);
18501856
if (py_name == NULL) {
@@ -1870,7 +1876,12 @@ hashlib_md_meth_names(PyObject *module)
18701876
return -1;
18711877
}
18721878

1879+
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
1880+
// get algorithms from all activated providers in default context
1881+
EVP_MD_do_all_provided(NULL, &_openssl_hash_name_mapper, &state);
1882+
#else
18731883
EVP_MD_do_all(&_openssl_hash_name_mapper, &state);
1884+
#endif
18741885

18751886
if (state.error) {
18761887
Py_DECREF(state.set);

0 commit comments

Comments
 (0)