³ò
Ü‘RMc           @   s8   d  Z  d e f d „  ƒ  YZ d „  Z h  Z d „  Z d S(   s	  
Two magic tricks for classes:

    class X:
        __metaclass__ = extendabletype
        ...

    # in some other file...
    class __extend__(X):
        ...      # and here you can add new methods and class attributes to X

Mostly useful together with the second trick, which lets you build
methods whose 'self' is a pair of objects instead of just one:

    class __extend__(pairtype(X, Y)):
        attribute = 42
        def method((x, y), other, arguments):
            ...

    pair(x, y).attribute
    pair(x, y).method(other, arguments)

This finds methods and class attributes based on the actual
class of both objects that go into the pair(), with the usual
rules of method/attribute overriding in (pairs of) subclasses.

For more information, see test_pairtype.
t   extendabletypec           B   s   e  Z d  Z d „  Z RS(   s€   A type with a syntax trick: 'class __extend__(t)' actually extends
    the definition of 't' instead of creating a new subclass.c         C   sŠ   | d j oZ xO | D]G }  x> | i  ƒ  D]0 \ } } | d j o q' n t |  | | ƒ q' Wq Wd  Sn  t t |  ƒ i |  | | | ƒ Sd  S(   Nt
   __extend__t
   __module__(   t   itemst   setattrt   Nonet   superR    t   __new__(   t   clst   namet   basest   dictt   keyt   value(    (    s&   /64/home/arigo/u/pypy/tool/pairtype.pyR   !   s      (   t   __name__R   t   __doc__R   (    (    (    s&   /64/home/arigo/u/pypy/tool/pairtype.pyR       s   c         C   s%   t  |  i | i ƒ } | |  | f ƒ S(   s   Return a pair object.(   t   pairtypet	   __class__(   t   at   bt   tp(    (    s&   /64/home/arigo/u/pypy/tool/pairtype.pyt   pair.   s    c         C   sØ   y t  |  | f } Wn½ t j
 o± d |  i | i f } g  } |  i D] } | t | | ƒ qI ~ } g  } | i D] } | t |  | ƒ qv ~ }	 t | |	 ƒ p t f }
 t | |
 h  ƒ } t  |  | f <n X| S(   s6   type(pair(a,b)) is pairtype(a.__class__, b.__class__).s   pairtype(%s, %s)(   t   pairtypecachet   KeyErrorR   t	   __bases__R   t   tupleR    (   t   cls1t   cls2R   R	   t   _[1]t   base1t   bases1t   _[2]t   base2t   bases2R
   (    (    s&   /64/home/arigo/u/pypy/tool/pairtype.pyR   5   s    --%N(   R   t   typeR    R   R   R   (    (    (    s&   /64/home/arigo/u/pypy/tool/pairtype.pys   <module>   s   	
