From deb1724d7a7d591d0c800f4bd38f74629f79e469 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sat, 31 May 2025 19:36:05 +0300 Subject: [PATCH 1/2] gh-134280: Disable constant folding for ~ with a boolean argument This moves the deprecation warning from compile time to runtime. --- Lib/test/test_peepholer.py | 1 + .../2025-05-31-19-24-54.gh-issue-134280.NDVbzY.rst | 2 ++ Python/flowgraph.c | 3 +++ 3 files changed, 6 insertions(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2025-05-31-19-24-54.gh-issue-134280.NDVbzY.rst diff --git a/Lib/test/test_peepholer.py b/Lib/test/test_peepholer.py index f33de3d420ca34..500930e2d0958e 100644 --- a/Lib/test/test_peepholer.py +++ b/Lib/test/test_peepholer.py @@ -292,6 +292,7 @@ def test_constant_folding_unaryop(self): ('---x', 'UNARY_NEGATIVE', None, False, None, None), ('~~~x', 'UNARY_INVERT', None, False, None, None), ('+++x', 'CALL_INTRINSIC_1', intrinsic_positive, False, None, None), + ('~True', 'UNARY_INVERT', None, False, None, None), ] for ( diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-05-31-19-24-54.gh-issue-134280.NDVbzY.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-05-31-19-24-54.gh-issue-134280.NDVbzY.rst new file mode 100644 index 00000000000000..f822721690975a --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-05-31-19-24-54.gh-issue-134280.NDVbzY.rst @@ -0,0 +1,2 @@ +Disable constant folding for ``~`` with a boolean argument. +This moves the deprecation warning from compile time to runtime. diff --git a/Python/flowgraph.c b/Python/flowgraph.c index 67ccf350b72ed6..af2b5df98fa506 100644 --- a/Python/flowgraph.c +++ b/Python/flowgraph.c @@ -1892,6 +1892,9 @@ eval_const_unaryop(PyObject *operand, int opcode, int oparg) result = PyNumber_Negative(operand); break; case UNARY_INVERT: + if (PyBool_Check(operand)) { // ~bool is deprecated + return NULL; + } result = PyNumber_Invert(operand); break; case UNARY_NOT: { From cb7d75494e5b384f31a71bdb6908e1a383a2a12a Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Tue, 10 Jun 2025 13:59:23 +0300 Subject: [PATCH 2/2] Add a comment about the temporary nature of the code. --- Python/flowgraph.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Python/flowgraph.c b/Python/flowgraph.c index 7514dacc06a604..1cb6f03169e3b5 100644 --- a/Python/flowgraph.c +++ b/Python/flowgraph.c @@ -1892,7 +1892,8 @@ eval_const_unaryop(PyObject *operand, int opcode, int oparg) result = PyNumber_Negative(operand); break; case UNARY_INVERT: - if (PyBool_Check(operand)) { // ~bool is deprecated + // XXX: This should be removed once the ~bool depreciation expires. + if (PyBool_Check(operand)) { return NULL; } result = PyNumber_Invert(operand);