Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 35 additions & 17 deletions Doc/c-api/exceptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -673,28 +673,46 @@ Signal Handling
single: SIGINT (C macro)
single: KeyboardInterrupt (built-in exception)

This function interacts with Python's signal handling.
Handle external interruptions, such as signals or activating a debugger,
whose processing has been delayed until it is safe
to run Python code and/or raise exceptions.

If the function is called from the main thread and under the main Python
interpreter, it checks whether a signal has been sent to the processes
and if so, invokes the corresponding signal handler. If the :mod:`signal`
module is supported, this can invoke a signal handler written in Python.
For example, pressing :kbd:`Ctrl-C` causes a terminal to send the
:py:data:`signal.SIGINT` signal.
This function executes the corresponding Python signal handler, which,
by default, raises the :exc:`KeyboardInterrupt` exception.

The function attempts to handle all pending signals, and then returns ``0``.
However, if a Python signal handler raises an exception, the error
indicator is set and the function returns ``-1`` immediately (such that
other pending signals may not have been handled yet: they will be on the
next :c:func:`PyErr_CheckSignals()` invocation).
:c:func:`!PyErr_CheckSignals` should be called by long-running C code
frequently enough so that the response appears immediate to humans.

If the function is called from a non-main thread, or under a non-main
Python interpreter, it does nothing and returns ``0``.
Handlers invoked by this function currently include:

This function can be called by long-running C code that wants to
be interruptible by user requests (such as by pressing Ctrl-C).
- Signal handlers, including Python functions registered using
the :mod:`signal` module.

.. note::
The default Python signal handler for :c:macro:`!SIGINT` raises the
:exc:`KeyboardInterrupt` exception.
Signal handlers are only run in the main thread of the main interpreter.

(This is where the function got the name: originally, signals
were the only way to interrupt the interpreter.)

- Running the garbage collector, if necessary.

- Executing a pending :ref:`remote debugger <remote-debugging>` script.

If any handler raises an exception, immediately return ``-1`` with that
exception set.
Any remaining interruptions are left to be processed on the next
:c:func:`PyErr_CheckSignals()` invocation, if appropriate.

If all handlers finish successfully, or there are no handlers to run,
return ``0``.

.. versionchanged:: 3.12
This function may now invoke the garbage collector.

.. versionchanged:: 3.14
This function may now execute a remote debugger script, if remote
debugging is enabled.


.. c:function:: void PyErr_SetInterrupt()
Expand Down
29 changes: 25 additions & 4 deletions Lib/test/test_hashlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,11 @@ def __init__(self, *args, **kwargs):
algorithms.add(algorithm.lower())

_blake2 = self._conditional_import_module('_blake2')
blake2_hashes = {'blake2b', 'blake2s'}
if _blake2:
algorithms.update({'blake2b', 'blake2s'})
algorithms.update(blake2_hashes)
else:
algorithms.difference_update(blake2_hashes)

self.constructors_to_test = {}
for algorithm in algorithms:
Expand Down Expand Up @@ -232,7 +235,12 @@ def test_algorithms_available(self):
# all available algorithms must be loadable, bpo-47101
self.assertNotIn("undefined", hashlib.algorithms_available)
for name in hashlib.algorithms_available:
digest = hashlib.new(name, usedforsecurity=False)
with self.subTest(name):
try:
_ = hashlib.new(name, usedforsecurity=False)
except ValueError as exc:
self.skip_if_blake2_not_builtin(name, exc)
raise

def test_usedforsecurity_true(self):
hashlib.new("sha256", usedforsecurity=True)
Expand Down Expand Up @@ -504,6 +512,7 @@ def test_sha3_256_update_over_4gb(self):
self.assertEqual(h.hexdigest(), "e2d4535e3b613135c14f2fe4e026d7ad8d569db44901740beffa30d430acb038")

@requires_resource('cpu')
@requires_blake2
def test_blake2_update_over_4gb(self):
# blake2s or blake2b doesn't matter based on how our C code is structured, this tests the
# common loop macro logic.
Expand Down Expand Up @@ -798,6 +807,12 @@ def test_case_sha512_3(self):
"e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973eb"+
"de0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b")

def skip_if_blake2_not_builtin(self, name, skip_reason):
# blake2 builtins may be absent if python built with
# a subset of --with-builtin-hashlib-hashes or none.
if "blake2" in name and "blake2" not in builtin_hashes:
self.skipTest(skip_reason)

def check_blake2(self, constructor, salt_size, person_size, key_size,
digest_size, max_offset):
self.assertEqual(constructor.SALT_SIZE, salt_size)
Expand Down Expand Up @@ -1080,10 +1095,16 @@ def test_sha256_gil(self):
def test_threaded_hashing_fast(self):
# Same as test_threaded_hashing_slow() but only tests some functions
# since otherwise test_hashlib.py becomes too slow during development.
for name in ['md5', 'sha1', 'sha256', 'sha3_256', 'blake2s']:
algos = ['md5', 'sha1', 'sha256', 'sha3_256', 'blake2s']
for name in algos:
if constructor := getattr(hashlib, name, None):
with self.subTest(name):
self.do_test_threaded_hashing(constructor, is_shake=False)
try:
self.do_test_threaded_hashing(constructor, is_shake=False)
except ValueError as exc:
self.skip_if_blake2_not_builtin(name, exc)
raise

if shake_128 := getattr(hashlib, 'shake_128', None):
self.do_test_threaded_hashing(shake_128, is_shake=True)

Expand Down
Loading