protobuf/python/google/protobuf/internal/testing_refleaks.py
Go to the documentation of this file.
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 
31 """A subclass of unittest.TestCase which checks for reference leaks.
32 
33 To use:
34 - Use testing_refleak.BaseTestCase instead of unittest.TestCase
35 - Configure and compile Python with --with-pydebug
36 
37 If sys.gettotalrefcount() is not available (because Python was built without
38 the Py_DEBUG option), then this module is a no-op and tests will run normally.
39 """
40 
41 import copyreg
42 import gc
43 import sys
44 import unittest
45 
46 
47 class LocalTestResult(unittest.TestResult):
48  """A TestResult which forwards events to a parent object, except for Skips."""
49 
50  def __init__(self, parent_result):
51  unittest.TestResult.__init__(self)
52  self.parent_result = parent_result
53 
54  def addError(self, test, error):
55  self.parent_result.addError(test, error)
56 
57  def addFailure(self, test, error):
58  self.parent_result.addFailure(test, error)
59 
60  def addSkip(self, test, reason):
61  pass
62 
63 
64 class ReferenceLeakCheckerMixin(object):
65  """A mixin class for TestCase, which checks reference counts."""
66 
67  NB_RUNS = 3
68 
69  def run(self, result=None):
70  # python_message.py registers all Message classes to some pickle global
71  # registry, which makes the classes immortal.
72  # We save a copy of this registry, and reset it before we could references.
73  self._saved_pickle_registry = copyreg.dispatch_table.copy()
74 
75  # Run the test twice, to warm up the instance attributes.
76  super(ReferenceLeakCheckerMixin, self).run(result=result)
77  super(ReferenceLeakCheckerMixin, self).run(result=result)
78 
79  oldrefcount = 0
80  local_result = LocalTestResult(result)
81 
82  refcount_deltas = []
83  for _ in range(self.NB_RUNS):
84  oldrefcount = self._getRefcounts()
85  super(ReferenceLeakCheckerMixin, self).run(result=local_result)
86  newrefcount = self._getRefcounts()
87  refcount_deltas.append(newrefcount - oldrefcount)
88  print(refcount_deltas, self)
89 
90  try:
91  self.assertEqual(refcount_deltas, [0] * self.NB_RUNS)
92  except Exception: # pylint: disable=broad-except
93  result.addError(self, sys.exc_info())
94 
95  def _getRefcounts(self):
96  copyreg.dispatch_table.clear()
97  copyreg.dispatch_table.update(self._saved_pickle_registry)
98  # It is sometimes necessary to gc.collect() multiple times, to ensure
99  # that all objects can be collected.
100  gc.collect()
101  gc.collect()
102  gc.collect()
103  return sys.gettotalrefcount()
104 
105 
106 if hasattr(sys, 'gettotalrefcount'):
107 
108  def TestCase(test_class):
109  new_bases = (ReferenceLeakCheckerMixin,) + test_class.__bases__
110  new_class = type(test_class)(
111  test_class.__name__, new_bases, dict(test_class.__dict__))
112  return new_class
113  SkipReferenceLeakChecker = unittest.skip
114 
115 else:
116  # When PyDEBUG is not enabled, run the tests normally.
117 
118  def TestCase(test_class):
119  return test_class
120 
121  def SkipReferenceLeakChecker(reason):
122  del reason # Don't skip, so don't need a reason.
123  def Same(func):
124  return func
125  return Same
google::protobuf.internal.testing_refleaks.LocalTestResult.addFailure
def addFailure(self, test, error)
Definition: bloaty/third_party/protobuf/python/google/protobuf/internal/testing_refleaks.py:65
google::protobuf.internal.testing_refleaks.LocalTestResult.addError
def addError(self, test, error)
Definition: bloaty/third_party/protobuf/python/google/protobuf/internal/testing_refleaks.py:62
capstone.range
range
Definition: third_party/bloaty/third_party/capstone/bindings/python/capstone/__init__.py:6
google::protobuf.internal.testing_refleaks.LocalTestResult.__init__
def __init__(self, parent_result)
Definition: bloaty/third_party/protobuf/python/google/protobuf/internal/testing_refleaks.py:58
google::protobuf.internal.testing_refleaks.LocalTestResult.parent_result
parent_result
Definition: bloaty/third_party/protobuf/python/google/protobuf/internal/testing_refleaks.py:60
google::protobuf.internal.testing_refleaks.ReferenceLeakCheckerMixin.NB_RUNS
int NB_RUNS
Definition: bloaty/third_party/protobuf/python/google/protobuf/internal/testing_refleaks.py:75
google::protobuf.internal.testing_refleaks.ReferenceLeakCheckerMixin._getRefcounts
def _getRefcounts(self)
Definition: bloaty/third_party/protobuf/python/google/protobuf/internal/testing_refleaks.py:103
google::protobuf.internal.testing_refleaks.LocalTestResult.addSkip
def addSkip(self, test, reason)
Definition: bloaty/third_party/protobuf/python/google/protobuf/internal/testing_refleaks.py:68
google::protobuf.internal.testing_refleaks.ReferenceLeakCheckerMixin._saved_pickle_registry
_saved_pickle_registry
Definition: bloaty/third_party/protobuf/python/google/protobuf/internal/testing_refleaks.py:81
asyncio_get_stats.type
type
Definition: asyncio_get_stats.py:37
google::protobuf.internal.testing_refleaks.ReferenceLeakCheckerMixin.run
def run(self, result=None)
Definition: bloaty/third_party/protobuf/python/google/protobuf/internal/testing_refleaks.py:77
google::protobuf.internal.testing_refleaks.LocalTestResult
Definition: bloaty/third_party/protobuf/python/google/protobuf/internal/testing_refleaks.py:55
google::protobuf.internal.testing_refleaks.TestCase
def TestCase(test_class)
Definition: bloaty/third_party/protobuf/python/google/protobuf/internal/testing_refleaks.py:116
google::protobuf.internal.testing_refleaks.SkipReferenceLeakChecker
SkipReferenceLeakChecker
Definition: bloaty/third_party/protobuf/python/google/protobuf/internal/testing_refleaks.py:121


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:32