[Zope-CVS] CVS: Packages/pypes/pypes - util.py:1.1 event.py:1.7 extent.py:1.10

Casey Duncan casey at zope.com
Tue Apr 27 23:24:34 EDT 2004


Update of /cvs-repository/Packages/pypes/pypes
In directory cvs.zope.org:/tmp/cvs-serv17651

Modified Files:
	event.py extent.py 
Added Files:
	util.py 
Log Message:
Add missing util module and tests
Use inspect.getmro() in place of home-grown function


=== Added File Packages/pypes/pypes/util.py ===
##############################################################################
#
# Copyright (c) 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Pypes utility functions

$Id: util.py,v 1.1 2004/04/28 03:24:32 caseman Exp $"""

from cPickle import loads, dumps
from types import ClassType

def classKey(obj):
    """Return a unique string key for the class of obj, unless obj is a class
    or type in which case just return the key for obj. This key can be used to 
    identify the class persistently and retrieve it using classFromKey()
    
    This implementation uses pickles as keys, which means that the class
    must be pickleable. Applications should not rely on this detail, however.
    """
    if (isinstance(obj, type) or isinstance(obj, ClassType)):
        cls = obj
    else:
        cls = getattr(obj, '__class__', type(obj))
    return dumps(cls)


def classFromKey(key):
    """Return the class for key, a key generated by classKey()
    
    Raise TypeError if key is not a string. Raise AttributeError if the class
    no longer exists. Raise ValueError if the key cannot be resolved or if
    it resolves to an object other than a type or class (due to module changes)
    """
    if not isinstance(key, str):
        raise TypeError, 'expected string key got %s' % type(key).__name__
    try:
        cls = loads(key)
    except AttributeError:
        raise
    except:
        raise ValueError, 'invalid class key'
    if isinstance(cls, (type, ClassType)):
        return cls
    else:
        raise ValueError, (
            'key resolved to %s not class or type' % type(cls).__name__)
            


=== Packages/pypes/pypes/event.py 1.6 => 1.7 ===
--- Packages/pypes/pypes/event.py:1.6	Sun Mar  7 05:09:12 2004
+++ Packages/pypes/pypes/event.py	Tue Apr 27 23:24:32 2004
@@ -18,13 +18,14 @@
 from __future__ import generators
 import cPickle
 from types import ClassType
+from inspect import getmro
 from BTrees.OOBTree import OOBTree, OOTreeSet
 from persistent import Persistent
 from persistent.wref import WeakRef
 from zope.interface import implements
 from pypes.interfaces import IEventService
 from pypes.exceptions import EventRegistrationError
-from pypes.util import mro, classKey, classFromKey
+from pypes.util import classKey, classFromKey
 
 
 class PypesMessage(object):
@@ -107,7 +108,7 @@
         return False            
 
     def wouldReceive(self, obj, message_type):
-        for super_type in mro(message_type):
+        for super_type in getmro(message_type):
             try:
                 reglist = self._listeners[classKey(super_type)]
             except KeyError:
@@ -133,7 +134,7 @@
         
     def iterReceivers(self, message_type):
         rlists = []
-        for super_type in mro(message_type):
+        for super_type in getmro(message_type):
             try:
                 rlists.append(self._listeners[classKey(super_type)])
             except KeyError:


=== Packages/pypes/pypes/extent.py 1.9 => 1.10 ===
--- Packages/pypes/pypes/extent.py:1.9	Wed Apr 21 01:27:25 2004
+++ Packages/pypes/pypes/extent.py	Tue Apr 27 23:24:32 2004
@@ -20,6 +20,7 @@
 from types import ClassType
 from sets import Set
 from itertools import chain
+from inspect import getmro
 from zope.interface import implements, directlyProvides
 from persistent import Persistent
 from BTrees.OOBTree import OOBTree, OOTreeSet, union, difference, intersection
@@ -29,7 +30,7 @@
 from pypes.interfaces import IExtentService, IExtentMap
 from pypes.interfaces import IExtent, ICanonicalExtent, IDerivedExtent
 from pypes.exceptions import SetLookupError
-from pypes.util import mro, classKey, classFromKey
+from pypes.util import classKey, classFromKey
 
 
 class Extent:
@@ -251,7 +252,7 @@
         """Update subclass lists for cls"""
         seen = Set()
         subclass_key = classKey(cls)
-        for superclass in mro(cls):
+        for superclass in getmro(cls):
             key = classKey(superclass)
             try:
                 subclasses = self._subclasses[key]
@@ -276,7 +277,7 @@
             # be slightly simpler/cheaper which seems worth the trade
             del self._instances[key]
             cls = getattr(obj, '__class__', type(obj))
-            for superclass in mro(cls):
+            for superclass in getmro(cls):
                 sclass_key = classKey(superclass)
                 try:
                     subclasses = self._subclasses[sclass_key]




More information about the Zope-CVS mailing list