From 45e5b1b0a97795e2ac82a306ce366efd55bd15b4 Mon Sep 17 00:00:00 2001 From: An Long Date: Fri, 28 Aug 2026 03:42:27 +0900 Subject: [PATCH 1/2] gh-156476: Ignore timeout in `_PySimpleQueue.get()` when block is false (#156477) Co-authored-by: Stan Ulbrych --- Lib/queue.py | 4 +++- Lib/test/test_queue.py | 5 +++++ .../Library/2026-08-28-00-59-00.gh-issue-156476.rQhxc7.rst | 3 +++ 3 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2026-08-28-00-59-00.gh-issue-156476.rQhxc7.rst diff --git a/Lib/queue.py b/Lib/queue.py index c0b359876543f7b..80db0b978b9c008 100644 --- a/Lib/queue.py +++ b/Lib/queue.py @@ -346,7 +346,9 @@ def get(self, block=True, timeout=None): available, else raise the Empty exception ('timeout' is ignored in that case). ''' - if timeout is not None and timeout < 0: + if not block: + timeout = None + elif timeout is not None and timeout < 0: raise ValueError("'timeout' must be a non-negative number") if not self._count.acquire(block, timeout): raise Empty diff --git a/Lib/test/test_queue.py b/Lib/test/test_queue.py index f2898de469e349b..e1a4515500625bd 100644 --- a/Lib/test/test_queue.py +++ b/Lib/test/test_queue.py @@ -956,6 +956,11 @@ def test_negative_timeout_raises_exception(self): with self.assertRaises(ValueError): q.get(timeout=-1) + def test_nonblocking_ignores_timeout(self): + q = self.q + with self.assertRaises(self.queue.Empty): + q.get(block=False, timeout=-1) + def test_order(self): # Test a pair of concurrent put() and get() q = self.q diff --git a/Misc/NEWS.d/next/Library/2026-08-28-00-59-00.gh-issue-156476.rQhxc7.rst b/Misc/NEWS.d/next/Library/2026-08-28-00-59-00.gh-issue-156476.rQhxc7.rst new file mode 100644 index 000000000000000..c92fce3bc05969a --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-08-28-00-59-00.gh-issue-156476.rQhxc7.rst @@ -0,0 +1,3 @@ +The pure Python implementation of :meth:`queue.SimpleQueue.get` now ignores +*timeout* when *block* is false, matching the documented behavior and +the C implementation. It previously raised :exc:`ValueError`. From 24e5a55ccb0c5db68136d29f4c16c0bc21407db2 Mon Sep 17 00:00:00 2001 From: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> Date: Thu, 27 Aug 2026 21:11:05 +0100 Subject: [PATCH 2/2] gh-156466: fix cleanup on error in codegen_setup_annotations_scope (#156473) --- Python/codegen.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/Python/codegen.c b/Python/codegen.c index bedf3b17c52ce44..df52f2c734e69ae 100644 --- a/Python/codegen.c +++ b/Python/codegen.c @@ -707,16 +707,8 @@ codegen_enter_scope(compiler *c, identifier name, int scope_type, } static int -codegen_setup_annotations_scope(compiler *c, location loc, - void *key, PyObject *name) +codegen_emit_annotations_prologue(compiler *c, location loc) { - _PyCompile_CodeUnitMetadata umd = { - .u_posonlyargcount = 1, - }; - RETURN_IF_ERROR( - codegen_enter_scope(c, name, COMPILE_SCOPE_ANNOTATIONS, - key, loc.lineno, NULL, &umd)); - // if .format > VALUE_WITH_FAKE_GLOBALS: raise NotImplementedError PyObject *value_with_fake_globals = PyLong_FromLong(_Py_ANNOTATE_FORMAT_VALUE_WITH_FAKE_GLOBALS); if (value_with_fake_globals == NULL) { @@ -736,6 +728,21 @@ codegen_setup_annotations_scope(compiler *c, location loc, return SUCCESS; } +static int +codegen_setup_annotations_scope(compiler *c, location loc, + void *key, PyObject *name) +{ + _PyCompile_CodeUnitMetadata umd = { + .u_posonlyargcount = 1, + }; + RETURN_IF_ERROR( + codegen_enter_scope(c, name, COMPILE_SCOPE_ANNOTATIONS, + key, loc.lineno, NULL, &umd)); + + RETURN_IF_ERROR_IN_SCOPE(c, codegen_emit_annotations_prologue(c, loc)); + return SUCCESS; +} + static int codegen_rename_annotations_format_param(PyCodeObject *co) {