From 9544948e7e2f288513137a62308e875dac086a18 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Mon, 12 Jun 2023 11:19:04 -0700 Subject: [PATCH 1/4] Remove support for legacy bytecode instructions (#105705) (A legacy instruction is of the form `instr(FOOBAR)`, i.e. missing the `(... -- ...)` stack/cache effect annotation.) --- Tools/cases_generator/generate_cases.py | 4 +--- Tools/cases_generator/parser.py | 7 ++----- Tools/cases_generator/test_generator.py | 14 -------------- 3 files changed, 3 insertions(+), 22 deletions(-) diff --git a/Tools/cases_generator/generate_cases.py b/Tools/cases_generator/generate_cases.py index 69216c17ed8780..3a003b3fba2600 100644 --- a/Tools/cases_generator/generate_cases.py +++ b/Tools/cases_generator/generate_cases.py @@ -230,7 +230,7 @@ class Instruction: # Parts of the underlying instruction definition inst: parser.InstDef - kind: typing.Literal["inst", "op", "legacy"] # Legacy means no (input -- output) + kind: typing.Literal["inst", "op"] name: str block: parser.Block block_text: list[str] # Block.text, less curlies, less PREDICT() calls @@ -856,8 +856,6 @@ def get_stack_effect_info( self, thing: parser.InstDef | parser.Macro | parser.Pseudo ) -> tuple[AnyInstruction | None, str, str]: def effect_str(effects: list[StackEffect]) -> str: - if getattr(thing, "kind", None) == "legacy": - return str(-1) n_effect, sym_effect = list_effect_size(effects) if sym_effect: return f"{sym_effect} + {n_effect}" if n_effect else sym_effect diff --git a/Tools/cases_generator/parser.py b/Tools/cases_generator/parser.py index 2c75989a7e8ab4..ac77e7eae81ad3 100644 --- a/Tools/cases_generator/parser.py +++ b/Tools/cases_generator/parser.py @@ -101,7 +101,7 @@ class OpName(Node): class InstHeader(Node): override: bool register: bool - kind: Literal["inst", "op", "legacy"] # Legacy means no (inputs -- outputs) + kind: Literal["inst", "op"] name: str inputs: list[InputEffect] outputs: list[OutputEffect] @@ -111,7 +111,7 @@ class InstHeader(Node): class InstDef(Node): override: bool register: bool - kind: Literal["inst", "op", "legacy"] + kind: Literal["inst", "op"] name: str inputs: list[InputEffect] outputs: list[OutputEffect] @@ -174,9 +174,6 @@ def inst_header(self) -> InstHeader | None: if self.expect(lx.RPAREN): if (tkn := self.peek()) and tkn.kind == lx.LBRACE: return InstHeader(override, register, kind, name, inp, outp) - elif self.expect(lx.RPAREN) and kind == "inst": - # No legacy stack effect if kind is "op". - return InstHeader(override, register, "legacy", name, [], []) return None def io_effect(self) -> tuple[list[InputEffect], list[OutputEffect]]: diff --git a/Tools/cases_generator/test_generator.py b/Tools/cases_generator/test_generator.py index 036094ac8ef487..412d3d14590050 100644 --- a/Tools/cases_generator/test_generator.py +++ b/Tools/cases_generator/test_generator.py @@ -62,20 +62,6 @@ def run_cases_test(input: str, expected: str): # print("End") assert actual.rstrip() == expected.rstrip() -def test_legacy(): - input = """ - inst(OP) { - spam(); - } - """ - output = """ - TARGET(OP) { - spam(); - DISPATCH(); - } - """ - run_cases_test(input, output) - def test_inst_no_args(): input = """ inst(OP, (--)) { From f0fb782ddb7208a59cfc38ec4bcbd8d1a81f8a58 Mon Sep 17 00:00:00 2001 From: Jay <74105438+weijay0804@users.noreply.github.com> Date: Tue, 13 Jun 2023 04:29:02 +0800 Subject: [PATCH 2/4] gh-105331: Change `asyncio.sleep` to raise ``ValueError` for nan (#105641) Co-authored-by: Guido van Rossum Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> --- Doc/library/asyncio-task.rst | 3 +++ Lib/asyncio/tasks.py | 4 ++++ Lib/test/test_asyncio/test_tasks.py | 15 +++++++++++++++ ...2023-06-11-09-14-30.gh-issue-105331.nlZvoW.rst | 2 ++ 4 files changed, 24 insertions(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2023-06-11-09-14-30.gh-issue-105331.nlZvoW.rst diff --git a/Doc/library/asyncio-task.rst b/Doc/library/asyncio-task.rst index fe8d028150403d..3618bcb6d7c6b5 100644 --- a/Doc/library/asyncio-task.rst +++ b/Doc/library/asyncio-task.rst @@ -426,6 +426,9 @@ Sleeping .. versionchanged:: 3.10 Removed the *loop* parameter. + .. versionchanged:: 3.13 + Raises :exc:`ValueError` if *delay* is :data:`~math.nan`. + Running Tasks Concurrently ========================== diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py index 8d5bde09ea9b5b..4250bb0753879a 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -15,6 +15,7 @@ import functools import inspect import itertools +import math import types import warnings import weakref @@ -646,6 +647,9 @@ async def sleep(delay, result=None): await __sleep0() return result + if math.isnan(delay): + raise ValueError("Invalid delay: NaN (not a number)") + loop = events.get_running_loop() future = loop.create_future() h = loop.call_later(delay, diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py index 6e8a51ce2555d5..4dfaff847edb90 100644 --- a/Lib/test/test_asyncio/test_tasks.py +++ b/Lib/test/test_asyncio/test_tasks.py @@ -1609,6 +1609,21 @@ async def sleeper(dt, arg): self.assertEqual(t.result(), 'yeah') self.assertAlmostEqual(0.1, loop.time()) + def test_sleep_when_delay_is_nan(self): + + def gen(): + yield + + loop = self.new_test_loop(gen) + + async def sleeper(): + await asyncio.sleep(float("nan")) + + t = self.new_task(loop, sleeper()) + + with self.assertRaises(ValueError): + loop.run_until_complete(t) + def test_sleep_cancel(self): def gen(): diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-06-11-09-14-30.gh-issue-105331.nlZvoW.rst b/Misc/NEWS.d/next/Core and Builtins/2023-06-11-09-14-30.gh-issue-105331.nlZvoW.rst new file mode 100644 index 00000000000000..4a3fee0dd64ae0 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2023-06-11-09-14-30.gh-issue-105331.nlZvoW.rst @@ -0,0 +1,2 @@ +Raise :exc:`ValueError` if the ``delay`` argument to :func:`asyncio.sleep` is a NaN (matching :func:`time.sleep`). + From ca3cc4b95d66f7527ebe0ba4cdb1907082d9bfc8 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Mon, 12 Jun 2023 23:35:07 +0200 Subject: [PATCH 3/4] gh-105375: Explicitly initialise all {Pickler,Unpickler}Object fields (#105686) All fields must be explicitly initialised to prevent manipulation of uninitialised fields in dealloc. Align initialisation order with the layout of the object structs. --- Modules/_pickle.c | 90 ++++++++++++++++++++++++++++------------------- 1 file changed, 53 insertions(+), 37 deletions(-) diff --git a/Modules/_pickle.c b/Modules/_pickle.c index e6eb9c741e1adc..9e70fee84e18d3 100644 --- a/Modules/_pickle.c +++ b/Modules/_pickle.c @@ -1145,42 +1145,49 @@ _Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t data_len) static PicklerObject * _Pickler_New(PickleState *st) { - PicklerObject *self; - - self = PyObject_GC_New(PicklerObject, st->Pickler_Type); - if (self == NULL) + PyMemoTable *memo = PyMemoTable_New(); + if (memo == NULL) { return NULL; + } + + const Py_ssize_t max_output_len = WRITE_BUF_SIZE; + PyObject *output_buffer = PyBytes_FromStringAndSize(NULL, max_output_len); + if (output_buffer == NULL) { + goto error; + } + PicklerObject *self = PyObject_GC_New(PicklerObject, st->Pickler_Type); + if (self == NULL) { + goto error; + } + + self->memo = memo; self->pers_func = NULL; + self->pers_func_self = NULL; self->dispatch_table = NULL; - self->buffer_callback = NULL; + self->reducer_override = NULL; self->write = NULL; + self->output_buffer = output_buffer; + self->output_len = 0; + self->max_output_len = max_output_len; self->proto = 0; self->bin = 0; self->framing = 0; self->frame_start = -1; + self->buf_size = 0; self->fast = 0; self->fast_nesting = 0; self->fix_imports = 0; self->fast_memo = NULL; - self->max_output_len = WRITE_BUF_SIZE; - self->output_len = 0; - self->reducer_override = NULL; - - self->memo = PyMemoTable_New(); - if (self->memo == NULL) { - Py_DECREF(self); - return NULL; - } - self->output_buffer = PyBytes_FromStringAndSize(NULL, - self->max_output_len); - if (self->output_buffer == NULL) { - Py_DECREF(self); - return NULL; - } + self->buffer_callback = NULL; PyObject_GC_Track(self); return self; + +error: + PyMem_Free(memo); + Py_XDECREF(output_buffer); + return NULL; } static int @@ -1628,14 +1635,31 @@ _Unpickler_MemoCleanup(UnpicklerObject *self) static UnpicklerObject * _Unpickler_New(PyObject *module) { - UnpicklerObject *self; + const int MEMO_SIZE = 32; + PyObject **memo = _Unpickler_NewMemo(MEMO_SIZE); + if (memo == NULL) { + return NULL; + } + PickleState *st = _Pickle_GetState(module); + PyObject *stack = Pdata_New(st); + if (stack == NULL) { + goto error; + } - self = PyObject_GC_New(UnpicklerObject, st->Unpickler_Type); - if (self == NULL) - return NULL; + UnpicklerObject *self = PyObject_GC_New(UnpicklerObject, + st->Unpickler_Type); + if (self == NULL) { + goto error; + } + self->stack = (Pdata *)stack; + self->memo = memo; + self->memo_size = MEMO_SIZE; + self->memo_len = 0; self->pers_func = NULL; + self->pers_func_self = NULL; + memset(&self->buffer, 0, sizeof(Py_buffer)); self->input_buffer = NULL; self->input_line = NULL; self->input_len = 0; @@ -1653,22 +1677,14 @@ _Unpickler_New(PyObject *module) self->marks_size = 0; self->proto = 0; self->fix_imports = 0; - memset(&self->buffer, 0, sizeof(Py_buffer)); - self->memo_size = 32; - self->memo_len = 0; - self->memo = _Unpickler_NewMemo(self->memo_size); - if (self->memo == NULL) { - Py_DECREF(self); - return NULL; - } - self->stack = (Pdata *)Pdata_New(st); - if (self->stack == NULL) { - Py_DECREF(self); - return NULL; - } PyObject_GC_Track(self); return self; + +error: + PyMem_Free(memo); + Py_XDECREF(stack); + return NULL; } /* Returns -1 (with an exception set) on failure, 0 on success. This may From 8da9d1b16319f4a6bd78435016ef1f4bef6e2b41 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Mon, 12 Jun 2023 14:55:15 -0700 Subject: [PATCH 4/4] gh-105540: Fix code generator tests (#105707) This involves expanding PEEK, POKE and JUMPBY macros, and removing super and register tests (those features no longer exist). --- Tools/cases_generator/test_generator.py | 175 +++++++----------------- 1 file changed, 47 insertions(+), 128 deletions(-) diff --git a/Tools/cases_generator/test_generator.py b/Tools/cases_generator/test_generator.py index 412d3d14590050..f8c79976fb722e 100644 --- a/Tools/cases_generator/test_generator.py +++ b/Tools/cases_generator/test_generator.py @@ -43,7 +43,8 @@ def run_cases_test(input: str, expected: str): temp_input.write(generate_cases.END_MARKER) temp_input.flush() temp_output = tempfile.NamedTemporaryFile("w+") - a = generate_cases.Analyzer(temp_input.name, temp_output.name) + temp_metadata = tempfile.NamedTemporaryFile("w+") + a = generate_cases.Analyzer([temp_input.name], temp_output.name, temp_metadata.name) a.parse() a.analyze() if a.errors: @@ -84,7 +85,7 @@ def test_inst_one_pop(): """ output = """ TARGET(OP) { - PyObject *value = PEEK(1); + PyObject *value = stack_pointer[-1]; spam(); STACK_SHRINK(1); DISPATCH(); @@ -103,7 +104,7 @@ def test_inst_one_push(): PyObject *res; spam(); STACK_GROW(1); - POKE(1, res); + stack_pointer[-1] = res; DISPATCH(); } """ @@ -117,10 +118,10 @@ def test_inst_one_push_one_pop(): """ output = """ TARGET(OP) { - PyObject *value = PEEK(1); + PyObject *value = stack_pointer[-1]; PyObject *res; spam(); - POKE(1, res); + stack_pointer[-1] = res; DISPATCH(); } """ @@ -134,12 +135,12 @@ def test_binary_op(): """ output = """ TARGET(OP) { - PyObject *right = PEEK(1); - PyObject *left = PEEK(2); + PyObject *right = stack_pointer[-1]; + PyObject *left = stack_pointer[-2]; PyObject *res; spam(); STACK_SHRINK(1); - POKE(1, res); + stack_pointer[-1] = res; DISPATCH(); } """ @@ -153,11 +154,11 @@ def test_overlap(): """ output = """ TARGET(OP) { - PyObject *right = PEEK(1); - PyObject *left = PEEK(2); + PyObject *right = stack_pointer[-1]; + PyObject *left = stack_pointer[-2]; PyObject *result; spam(); - POKE(1, result); + stack_pointer[-1] = result; DISPATCH(); } """ @@ -167,11 +168,8 @@ def test_predictions_and_eval_breaker(): input = """ inst(OP1, (--)) { } - inst(OP2, (--)) { - } inst(OP3, (arg -- res)) { DEOPT_IF(xxx, OP1); - PREDICT(OP2); CHECK_EVAL_BREAKER(); } """ @@ -181,17 +179,11 @@ def test_predictions_and_eval_breaker(): DISPATCH(); } - TARGET(OP2) { - PREDICTED(OP2); - DISPATCH(); - } - TARGET(OP3) { - PyObject *arg = PEEK(1); + PyObject *arg = stack_pointer[-1]; PyObject *res; DEOPT_IF(xxx, OP1); - POKE(1, res); - PREDICT(OP2); + stack_pointer[-1] = res; CHECK_EVAL_BREAKER(); DISPATCH(); } @@ -234,12 +226,12 @@ def test_error_if_pop(): """ output = """ TARGET(OP) { - PyObject *right = PEEK(1); - PyObject *left = PEEK(2); + PyObject *right = stack_pointer[-1]; + PyObject *left = stack_pointer[-2]; PyObject *res; if (cond) goto pop_2_label; STACK_SHRINK(1); - POKE(1, res); + stack_pointer[-1] = res; DISPATCH(); } """ @@ -252,11 +244,11 @@ def test_cache_effect(): """ output = """ TARGET(OP) { - PyObject *value = PEEK(1); + PyObject *value = stack_pointer[-1]; uint16_t counter = read_u16(&next_instr[0].cache); uint32_t extra = read_u32(&next_instr[1].cache); STACK_SHRINK(1); - JUMPBY(3); + next_instr += 3; DISPATCH(); } """ @@ -275,59 +267,6 @@ def test_suppress_dispatch(): """ run_cases_test(input, output) -def test_super_instruction(): - # TODO: Test cache effect - input = """ - inst(OP1, (counter/1, arg --)) { - op1(); - } - inst(OP2, (extra/2, arg --)) { - op2(); - } - super(OP) = OP1 + OP2; - """ - output = """ - TARGET(OP1) { - PyObject *arg = PEEK(1); - uint16_t counter = read_u16(&next_instr[0].cache); - op1(); - STACK_SHRINK(1); - JUMPBY(1); - DISPATCH(); - } - - TARGET(OP2) { - PyObject *arg = PEEK(1); - uint32_t extra = read_u32(&next_instr[0].cache); - op2(); - STACK_SHRINK(1); - JUMPBY(2); - DISPATCH(); - } - - TARGET(OP) { - PyObject *_tmp_1 = PEEK(1); - PyObject *_tmp_2 = PEEK(2); - { - PyObject *arg = _tmp_1; - uint16_t counter = read_u16(&next_instr[0].cache); - op1(); - } - JUMPBY(1); - NEXTOPARG(); - JUMPBY(1); - { - PyObject *arg = _tmp_2; - uint32_t extra = read_u32(&next_instr[0].cache); - op2(); - } - JUMPBY(2); - STACK_SHRINK(2); - DISPATCH(); - } - """ - run_cases_test(input, output) - def test_macro_instruction(): input = """ inst(OP1, (counter/1, left, right -- left, right)) { @@ -344,18 +283,18 @@ def test_macro_instruction(): """ output = """ TARGET(OP1) { - PyObject *right = PEEK(1); - PyObject *left = PEEK(2); + PyObject *right = stack_pointer[-1]; + PyObject *left = stack_pointer[-2]; uint16_t counter = read_u16(&next_instr[0].cache); op1(left, right); - JUMPBY(1); + next_instr += 1; DISPATCH(); } TARGET(OP) { - PyObject *_tmp_1 = PEEK(1); - PyObject *_tmp_2 = PEEK(2); - PyObject *_tmp_3 = PEEK(3); + PyObject *_tmp_1 = stack_pointer[-1]; + PyObject *_tmp_2 = stack_pointer[-2]; + PyObject *_tmp_3 = stack_pointer[-3]; { PyObject *right = _tmp_1; PyObject *left = _tmp_2; @@ -373,22 +312,22 @@ def test_macro_instruction(): res = op2(arg2, left, right); _tmp_3 = res; } - JUMPBY(5); + next_instr += 5; static_assert(INLINE_CACHE_ENTRIES_OP == 5, "incorrect cache size"); STACK_SHRINK(2); - POKE(1, _tmp_3); + stack_pointer[-1] = _tmp_3; DISPATCH(); } TARGET(OP3) { - PyObject *right = PEEK(1); - PyObject *left = PEEK(2); - PyObject *arg2 = PEEK(3); + PyObject *right = stack_pointer[-1]; + PyObject *left = stack_pointer[-2]; + PyObject *arg2 = stack_pointer[-3]; PyObject *res; res = op3(arg2, left, right); STACK_SHRINK(2); - POKE(1, res); - JUMPBY(5); + stack_pointer[-1] = res; + next_instr += 5; DISPATCH(); } """ @@ -402,9 +341,9 @@ def test_array_input(): """ output = """ TARGET(OP) { - PyObject *above = PEEK(1); - PyObject **values = &PEEK(1 + oparg*2); - PyObject *below = PEEK(2 + oparg*2); + PyObject *above = stack_pointer[-1]; + PyObject **values = (stack_pointer - (1 + oparg*2)); + PyObject *below = stack_pointer[-(2 + oparg*2)]; spam(); STACK_SHRINK(oparg*2); STACK_SHRINK(2); @@ -426,8 +365,8 @@ def test_array_output(): PyObject *above; spam(values, oparg); STACK_GROW(oparg*3); - POKE(1, above); - POKE(2 + oparg*3, below); + stack_pointer[-1] = above; + stack_pointer[-(2 + oparg*3)] = below; DISPATCH(); } """ @@ -441,11 +380,11 @@ def test_array_input_output(): """ output = """ TARGET(OP) { - PyObject **values = &PEEK(oparg); + PyObject **values = (stack_pointer - oparg); PyObject *above; spam(values, oparg); STACK_GROW(1); - POKE(1, above); + stack_pointer[-1] = above; DISPATCH(); } """ @@ -459,8 +398,8 @@ def test_array_error_if(): """ output = """ TARGET(OP) { - PyObject **values = &PEEK(oparg); - PyObject *extra = PEEK(1 + oparg); + PyObject **values = (stack_pointer - oparg); + PyObject *extra = stack_pointer[-(1 + oparg)]; if (oparg == 0) { STACK_SHRINK(oparg); goto pop_1_somewhere; } STACK_SHRINK(oparg); STACK_SHRINK(1); @@ -469,26 +408,6 @@ def test_array_error_if(): """ run_cases_test(input, output) -def test_register(): - input = """ - register inst(OP, (counter/1, left, right -- result)) { - result = op(left, right); - } - """ - output = """ - TARGET(OP) { - PyObject *left = REG(oparg1); - PyObject *right = REG(oparg2); - PyObject *result; - uint16_t counter = read_u16(&next_instr[0].cache); - result = op(left, right); - Py_XSETREF(REG(oparg3), result); - JUMPBY(1); - DISPATCH(); - } - """ - run_cases_test(input, output) - def test_cond_effect(): input = """ inst(OP, (aa, input if ((oparg & 1) == 1), cc -- xx, output if (oparg & 2), zz)) { @@ -497,18 +416,18 @@ def test_cond_effect(): """ output = """ TARGET(OP) { - PyObject *cc = PEEK(1); - PyObject *input = ((oparg & 1) == 1) ? PEEK(1 + (((oparg & 1) == 1) ? 1 : 0)) : NULL; - PyObject *aa = PEEK(2 + (((oparg & 1) == 1) ? 1 : 0)); + PyObject *cc = stack_pointer[-1]; + PyObject *input = ((oparg & 1) == 1) ? stack_pointer[-(1 + (((oparg & 1) == 1) ? 1 : 0))] : NULL; + PyObject *aa = stack_pointer[-(2 + (((oparg & 1) == 1) ? 1 : 0))]; PyObject *xx; PyObject *output = NULL; PyObject *zz; output = spam(oparg, input); STACK_SHRINK((((oparg & 1) == 1) ? 1 : 0)); STACK_GROW(((oparg & 2) ? 1 : 0)); - POKE(1, zz); - if (oparg & 2) { POKE(1 + ((oparg & 2) ? 1 : 0), output); } - POKE(2 + ((oparg & 2) ? 1 : 0), xx); + stack_pointer[-1] = zz; + if (oparg & 2) { stack_pointer[-(1 + ((oparg & 2) ? 1 : 0))] = output; } + stack_pointer[-(2 + ((oparg & 2) ? 1 : 0))] = xx; DISPATCH(); } """