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-96461: clarify the meaning of the oparg for CACHE and COPY opcode #96462
base: main
Are you sure you want to change the base?
Conversation
Doc/library/dis.rst
Outdated
@@ -398,13 +401,17 @@ The Python compiler currently generates the following bytecode instructions. | |||
Push the *i*-th item to the top of the stack. The item is not removed from its | |||
original location. | |||
|
|||
The stack is indexed from 1, so COPY 1 will copy TOS. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe instead of adding this, we could define TOS(i)
at the top along with TOS1, TOS2 etc, and then reword the definition here to use those.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried something along those lines in my last commit let me know if you like it better this way.
Doc/library/dis.rst
Outdated
@@ -382,6 +382,10 @@ The Python compiler currently generates the following bytecode instructions. | |||
|
|||
**General instructions** | |||
|
|||
In the following, TOS(i) refer to the *i*-th item on the stack which is index from 1. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the following, TOS(i) refer to the *i*-th item on the stack which is index from 1. | |
In the following, TOS(i) refers to the *i*-th item on the stack which is indexed from 1. |
Doc/library/dis.rst
Outdated
@@ -382,6 +382,10 @@ The Python compiler currently generates the following bytecode instructions. | |||
|
|||
**General instructions** | |||
|
|||
In the following, TOS(i) refer to the *i*-th item on the stack which is index from 1. | |||
We also use as shorthand TOS = TOS(1) which is the top-of-stack. | |||
TOS1, TOS2, TOS3 are the second, third and fourth items on the stack, respectively. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a bit unfortunate that TOS1 = TOS(2), TOS2 = TOS(3), etc.
@markshannon - would you like to change this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was another reason for my previous formulation that avoided ro shed too much light on this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally we count from one for stack items, so I would like to change it. Ideally drop TOS1
etc, all together.
The current explanation is not explicit about what is popped or not, so the shorthand is not helpful.
@@ -395,15 +399,15 @@ The Python compiler currently generates the following bytecode instructions. | |||
|
|||
.. opcode:: COPY (i) | |||
|
|||
Push the *i*-th item to the top of the stack. The item is not removed from its | |||
Push TOS(i) to the top of the stack. The item is not removed from its |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe "Push a copy of i
th item (counting from 1) to the top of the stack ..."
Doc/library/dis.rst
Outdated
original location. | ||
|
||
.. versionadded:: 3.11 | ||
|
||
|
||
.. opcode:: SWAP (i) | ||
|
||
Swap TOS with the item at position *i*. | ||
Swap TOS with TOS(i). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe "Swap the first and i
th item on the stack (counting from 1).
I can rephrase everything to not use TOS(i) and repeat that the stack is indexed from 1 in each relevant place however they are quite a few occurrences (many more than what this PR currently does). @iritkatriel do you agree with @markshannon on this ? |
I agree that we don't need to have both TOS(i) and TOSi, I think removing the TOSis is the smaller change, amirite? @markshannon - were you suggesting to add "(counting from 1)" on each place where TOS(i) is used? Or just once where it's defined or used for the first time? |
Removing the TOSi should be a smaller change yes and would not entail any large duplication. |
I went ahead and replaced TOS by TOS(1) I am not sure this a clear win for the reader TBH. Let me know what you think. |
I don't know if this has already been considered, but one approach might be to eliminate "
What do you think? |
That may lead to clearer description indeed. I must say I first tried to make the changes minimal but I can undertake a larger changes if it is generallyvthough to be useful. |
This could be backported to Python 3.11