e11873f
diff -rU3 greenlet-2.0.2-orig/src/greenlet/tests/leakcheck.py greenlet-2.0.2/src/greenlet/tests/leakcheck.py
e11873f
--- greenlet-2.0.2-orig/src/greenlet/tests/leakcheck.py	2023-01-28 15:19:12.000000000 +0100
e11873f
+++ greenlet-2.0.2/src/greenlet/tests/leakcheck.py	2023-06-14 17:49:32.395412453 +0200
e11873f
@@ -30,39 +30,7 @@
e11873f
 from functools import wraps
e11873f
 import unittest
e11873f
 
e11873f
-
e11873f
-import objgraph
e11873f
-
e11873f
-# graphviz 0.18 (Nov 7 2021), available only on Python 3.6 and newer,
e11873f
-# has added type hints (sigh). It wants to use ``typing.Literal`` for
e11873f
-# some stuff, but that's only available on Python 3.9+. If that's not
e11873f
-# found, it creates a ``unittest.mock.MagicMock`` object and annotates
e11873f
-# with that. These are GC'able objects, and doing almost *anything*
e11873f
-# with them results in an explosion of objects. For example, trying to
e11873f
-# compare them for equality creates new objects. This causes our
e11873f
-# leakchecks to fail, with reports like:
e11873f
-#
e11873f
-# greenlet.tests.leakcheck.LeakCheckError: refcount increased by [337, 1333, 343, 430, 530, 643, 769]
e11873f
-# _Call          1820      +546
e11873f
-# dict           4094       +76
e11873f
-# MagicProxy      585       +73
e11873f
-# tuple          2693       +66
e11873f
-# _CallList        24        +3
e11873f
-# weakref        1441        +1
e11873f
-# function       5996        +1
e11873f
-# type            736        +1
e11873f
-# cell            592        +1
e11873f
-# MagicMock         8        +1
e11873f
-#
e11873f
-# To avoid this, we *could* filter this type of object out early. In
e11873f
-# principle it could leak, but we don't use mocks in greenlet, so it
e11873f
-# doesn't leak from us. However, a further issue is that ``MagicMock``
e11873f
-# objects have subobjects that are also GC'able, like ``_Call``, and
e11873f
-# those create new mocks of their own too. So we'd have to filter them
e11873f
-# as well, and they're not public. That's OK, we can workaround the
e11873f
-# problem by being very careful to never compare by equality or other
e11873f
-# user-defined operators, only using object identity or other builtin
e11873f
-# functions.
e11873f
+# Edited for Fedora to avoid missing dependency
e11873f
 
e11873f
 RUNNING_ON_GITHUB_ACTIONS = os.environ.get('GITHUB_ACTIONS')
e11873f
 RUNNING_ON_TRAVIS = os.environ.get('TRAVIS') or RUNNING_ON_GITHUB_ACTIONS
e11873f
@@ -74,53 +42,15 @@
e11873f
 ONLY_FAILING_LEAKCHECKS = os.environ.get('GREENLET_ONLY_FAILING_LEAKCHECKS')
e11873f
 
e11873f
 def ignores_leakcheck(func):
e11873f
-    """
e11873f
-    Ignore the given object during leakchecks.
e11873f
-
e11873f
-    Can be applied to a method, in which case the method will run, but
e11873f
-    will not be subject to leak checks.
e11873f
-
e11873f
-    If applied to a class, the entire class will be skipped during leakchecks. This
e11873f
-    is intended to be used for classes that are very slow and cause problems such as
e11873f
-    test timeouts; typically it will be used for classes that are subclasses of a base
e11873f
-    class and specify variants of behaviour (such as pool sizes).
e11873f
-    """
e11873f
-    func.ignore_leakcheck = True
e11873f
     return func
e11873f
 
e11873f
 def fails_leakcheck(func):
e11873f
-    """
e11873f
-    Mark that the function is known to leak.
e11873f
-    """
e11873f
-    func.fails_leakcheck = True
e11873f
-    if SKIP_FAILING_LEAKCHECKS:
e11873f
-        func = unittest.skip("Skipping known failures")(func)
e11873f
     return func
e11873f
 
e11873f
 class LeakCheckError(AssertionError):
e11873f
     pass
e11873f
 
e11873f
-if hasattr(sys, 'getobjects'):
e11873f
-    # In a Python build with ``--with-trace-refs``, make objgraph
e11873f
-    # trace *all* the objects, not just those that are tracked by the
e11873f
-    # GC
e11873f
-    class _MockGC(object):
e11873f
-        def get_objects(self):
e11873f
-            return sys.getobjects(0) # pylint:disable=no-member
e11873f
-        def __getattr__(self, name):
e11873f
-            return getattr(gc, name)
e11873f
-    objgraph.gc = _MockGC()
e11873f
-    fails_strict_leakcheck = fails_leakcheck
e11873f
-else:
e11873f
-    def fails_strict_leakcheck(func):
e11873f
-        """
e11873f
-        Decorator for a function that is known to fail when running
e11873f
-        strict (``sys.getobjects()``) leakchecks.
e11873f
-
e11873f
-        This type of leakcheck finds all objects, even those, such as
e11873f
-        strings, which are not tracked by the garbage collector.
e11873f
-        """
e11873f
-        return func
e11873f
+fails_strict_leakcheck = fails_leakcheck
e11873f
 
