记 suds 模块循环依赖的坑-RuntimeError: maximum recursion depth exceeded 转

上一篇 / 下一篇  2016-10-20 13:49:34 / 个人分类:python

记 suds 模块循环依赖的坑-RuntimeError: maximum recursion depth exceeded

下面是soa接口调用的核心代码

复制代码
#! /usr/bin/python#coding:utf-8fromsuds.clientimportClient
defSoaRequest(wsdl,fnname,data): soaService=Client(wsdl).service soaRep=getattr(soaService,fnname)(data)returnsoaRep
复制代码

问题就这样出现了:

我调用一个接口,总是报错,见下图:

之后Debug断点定位到suds模块的sxbasic.py文件中的Import类的open方法

复制代码
defopen(self, options):"""Open and import the refrenced schema.
        @param options: An options dictionary.
        @type options: L{options.Options}
        @return: The referenced schema.
        @rtype: L{Schema}"""ifself.opened:returnself.opened=True
        log.debug('%s, importing ns="%s", location="%s"', self.id, self.ns[1], self.location)
        result=self.locate()ifresultisNone:ifself.locationisNone:
                log.debug('imported schema (%s) not-found', self.ns[1])else:result =self.download(options)
        log.debug('imported:\n%s', result)returnresult
复制代码
self.location 为None时日志打印,否则就执行download
而我测试的接口有个这种问题,A中依赖B,B中依赖C,C中依赖A,循环依赖
运行后控制台就会报错:
RuntimeError: maximum recursion depth exceeded

是不是递归深度不够呢(个人觉得循环的话,深度再大有什么用了)
importsys  
sys.setrecursionlimit(1000000)

设置100万的递归深度,再运行直接 stack overflow,溢出了

好吧,那就改吧
在类Import外定义数组变量resultList = [] 
复制代码
defopen(self, options):globalresultListifself.opened:returnself.opened=True
        log.debug('%s, importing ns="%s", location="%s"', self.id, self.ns[1], self.location)
        result=self.locate()ifresultisNone:ifself.locationisNone:
                log.debug('imported schema (%s) not-found', self.ns[1])else:ifself.locationinresultList:    #若location存在List中,则打印debug日志,不加载options
                    log.debug('location is already in resultList')else:
                    resultList.append(self.location)  #location不存在时,List中append后,加载options
                    result=self.download(options)
        log.debug('imported:\n%s', result)returnresult
复制代码

 去除了循环依赖,出现另外一个问题:

先看下我soap接口的通用调用方法:

复制代码
#! /usr/bin/python#coding:utf-8importsys
reload(sys)
sys.setdefaultencoding("utf-8")fromsuds.clientimportClientdefSoaRequest(wsdl,fnname,data):#soap接口调用方法soaService=Client(wsdl).service
    soaRep=getattr(soaService,fnname)(data)returnsoaRep
复制代码

通过这个方法去逐行调用excel中的用例参数,来实现数据驱动的接口自动化框架

但是单个接口,多组参数用例的组合场景,运用上面的脚本后就报错

Type not found.......

考虑到可能是因为第一行用例执行过程中,在List中append 进location,到执行第二条用例的时候,由于和第一行用例的接口是一样的,故而List判断中就没有进入到download分支,所以就抛出了not found的异常

解决方法是在执行用例前,清空list

改造soap接口核心调用程序

复制代码
#! /usr/bin/python#coding:utf-8importsys
reload(sys)
sys.setdefaultencoding("utf-8")fromsuds.clientimportClientfromsuds.xsdimportsxbasicdefSoaRequest(wsdl,fnname,data):#soap接口调用方法sxbasic.resultList=[]#初始化location列表soaService =Client(wsdl).service
    soaRep=getattr(soaService,fnname)(data)returnsoaRep
复制代码

 

这样就万事大吉了

 

如果有人有更好的解决方案,更简便的处理方法,欢迎沟通交流 本人邮箱:270193176@qq.com


TAG: recursion

 

评分:0

我来说两句

Open Toolbar