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

noreply@sourceforge.net noreply@sourceforge.net
Fri, 04 Oct 2002 03:43:56 -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: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Armin Rigo (arigo)
Assigned to: Nobody/Anonymous (nobody)
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: 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