#! /usr/bin/env python"""Find the maximum recursion limit that prevents core dumpsThis script finds the maximum safe recursion limit on a particularplatform. If you need to change the recursion limit on your system,this script will tell you a safe upper bound. To use the new limit,call sys.setrecursionlimit.This module implements several ways to create infinite recursion inPython. Different implementations end up pushing different numbers ofC stack frames, depending on how many calls through Python's abstractC API occur.After each round of tests, it prints a messageLimit of NNNN is fine.It ends when Python causes a segmentation fault because the limit istoo high. On platforms like Mac and Windows, it should exit with aMemoryError.NB: A program that does not use __methods__ can set a higher limit."""importsysclassRecursiveBlowup1:def__init__(self):self.__init__()deftest_init():returnRecursiveBlowup1()classRecursiveBlowup2:def__repr__(self):returnrepr(self)deftest_repr():returnrepr(RecursiveBlowup2())classRecursiveBlowup4:def__add__(self,x):returnx+selfdeftest_add():returnRecursiveBlowup4()+RecursiveBlowup4()classRecursiveBlowup5:def__getattr__(self,attr):returngetattr(self,attr)deftest_getattr():returnRecursiveBlowup5().attrclassRecursiveBlowup6:def__getitem__(self,item):returnself[item-2]+self[item-1]deftest_getitem():returnRecursiveBlowup6()[5]deftest_recurse():returntest_recurse()defcheck_limit(n,test_func_name):sys.setrecursionlimit(n)iftest_func_name.startswith("test_"):printtest_func_name[5:]else:printtest_func_nametest_func=globals()[test_func_name]try:test_func()exceptRuntimeError:passelse:print"Yikes!"limit=1000while1:check_limit(limit,"test_recurse")check_limit(limit,"test_add")check_limit(limit,"test_repr")check_limit(limit,"test_init")check_limit(limit,"test_getattr")check_limit(limit,"test_getitem")print"Limit of %d is fine"%limitlimit=limit+100