[Python-checkins] CVS: python/dist/src/Python sysmodule.c,2.78,2.79

Barry Warsaw python-dev@python.org
Wed, 6 Dec 2000 13:47:17 -0800


Update of /cvsroot/python/python/dist/src/Python
In directory slayer.i.sourceforge.net:/tmp/cvs-serv7671

Modified Files:
	sysmodule.c 
Log Message:
_getframe(): New sys module function for getting at the stack frame.
Implements and closes SF patch #102106, with Guido's suggested
documentation changes.


Index: sysmodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/sysmodule.c,v
retrieving revision 2.78
retrieving revision 2.79
diff -C2 -r2.78 -r2.79
*** sysmodule.c	2000/09/01 23:29:28	2.78
--- sysmodule.c	2000/12/06 21:47:14	2.79
***************
*** 16,19 ****
--- 16,21 ----
  
  #include "Python.h"
+ #include "compile.h"
+ #include "frameobject.h"
  
  #include "osdefs.h"
***************
*** 285,288 ****
--- 287,324 ----
  #endif
  
+ static char getframe_doc[] =
+ "_getframe([depth]) -> frameobject\n\
+ \n\
+ Return a frame object from the call stack.  If optional integer depth is\n\
+ given, return the frame object that many calls below the top of the stack.\n\
+ If that is deeper than the call stack, ValueError is raised.  The default\n\
+ for depth is zero, returning the frame at the top of the call stack.\n\
+ \n\
+ This function should be used for internal and specialized\n\
+ purposes only.";
+ 
+ static PyObject *
+ sys_getframe(PyObject *self, PyObject *args)
+ {
+ 	PyFrameObject *f = PyThreadState_Get()->frame;
+ 	int depth = -1;
+ 
+ 	if (!PyArg_ParseTuple(args, "|i:_getframe", &depth))
+ 		return NULL;
+ 
+ 	while (depth > 0 && f != NULL) {
+ 		f = f->f_back;
+ 		--depth;
+ 	}
+ 	if (f == NULL) {
+ 		PyErr_SetString(PyExc_ValueError,
+ 				"call stack is not deep enough");
+ 		return NULL;
+ 	}
+ 	Py_INCREF(f);
+ 	return (PyObject*)f;
+ }
+ 
+ 
  #ifdef Py_TRACE_REFS
  /* Defined in objects.c because it uses static globals if that file */
***************
*** 314,317 ****
--- 350,354 ----
  	{"getrecursionlimit", sys_getrecursionlimit, 1,
  	 getrecursionlimit_doc},
+ 	{"_getframe", sys_getframe, 1, getframe_doc},
  #ifdef USE_MALLOPT
  	{"mdebug",	sys_mdebug, 1},