3 SLj@sdZddlZddlZy ddlZWnek r<ddlZYnXy ddlZWnek rfddlZYnXGdddejZ Gdddej Z e edre Z ejZnej Z dd ZdS) a[A subclass of unittest.TestCase which checks for reference leaks. To use: - Use testing_refleak.BaseTestCase instead of unittest.TestCase - Configure and compile Python with --with-pydebug If sys.gettotalrefcount() is not available (because Python was built without the Py_DEBUG option), then this module is a no-op and tests will run normally. Nc@s0eZdZdZddZddZddZdd Zd S) LocalTestResultzHA TestResult which forwards events to a parent object, except for Skips.cCstjj|||_dS)N)unittest TestResult__init__ parent_result)selfrr&/usr/lib/python3.6/testing_refleaks.pyr:s zLocalTestResult.__init__cCs|jj||dS)N)raddError)rtesterrorrrr r >szLocalTestResult.addErrorcCs|jj||dS)N)r addFailure)rr r rrr r AszLocalTestResult.addFailurecCsdS)Nr)rr reasonrrr addSkipDszLocalTestResult.addSkipN)__name__ __module__ __qualname____doc__rr r rrrrr r7s rcs.eZdZdZdZdfdd ZddZZS) ReferenceLeakCheckerTestCasezHA TestCase which runs tests multiple times, collecting reference counts.Nc stjj|_tt|j|dtt|j|dd}t|}g}xBt|j D]4}|j }tt|j|d|j }|j ||qLWt ||y|j |dg|j Wn$tk r|j|tjYnXdS)N)resultr)copyregdispatch_tablecopy_saved_pickle_registrysuperrrunrrangeNB_RUNS _getRefcountsappendprintZ assertEqual Exceptionr sysexc_info)rrZ oldrefcountZ local_resultZrefcount_deltas_Z newrefcount) __class__rr rMs   z ReferenceLeakCheckerTestCase.runcCs8tjjtjj|jtjtjtjtjS)N) rrclearupdatergcZcollectr#gettotalrefcount)rrrr rgs  z*ReferenceLeakCheckerTestCase._getRefcounts)N)rrrrrrr __classcell__rr)r&r rHsrr*cCs~dd}|S)NcSs|S)Nr)funcrrr Same|sz&SkipReferenceLeakChecker..Samer)rr-rrr SkipReferenceLeakCheckerzsr.)rr)r#Zcopy_regr ImportErrorZ unittest2rrrZTestCaserhasattrZ BaseTestCaseskipr.rrrr 's"  *