简体   繁体   English

runtimeerror:最大递归深度超过python

[英]runtimeerror: maximum recursion depth exceeded python

I keep getting this error"RuntimeError: maximum recursion depth exceeded while calling a Python object" when I try running my host program for smart card test tool with 600 tests and I get this error after the 300th test, I tried "sys.setrecursionlimit(10000)" and that fixed the problem, but I do know that this is not the best way to go about this error, how can I change my code so I don't run into this error: 当我尝试使用600个测试运行智能卡测试工具的主机程序时,我一直收到此错误“ RuntimeError:调用Python对象时超出了最大递归深度”,而在第300次测试后却收到此错误,我尝试了“ sys.setrecursionlimit( 10000)”,并且解决了该问题,但我确实知道这不是解决此错误的最佳方法,该如何更改代码,以免遇到此错误:

def SndRcv(self,request):
    print ">> ", request
    device_api.send(request)
    resp = device_api.receive()
    print "<< ", resp
    self.processResponse(resp)

def processResponse(self, K400Message):
    global mWaitingCardRemoval
    ciMsg = card_interface_response
    ciMsgType = card_interface_response.ci_msg

    if ciMsgType is None:
        print 'weird, malformed protobuf response'
        return
    whichMsg = ciMsgType.WhichOneof('msg')
    print 'msg = ' + str(whichMsg)
    if whichMsg is 'collision':
        self.StartSession()
    elif whichMsg is 'card_removed':
        if ciMsgType.issuer== ci.CARD_INTERFACE_MASK_CxLESS:                
            mWaitingCardRemoval &= ~(ciMsgType.issuer)
            if EndofSession is False:
                self.parseMessage()
            if mWaitingCardRemoval !=0:
                self.parseMessage()
            self.StartSession()
    elif whichMsg is 'waiting_removal':
        if EndofSession is False:
            self.parseMessage()
        else:
            mWaitingCardRemoval |= ciMsgType.issuer
    elif whichMsg is 'card_detected':
        mode = ciMsgType.issuer
        reqMsg = pm.get_Deactivate((ci.CARD_INTERFACE_MASK_ANY)& ~(ciMsgType.issuer))
        self.SendOnly(reqMsg)
        acceptMsg = pm.get_Activate(mode)
        self.SndRcv(acceptMsg)
    elif whichMsg is 'card_ready':
        self.StartLoop(ciMsgType.issuer)
    elif whichMsg is 'rapdu':
        self.processCardAPDUResponse(ciMsgType.issuer, ciMsg.data.encode('hex'))
    elif whichMsg is 'card_not_responding':
        if ciMsgType.issuer == ci.CARD_INTERFACE_MASK_CONTACT:
            self.EndCardSession(ciMsgType.issuer,True)
        else:
            self.EndCardSession(ciMsgType.issuer, False)
    elif whichMsg is 'resp_special':
        if ciMsg.data.encode('hex') > 0:
            logging.info(ciMsg.data.encode('hex'))
        else:
            logging.info("")

You used recursion to code an inherently iterative process. 您使用递归来编码一个固有的迭代过程。 You're not actually reducing a large problem to a smaller one; 您实际上并没有将一个大问题减少到一个较小的问题。 you're stepping through a sequence of inputs. 您正在逐步进行一系列输入。 Once you process an input and report the response, you're done with it. 处理输入并报告响应后,就完成了。 There's no reason to keep it's context on the call stack. 没有理由将其上下文保留在调用堆栈中。 When you hit the final test and return through your thousand-plus calls, you do nothing with the results or function state to return to the main program. 当您到达最终测试并通过上千次调用返回时,您对结果或函数状态将不执行任何操作以返回主程序。

Rewrite this as a straightforward iteration. 将其重写为简单的迭代。 How do you start? 你如何开始? How do you progress from one test to another? 您如何从一项测试过渡到另一项? How do you know when you're done? 完成后如何知道? For instance, it's quite possible that your outermost loop will hinge on a simple while : 例如,最外层的循环很可能取决于一个简单的while

# Get first response
while ciMsgType is not None:
    # Process this response
    # Get next response

Does that get you moving? 那会让你动起来吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM