Skip to content

GH-106008: Make implicit boolean conversions explicit #106003

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,9 @@ Parser/parser.c generated
Parser/token.c generated
Programs/test_frozenmain.h generated
Python/Python-ast.c generated
Python/generated_cases.c.h generated
Python/executor_cases.c.h generated
Python/generated_cases.c.h generated
Python/opcode_metadata.h generated
Python/opcode_targets.h generated
Python/stdlib_module_names.h generated
Tools/peg_generator/pegen/grammar_parser.py generated
Expand Down
23 changes: 22 additions & 1 deletion Doc/library/dis.rst
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,9 @@ result back on the stack.

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

.. versionchanged:: 3.13
This instruction now requires an exact :class:`bool` operand.


.. opcode:: UNARY_INVERT

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


.. opcode:: TO_BOOL

Implements ``STACK[-1] = bool(STACK[-1])``.

.. versionadded:: 3.13


**Binary and in-place operations**

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

Performs a Boolean operation. The operation name can be found in
``cmp_op[opname]``.
``cmp_op[opname >> 5]``. If the fifth-lowest bit of ``opname`` is set
(``opname & 16``), the result should be coerced to ``bool``.

.. versionchanged:: 3.13
The fifth-lowest bit of the oparg now indicates a forced conversion to
:class:`bool`.


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

.. versionchanged:: 3.13
This instruction now requires an exact :class:`bool` operand.

.. opcode:: POP_JUMP_IF_FALSE (delta)

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

.. versionchanged:: 3.13
This instruction now requires an exact :class:`bool` operand.

.. opcode:: POP_JUMP_IF_NOT_NONE (delta)

If ``STACK[-1]`` is not ``None``, increments the bytecode counter by *delta*.
Expand Down
8 changes: 8 additions & 0 deletions Include/internal/pycore_code.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ typedef struct {

#define INLINE_CACHE_ENTRIES_SEND CACHE_ENTRIES(_PySendCache)

typedef struct {
uint16_t counter;
uint16_t version[2];
} _PyToBoolCache;

#define INLINE_CACHE_ENTRIES_TO_BOOL CACHE_ENTRIES(_PyToBoolCache)

// Borrowed references to common callables:
struct callable_cache {
PyObject *isinstance;
Expand Down Expand Up @@ -246,6 +253,7 @@ extern void _Py_Specialize_UnpackSequence(PyObject *seq, _Py_CODEUNIT *instr,
int oparg);
extern void _Py_Specialize_ForIter(PyObject *iter, _Py_CODEUNIT *instr, int oparg);
extern void _Py_Specialize_Send(PyObject *receiver, _Py_CODEUNIT *instr);
extern void _Py_Specialize_ToBool(PyObject *value, _Py_CODEUNIT *instr);

/* Finalizer function for static codeobjects used in deepfreeze.py */
extern void _PyStaticCode_Fini(PyCodeObject *co);
Expand Down
137 changes: 69 additions & 68 deletions Include/internal/pycore_opcode.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading