This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: getframe hook (Psyco #1)
Type: Stage:
Components: Interpreter Core Versions: Python 2.3
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: mwh Nosy List: arigo, gvanrossum, mwh
Priority: normal Keywords: patch

Created on 2002-10-01 23:17 by arigo, last changed 2022-04-10 16:05 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
diff-2.2 arigo, 2002-10-01 23:17 context diff against the 2.2 branch
second-diff-2.2 arigo, 2002-10-04 15:46 simpler patch, context diff against the 2.2 branch
diff-2.3 arigo, 2002-10-07 14:32 for 2.3 (no difference here apart from line numbers)
Messages (7)
msg41280 - (view) Author: Armin Rigo (arigo) * (Python committer) Date: 2002-10-01 23:17
Psyco-friendly patch #1

Allow Psyco-like extension modules to quickly
plug their own notion of frame call stack into Python,
for the use of
the interpreter's parts that rely on the call stack for
context
information.

For example, the code that creates a new class
implicitely
reads the top frame's globals if the class does not
explicitely defines
a __module__ attribute.  With this new hook, Psyco can
provide such code with the expected frame object.


pystate.h: The PyThreadState structure has a new field
added at its end:

    Py_getframehook getframe;

where

    typedef PyFrameObject
*(*Py_getframehook)(PyThreadState *, int);

This field points to a function that returns the nth
frame object in the
call stack.  By default, it points to a standard
function that starts
with tstate->frame and walks their f_back fields, just
like the
implementation of sys._getframe().

The purpose of this is to allow Psyco to hook another
function at this
point, in order to lazily emulate the frame objects
that correspond to
frames executed by Psyco.


sysmodule.c: sys_getframe() calls the hook.

ceval.c: PyEval_GetFrame() calls the hook.

various other places in ceval.c:
  replaced PyThreadState_Get()->frame with
PyEval_GetFrame()
  so that the hook will be called.

pyexpat.c: replaced a
PyThreadState_Get()->frame->f_globals
  with PyEval_GetGlobals().

Note that there are other places using 'frame' and the
'f_back'
pointers which have not been changed because they are
concerned with actual (classical) bytecode
interpretation.  The hook is only used in places that
are interested in obtaining contextual information
(like what the previous frame's globals are), not in
places that
actually builds frames in which bytecode will be
interpreted.

Compatibility: third-party extension modules directly
reading
frame, like Expat before this patch, will exhibit a
marginally wrong
behavior with Psyco until they are modified to call the
hook (or better
the "official" interpreter routines that are modified
to so do).  It
does not break anything at all as long as we are not
using Psyco.

Performance overhead: one more indirect call isn't
heavy.  More
importantly, I don't expect the concerned functions to
be used more
than occasionally in any code.
msg41281 - (view) Author: Michael Hudson (mwh) (Python committer) Date: 2002-10-04 10:43
Logged In: YES 
user_id=6656

I'd be uneasy about a change of this subtlety going into the
2.2 branch.

Aren't there other ways you can do this?
msg41282 - (view) Author: Armin Rigo (arigo) * (Python committer) Date: 2002-10-04 15:46
Logged In: YES 
user_id=4771

Here is a simpler patch doing only the one thing that I
really cannot work around in Psyco (and I've tried, believe
me!) :  a way to hook my own replacement function for
PyEval_GetFrame().

No more sysmodule change.  Just a few places here and there
with 'PyThreadState_Get()->frame' replaced with
'PyEval_GetFrame()' so that my hook will trigger.

Is the new patch clean enough ?  If so I'll assign it to
Guido for review.
msg41283 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2002-10-06 20:46
Logged In: YES 
user_id=6380

I'd like to get this int. 2.2.2. MWH, can you check it in?
msg41284 - (view) Author: Michael Hudson (mwh) (Python committer) Date: 2002-10-07 09:48
Logged In: YES 
user_id=6656

OK, checked in as:

Include/pystate.h revision 2.18.16.2
Modules/pyexpat.c revision 2.57.6.4
Python/ceval.c revision 2.301.4.8
Python/pystate.c revision 2.20.16.1
msg41285 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2002-10-07 12:34
Logged In: YES 
user_id=6380

Thanks much Michael for the three sets of Psyco checkins in
2.2.2!

Armin, I think for Python 2.3 some patches must be different
because there's no SET_LINENO opcode. Can you provide
updated versions for those?
msg41286 - (view) Author: Michael Hudson (mwh) (Python committer) Date: 2002-11-08 12:59
Logged In: YES 
user_id=6656

I got lazy and checked in all the psyco patches at once:

Include/pystate.h revision 2.21
Modules/pyexpat.c revision 2.76
Python/ceval.c revision 2.340
Python/pystate.c revision 2.22
History
Date User Action Args
2022-04-10 16:05:43adminsetgithub: 37245
2002-10-01 23:17:21arigocreate