From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) by finch.gentoo.org (Postfix) with ESMTP id 990EC138202 for ; Thu, 29 Aug 2013 12:36:38 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id D8782E0B07; Thu, 29 Aug 2013 12:36:35 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) (using TLSv1 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 4876BE0B07 for ; Thu, 29 Aug 2013 12:36:35 +0000 (UTC) Received: from hornbill.gentoo.org (hornbill.gentoo.org [94.100.119.163]) (using TLSv1 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 4CF4133ECF5 for ; Thu, 29 Aug 2013 12:36:34 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by hornbill.gentoo.org (Postfix) with ESMTP id DD171E545F for ; Thu, 29 Aug 2013 12:36:32 +0000 (UTC) From: "André Erdmann" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "André Erdmann" Message-ID: <1377767677.c5c196a9a80e19a1f61379db1c9b9592cf6a1fc6.dywi@gentoo> Subject: [gentoo-commits] proj/R_overlay:master commit in: roverlay/util/ X-VCS-Repository: proj/R_overlay X-VCS-Files: roverlay/util/objects.py X-VCS-Directories: roverlay/util/ X-VCS-Committer: dywi X-VCS-Committer-Name: André Erdmann X-VCS-Revision: c5c196a9a80e19a1f61379db1c9b9592cf6a1fc6 X-VCS-Branch: master Date: Thu, 29 Aug 2013 12:36:32 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Archives-Salt: 906191b6-0051-44da-aa42-cf8cbc72cf96 X-Archives-Hash: 4470775869c9b0c019d81e0c83f52990 commit: c5c196a9a80e19a1f61379db1c9b9592cf6a1fc6 Author: André Erdmann mailerd de> AuthorDate: Thu Aug 29 09:14:37 2013 +0000 Commit: André Erdmann mailerd de> CommitDate: Thu Aug 29 09:14:37 2013 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=proj/R_overlay.git;a=commit;h=c5c196a9 roverlay/util/objects: weak-referenced object view expose a (sub-)set of an object's information without depending on its existence (= keeping it in memory due to reference count). --- roverlay/util/objects.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/roverlay/util/objects.py b/roverlay/util/objects.py index dd19046..28cc4e8 100644 --- a/roverlay/util/objects.py +++ b/roverlay/util/objects.py @@ -4,6 +4,8 @@ # Distributed under the terms of the GNU General Public License; # either version 2 of the License, or (at your option) any later version. +import weakref + class MethodNotImplementedError ( NotImplementedError ): def __init__ ( self, obj, method, msg=None ): if isinstance ( obj, str ): @@ -67,3 +69,40 @@ def abstractmethod ( func=None ): def not_implemented ( func=None ): return _get_exception_wrapper ( MethodNotImplementedError, func ) # --- end of not_implemented (...) --- + + +class ObjectView ( object ): + + class ObjectDisappeared ( Exception ): + pass + # --- end of ObjectDisappeared --- + + def __init__ ( self, obj ): + super ( ObjectView, self ).__init__() + self.obj_ref = weakref.ref ( obj ) if obj is not None else None + # --- end of __init__ (...) --- + + def __bool__ ( self ): + return ( self.obj_ref is not None and self.obj_ref() is not None ) + # --- end of __bool__ (...) --- + + def deref_unsafe ( self ): + return None if self.obj_ref is None else self.obj_ref() + # --- end of deref_unsafe (...) --- + + def deref_safe ( self ): + obj = None if self.obj_ref is None else self.obj_ref() + if obj is None: + raise self.__class__.ObjectDisappeared() + else: + return obj + # --- end of deref_safe (...) --- + + deref = deref_safe + + @abstractmethod + def update ( self ): + pass + # --- end of update (...) --- + +# --- end of ObjectView ---