<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE TIP SYSTEM "http://tcl.activestate.com/cgi-bin/tct/tip/tipxml.dtd">
<!-- Converted at Thu Feb 09 11:48:47 GMT 2012 -->
<!-- TIP AutoGenerator - written by Donal K. Fellows -->

<TIP number='87'>
<header><title>Allow Tcl Access to the Recursion Limit</title><author address="mailto:sct@po.cwru.edu">Stephen Trier</author><author address="mailto:richard.suchenwirth-bauersachs@siemens.com">Richard Suchenwirth</author><status type='project' state='final' tclversion="8.4" vote='after'>$Revision: 1.11 $</status><history></history><created day='19' month='feb' year='2002' /><discussions url='news:comp.lang.tcl'/><keyword>Tcl_SetRecusionLimit {recursion limit}</keyword></header>
<abstract>An extension to the [interp] command, [interp recursionlimit], will permit Tcl scripts to control their own recursion limits. Until now, this limit has been changeable from a C API, but not from within Tcl.</abstract>
<body><section title="Rationale">
<para>As of Tcl 8.4a3, Tcl scripts must live with the default recursion depth of 1000 nested calls to the <emph style="italic">Tcl_Eval</emph> family of functions or resort to C code to change the limit. Nevertheless, Tcl programmers may find it useful to reduce the limit when debugging or to increase it for scripts that include deeply recursive functions. The changes proposed in this TIP will make this possible in pure Tcl code.</para>
</section>
<section title="Specification">
<describe><item.d name='generic/tclInterp.c'><para>Add subcommands to [interp] and to the slave interpreter object command with the following syntax:</para><para>interp recursionlimit <emph style="italic">path</emph> <emph style="italic">?newlimit?</emph></para><para><emph style="italic">slave</emph> recursionlimit <emph style="italic">?newlimit?</emph></para><para>The parameter <emph style="italic">newlimit</emph> must be a positive integer. When it is present, the limit is changed to <emph style="italic">newlimit</emph> and the command returns the new recursion limit. If the <emph style="italic">newlimit</emph> parameter is absent, the command returns the current recursion limit.</para><para>No maximum value is enforced. It is the programmer&apos;s responsibility to ensure the recursion limit will not overflow the process stack.</para><para>A safe interpreter is not allowed to change the recursion limit for itself nor for any other interpreter. Attempting to do so will generate an error. Safe interpreters are allowed to query recursion limits.</para><para>If an interpreter changes its own recursion limit to a value lower than the current Tcl_Eval nesting level, the limit will be changed, then an error message appropriate to this particular situation will be issued by the recursionlimit command. (Error text: &quot;falling back due to new recursion limit&quot;)</para><para>If an interpreter changes a sub-interpreter&apos;s recursion limit to less than the sub-interpreter&apos;s current Tcl_Eval nesting level, no immediate error is issued. The sub-interpreter will throw a &quot;too many nested calls to Tcl_Eval (infinite loop?)&quot; error if its nesting is still deeper than its recursion limit when next a command is executed in its context.</para></item.d><item.d name='generic/tclTest.c'><para>Remove the now-unnecessary testsetrecursionlimit command.</para></item.d><item.d name='doc/interp.n'><para>Add documentation for the new subcommands, including a warning about stack overflow, much like the warning in the documentation for <emph style="italic">Tcl_SetRecursionLimit()</emph>.</para></item.d><item.d name='test/interp.test'><para>Add tests for the new subcommands.</para></item.d></describe>
</section>
<section title="Comments Received">
<para>Discussion of this TIP took place in the following threads:</para>
<para><url ref="http://groups.google.com/groups?hl=en&amp;threadm=3C6D0A88.5DC9D8B4%40utdt.edu"/></para>
<para><url ref="http://groups.google.com/groups?hl=en&amp;threadm=3C73E98A.8ED9DDE6%40cisco.com"/></para>
<para><url ref="http://www.geocrawler.com/mail/thread.php3?subject=%5BTCLCORE%5D+TIP+%2387%3A+Allow+Tcl+Access+to+the+Recursion+Limit&amp;list=7375"/></para>
<para>Using a command or variable <emph style="italic">::tcl::recursionLimit</emph> to manipulate the limit was initially considered, but Miguel Sofer suggested making the function a subcommand of [interp] because the recursion limit is logically an attribute of each interpreter. Miguel also pointed out that implementing <emph style="italic">TclpCheckStackSpace()</emph> for Unix would mitigate the dangers of setting the recursion limit too high.</para>
<para>comp.lang.tcl saw some discussion of whether it would be appropriate to have a way to completely remove the recursion limit. The consensus was to not add such a feature.</para>
<para>The initial version of this TIP did not provide for a diagnostic error message for the case where the nesting is already deeper than the new recursion level. Ken Fitch, Don Porter, Miguel Sofer, and Donal Fellows discussed whether this was important. This version of the TIP uses Donal Fellows&apos;s suggestion of changing the recursion limit as requested, but providing a meaningful error message if the nesting is too deep for the new limit.</para>
<para>Donal Fellows suggested that slave interpreters should inherit their recursion limit from their parent. As it turns out, this behavior was already present but was not documented. The reference implementation documents it.</para>
</section>
<section title="Reference Implementation">
<para>An implementation of this TIP, with tests and documentation, is patch number 522849 on SourceForge.</para>
</section>
<section title="Copyright">
<para>This document is in the public domain.</para>
</section>
</body></TIP>

