Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Lib/_abcoll.py

    r2 r391  
    2222### ONE-TRICK PONIES ###
    2323
     24def _hasattr(C, attr):
     25    try:
     26        return any(attr in B.__dict__ for B in C.__mro__)
     27    except AttributeError:
     28        # Old-style class
     29        return hasattr(C, attr)
     30
     31
    2432class Hashable:
    2533    __metaclass__ = ABCMeta
     
    3240    def __subclasshook__(cls, C):
    3341        if cls is Hashable:
    34             for B in C.__mro__:
    35                 if "__hash__" in B.__dict__:
    36                     if B.__dict__["__hash__"]:
    37                         return True
    38                     break
     42            try:
     43                for B in C.__mro__:
     44                    if "__hash__" in B.__dict__:
     45                        if B.__dict__["__hash__"]:
     46                            return True
     47                        break
     48            except AttributeError:
     49                # Old-style class
     50                if getattr(C, "__hash__", None):
     51                    return True
    3952        return NotImplemented
    4053
     
    5164    def __subclasshook__(cls, C):
    5265        if cls is Iterable:
    53             if any("__iter__" in B.__dict__ for B in C.__mro__):
     66            if _hasattr(C, "__iter__"):
    5467                return True
    5568        return NotImplemented
     
    6275    @abstractmethod
    6376    def next(self):
     77        'Return the next item from the iterator. When exhausted, raise StopIteration'
    6478        raise StopIteration
    6579
     
    7084    def __subclasshook__(cls, C):
    7185        if cls is Iterator:
    72             if any("next" in B.__dict__ for B in C.__mro__):
     86            if _hasattr(C, "next") and _hasattr(C, "__iter__"):
    7387                return True
    7488        return NotImplemented
     
    8599    def __subclasshook__(cls, C):
    86100        if cls is Sized:
    87             if any("__len__" in B.__dict__ for B in C.__mro__):
     101            if _hasattr(C, "__len__"):
    88102                return True
    89103        return NotImplemented
     
    100114    def __subclasshook__(cls, C):
    101115        if cls is Container:
    102             if any("__contains__" in B.__dict__ for B in C.__mro__):
     116            if _hasattr(C, "__contains__"):
    103117                return True
    104118        return NotImplemented
     
    115129    def __subclasshook__(cls, C):
    116130        if cls is Callable:
    117             if any("__call__" in B.__dict__ for B in C.__mro__):
     131            if _hasattr(C, "__call__"):
    118132                return True
    119133        return NotImplemented
     
    182196
    183197    def isdisjoint(self, other):
     198        'Return True if two sets have a null intersection.'
    184199        for value in other:
    185200            if value in self:
     
    247262
    248263class MutableSet(Set):
     264    """A mutable set is a finite, iterable container.
     265
     266    This class provides concrete generic implementations of all
     267    methods except for __contains__, __iter__, __len__,
     268    add(), and discard().
     269
     270    To override the comparisons (presumably for speed, as the
     271    semantics are fixed), all you have to do is redefine __le__ and
     272    then the other operations will automatically follow suit.
     273    """
    249274
    250275    @abstractmethod
     
    293318
    294319    def __ixor__(self, it):
    295         if not isinstance(it, Set):
    296             it = self._from_iterable(it)
    297         for value in it:
    298             if value in self:
     320        if it is self:
     321            self.clear()
     322        else:
     323            if not isinstance(it, Set):
     324                it = self._from_iterable(it)
     325            for value in it:
     326                if value in self:
     327                    self.discard(value)
     328                else:
     329                    self.add(value)
     330        return self
     331
     332    def __isub__(self, it):
     333        if it is self:
     334            self.clear()
     335        else:
     336            for value in it:
    299337                self.discard(value)
    300             else:
    301                 self.add(value)
    302338        return self
    303339
    304     def __isub__(self, it):
    305         for value in it:
    306             self.discard(value)
    307         return self
    308 
    309340MutableSet.register(set)
    310341
     
    314345
    315346class Mapping(Sized, Iterable, Container):
     347
     348    """A Mapping is a generic container for associating key/value
     349    pairs.
     350
     351    This class provides concrete generic implementations of all
     352    methods except for __getitem__, __iter__, and __len__.
     353
     354    """
    316355
    317356    @abstractmethod
     
    320359
    321360    def get(self, key, default=None):
     361        'D.get(k[,d]) -> D[k] if k in D, else d.  d defaults to None.'
    322362        try:
    323363            return self[key]
     
    334374
    335375    def iterkeys(self):
     376        'D.iterkeys() -> an iterator over the keys of D'
    336377        return iter(self)
    337378
    338379    def itervalues(self):
     380        'D.itervalues() -> an iterator over the values of D'
    339381        for key in self:
    340382            yield self[key]
    341383
    342384    def iteritems(self):
     385        'D.iteritems() -> an iterator over the (key, value) items of D'
    343386        for key in self:
    344387            yield (key, self[key])
    345388
    346389    def keys(self):
     390        "D.keys() -> list of D's keys"
    347391        return list(self)
    348392
    349393    def items(self):
     394        "D.items() -> list of D's (key, value) pairs, as 2-tuples"
    350395        return [(key, self[key]) for key in self]
    351396
    352397    def values(self):
     398        "D.values() -> list of D's values"
    353399        return [self[key] for key in self]
    354400
     
    357403
    358404    def __eq__(self, other):
    359         return isinstance(other, Mapping) and \
    360                dict(self.items()) == dict(other.items())
     405        if not isinstance(other, Mapping):
     406            return NotImplemented
     407        return dict(self.items()) == dict(other.items())
    361408
    362409    def __ne__(self, other):
     
    371418        return len(self._mapping)
    372419
     420    def __repr__(self):
     421        return '{0.__class__.__name__}({0._mapping!r})'.format(self)
     422
    373423
    374424class KeysView(MappingView, Set):
     425
     426    @classmethod
     427    def _from_iterable(self, it):
     428        return set(it)
    375429
    376430    def __contains__(self, key):
     
    383437
    384438class ItemsView(MappingView, Set):
     439
     440    @classmethod
     441    def _from_iterable(self, it):
     442        return set(it)
    385443
    386444    def __contains__(self, item):
     
    413471class MutableMapping(Mapping):
    414472
     473    """A MutableMapping is a generic container for associating
     474    key/value pairs.
     475
     476    This class provides concrete generic implementations of all
     477    methods except for __getitem__, __setitem__, __delitem__,
     478    __iter__, and __len__.
     479
     480    """
     481
    415482    @abstractmethod
    416483    def __setitem__(self, key, value):
     
    424491
    425492    def pop(self, key, default=__marker):
     493        '''D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
     494          If key is not found, d is returned if given, otherwise KeyError is raised.
     495        '''
    426496        try:
    427497            value = self[key]
     
    435505
    436506    def popitem(self):
     507        '''D.popitem() -> (k, v), remove and return some (key, value) pair
     508           as a 2-tuple; but raise KeyError if D is empty.
     509        '''
    437510        try:
    438511            key = next(iter(self))
     
    444517
    445518    def clear(self):
     519        'D.clear() -> None.  Remove all items from D.'
    446520        try:
    447521            while True:
     
    450524            pass
    451525
    452     def update(self, other=(), **kwds):
     526    def update(*args, **kwds):
     527        ''' D.update([E, ]**F) -> None.  Update D from mapping/iterable E and F.
     528            If E present and has a .keys() method, does:     for k in E: D[k] = E[k]
     529            If E present and lacks .keys() method, does:     for (k, v) in E: D[k] = v
     530            In either case, this is followed by: for k, v in F.items(): D[k] = v
     531        '''
     532        if len(args) > 2:
     533            raise TypeError("update() takes at most 2 positional "
     534                            "arguments ({} given)".format(len(args)))
     535        elif not args:
     536            raise TypeError("update() takes at least 1 argument (0 given)")
     537        self = args[0]
     538        other = args[1] if len(args) >= 2 else ()
     539
    453540        if isinstance(other, Mapping):
    454541            for key in other:
     
    464551
    465552    def setdefault(self, key, default=None):
     553        'D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D'
    466554        try:
    467555            return self[key]
     
    508596
    509597    def index(self, value):
     598        '''S.index(value) -> integer -- return first index of value.
     599           Raises ValueError if the value is not present.
     600        '''
    510601        for i, v in enumerate(self):
    511602            if v == value:
     
    514605
    515606    def count(self, value):
     607        'S.count(value) -> integer -- return number of occurrences of value'
    516608        return sum(1 for v in self if v == value)
    517609
     
    524616class MutableSequence(Sequence):
    525617
     618    """All the operations on a read-only sequence.
     619
     620    Concrete subclasses must provide __new__ or __init__,
     621    __getitem__, __setitem__, __delitem__, __len__, and insert().
     622
     623    """
     624
    526625    @abstractmethod
    527626    def __setitem__(self, index, value):
     
    534633    @abstractmethod
    535634    def insert(self, index, value):
     635        'S.insert(index, object) -- insert object before index'
    536636        raise IndexError
    537637
    538638    def append(self, value):
     639        'S.append(object) -- append object to the end of the sequence'
    539640        self.insert(len(self), value)
    540641
    541642    def reverse(self):
     643        'S.reverse() -- reverse *IN PLACE*'
    542644        n = len(self)
    543645        for i in range(n//2):
     
    545647
    546648    def extend(self, values):
     649        'S.extend(iterable) -- extend sequence by appending elements from the iterable'
    547650        for v in values:
    548651            self.append(v)
    549652
    550653    def pop(self, index=-1):
     654        '''S.pop([index]) -> item -- remove and return item at index (default last).
     655           Raise IndexError if list is empty or index is out of range.
     656        '''
    551657        v = self[index]
    552658        del self[index]
     
    554660
    555661    def remove(self, value):
     662        '''S.remove(value) -- remove first occurrence of value.
     663           Raise ValueError if the value is not present.
     664        '''
    556665        del self[self.index(value)]
    557666
Note: See TracChangeset for help on using the changeset viewer.