[Python-checkins] CVS: python/dist/src/Python ceval.c,2.199,2.200 sysmodule.c,2.76,2.77

Jeremy Hylton python-dev@python.org
Thu, 31 Aug 2000 12:23:04 -0700


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

Modified Files:
	ceval.c sysmodule.c 
Log Message:
add user-modifiable recursion_limit

ceval.c:
    define recurion_limit (static), default value is 2500
    define Py_GetRecursionLimit and Py_SetRecursionLimit
    raise RuntimeError if limit is exceeded
PC/config.h:
    remove plat-specific definition
sysmodule.c:
    add sys.(get|set)recursionlimit


Index: ceval.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/ceval.c,v
retrieving revision 2.199
retrieving revision 2.200
diff -C2 -r2.199 -r2.200
*** ceval.c	2000/08/30 20:25:01	2.199
--- ceval.c	2000/08/31 19:23:01	2.200
***************
*** 299,302 ****
--- 299,316 ----
  
  
+ /* The interpreter's recursion limit */
+ 
+ static int recursion_limit = 2500;
+ 
+ int Py_GetRecursionLimit(void)
+ {
+ 	return recursion_limit;
+ }
+ 
+ void Py_SetRecursionLimit(int new_limit)
+ {
+ 	recursion_limit = new_limit;
+ }
+ 
  /* Status code for main loop (reason for stack unwind) */
  
***************
*** 327,334 ****
  /* Interpreter main loop */
  
- #ifndef MAX_RECURSION_DEPTH
- #define MAX_RECURSION_DEPTH 10000
- #endif
- 
  static PyObject *
  eval_code2(PyCodeObject *co, PyObject *globals, PyObject *locals,
--- 341,344 ----
***************
*** 566,570 ****
  	}
  
! 	if (++tstate->recursion_depth > MAX_RECURSION_DEPTH) {
  		--tstate->recursion_depth;
  		PyErr_SetString(PyExc_RuntimeError,
--- 576,580 ----
  	}
  
! 	if (++tstate->recursion_depth > recursion_limit) {
  		--tstate->recursion_depth;
  		PyErr_SetString(PyExc_RuntimeError,

Index: sysmodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/sysmodule.c,v
retrieving revision 2.76
retrieving revision 2.77
diff -C2 -r2.76 -r2.77
*** sysmodule.c	2000/08/31 15:21:11	2.76
--- sysmodule.c	2000/08/31 19:23:01	2.77
***************
*** 200,203 ****
--- 200,242 ----
  n instructions.  This also affects how often thread switches occur.";
  
+ static PyObject *
+ sys_setrecursionlimit(PyObject *self, PyObject *args)
+ {
+ 	int new_limit;
+ 	if (!PyArg_ParseTuple(args, "i:setrecursionlimit", &new_limit))
+ 		return NULL;
+ 	if (new_limit <= 0) {
+ 		PyErr_SetString(PyExc_ValueError, 
+ 				"recursion limit must be positive");  
+ 		return NULL;
+ 	}
+ 	Py_SetRecursionLimit(new_limit);
+ 	Py_INCREF(Py_None);
+ 	return Py_None;
+ }
+ 
+ static char setrecursionlimit_doc[] =
+ "setrecursionlimit(n)\n\
+ \n\
+ Set the maximum depth of the Python interpreter stack to n.  This\n\
+ limit prevents infinite recursion from causing an overflow of the C\n\
+ stack and crashing Python.  The highest possible limit is platform-\n\
+ dependent.";
+ 
+ static PyObject *
+ sys_getrecursionlimit(PyObject *self, PyObject *args)
+ {
+ 	if (!PyArg_ParseTuple(args, ":getrecursionlimit"))
+ 		return NULL;
+ 	return PyInt_FromLong(Py_GetRecursionLimit());
+ }
+ 
+ static char getrecursionlimit_doc[] =
+ "getrecursionlimit()\n\
+ \n\
+ Return the current value of the recursion limit, the maximum depth\n\
+ of the Python interpreter stack.  This limit prevents infinite\n\
+ recursion from causing an overflow of the C stack and crashing Python.";
+ 
  #ifdef USE_MALLOPT
  /* Link with -lmalloc (or -lmpc) on an SGI */
***************
*** 269,273 ****
  	{"exc_info",	sys_exc_info, 1, exc_info_doc},
  	{"exit",	sys_exit, 0, exit_doc},
! 	{"getdefaultencoding", sys_getdefaultencoding, 1, getdefaultencoding_doc},
  #ifdef COUNT_ALLOCS
  	{"getcounts",	sys_getcounts, 1},
--- 308,313 ----
  	{"exc_info",	sys_exc_info, 1, exc_info_doc},
  	{"exit",	sys_exit, 0, exit_doc},
! 	{"getdefaultencoding", sys_getdefaultencoding, 1,
! 	 getdefaultencoding_doc}, 
  #ifdef COUNT_ALLOCS
  	{"getcounts",	sys_getcounts, 1},
***************
*** 281,290 ****
  #endif
  	{"getrefcount",	sys_getrefcount, 1, getrefcount_doc},
  #ifdef USE_MALLOPT
  	{"mdebug",	sys_mdebug, 1},
  #endif
! 	{"setdefaultencoding", sys_setdefaultencoding, 1, setdefaultencoding_doc},
! 	{"setcheckinterval",	sys_setcheckinterval, 1, setcheckinterval_doc},
  	{"setprofile",	sys_setprofile, 0, setprofile_doc},
  	{"settrace",	sys_settrace, 0, settrace_doc},
  	{NULL,		NULL}		/* sentinel */
--- 321,336 ----
  #endif
  	{"getrefcount",	sys_getrefcount, 1, getrefcount_doc},
+ 	{"getrecursionlimit", sys_getrecursionlimit, 1,
+ 	 getrecursionlimit_doc},
  #ifdef USE_MALLOPT
  	{"mdebug",	sys_mdebug, 1},
  #endif
! 	{"setdefaultencoding", sys_setdefaultencoding, 1,
! 	 setdefaultencoding_doc}, 
! 	{"setcheckinterval",	sys_setcheckinterval, 1,
! 	 setcheckinterval_doc}, 
  	{"setprofile",	sys_setprofile, 0, setprofile_doc},
+ 	{"setrecursionlimit", sys_setrecursionlimit, 1,
+ 	 setrecursionlimit_doc},
  	{"settrace",	sys_settrace, 0, settrace_doc},
  	{NULL,		NULL}		/* sentinel */
***************
*** 377,382 ****
--- 423,430 ----
  exit() -- exit the interpreter by raising SystemExit\n\
  getrefcount() -- return the reference count for an object (plus one :-)\n\
+ getrecursionlimit() -- return the max recursion depth for the interpreter\n\
  setcheckinterval() -- control how often the interpreter checks for events\n\
  setprofile() -- set the global profiling function\n\
+ setrecursionlimit() -- set the max recursion depth for the interpreter\n\
  settrace() -- set the global debug tracing function\n\
  "