comparison Python/sysmodule.c @ 15487:f0ca4c67957b legacy-trunk

_getframe(): New sys module function for getting at the stack frame. Implements and closes SF patch #102106, with Guido's suggested documentation changes.
author Barry Warsaw <barry@python.org>
date Wed, 06 Dec 2000 21:47:46 +0000
parents 6f6f965c5e2d
children 0f42f6669e80
comparison
equal deleted inserted replaced
15486:b3398c452d95 15487:f0ca4c67957b
13 - argv: script arguments (list of strings) 13 - argv: script arguments (list of strings)
14 - ps1, ps2: optional primary and secondary prompts (strings) 14 - ps1, ps2: optional primary and secondary prompts (strings)
15 */ 15 */
16 16
17 #include "Python.h" 17 #include "Python.h"
18 #include "compile.h"
19 #include "frameobject.h"
18 20
19 #include "osdefs.h" 21 #include "osdefs.h"
20 22
21 #ifdef HAVE_UNISTD_H 23 #ifdef HAVE_UNISTD_H
22 #include <unistd.h> 24 #include <unistd.h>
282 return NULL; 284 return NULL;
283 return get_counts(); 285 return get_counts();
284 } 286 }
285 #endif 287 #endif
286 288
289 static char getframe_doc[] =
290 "_getframe([depth]) -> frameobject\n\
291 \n\
292 Return a frame object from the call stack. If optional integer depth is\n\
293 given, return the frame object that many calls below the top of the stack.\n\
294 If that is deeper than the call stack, ValueError is raised. The default\n\
295 for depth is zero, returning the frame at the top of the call stack.\n\
296 \n\
297 This function should be used for internal and specialized\n\
298 purposes only.";
299
300 static PyObject *
301 sys_getframe(PyObject *self, PyObject *args)
302 {
303 PyFrameObject *f = PyThreadState_Get()->frame;
304 int depth = -1;
305
306 if (!PyArg_ParseTuple(args, "|i:_getframe", &depth))
307 return NULL;
308
309 while (depth > 0 && f != NULL) {
310 f = f->f_back;
311 --depth;
312 }
313 if (f == NULL) {
314 PyErr_SetString(PyExc_ValueError,
315 "call stack is not deep enough");
316 return NULL;
317 }
318 Py_INCREF(f);
319 return (PyObject*)f;
320 }
321
322
287 #ifdef Py_TRACE_REFS 323 #ifdef Py_TRACE_REFS
288 /* Defined in objects.c because it uses static globals if that file */ 324 /* Defined in objects.c because it uses static globals if that file */
289 extern PyObject *_Py_GetObjects(PyObject *, PyObject *); 325 extern PyObject *_Py_GetObjects(PyObject *, PyObject *);
290 #endif 326 #endif
291 327
311 {"gettotalrefcount", sys_gettotalrefcount, 1}, 347 {"gettotalrefcount", sys_gettotalrefcount, 1},
312 #endif 348 #endif
313 {"getrefcount", sys_getrefcount, 1, getrefcount_doc}, 349 {"getrefcount", sys_getrefcount, 1, getrefcount_doc},
314 {"getrecursionlimit", sys_getrecursionlimit, 1, 350 {"getrecursionlimit", sys_getrecursionlimit, 1,
315 getrecursionlimit_doc}, 351 getrecursionlimit_doc},
352 {"_getframe", sys_getframe, 1, getframe_doc},
316 #ifdef USE_MALLOPT 353 #ifdef USE_MALLOPT
317 {"mdebug", sys_mdebug, 1}, 354 {"mdebug", sys_mdebug, 1},
318 #endif 355 #endif
319 {"setdefaultencoding", sys_setdefaultencoding, 1, 356 {"setdefaultencoding", sys_setdefaultencoding, 1,
320 setdefaultencoding_doc}, 357 setdefaultencoding_doc},