Skip to content

Commit 7b2d94d

Browse files
authored
GH-106008: Make implicit boolean conversions explicit (GH-106003)
1 parent 6e9f83d commit 7b2d94d

20 files changed

+1721
-1145
lines changed

.gitattributes

+2-1
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,9 @@ Parser/parser.c generated
8585
Parser/token.c generated
8686
Programs/test_frozenmain.h generated
8787
Python/Python-ast.c generated
88-
Python/generated_cases.c.h generated
8988
Python/executor_cases.c.h generated
89+
Python/generated_cases.c.h generated
90+
Python/opcode_metadata.h generated
9091
Python/opcode_targets.h generated
9192
Python/stdlib_module_names.h generated
9293
Tools/peg_generator/pegen/grammar_parser.py generated

Doc/library/dis.rst

+22-1
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,9 @@ result back on the stack.
529529

530530
Implements ``STACK[-1] = not STACK[-1]``.
531531

532+
.. versionchanged:: 3.13
533+
This instruction now requires an exact :class:`bool` operand.
534+
532535

533536
.. opcode:: UNARY_INVERT
534537

@@ -548,6 +551,13 @@ result back on the stack.
548551
.. versionadded:: 3.5
549552

550553

554+
.. opcode:: TO_BOOL
555+
556+
Implements ``STACK[-1] = bool(STACK[-1])``.
557+
558+
.. versionadded:: 3.13
559+
560+
551561
**Binary and in-place operations**
552562

553563
Binary operations remove the top two items from the stack (``STACK[-1]`` and
@@ -1127,7 +1137,12 @@ iterations of the loop.
11271137
.. opcode:: COMPARE_OP (opname)
11281138

11291139
Performs a Boolean operation. The operation name can be found in
1130-
``cmp_op[opname]``.
1140+
``cmp_op[opname >> 5]``. If the fifth-lowest bit of ``opname`` is set
1141+
(``opname & 16``), the result should be coerced to ``bool``.
1142+
1143+
.. versionchanged:: 3.13
1144+
The fifth-lowest bit of the oparg now indicates a forced conversion to
1145+
:class:`bool`.
11311146

11321147

11331148
.. opcode:: IS_OP (invert)
@@ -1191,6 +1206,9 @@ iterations of the loop.
11911206
.. versionchanged:: 3.12
11921207
This is no longer a pseudo-instruction.
11931208

1209+
.. versionchanged:: 3.13
1210+
This instruction now requires an exact :class:`bool` operand.
1211+
11941212
.. opcode:: POP_JUMP_IF_FALSE (delta)
11951213

11961214
If ``STACK[-1]`` is false, increments the bytecode counter by *delta*.
@@ -1204,6 +1222,9 @@ iterations of the loop.
12041222
.. versionchanged:: 3.12
12051223
This is no longer a pseudo-instruction.
12061224

1225+
.. versionchanged:: 3.13
1226+
This instruction now requires an exact :class:`bool` operand.
1227+
12071228
.. opcode:: POP_JUMP_IF_NOT_NONE (delta)
12081229

12091230
If ``STACK[-1]`` is not ``None``, increments the bytecode counter by *delta*.

Include/internal/pycore_code.h

+8
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ typedef struct {
101101

102102
#define INLINE_CACHE_ENTRIES_SEND CACHE_ENTRIES(_PySendCache)
103103

104+
typedef struct {
105+
uint16_t counter;
106+
uint16_t version[2];
107+
} _PyToBoolCache;
108+
109+
#define INLINE_CACHE_ENTRIES_TO_BOOL CACHE_ENTRIES(_PyToBoolCache)
110+
104111
// Borrowed references to common callables:
105112
struct callable_cache {
106113
PyObject *isinstance;
@@ -246,6 +253,7 @@ extern void _Py_Specialize_UnpackSequence(PyObject *seq, _Py_CODEUNIT *instr,
246253
int oparg);
247254
extern void _Py_Specialize_ForIter(PyObject *iter, _Py_CODEUNIT *instr, int oparg);
248255
extern void _Py_Specialize_Send(PyObject *receiver, _Py_CODEUNIT *instr);
256+
extern void _Py_Specialize_ToBool(PyObject *value, _Py_CODEUNIT *instr);
249257

250258
/* Finalizer function for static codeobjects used in deepfreeze.py */
251259
extern void _PyStaticCode_Fini(PyCodeObject *co);

Include/internal/pycore_opcode.h

+69-68
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)