◐ Shell
clean mode source ↗

bpo-28810: Document remaining bytecode changes in 3.6 by ilevkivskyi · Pull Request #651 · python/cpython

@mention-bot

serhiy-storchaka



The Python compiler currently generates the following bytecode instructions.
Every instruction takes 2 bytes.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be moved to the top of the document, under impl-detail block, and decorated with versionchanged.

This generator function uses the ``co_firstlineno`` and ``co_lnotab``
attributes of the code object *code* to find the offsets which are starts of
lines in the source code. They are generated as ``(offset, lineno)`` pairs.
Functions decoding directly ``co_lnotab`` should use a signed

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too much details for common user of the dis module, but too few details for reproducing decoding algorithm. I think just adding the reference to Objects/lnotab_notes.txt is enough.

@ilevkivskyi

@serhiy-storchaka Thanks!

I think this should be moved to the top of the document, under impl-detail block

I was also thinking about this. I implemented both your comments in a new commit.

ncoghlan

This generator function uses the ``co_firstlineno`` and ``co_lnotab``
attributes of the code object *code* to find the offsets which are starts of
lines in the source code. They are generated as ``(offset, lineno)`` pairs.
See ``Objects/lnotab_notes.txt`` for the ``co_lnotab`` format and how to

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a source ref here turn this into a suitable hyperlink to GitHub:

:source:`Objects/lnotab_notes.txt`

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, Nick!
Implemented this is a new commit.

@ilevkivskyi

By the way, I have noticed that the example in Objects/lnotab_notes.txt is outdated. It still uses Python 2 and old bytecode (not 2 bytes per instruction).

Should I update it in this PR or open a separate issue and make a separate PR?

@brettcannon

@ilevkivskyi

separate PR would be great!

OK, will do this.
So it looks like this PR could be merged now.

serhiy-storchaka

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The meaning of HAVE_ARGUMENT was changed too. Now all instructions have an argument, but opcodes < HAVE_ARGUMENT ignore it.

how to decode it.

.. versionchanged:: 3.6
Added support for negative line number delta.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks as implementation detail that should be described in lnotab_notes.txt. The change visible for the user of findlinestarts() is that line numbers now can not be monotonically increasing.

work across Python VMs or Python releases.

.. versionchanged:: 3.6
Use fixed 2 bytes per instruction for all instructions.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be worth to mention that instructions of variable length was used before.

@ilevkivskyi

serhiy-storchaka

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know whether the changes use good English, but in general LGTM.

vstinner

how to decode it.

.. versionchanged:: 3.6
Line numbers can be not monotonically increasing.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"not monotonically" is not easy to understand. In What's New in Python 3.6, I wrote:

"The format of the co_lnotab attribute of code objects changed to support a negative line number delta"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is how I initially wrote it, but @serhiy-storchaka wanted me to change this to this wording.
His motivation is that negative delta is not a visible effect for user, see #651 (comment)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the wording for describing the implementation details of co_lnotab. What is important for the user of findlinestarts() which shouldn't know anything about co_lnotab is that now this function can produce non-monotonic line numbers. That can affect user code.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just say "Line numbers can now go backwards." if you prefer. I would like to avoid "monotonically", especially "not monotonically".

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line numbers can now go backwards.

This sounds not very clear to me. I changed this to an alternative formulation "Line numbers can be decreasing. Before, they were always increasing." I hope this works.

brettcannon


.. versionchanged:: 3.6
Use fixed 2 bytes per instruction for all instructions. Instructions
of variable length were used before.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would phrase this as "Use 2 bytes for each instruction. Previously the number of bytes varied by instruction."

those which do ``>= HAVE_ARGUMENT``.

.. versionchanged:: 3.6
Now every instruction have an argument, but opcodes ``< HAVE_ARGUMENT``

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Now every instruction has an argument"

@ilevkivskyi

brettcannon

brettcannon pushed a commit to brettcannon/cpython that referenced this pull request

Mar 24, 2017
(cherry picked from commit 8f9e1bb)

brettcannon added a commit that referenced this pull request

Mar 24, 2017
(cherry picked from commit 8f9e1bb)