店播爬取Python脚本

testing_refleaks.py 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # Protocol Buffers - Google's data interchange format
  2. # Copyright 2008 Google Inc. All rights reserved.
  3. # https://developers.google.com/protocol-buffers/
  4. #
  5. # Redistribution and use in source and binary forms, with or without
  6. # modification, are permitted provided that the following conditions are
  7. # met:
  8. #
  9. # * Redistributions of source code must retain the above copyright
  10. # notice, this list of conditions and the following disclaimer.
  11. # * Redistributions in binary form must reproduce the above
  12. # copyright notice, this list of conditions and the following disclaimer
  13. # in the documentation and/or other materials provided with the
  14. # distribution.
  15. # * Neither the name of Google Inc. nor the names of its
  16. # contributors may be used to endorse or promote products derived from
  17. # this software without specific prior written permission.
  18. #
  19. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. """A subclass of unittest.TestCase which checks for reference leaks.
  31. To use:
  32. - Use testing_refleak.BaseTestCase instead of unittest.TestCase
  33. - Configure and compile Python with --with-pydebug
  34. If sys.gettotalrefcount() is not available (because Python was built without
  35. the Py_DEBUG option), then this module is a no-op and tests will run normally.
  36. """
  37. import gc
  38. import sys
  39. try:
  40. import copy_reg as copyreg #PY26
  41. except ImportError:
  42. import copyreg
  43. try:
  44. import unittest2 as unittest #PY26
  45. except ImportError:
  46. import unittest
  47. class LocalTestResult(unittest.TestResult):
  48. """A TestResult which forwards events to a parent object, except for Skips."""
  49. def __init__(self, parent_result):
  50. unittest.TestResult.__init__(self)
  51. self.parent_result = parent_result
  52. def addError(self, test, error):
  53. self.parent_result.addError(test, error)
  54. def addFailure(self, test, error):
  55. self.parent_result.addFailure(test, error)
  56. def addSkip(self, test, reason):
  57. pass
  58. class ReferenceLeakCheckerMixin(object):
  59. """A mixin class for TestCase, which checks reference counts."""
  60. NB_RUNS = 3
  61. def run(self, result=None):
  62. # python_message.py registers all Message classes to some pickle global
  63. # registry, which makes the classes immortal.
  64. # We save a copy of this registry, and reset it before we could references.
  65. self._saved_pickle_registry = copyreg.dispatch_table.copy()
  66. # Run the test twice, to warm up the instance attributes.
  67. super(ReferenceLeakCheckerMixin, self).run(result=result)
  68. super(ReferenceLeakCheckerMixin, self).run(result=result)
  69. oldrefcount = 0
  70. local_result = LocalTestResult(result)
  71. refcount_deltas = []
  72. for _ in range(self.NB_RUNS):
  73. oldrefcount = self._getRefcounts()
  74. super(ReferenceLeakCheckerMixin, self).run(result=local_result)
  75. newrefcount = self._getRefcounts()
  76. refcount_deltas.append(newrefcount - oldrefcount)
  77. print(refcount_deltas, self)
  78. try:
  79. self.assertEqual(refcount_deltas, [0] * self.NB_RUNS)
  80. except Exception: # pylint: disable=broad-except
  81. result.addError(self, sys.exc_info())
  82. def _getRefcounts(self):
  83. copyreg.dispatch_table.clear()
  84. copyreg.dispatch_table.update(self._saved_pickle_registry)
  85. # It is sometimes necessary to gc.collect() multiple times, to ensure
  86. # that all objects can be collected.
  87. gc.collect()
  88. gc.collect()
  89. gc.collect()
  90. return sys.gettotalrefcount()
  91. if hasattr(sys, 'gettotalrefcount'):
  92. def TestCase(test_class):
  93. new_bases = (ReferenceLeakCheckerMixin,) + test_class.__bases__
  94. new_class = type(test_class)(
  95. test_class.__name__, new_bases, dict(test_class.__dict__))
  96. return new_class
  97. SkipReferenceLeakChecker = unittest.skip
  98. else:
  99. # When PyDEBUG is not enabled, run the tests normally.
  100. def TestCase(test_class):
  101. return test_class
  102. def SkipReferenceLeakChecker(reason):
  103. del reason # Don't skip, so don't need a reason.
  104. def Same(func):
  105. return func
  106. return Same