[IronPython] Infinite recursion and System.StackOverflowException

Haibo Luo haiboluo at exchange.microsoft.com
Wed Sep 27 07:46:43 CEST 2006

You may need something like


in your PythonEngine code.

In command line, "-X:MaxRecursion 1001" plays the same role.

-----Original Message-----
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Mike McGavin
Sent: Tuesday, September 26, 2006 10:28 PM
To: Discussion of IronPython
Subject: [IronPython] Infinite recursion and System.StackOverflowException


What's the expected behaviour with infinite recursion in IronPython,
and is there some way I can set a maximum recursion depth on a
PythonEngine object?

Here's a simplified example of the problem I'm having -- I've noticed
that if I type the following code into my Cygwin python interpreter
(and presumably most other interpreters):


>>> def q(w):
>>>     print w
>>>     q(w+1)
>>> q(1)


...I get the following output:

Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 3, in q
  File "<stdin>", line 3, in q
  File "<stdin>", line 3, in q
RuntimeError: maximum recursion depth exceeded

If I type identical code into an ipy.exe console, it gets up to
printing recursion 5208, before I get the Windows popup dialog
indicating that ipy.exe has encountered a problem and needs to close.
On debugging, it turns out that a System.StackOverflowException has
been raised.  (No big surprise, I guess.)

MSDN documentation on the StackOverflowException (see
) basically states that it can't and shouldn't be caught, and instead,
it should be up to the application to make sure there's no infinite

I guess the concern I have is that when trying to use IronPython as a
scripting engine for an application, I don't really want to trust the
python code that's being executed.  It'd be a bit annoying if someone
could break the application (accidentally or otherwise) by coding some
infinite recursion and generating a system exception that can't be
caught by the host app.

I'm not sure what the main goals of IronPython are with respect to
being a .Net language and following the same CLR rules, but for me at
least, it'd be nice if it could keep track of recursion depth.  Maybe
being able to set a maximum recursion depth on a PythonEngine object
before having it execute some code, or just being able to trap it
somehow, could be one way to do this.

I'd appreciate any thoughts or feedback.

users mailing list
users at lists.ironpython.com

More information about the Ironpython-users mailing list