[Patches] [ python-Patches-617309 ] getframe hook (Psyco #1)

noreply@sourceforge.net noreply@sourceforge.net
Mon, 07 Oct 2002 07:31:59 -0700


Patches item #617309, was opened at 2002-10-01 23:17
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=617309&group_id=5470

Category: Core (C code)
Group: Python 2.2.x
Status: Closed
Resolution: Accepted
Priority: 5
Submitted By: Armin Rigo (arigo)
Assigned to: Guido van Rossum (gvanrossum)
Summary: getframe hook (Psyco #1)

Initial Comment:
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.


----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2002-10-07 12:34

Message:
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?

----------------------------------------------------------------------

Comment By: Michael Hudson (mwh)
Date: 2002-10-07 09:48

Message:
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

----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2002-10-06 20:46

Message:
Logged In: YES 
user_id=6380

I'd like to get this int. 2.2.2. MWH, can you check it in?

----------------------------------------------------------------------

Comment By: Armin Rigo (arigo)
Date: 2002-10-04 15:46

Message:
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.

----------------------------------------------------------------------

Comment By: Michael Hudson (mwh)
Date: 2002-10-04 10:43

Message:
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?

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=617309&group_id=5470