From 87efa4fe80f8d4480d14de66c871bb9d6a4e1cb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tin=20Tvrtkovi=C4=87?= Date: Sun, 16 Jan 2022 02:37:19 +0100 Subject: [PATCH] Fix set_closure_cell on 3.11 --- src/attr/_compat.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/attr/_compat.py b/src/attr/_compat.py index dc0cb02b6..d1f303276 100644 --- a/src/attr/_compat.py +++ b/src/attr/_compat.py @@ -211,15 +211,22 @@ def force_x_to_be_a_cell(): # pragma: no cover ) set_first_freevar_code = types.CodeType(*args) - def set_closure_cell(cell, value): - # Create a function using the set_first_freevar_code, - # whose first closure cell is `cell`. Calling it will - # change the value of that cell. - setter = types.FunctionType( - set_first_freevar_code, {}, "setter", (), (cell,) - ) - # And call it to set the cell. - setter(value) + if sys.version_info >= (3, 11): + + def set_closure_cell(cell, value): + cell.cell_contents = value + + else: + + def set_closure_cell(cell, value): + # Create a function using the set_first_freevar_code, + # whose first closure cell is `cell`. Calling it will + # change the value of that cell. + setter = types.FunctionType( + set_first_freevar_code, {}, "setter", (), (cell,) + ) + # And call it to set the cell. + setter(value) # Make sure it works on this interpreter: def make_func_with_cell():