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

Add f_state attribute to FrameObjects. #91202

Open
markshannon opened this issue Mar 17, 2022 · 11 comments
Open

Add f_state attribute to FrameObjects. #91202

markshannon opened this issue Mar 17, 2022 · 11 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@markshannon
Copy link
Member

BPO 47046
Nosy @nedbat, @markshannon, @brandtbucher

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2022-03-17.16:40:39.177>
labels = []
title = 'Add `f_state` attribute to FrameObjects.'
updated_at = <Date 2022-04-08.11:36:06.350>
user = 'https://github.com/markshannon'

bugs.python.org fields:

activity = <Date 2022-04-08.11:36:06.350>
actor = 'Mark.Shannon'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = []
creation = <Date 2022-03-17.16:40:39.177>
creator = 'Mark.Shannon'
dependencies = []
files = []
hgrepos = []
issue_num = 47046
keywords = []
message_count = 3.0
messages = ['415427', '415456', '416978']
nosy_count = 3.0
nosy_names = ['nedbat', 'Mark.Shannon', 'brandtbucher']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = None
url = 'https://bugs.python.org/issue47046'
versions = []

@markshannon
Copy link
Member Author

When tracing, the event supplied is insufficient to determine what is actually happening.

E.g. A "call" event could be a call to a function or resuming a generator or coroutine.

Adding a state field to the FrameObject would allow these cases to be disambiguated without having to make dubious deductions from f_lasti or other frame attributes.

The proposed states would be:

FRAME_CREATED # Frame created, but not executed at all
FRAME_SUSPENDED # Frame suspended after yield or yield from
FRAME_EXECUTING # Frame is executed normally
FRAME_COMPLETED # Frame has completed
FRAME_CLEARED # Frame has been cleared

Ned, any other states that you might need to know about?

@nedbat
Copy link
Member

nedbat commented Mar 17, 2022

These look great. Currently, coverage.py only tries to distinguish between return/yield, which I guess will now be COMPLETED and SUSPENDED?

@markshannon
Copy link
Member Author

Don't you need to know if a "call" event is a call or the resumption of a generator?

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
@nedbat
Copy link
Member

nedbat commented Apr 10, 2022

@markshannon Sorry, you are right, I also distinguish between call and resume:

#ifdef RESUME   // 3.11.0a4
    /*
     * The current opcode is guaranteed to be RESUME. The argument
     * determines what kind of resume it is.
     */
    PyObject * pCode = MyFrame_GetCode(frame)->co_code;
    real_call = (PyBytes_AS_STRING(pCode)[PyFrame_GetLasti(frame) + 1] == 0);
#else
    // f_lasti is -1 for a true call, and a real byte offset for a generator re-entry.
    real_call = (PyFrame_GetLasti(frame) < 0);
#endif

    if (real_call) {
        self->pcur_entry->last_line = -MyFrame_GetCode(frame)->co_firstlineno;
    }
    else {
        self->pcur_entry->last_line = PyFrame_GetLineNumber(frame);
    }

@AlexWaygood AlexWaygood added the type-feature A feature request or enhancement label Apr 10, 2022
@markshannon
Copy link
Member Author

markshannon commented Apr 11, 2022

As our automation doesn't seem to be quite there yet, here's a link to the PR implementing this:
#91443

@markshannon
Copy link
Member Author

Unfortunately, this isn't useful for distinguishing events during tracing.

Because "call" events occur immediately after the call or resumption, then f_state is FRAME_EXECUTING for both calls and resumptions.
Similarly, "return" events occur immediately before the return or yield, so f_state is FRAME_EXECUTING is for both returns and yields.

We can't add new events, or change the sequencing without breaking things, so it looks like we will have to wait for PEP 669 and 3.12 to have a nice API for events. 😞

@nedbat
Copy link
Member

nedbat commented Apr 11, 2022

Can you make a recommendation for the best way to make this distinction in 3.11?

@markshannon
Copy link
Member Author

markshannon commented Apr 21, 2022

Can you make a recommendation for the best way to make this distinction in 3.11?

We aren't going to change anything for 3.11, so your check of the RESUME operand is probably best.

@nedbat
Copy link
Member

nedbat commented May 2, 2022

I've fixed the coverage.py compilation errors in the nedbat/three-eleven-beta branch, but the results are wrong for generators. I'm not sure what detail I'm missing. @markshannon? @vstinner?

@vstinner
Copy link
Member

vstinner commented May 2, 2022

I've fixed the coverage.py compilation errors

Which compiler errors? Which Python version are you using (release or the current main branch)?

@vstinner
Copy link
Member

vstinner commented May 2, 2022

Is your comment related to nedbat/coveragepy#1367 (comment) issue?

@iritkatriel iritkatriel added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Nov 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

5 participants