e11873f
 class ignores_types_in_strict_leakcheck(object):
e11873f
     def __init__(self, types):
e11873f
@@ -129,190 +59,5 @@
e11873f
         func.leakcheck_ignore_types = self.types
e11873f
         return func
e11873f
 
e11873f
-class _RefCountChecker(object):
e11873f
-
e11873f
-    # Some builtin things that we ignore
e11873f
-    # XXX: Those things were ignored by gevent, but they're important here,
e11873f
-    # presumably.
e11873f
-    IGNORED_TYPES = () #(tuple, dict, types.FrameType, types.TracebackType)
e11873f
-
e11873f
-    def __init__(self, testcase, function):
e11873f
-        self.testcase = testcase
e11873f
-        self.function = function
e11873f
-        self.deltas = []
e11873f
-        self.peak_stats = {}
e11873f
-        self.ignored_types = ()
e11873f
-
e11873f
-        # The very first time we are called, we have already been
e11873f
-        # self.setUp() by the test runner, so we don't need to do it again.
e11873f
-        self.needs_setUp = False
e11873f
-
e11873f
-    def _include_object_p(self, obj):
e11873f
-        # pylint:disable=too-many-return-statements
e11873f
-        #
e11873f
-        # See the comment block at the top. We must be careful to
e11873f
-        # avoid invoking user-defined operations.
e11873f
-        if obj is self:
e11873f
-            return False
e11873f
-        kind = type(obj)
e11873f
-        # ``self._include_object_p == obj`` returns NotImplemented
e11873f
-        # for non-function objects, which causes the interpreter
e11873f
-        # to try to reverse the order of arguments...which leads
e11873f
-        # to the explosion of mock objects. We don't want that, so we implement
e11873f
-        # the check manually.
e11873f
-        if kind == type(self._include_object_p):
e11873f
-            try:
e11873f
-                # pylint:disable=not-callable
e11873f
-                exact_method_equals = self._include_object_p.__eq__(obj)
e11873f
-            except AttributeError:
e11873f
-                # Python 2.7 methods may only have __cmp__, and that raises a
e11873f
-                # TypeError for non-method arguments
e11873f
-                # pylint:disable=no-member
e11873f
-                exact_method_equals = self._include_object_p.__cmp__(obj) == 0
e11873f
-
e11873f
-            if exact_method_equals is not NotImplemented and exact_method_equals:
e11873f
-                return False
e11873f
-
e11873f
-        # Similarly, we need to check identity in our __dict__ to avoid mock explosions.
e11873f
-        for x in self.__dict__.values():
e11873f
-            if obj is x:
e11873f
-                return False
e11873f
-
e11873f
-
e11873f
-        if kind in self.ignored_types or kind in self.IGNORED_TYPES:
e11873f
-            return False
e11873f
-
e11873f
-        return True
e11873f
-
e11873f
-    def _growth(self):
e11873f
-        return objgraph.growth(limit=None, peak_stats=self.peak_stats,
e11873f
-                               filter=self._include_object_p)
e11873f
-
e11873f
-    def _report_diff(self, growth):
e11873f
-        if not growth:
e11873f
-            return "<Unable to calculate growth>"
e11873f
-
e11873f
-        lines = []
e11873f
-        width = max(len(name) for name, _, _ in growth)
e11873f
-        for name, count, delta in growth:
e11873f
-            lines.append('%-*s%9d %+9d' % (width, name, count, delta))
e11873f
-
e11873f
-        diff = '\n'.join(lines)
e11873f
-        return diff
e11873f
-
e11873f
-
e11873f
-    def _run_test(self, args, kwargs):
e11873f
-        gc_enabled = gc.isenabled()
e11873f
-        gc.disable()
e11873f
-
e11873f
-        if self.needs_setUp:
e11873f
-            self.testcase.setUp()
e11873f
-            self.testcase.skipTearDown = False
e11873f
-        try:
e11873f
-            self.function(self.testcase, *args, **kwargs)
e11873f
-        finally:
e11873f
-            self.testcase.tearDown()
e11873f
-            self.testcase.doCleanups()
e11873f
-            self.testcase.skipTearDown = True
e11873f
-            self.needs_setUp = True
e11873f
-            if gc_enabled:
e11873f
-                gc.enable()
e11873f
-
e11873f
-    def _growth_after(self):
e11873f
-        # Grab post snapshot
e11873f
-        if 'urlparse' in sys.modules:
e11873f
-            sys.modules['urlparse'].clear_cache()
e11873f
-        if 'urllib.parse' in sys.modules:
e11873f
-            sys.modules['urllib.parse'].clear_cache()
e11873f
-
e11873f
-        return self._growth()
e11873f
-
e11873f
-    def _check_deltas(self, growth):
e11873f
-        # Return false when we have decided there is no leak,
e11873f
-        # true if we should keep looping, raises an assertion
e11873f
-        # if we have decided there is a leak.
e11873f
-
e11873f
-        deltas = self.deltas
e11873f
-        if not deltas:
e11873f
-            # We haven't run yet, no data, keep looping
e11873f
-            return True
e11873f
-
e11873f
-        if gc.garbage:
e11873f
-            raise LeakCheckError("Generated uncollectable garbage %r" % (gc.garbage,))
e11873f
-
e11873f
-
e11873f
-        # the following configurations are classified as "no leak"
e11873f
-        # [0, 0]
e11873f
-        # [x, 0, 0]
e11873f
-        # [... a, b, c, d]  where a+b+c+d = 0
e11873f
-        #
e11873f
-        # the following configurations are classified as "leak"
e11873f
-        # [... z, z, z]  where z > 0
e11873f
-
e11873f
-        if deltas[-2:] == [0, 0] and len(deltas) in (2, 3):
e11873f
-            return False
e11873f
-
e11873f
-        if deltas[-3:] == [0, 0, 0]:
e11873f
-            return False
e11873f
-
e11873f
-        if len(deltas) >= 4 and sum(deltas[-4:]) == 0:
e11873f
-            return False
e11873f
-
e11873f
-        if len(deltas) >= 3 and deltas[-1] > 0 and deltas[-1] == deltas[-2] and deltas[-2] == deltas[-3]:
e11873f
-            diff = self._report_diff(growth)
e11873f
-            raise LeakCheckError('refcount increased by %r\n%s' % (deltas, diff))
e11873f
-
e11873f
-        # OK, we don't know for sure yet. Let's search for more
e11873f
-        if sum(deltas[-3:]) <= 0 or sum(deltas[-4:]) <= 0 or deltas[-4:].count(0) >= 2:
e11873f
-            # this is suspicious, so give a few more runs
e11873f
-            limit = 11
e11873f
-        else:
e11873f
-            limit = 7
e11873f
-        if len(deltas) >= limit:
e11873f
-            raise LeakCheckError('refcount increased by %r\n%s'
e11873f
-                                 % (deltas,
e11873f
-                                    self._report_diff(growth)))
e11873f
-
e11873f
-        # We couldn't decide yet, keep going
e11873f
-        return True
e11873f
-
e11873f
-    def __call__(self, args, kwargs):
e11873f
-        for _ in range(3):
e11873f
-            gc.collect()
e11873f
-
e11873f
-        expect_failure = getattr(self.function, 'fails_leakcheck', False)
e11873f
-        if expect_failure:
e11873f
-            self.testcase.expect_greenlet_leak = True
e11873f
-        self.ignored_types = getattr(self.function, "leakcheck_ignore_types", ())
e11873f
-
e11873f
-        # Capture state before; the incremental will be
e11873f
-        # updated by each call to _growth_after
e11873f
-        growth = self._growth()
e11873f
-
e11873f
-        try:
e11873f
-            while self._check_deltas(growth):
e11873f
-                self._run_test(args, kwargs)
e11873f
-
e11873f
-                growth = self._growth_after()
e11873f
-
e11873f
-                self.deltas.append(sum((stat[2] for stat in growth)))
e11873f
-        except LeakCheckError:
e11873f
-            if not expect_failure:
e11873f
-                raise
e11873f
-        else:
e11873f
-            if expect_failure:
e11873f
-                raise LeakCheckError("Expected %s to leak but it did not." % (self.function,))
e11873f
-
e11873f
 def wrap_refcount(method):
e11873f
-    if getattr(method, 'ignore_leakcheck', False) or SKIP_LEAKCHECKS:
e11873f
-        return method
e11873f
-
e11873f
-    @wraps(method)
e11873f
-    def wrapper(self, *args, **kwargs): # pylint:disable=too-many-branches
e11873f
-        if getattr(self, 'ignore_leakcheck', False):
e11873f
-            raise unittest.SkipTest("This class ignored during leakchecks")
e11873f
-        if ONLY_FAILING_LEAKCHECKS and not getattr(method, 'fails_leakcheck', False):
e11873f
-            raise unittest.SkipTest("Only running tests that fail leakchecks.")
e11873f
-        return _RefCountChecker(self, method)(args, kwargs)
e11873f
-
e11873f
-    return wrapper
e11873f
+    return method