Skip to content
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

GH-106008: Make implicit boolean conversions explicit #106003

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ Parser/token.c generated
Programs/test_frozenmain.h generated
Python/Python-ast.c 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 @@ -43,12 +43,12 @@
adaptive bytecode can be shown by passing ``adaptive=True``.


Example: Given the function :func:`myfunc`::

Check warning on line 46 in Doc/library/dis.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:func reference target not found: myfunc

def myfunc(alist):
return len(alist)

the following command can be used to display the disassembly of

Check warning on line 51 in Doc/library/dis.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:func reference target not found: myfunc
:func:`myfunc`:

.. doctest::
Expand Down Expand Up @@ -529,6 +529,9 @@

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 @@
.. 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 @@ -841,7 +851,7 @@

.. opcode:: LOAD_BUILD_CLASS

Pushes :func:`builtins.__build_class__` onto the stack. It is later called

Check warning on line 854 in Doc/library/dis.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:func reference target not found: builtins.__build_class__
to construct a class.


Expand Down Expand Up @@ -898,14 +908,14 @@

.. opcode:: STORE_NAME (namei)

Implements ``name = STACK.pop()``. *namei* is the index of *name* in the attribute

Check warning on line 911 in Doc/library/dis.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:attr reference target not found: co_names
:attr:`co_names` of the code object. The compiler tries to use
:opcode:`STORE_FAST` or :opcode:`STORE_GLOBAL` if possible.


.. opcode:: DELETE_NAME (namei)

Implements ``del name``, where *namei* is the index into :attr:`co_names`

Check warning on line 918 in Doc/library/dis.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:attr reference target not found: co_names
attribute of the code object.


Expand Down Expand Up @@ -944,7 +954,7 @@
value = STACK.pop()
obj.name = value

where *namei* is the index of name in :attr:`co_names`.

Check warning on line 957 in Doc/library/dis.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:attr reference target not found: co_names

.. opcode:: DELETE_ATTR (namei)

Expand All @@ -953,7 +963,7 @@
obj = STACK.pop()
del obj.name

where *namei* is the index of name into :attr:`co_names`.

Check warning on line 966 in Doc/library/dis.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:attr reference target not found: co_names


.. opcode:: STORE_GLOBAL (namei)
Expand Down Expand Up @@ -1127,7 +1137,12 @@
.. 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 @@
.. 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 @@
.. 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.