]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-134280: Disable constant folding for ~ with a boolean argument (GH-134982)
authorSerhiy Storchaka <storchaka@gmail.com>
Tue, 1 Jul 2025 17:24:04 +0000 (20:24 +0300)
committerGitHub <noreply@github.com>
Tue, 1 Jul 2025 17:24:04 +0000 (20:24 +0300)
This moves the deprecation warning from compile time to run time.

Lib/test/test_peepholer.py
Misc/NEWS.d/next/Core_and_Builtins/2025-05-31-19-24-54.gh-issue-134280.NDVbzY.rst [new file with mode: 0644]
Python/flowgraph.c

index 3d7300e14802560ce42a2245f96554082c0a69a0..98629df457460e852b8e99deb379d0ae3ab120cf 100644 (file)
@@ -292,6 +292,7 @@ class TestTranforms(BytecodeTestCase):
             ('---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 (file)
index 0000000..f822721
--- /dev/null
@@ -0,0 +1,2 @@
+Disable constant folding for ``~`` with a boolean argument.
+This moves the deprecation warning from compile time to runtime.
index 2adc8c84d83974e9bea5262018810046728f66ed..1cb6f03169e3b5083d38cd00a946fa74bdea5f21 100644 (file)
@@ -1892,6 +1892,10 @@ eval_const_unaryop(PyObject *operand, int opcode, int oparg)
             result = PyNumber_Negative(operand);
             break;
         case UNARY_INVERT:
+            // XXX: This should be removed once the ~bool depreciation expires.
+            if (PyBool_Check(operand)) {
+                return NULL;
+            }
             result = PyNumber_Invert(operand);
             break;
         case UNARY_NOT: {