public inbox for gentoo-commits@lists.gentoo.org
 help / color / mirror / Atom feed
From: "Brian Harring (ferringb)" <ferringb@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] gentoo-x86 commit in dev-python/snakeoil/files: snakeoil-0.4.4-atexit-weakref.patch
Date: Fri, 11 Nov 2011 10:41:38 +0000 (UTC)	[thread overview]
Message-ID: <20111111104138.A10942004C@flycatcher.gentoo.org> (raw)

ferringb    11/11/11 10:41:38

  Added:                snakeoil-0.4.4-atexit-weakref.patch
  Log:
  pull in WeakRefFinalizer fix for strongly referenced instances at sys.exit; this primarily helps pkgcore ensuring binpkg caches are flushed on the way out
  
  (Portage version: 2.1.10.19/cvs/Linux x86_64)

Revision  Changes    Path
1.1                  dev-python/snakeoil/files/snakeoil-0.4.4-atexit-weakref.patch

file : http://sources.gentoo.org/viewvc.cgi/gentoo-x86/dev-python/snakeoil/files/snakeoil-0.4.4-atexit-weakref.patch?rev=1.1&view=markup
plain: http://sources.gentoo.org/viewvc.cgi/gentoo-x86/dev-python/snakeoil/files/snakeoil-0.4.4-atexit-weakref.patch?rev=1.1&content-type=text/plain

Index: snakeoil-0.4.4-atexit-weakref.patch
===================================================================
From 2ad8113511381ae502a000809700cb672c073f92 Mon Sep 17 00:00:00 2001
From: Brian Harring <ferringb@gmail.com>
Date: Fri, 11 Nov 2011 02:04:43 -0800
Subject: [PATCH] WeakRefFinalizer: use atexit to cleanup any strongly
 referenced instances at sys.exit

---
 NEWS                 |    4 ++++
 snakeoil/weakrefs.py |   23 ++++++++++++++++++++---
 2 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/NEWS b/NEWS
index 04015ee..4fbfb0e 100644
--- a/NEWS
+++ b/NEWS
@@ -2,6 +2,10 @@ Snakeoil Release Notes
 ======================
 
 
+* Fix WeakRefFinalizer so that instances that are still strongly referenced
+  at the time of sys.exit have their finalizers ran via atexit.
+
+
 snakeoil 0.4.4: Oct 26th, 2011
 
 * use sane permissions for directories created for tests.
diff --git a/snakeoil/weakrefs.py b/snakeoil/weakrefs.py
index 1a25a9c..272b4d3 100644
--- a/snakeoil/weakrefs.py
+++ b/snakeoil/weakrefs.py
@@ -10,13 +10,15 @@ __all__ = ("WeakValCache", "WeakRefFinalizer")
 # Unused import
 # pylint: disable-msg=W0611
 
+import atexit
+
 try:
     # No name in module
     # pylint: disable-msg=E0611
     from snakeoil._caching import WeakValCache
-    from weakref import ref
+    from weakref import ref, WeakKeyDictionary
 except ImportError:
-    from weakref import WeakValueDictionary as WeakValCache, ref
+    from weakref import WeakValueDictionary as WeakValCache, ref, WeakKeyDictionary
 
 from snakeoil.obj import make_kls, BaseDelayedObject
 from snakeoil.currying import partial
@@ -38,7 +40,6 @@ class WeakRefProxy(BaseDelayedObject):
         obj.__enable_finalization__(weakref)
         return obj
 
-
 def __enable_finalization__(self, weakref):
     # note we directly access the class, to ensure the instance hasn't overshadowed.
     self.__class__.__finalizer_weakrefs__[id(self)] = weakref
@@ -116,6 +117,9 @@ class WeakRefFinalizer(type):
     >>> del obj
     finalization invoked: bar
     """
+
+    __known_classes__ = WeakKeyDictionary()
+
     def __new__(cls, name, bases, d):
         if '__del__' in d:
             d['__finalizer__'] = d.pop("__del__")
@@ -137,6 +141,7 @@ class WeakRefFinalizer(type):
         new_cls = super(WeakRefFinalizer, cls).__new__(cls, name, bases, d)
         new_cls.__proxy_class__ = partial(make_kls(new_cls, WeakRefProxy), cls, lambda x:x)
         new_cls.__proxy_class__.__name__ = name
+        cls.__known_classes__[new_cls] = True
         return new_cls
 
     def __call__(cls, *a, **kw):
@@ -146,3 +151,15 @@ class WeakRefFinalizer(type):
         # weakref registration
         getattr(proxy, '__finalizer__')
         return proxy
+
+    @classmethod
+    def _atexit_cleanup(cls):
+        # cleanup any instances strongly referenced at the time of sys.exit
+        target_classes = cls.__known_classes__.keys()
+        for target_cls in target_classes:
+            for target_ref in target_cls.__finalizer_weakrefs__.values():
+                obj = target_ref()
+                if obj is not None:
+                    obj.__finalizer__()
+
+atexit.register(WeakRefFinalizer._atexit_cleanup)
-- 
1.7.8.rc1







             reply	other threads:[~2011-11-11 10:42 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-11-11 10:41 Brian Harring (ferringb) [this message]
  -- strict thread matches above, loose matches on Subject: below --
2011-12-14 10:31 [gentoo-commits] gentoo-x86 commit in dev-python/snakeoil/files: snakeoil-0.4.4-atexit-weakref.patch Brian Harring (ferringb)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20111111104138.A10942004C@flycatcher.gentoo.org \
    --to=ferringb@gentoo.org \
    --cc=gentoo-commits@lists.gentoo.org \
    --cc=gentoo-dev@lists.gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox