18.02.20 20:13, Guido van Rossum пише:
I am a little confused how you get from "there are extra frames in the traceback" to "modify exec() to run code in another frame".

Sorry for skipping several steps. I'll try to expand them here. PEP 558 does not have relation to this, as frame is not the part of locals().

exec() is used by the import machinery to execute the module code. Currently frames look like this:

    import foo  # user code
    # many frames in the import machinery
    # ...
    # ...
    exec(code, module.__dict__)  # still in the import machinery
    # user code in foo.py

I propose to add a parameter to exec() which will allow to execute the code in the imported mode as exec() was called immediately from the importer code:

    import foo  # user code
    # user code in foo.py

As it was when the import machinery was implemented in C.

I did not propose to modify exec() to run code in another frame, I proposde to modify exec() to run code in new frame whose parent is a specified frame (either arbitrary frame or on of ancestors of the caller frame).

I think it may be not difficult to do. I ask whether there are other applications of this or similar feature and what form of the feature will be more convenient.

It just occurred to me that it may be not a feature of exec(), but a separate function which will allow to execute an arbitrary function, not just a Python code, as it was called from the different frame.

    apply_in_frame(frame, func, /, *args, **kwargs)

or

    apply_with_frames_skipped(stacklevel, func, /, *args, **kwargs)

The question is how to call this function and where to put it. In the first variant it could be a method of frame.

_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/LEYVARMREKR74BKUOPSSIKN7ZF674WDQ/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to