* add print_, exec_ and _reraise helpers for 2-3 compatible code
* consolidate builtins implementation to be compatible with >=2.3 --HG-- branch : trunk
This commit is contained in:
parent
91f90d27ee
commit
5e95feaf90
|
@ -1,6 +1,9 @@
|
|||
Changes between 1.0.x and 'trunk'
|
||||
=====================================
|
||||
|
||||
* consolidate builtins implementation to be compatible with >=2.3,
|
||||
add print_, exec_ and _reraise helpers for 2-3 compatible code
|
||||
|
||||
* deprecate py.compat.doctest|subprocess|textwrap|optparse
|
||||
|
||||
* deprecate py.magic.autopath, remove py/magic directory
|
||||
|
|
|
@ -136,13 +136,16 @@ initpkg(__name__,
|
|||
|
||||
# backports and additions of builtins
|
||||
'builtin.__doc__' : ('./builtin/__init__.py', '__doc__'),
|
||||
'builtin.enumerate' : ('./builtin/enumerate.py', 'enumerate'),
|
||||
'builtin.reversed' : ('./builtin/reversed.py', 'reversed'),
|
||||
'builtin.sorted' : ('./builtin/sorted.py', 'sorted'),
|
||||
'builtin.BaseException' : ('./builtin/exception.py', 'BaseException'),
|
||||
'builtin.GeneratorExit' : ('./builtin/exception.py', 'GeneratorExit'),
|
||||
'builtin.set' : ('./builtin/set.py', 'set'),
|
||||
'builtin.frozenset' : ('./builtin/set.py', 'frozenset'),
|
||||
'builtin.enumerate' : ('./builtin/builtin24.py', 'enumerate'),
|
||||
'builtin.reversed' : ('./builtin/builtin24.py', 'reversed'),
|
||||
'builtin.sorted' : ('./builtin/builtin24.py', 'sorted'),
|
||||
'builtin.set' : ('./builtin/builtin24.py', 'set'),
|
||||
'builtin.frozenset' : ('./builtin/builtin24.py', 'frozenset'),
|
||||
'builtin.BaseException' : ('./builtin/builtin25.py', 'BaseException'),
|
||||
'builtin.GeneratorExit' : ('./builtin/builtin25.py', 'GeneratorExit'),
|
||||
'builtin.print_' : ('./builtin/builtin31.py', 'print_'),
|
||||
'builtin._reraise' : ('./builtin/builtin31.py', '_reraise'),
|
||||
'builtin.exec_' : ('./builtin/builtin31.py', 'exec_'),
|
||||
|
||||
# gateways into remote contexts
|
||||
'execnet.__doc__' : ('./execnet/__init__.py', '__doc__'),
|
||||
|
|
|
@ -0,0 +1,71 @@
|
|||
try:
|
||||
reversed = reversed
|
||||
except NameError:
|
||||
def reversed(sequence):
|
||||
"""reversed(sequence) -> reverse iterator over values of the sequence
|
||||
|
||||
Return a reverse iterator
|
||||
"""
|
||||
if hasattr(sequence, '__reversed__'):
|
||||
return sequence.__reversed__()
|
||||
if not hasattr(sequence, '__getitem__'):
|
||||
raise TypeError("argument to reversed() must be a sequence")
|
||||
return reversed_iterator(sequence)
|
||||
|
||||
class reversed_iterator(object):
|
||||
|
||||
def __init__(self, seq):
|
||||
self.seq = seq
|
||||
self.remaining = len(seq)
|
||||
|
||||
def __iter__(self):
|
||||
return self
|
||||
|
||||
def next(self):
|
||||
i = self.remaining
|
||||
if i > 0:
|
||||
i -= 1
|
||||
item = self.seq[i]
|
||||
self.remaining = i
|
||||
return item
|
||||
raise StopIteration
|
||||
|
||||
def __length_hint__(self):
|
||||
return self.remaining
|
||||
|
||||
try:
|
||||
sorted = sorted
|
||||
except NameError:
|
||||
builtin_cmp = cmp # need to use cmp as keyword arg
|
||||
|
||||
def sorted(iterable, cmp=None, key=None, reverse=0):
|
||||
use_cmp = None
|
||||
if key is not None:
|
||||
if cmp is None:
|
||||
def use_cmp(x, y):
|
||||
return builtin_cmp(x[0], y[0])
|
||||
else:
|
||||
def use_cmp(x, y):
|
||||
return cmp(x[0], y[0])
|
||||
l = [(key(element), element) for element in iterable]
|
||||
else:
|
||||
if cmp is not None:
|
||||
use_cmp = cmp
|
||||
l = list(iterable)
|
||||
if use_cmp is not None:
|
||||
l.sort(use_cmp)
|
||||
else:
|
||||
l.sort()
|
||||
if reverse:
|
||||
l.reverse()
|
||||
if key is not None:
|
||||
return [element for (_, element) in l]
|
||||
return l
|
||||
|
||||
try:
|
||||
set, frozenset = set, frozenset
|
||||
except NameError:
|
||||
from sets import set, frozenset
|
||||
|
||||
# pass through
|
||||
enumerate = enumerate
|
|
@ -1,3 +1,4 @@
|
|||
|
||||
try:
|
||||
BaseException = BaseException
|
||||
except NameError:
|
|
@ -0,0 +1,40 @@
|
|||
import sys
|
||||
|
||||
if sys.version_info >= (3, 0):
|
||||
exec ("print_ = print ; exec_=exec")
|
||||
else:
|
||||
def print_(*args, **kwargs):
|
||||
""" minimal backport of py3k print statement. """
|
||||
sep = 'sep' in kwargs and kwargs.pop('sep') or ' '
|
||||
end = 'end' in kwargs and kwargs.pop('end') or '\n'
|
||||
file = 'file' in kwargs and kwargs.pop('file') or sys.stdout
|
||||
if kwargs:
|
||||
args = ", ".join([str(x) for x in kwargs])
|
||||
raise TypeError("invalid keyword arguments: %s" % args)
|
||||
out = sep.join([str(x) for x in args]) + end
|
||||
file.write(out)
|
||||
|
||||
def exec_(obj, globals=None, locals=None):
|
||||
""" minimal backport of py3k exec statement. """
|
||||
if globals is None:
|
||||
frame = sys._getframe(1)
|
||||
globals = frame.f_globals
|
||||
if locals is None:
|
||||
locals = frame.f_locals
|
||||
elif locals is None:
|
||||
locals = globals
|
||||
exec2(obj, globals, locals)
|
||||
|
||||
if sys.version_info >= (3,0):
|
||||
exec ("""
|
||||
def _reraise(cls, val, tb):
|
||||
assert hasattr(val, '__traceback__')
|
||||
raise val
|
||||
""")
|
||||
else:
|
||||
exec ("""
|
||||
def _reraise(cls, val, tb):
|
||||
raise cls, val, tb
|
||||
def exec2(obj, globals, locals):
|
||||
exec obj in globals, locals
|
||||
""")
|
|
@ -1,9 +0,0 @@
|
|||
from __future__ import generators
|
||||
try:
|
||||
enumerate = enumerate
|
||||
except NameError:
|
||||
def enumerate(iterable):
|
||||
i = 0
|
||||
for x in iterable:
|
||||
yield i, x
|
||||
i += 1
|
|
@ -1,37 +0,0 @@
|
|||
from __future__ import generators
|
||||
try:
|
||||
reversed = reversed
|
||||
except NameError:
|
||||
|
||||
def reversed(sequence):
|
||||
"""reversed(sequence) -> reverse iterator over values of the sequence
|
||||
|
||||
Return a reverse iterator
|
||||
"""
|
||||
if hasattr(sequence, '__reversed__'):
|
||||
return sequence.__reversed__()
|
||||
if not hasattr(sequence, '__getitem__'):
|
||||
raise TypeError("argument to reversed() must be a sequence")
|
||||
return reversed_iterator(sequence)
|
||||
|
||||
|
||||
class reversed_iterator(object):
|
||||
|
||||
def __init__(self, seq):
|
||||
self.seq = seq
|
||||
self.remaining = len(seq)
|
||||
|
||||
def __iter__(self):
|
||||
return self
|
||||
|
||||
def next(self):
|
||||
i = self.remaining
|
||||
if i > 0:
|
||||
i -= 1
|
||||
item = self.seq[i]
|
||||
self.remaining = i
|
||||
return item
|
||||
raise StopIteration
|
||||
|
||||
def __length_hint__(self):
|
||||
return self.remaining
|
|
@ -1,587 +0,0 @@
|
|||
"""Classes to represent arbitrary sets (including sets of sets).
|
||||
|
||||
This module implements sets using dictionaries whose values are
|
||||
ignored. The usual operations (union, intersection, deletion, etc.)
|
||||
are provided as both methods and operators.
|
||||
|
||||
Important: sets are not sequences! While they support 'x in s',
|
||||
'len(s)', and 'for x in s', none of those operations are unique for
|
||||
sequences; for example, mappings support all three as well. The
|
||||
characteristic operation for sequences is subscripting with small
|
||||
integers: s[i], for i in range(len(s)). Sets don't support
|
||||
subscripting at all. Also, sequences allow multiple occurrences and
|
||||
their elements have a definite order; sets on the other hand don't
|
||||
record multiple occurrences and don't remember the order of element
|
||||
insertion (which is why they don't support s[i]).
|
||||
|
||||
The following classes are provided:
|
||||
|
||||
BaseSet -- All the operations common to both mutable and immutable
|
||||
sets. This is an abstract class, not meant to be directly
|
||||
instantiated.
|
||||
|
||||
Set -- Mutable sets, subclass of BaseSet; not hashable.
|
||||
|
||||
ImmutableSet -- Immutable sets, subclass of BaseSet; hashable.
|
||||
|
||||
|
||||
_TemporarilyImmutableSet -- A wrapper around a Set, hashable,
|
||||
giving the same hash value as the immutable set equivalent
|
||||
would have. Do not use this class directly.
|
||||
|
||||
Only hashable objects can be added to a Set. In particular, you cannot
|
||||
really add a Set as an element to another Set; if you try, what is
|
||||
actually added is an ImmutableSet built from it (it compares equal to
|
||||
the one you tried adding).
|
||||
|
||||
When you ask if `x in y' where x is a Set and y is a Set or
|
||||
ImmutableSet, x is wrapped into a _TemporarilyImmutableSet z, and
|
||||
what's tested is actually `z in y'.
|
||||
|
||||
"""
|
||||
|
||||
# Code history:
|
||||
#
|
||||
# - Greg V. Wilson wrote the first version, using a different approach
|
||||
# to the mutable/immutable problem, and inheriting from dict.
|
||||
#
|
||||
# - Alex Martelli modified Greg's version to implement the current
|
||||
# Set/ImmutableSet approach, and make the data an attribute.
|
||||
#
|
||||
# - Guido van Rossum rewrote much of the code, made some API changes,
|
||||
# and cleaned up the docstrings.
|
||||
#
|
||||
# - Raymond Hettinger added a number of speedups and other
|
||||
# improvements.
|
||||
|
||||
from __future__ import generators
|
||||
try:
|
||||
from itertools import ifilter, ifilterfalse
|
||||
except ImportError:
|
||||
# Code to make the module run under Py2.2
|
||||
def ifilter(predicate, iterable):
|
||||
if predicate is None:
|
||||
def predicate(x):
|
||||
return x
|
||||
for x in iterable:
|
||||
if predicate(x):
|
||||
yield x
|
||||
def ifilterfalse(predicate, iterable):
|
||||
if predicate is None:
|
||||
def predicate(x):
|
||||
return x
|
||||
for x in iterable:
|
||||
if not predicate(x):
|
||||
yield x
|
||||
try:
|
||||
True, False
|
||||
except NameError:
|
||||
True, False = (0==0, 0!=0)
|
||||
|
||||
__all__ = ['BaseSet', 'Set', 'ImmutableSet']
|
||||
|
||||
class BaseSet(object):
|
||||
"""Common base class for mutable and immutable sets."""
|
||||
|
||||
__slots__ = ['_data']
|
||||
|
||||
# Constructor
|
||||
|
||||
def __init__(self):
|
||||
"""This is an abstract class."""
|
||||
# Don't call this from a concrete subclass!
|
||||
if self.__class__ is BaseSet:
|
||||
raise TypeError, ("BaseSet is an abstract class. "
|
||||
"Use Set or ImmutableSet.")
|
||||
|
||||
# Standard protocols: __len__, __repr__, __str__, __iter__
|
||||
|
||||
def __len__(self):
|
||||
"""Return the number of elements of a set."""
|
||||
return len(self._data)
|
||||
|
||||
def __repr__(self):
|
||||
"""Return string representation of a set.
|
||||
|
||||
This looks like 'Set([<list of elements>])'.
|
||||
"""
|
||||
return self._repr()
|
||||
|
||||
# __str__ is the same as __repr__
|
||||
__str__ = __repr__
|
||||
|
||||
def _repr(self, sorted=False):
|
||||
elements = self._data.keys()
|
||||
if sorted:
|
||||
elements.sort()
|
||||
return '%s(%r)' % (self.__class__.__name__, elements)
|
||||
|
||||
def __iter__(self):
|
||||
"""Return an iterator over the elements or a set.
|
||||
|
||||
This is the keys iterator for the underlying dict.
|
||||
"""
|
||||
return self._data.iterkeys()
|
||||
|
||||
# Three-way comparison is not supported. However, because __eq__ is
|
||||
# tried before __cmp__, if Set x == Set y, x.__eq__(y) returns True and
|
||||
# then cmp(x, y) returns 0 (Python doesn't actually call __cmp__ in this
|
||||
# case).
|
||||
|
||||
def __cmp__(self, other):
|
||||
raise TypeError, "can't compare sets using cmp()"
|
||||
|
||||
# Equality comparisons using the underlying dicts. Mixed-type comparisons
|
||||
# are allowed here, where Set == z for non-Set z always returns False,
|
||||
# and Set != z always True. This allows expressions like "x in y" to
|
||||
# give the expected result when y is a sequence of mixed types, not
|
||||
# raising a pointless TypeError just because y contains a Set, or x is
|
||||
# a Set and y contain's a non-set ("in" invokes only __eq__).
|
||||
# Subtle: it would be nicer if __eq__ and __ne__ could return
|
||||
# NotImplemented instead of True or False. Then the other comparand
|
||||
# would get a chance to determine the result, and if the other comparand
|
||||
# also returned NotImplemented then it would fall back to object address
|
||||
# comparison (which would always return False for __eq__ and always
|
||||
# True for __ne__). However, that doesn't work, because this type
|
||||
# *also* implements __cmp__: if, e.g., __eq__ returns NotImplemented,
|
||||
# Python tries __cmp__ next, and the __cmp__ here then raises TypeError.
|
||||
|
||||
def __eq__(self, other):
|
||||
if isinstance(other, BaseSet):
|
||||
return self._data == other._data
|
||||
else:
|
||||
return False
|
||||
|
||||
def __ne__(self, other):
|
||||
if isinstance(other, BaseSet):
|
||||
return self._data != other._data
|
||||
else:
|
||||
return True
|
||||
|
||||
# Copying operations
|
||||
|
||||
def copy(self):
|
||||
"""Return a shallow copy of a set."""
|
||||
result = self.__class__()
|
||||
result._data.update(self._data)
|
||||
return result
|
||||
|
||||
__copy__ = copy # For the copy module
|
||||
|
||||
def __deepcopy__(self, memo):
|
||||
"""Return a deep copy of a set; used by copy module."""
|
||||
# This pre-creates the result and inserts it in the memo
|
||||
# early, in case the deep copy recurses into another reference
|
||||
# to this same set. A set can't be an element of itself, but
|
||||
# it can certainly contain an object that has a reference to
|
||||
# itself.
|
||||
from copy import deepcopy
|
||||
result = self.__class__()
|
||||
memo[id(self)] = result
|
||||
data = result._data
|
||||
value = True
|
||||
for elt in self:
|
||||
data[deepcopy(elt, memo)] = value
|
||||
return result
|
||||
|
||||
# Standard set operations: union, intersection, both differences.
|
||||
# Each has an operator version (e.g. __or__, invoked with |) and a
|
||||
# method version (e.g. union).
|
||||
# Subtle: Each pair requires distinct code so that the outcome is
|
||||
# correct when the type of other isn't suitable. For example, if
|
||||
# we did "union = __or__" instead, then Set().union(3) would return
|
||||
# NotImplemented instead of raising TypeError (albeit that *why* it
|
||||
# raises TypeError as-is is also a bit subtle).
|
||||
|
||||
def __or__(self, other):
|
||||
"""Return the union of two sets as a new set.
|
||||
|
||||
(I.e. all elements that are in either set.)
|
||||
"""
|
||||
if not isinstance(other, BaseSet):
|
||||
return NotImplemented
|
||||
return self.union(other)
|
||||
|
||||
def union(self, other):
|
||||
"""Return the union of two sets as a new set.
|
||||
|
||||
(I.e. all elements that are in either set.)
|
||||
"""
|
||||
result = self.__class__(self)
|
||||
result._update(other)
|
||||
return result
|
||||
|
||||
def __and__(self, other):
|
||||
"""Return the intersection of two sets as a new set.
|
||||
|
||||
(I.e. all elements that are in both sets.)
|
||||
"""
|
||||
if not isinstance(other, BaseSet):
|
||||
return NotImplemented
|
||||
return self.intersection(other)
|
||||
|
||||
def intersection(self, other):
|
||||
"""Return the intersection of two sets as a new set.
|
||||
|
||||
(I.e. all elements that are in both sets.)
|
||||
"""
|
||||
if not isinstance(other, BaseSet):
|
||||
other = Set(other)
|
||||
if len(self) <= len(other):
|
||||
little, big = self, other
|
||||
else:
|
||||
little, big = other, self
|
||||
common = ifilter(big._data.has_key, little)
|
||||
return self.__class__(common)
|
||||
|
||||
def __xor__(self, other):
|
||||
"""Return the symmetric difference of two sets as a new set.
|
||||
|
||||
(I.e. all elements that are in exactly one of the sets.)
|
||||
"""
|
||||
if not isinstance(other, BaseSet):
|
||||
return NotImplemented
|
||||
return self.symmetric_difference(other)
|
||||
|
||||
def symmetric_difference(self, other):
|
||||
"""Return the symmetric difference of two sets as a new set.
|
||||
|
||||
(I.e. all elements that are in exactly one of the sets.)
|
||||
"""
|
||||
result = self.__class__()
|
||||
data = result._data
|
||||
value = True
|
||||
selfdata = self._data
|
||||
try:
|
||||
otherdata = other._data
|
||||
except AttributeError:
|
||||
otherdata = Set(other)._data
|
||||
for elt in ifilterfalse(otherdata.has_key, selfdata):
|
||||
data[elt] = value
|
||||
for elt in ifilterfalse(selfdata.has_key, otherdata):
|
||||
data[elt] = value
|
||||
return result
|
||||
|
||||
def __sub__(self, other):
|
||||
"""Return the difference of two sets as a new Set.
|
||||
|
||||
(I.e. all elements that are in this set and not in the other.)
|
||||
"""
|
||||
if not isinstance(other, BaseSet):
|
||||
return NotImplemented
|
||||
return self.difference(other)
|
||||
|
||||
def difference(self, other):
|
||||
"""Return the difference of two sets as a new Set.
|
||||
|
||||
(I.e. all elements that are in this set and not in the other.)
|
||||
"""
|
||||
result = self.__class__()
|
||||
data = result._data
|
||||
try:
|
||||
otherdata = other._data
|
||||
except AttributeError:
|
||||
otherdata = Set(other)._data
|
||||
value = True
|
||||
for elt in ifilterfalse(otherdata.has_key, self):
|
||||
data[elt] = value
|
||||
return result
|
||||
|
||||
# Membership test
|
||||
|
||||
def __contains__(self, element):
|
||||
"""Report whether an element is a member of a set.
|
||||
|
||||
(Called in response to the expression `element in self'.)
|
||||
"""
|
||||
try:
|
||||
return element in self._data
|
||||
except TypeError:
|
||||
transform = getattr(element, "__as_temporarily_immutable__", None)
|
||||
if transform is None:
|
||||
raise # re-raise the TypeError exception we caught
|
||||
return transform() in self._data
|
||||
|
||||
# Subset and superset test
|
||||
|
||||
def issubset(self, other):
|
||||
"""Report whether another set contains this set."""
|
||||
self._binary_sanity_check(other)
|
||||
if len(self) > len(other): # Fast check for obvious cases
|
||||
return False
|
||||
for elt in ifilterfalse(other._data.has_key, self):
|
||||
return False
|
||||
return True
|
||||
|
||||
def issuperset(self, other):
|
||||
"""Report whether this set contains another set."""
|
||||
self._binary_sanity_check(other)
|
||||
if len(self) < len(other): # Fast check for obvious cases
|
||||
return False
|
||||
for elt in ifilterfalse(self._data.has_key, other):
|
||||
return False
|
||||
return True
|
||||
|
||||
# Inequality comparisons using the is-subset relation.
|
||||
__le__ = issubset
|
||||
__ge__ = issuperset
|
||||
|
||||
def __lt__(self, other):
|
||||
self._binary_sanity_check(other)
|
||||
return len(self) < len(other) and self.issubset(other)
|
||||
|
||||
def __gt__(self, other):
|
||||
self._binary_sanity_check(other)
|
||||
return len(self) > len(other) and self.issuperset(other)
|
||||
|
||||
# Assorted helpers
|
||||
|
||||
def _binary_sanity_check(self, other):
|
||||
# Check that the other argument to a binary operation is also
|
||||
# a set, raising a TypeError otherwise.
|
||||
if not isinstance(other, BaseSet):
|
||||
raise TypeError, "Binary operation only permitted between sets"
|
||||
|
||||
def _compute_hash(self):
|
||||
# Calculate hash code for a set by xor'ing the hash codes of
|
||||
# the elements. This ensures that the hash code does not depend
|
||||
# on the order in which elements are added to the set. This is
|
||||
# not called __hash__ because a BaseSet should not be hashable;
|
||||
# only an ImmutableSet is hashable.
|
||||
result = 0
|
||||
for elt in self:
|
||||
result ^= hash(elt)
|
||||
return result
|
||||
|
||||
def _update(self, iterable):
|
||||
# The main loop for update() and the subclass __init__() methods.
|
||||
data = self._data
|
||||
|
||||
# Use the fast update() method when a dictionary is available.
|
||||
if isinstance(iterable, BaseSet):
|
||||
data.update(iterable._data)
|
||||
return
|
||||
|
||||
value = True
|
||||
|
||||
if type(iterable) in (list, tuple, xrange):
|
||||
# Optimized: we know that __iter__() and next() can't
|
||||
# raise TypeError, so we can move 'try:' out of the loop.
|
||||
it = iter(iterable)
|
||||
while True:
|
||||
try:
|
||||
for element in it:
|
||||
data[element] = value
|
||||
return
|
||||
except TypeError:
|
||||
transform = getattr(element, "__as_immutable__", None)
|
||||
if transform is None:
|
||||
raise # re-raise the TypeError exception we caught
|
||||
data[transform()] = value
|
||||
else:
|
||||
# Safe: only catch TypeError where intended
|
||||
for element in iterable:
|
||||
try:
|
||||
data[element] = value
|
||||
except TypeError:
|
||||
transform = getattr(element, "__as_immutable__", None)
|
||||
if transform is None:
|
||||
raise # re-raise the TypeError exception we caught
|
||||
data[transform()] = value
|
||||
|
||||
|
||||
class ImmutableSet(BaseSet):
|
||||
"""Immutable set class."""
|
||||
|
||||
__slots__ = ['_hashcode']
|
||||
|
||||
# BaseSet + hashing
|
||||
|
||||
def __init__(self, iterable=None):
|
||||
"""Construct an immutable set from an optional iterable."""
|
||||
self._hashcode = None
|
||||
self._data = {}
|
||||
if iterable is not None:
|
||||
self._update(iterable)
|
||||
|
||||
def __hash__(self):
|
||||
if self._hashcode is None:
|
||||
self._hashcode = self._compute_hash()
|
||||
return self._hashcode
|
||||
|
||||
def __getstate__(self):
|
||||
return self._data, self._hashcode
|
||||
|
||||
def __setstate__(self, state):
|
||||
self._data, self._hashcode = state
|
||||
|
||||
class Set(BaseSet):
|
||||
""" Mutable set class."""
|
||||
|
||||
__slots__ = []
|
||||
|
||||
# BaseSet + operations requiring mutability; no hashing
|
||||
|
||||
def __init__(self, iterable=None):
|
||||
"""Construct a set from an optional iterable."""
|
||||
self._data = {}
|
||||
if iterable is not None:
|
||||
self._update(iterable)
|
||||
|
||||
def __getstate__(self):
|
||||
# getstate's results are ignored if it is not
|
||||
return self._data,
|
||||
|
||||
def __setstate__(self, data):
|
||||
self._data, = data
|
||||
|
||||
def __hash__(self):
|
||||
"""A Set cannot be hashed."""
|
||||
# We inherit object.__hash__, so we must deny this explicitly
|
||||
raise TypeError, "Can't hash a Set, only an ImmutableSet."
|
||||
|
||||
# In-place union, intersection, differences.
|
||||
# Subtle: The xyz_update() functions deliberately return None,
|
||||
# as do all mutating operations on built-in container types.
|
||||
# The __xyz__ spellings have to return self, though.
|
||||
|
||||
def __ior__(self, other):
|
||||
"""Update a set with the union of itself and another."""
|
||||
self._binary_sanity_check(other)
|
||||
self._data.update(other._data)
|
||||
return self
|
||||
|
||||
def union_update(self, other):
|
||||
"""Update a set with the union of itself and another."""
|
||||
self._update(other)
|
||||
|
||||
def __iand__(self, other):
|
||||
"""Update a set with the intersection of itself and another."""
|
||||
self._binary_sanity_check(other)
|
||||
self._data = (self & other)._data
|
||||
return self
|
||||
|
||||
def intersection_update(self, other):
|
||||
"""Update a set with the intersection of itself and another."""
|
||||
if isinstance(other, BaseSet):
|
||||
self &= other
|
||||
else:
|
||||
self._data = (self.intersection(other))._data
|
||||
|
||||
def __ixor__(self, other):
|
||||
"""Update a set with the symmetric difference of itself and another."""
|
||||
self._binary_sanity_check(other)
|
||||
self.symmetric_difference_update(other)
|
||||
return self
|
||||
|
||||
def symmetric_difference_update(self, other):
|
||||
"""Update a set with the symmetric difference of itself and another."""
|
||||
data = self._data
|
||||
value = True
|
||||
if not isinstance(other, BaseSet):
|
||||
other = Set(other)
|
||||
if self is other:
|
||||
self.clear()
|
||||
for elt in other:
|
||||
if elt in data:
|
||||
del data[elt]
|
||||
else:
|
||||
data[elt] = value
|
||||
|
||||
def __isub__(self, other):
|
||||
"""Remove all elements of another set from this set."""
|
||||
self._binary_sanity_check(other)
|
||||
self.difference_update(other)
|
||||
return self
|
||||
|
||||
def difference_update(self, other):
|
||||
"""Remove all elements of another set from this set."""
|
||||
data = self._data
|
||||
if not isinstance(other, BaseSet):
|
||||
other = Set(other)
|
||||
if self is other:
|
||||
self.clear()
|
||||
for elt in ifilter(data.has_key, other):
|
||||
del data[elt]
|
||||
|
||||
# Python dict-like mass mutations: update, clear
|
||||
|
||||
def update(self, iterable):
|
||||
"""Add all values from an iterable (such as a list or file)."""
|
||||
self._update(iterable)
|
||||
|
||||
def clear(self):
|
||||
"""Remove all elements from this set."""
|
||||
self._data.clear()
|
||||
|
||||
# Single-element mutations: add, remove, discard
|
||||
|
||||
def add(self, element):
|
||||
"""Add an element to a set.
|
||||
|
||||
This has no effect if the element is already present.
|
||||
"""
|
||||
try:
|
||||
self._data[element] = True
|
||||
except TypeError:
|
||||
transform = getattr(element, "__as_immutable__", None)
|
||||
if transform is None:
|
||||
raise # re-raise the TypeError exception we caught
|
||||
self._data[transform()] = True
|
||||
|
||||
def remove(self, element):
|
||||
"""Remove an element from a set; it must be a member.
|
||||
|
||||
If the element is not a member, raise a KeyError.
|
||||
"""
|
||||
try:
|
||||
del self._data[element]
|
||||
except TypeError:
|
||||
transform = getattr(element, "__as_temporarily_immutable__", None)
|
||||
if transform is None:
|
||||
raise # re-raise the TypeError exception we caught
|
||||
del self._data[transform()]
|
||||
|
||||
def discard(self, element):
|
||||
"""Remove an element from a set if it is a member.
|
||||
|
||||
If the element is not a member, do nothing.
|
||||
"""
|
||||
try:
|
||||
self.remove(element)
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
def pop(self):
|
||||
"""Remove and return an arbitrary set element."""
|
||||
return self._data.popitem()[0]
|
||||
|
||||
def __as_immutable__(self):
|
||||
# Return a copy of self as an immutable set
|
||||
return ImmutableSet(self)
|
||||
|
||||
def __as_temporarily_immutable__(self):
|
||||
# Return self wrapped in a temporarily immutable set
|
||||
return _TemporarilyImmutableSet(self)
|
||||
|
||||
|
||||
class _TemporarilyImmutableSet(BaseSet):
|
||||
# Wrap a mutable set as if it was temporarily immutable.
|
||||
# This only supplies hashing and equality comparisons.
|
||||
|
||||
def __init__(self, set):
|
||||
self._set = set
|
||||
self._data = set._data # Needed by ImmutableSet.__eq__()
|
||||
|
||||
def __hash__(self):
|
||||
return self._set._compute_hash()
|
||||
|
||||
|
||||
try:
|
||||
set, frozenset = set, frozenset
|
||||
except NameError:
|
||||
try:
|
||||
from sets import Set as set, ImmutableSet as frozenset
|
||||
except ImportError:
|
||||
set = Set
|
||||
frozenset = ImmutableSet
|
|
@ -1,31 +0,0 @@
|
|||
builtin_cmp = cmp # need to use cmp as keyword arg
|
||||
|
||||
def _sorted(iterable, cmp=None, key=None, reverse=0):
|
||||
use_cmp = None
|
||||
if key is not None:
|
||||
if cmp is None:
|
||||
def use_cmp(x, y):
|
||||
return builtin_cmp(x[0], y[0])
|
||||
else:
|
||||
def use_cmp(x, y):
|
||||
return cmp(x[0], y[0])
|
||||
l = [(key(element), element) for element in iterable]
|
||||
else:
|
||||
if cmp is not None:
|
||||
use_cmp = cmp
|
||||
l = list(iterable)
|
||||
#print l
|
||||
if use_cmp is not None:
|
||||
l.sort(use_cmp)
|
||||
else:
|
||||
l.sort()
|
||||
if reverse:
|
||||
l.reverse()
|
||||
if key is not None:
|
||||
return [element for (_, element) in l]
|
||||
return l
|
||||
|
||||
try:
|
||||
sorted = sorted
|
||||
except NameError:
|
||||
sorted = _sorted
|
|
@ -0,0 +1,95 @@
|
|||
import sys
|
||||
import py
|
||||
from py.builtin import set, frozenset, reversed, sorted
|
||||
|
||||
def test_enumerate():
|
||||
l = [0,1,2]
|
||||
for i,x in enumerate(l):
|
||||
assert i == x
|
||||
|
||||
def test_BaseException():
|
||||
assert issubclass(IndexError, py.builtin.BaseException)
|
||||
assert issubclass(Exception, py.builtin.BaseException)
|
||||
assert issubclass(KeyboardInterrupt, py.builtin.BaseException)
|
||||
|
||||
class MyRandomClass(object):
|
||||
pass
|
||||
assert not issubclass(MyRandomClass, py.builtin.BaseException)
|
||||
|
||||
assert py.builtin.BaseException.__module__ == 'exceptions'
|
||||
assert Exception.__name__ == 'Exception'
|
||||
|
||||
|
||||
def test_GeneratorExit():
|
||||
assert py.builtin.GeneratorExit.__module__ == 'exceptions'
|
||||
assert issubclass(py.builtin.GeneratorExit, py.builtin.BaseException)
|
||||
|
||||
def test_reversed():
|
||||
reversed = py.builtin.reversed
|
||||
r = reversed("hello")
|
||||
assert iter(r) is r
|
||||
assert r.next() == "o"
|
||||
assert r.next() == "l"
|
||||
assert r.next() == "l"
|
||||
assert r.next() == "e"
|
||||
assert r.next() == "h"
|
||||
py.test.raises(StopIteration, r.next)
|
||||
assert list(reversed(list(reversed("hello")))) == ['h','e','l','l','o']
|
||||
py.test.raises(TypeError, reversed, reversed("hello"))
|
||||
|
||||
def test_simple():
|
||||
s = set([1, 2, 3, 4])
|
||||
assert s == set([3, 4, 2, 1])
|
||||
s1 = s.union(set([5, 6]))
|
||||
assert 5 in s1
|
||||
assert 1 in s1
|
||||
|
||||
def test_frozenset():
|
||||
s = set([frozenset([0, 1]), frozenset([1, 0])])
|
||||
assert len(s) == 1
|
||||
|
||||
def test_sorted():
|
||||
for s in [py.builtin.sorted]:
|
||||
def test():
|
||||
assert s([3, 2, 1]) == [1, 2, 3]
|
||||
assert s([1, 2, 3], reverse=True) == [3, 2, 1]
|
||||
l = s([1, 2, 3, 4, 5, 6], key=lambda x: x % 2)
|
||||
assert l == [2, 4, 6, 1, 3, 5]
|
||||
l = s([1, 2, 3, 4], cmp=lambda x, y: -cmp(x, y))
|
||||
assert l == [4, 3, 2, 1]
|
||||
l = s([1, 2, 3, 4], cmp=lambda x, y: -cmp(x, y),
|
||||
key=lambda x: x % 2)
|
||||
assert l == [1, 3, 2, 4]
|
||||
|
||||
def compare(x, y):
|
||||
assert type(x) == str
|
||||
assert type(y) == str
|
||||
return cmp(x, y)
|
||||
data = 'The quick Brown fox Jumped over The lazy Dog'.split()
|
||||
s(data, cmp=compare, key=str.lower)
|
||||
yield test
|
||||
|
||||
|
||||
def test_print_simple():
|
||||
from py.builtin import print_
|
||||
f = py.io.TextIO()
|
||||
print_("hello", "world", file=f)
|
||||
s = f.getvalue()
|
||||
assert s == u"hello world\n"
|
||||
py.test.raises(TypeError, "print_(hello=3)")
|
||||
|
||||
def test_reraise():
|
||||
from py.builtin import _reraise
|
||||
try:
|
||||
raise Exception()
|
||||
except Exception:
|
||||
cls, val, tb = sys.exc_info()
|
||||
excinfo = py.test.raises(Exception, "_reraise(cls, val, tb)")
|
||||
|
||||
def test_exec():
|
||||
l = []
|
||||
py.builtin.exec_("l.append(1)")
|
||||
assert l == [1]
|
||||
d = {}
|
||||
py.builtin.exec_("x=4", d)
|
||||
assert d['x'] == 4
|
|
@ -1,7 +0,0 @@
|
|||
import sys
|
||||
import py
|
||||
|
||||
def test_enumerate():
|
||||
l = [0,1,2]
|
||||
for i,x in py.builtin.enumerate(l):
|
||||
assert i == x
|
|
@ -1,18 +0,0 @@
|
|||
import py
|
||||
|
||||
def test_BaseException():
|
||||
assert issubclass(IndexError, py.builtin.BaseException)
|
||||
assert issubclass(Exception, py.builtin.BaseException)
|
||||
assert issubclass(KeyboardInterrupt, py.builtin.BaseException)
|
||||
|
||||
class MyRandomClass(object):
|
||||
pass
|
||||
assert not issubclass(MyRandomClass, py.builtin.BaseException)
|
||||
|
||||
assert py.builtin.BaseException.__module__ == 'exceptions'
|
||||
assert Exception.__name__ == 'Exception'
|
||||
|
||||
|
||||
def test_GeneratorExit():
|
||||
assert py.builtin.GeneratorExit.__module__ == 'exceptions'
|
||||
assert issubclass(py.builtin.GeneratorExit, py.builtin.BaseException)
|
|
@ -1,14 +0,0 @@
|
|||
from py.builtin import reversed
|
||||
from py.test import raises
|
||||
|
||||
def test_reversed():
|
||||
r = reversed("hello")
|
||||
assert iter(r) is r
|
||||
assert r.next() == "o"
|
||||
assert r.next() == "l"
|
||||
assert r.next() == "l"
|
||||
assert r.next() == "e"
|
||||
assert r.next() == "h"
|
||||
raises(StopIteration, r.next)
|
||||
assert list(reversed(list(reversed("hello")))) == ['h','e','l','l','o']
|
||||
raises(TypeError, reversed, reversed("hello"))
|
|
@ -1,14 +0,0 @@
|
|||
# some small tests to see whether sets are there and work
|
||||
|
||||
from py.builtin import set, frozenset
|
||||
|
||||
def test_simple():
|
||||
s = set([1, 2, 3, 4])
|
||||
assert s == set([3, 4, 2, 1])
|
||||
s1 = s.union(set([5, 6]))
|
||||
assert 5 in s1
|
||||
assert 1 in s1
|
||||
|
||||
def test_frozenset():
|
||||
s = set([frozenset([0, 1]), frozenset([1, 0])])
|
||||
assert len(s) == 1
|
|
@ -1,25 +0,0 @@
|
|||
from __future__ import generators
|
||||
import py
|
||||
from py.__.builtin.sorted import _sorted
|
||||
|
||||
def test_sorted():
|
||||
for s in [_sorted, py.builtin.sorted]:
|
||||
def test():
|
||||
assert s([3, 2, 1]) == [1, 2, 3]
|
||||
assert s([1, 2, 3], reverse=True) == [3, 2, 1]
|
||||
l = s([1, 2, 3, 4, 5, 6], key=lambda x: x % 2)
|
||||
assert l == [2, 4, 6, 1, 3, 5]
|
||||
l = s([1, 2, 3, 4], cmp=lambda x, y: -cmp(x, y))
|
||||
assert l == [4, 3, 2, 1]
|
||||
l = s([1, 2, 3, 4], cmp=lambda x, y: -cmp(x, y),
|
||||
key=lambda x: x % 2)
|
||||
assert l == [1, 3, 2, 4]
|
||||
|
||||
def compare(x, y):
|
||||
assert type(x) == str
|
||||
assert type(y) == str
|
||||
return cmp(x, y)
|
||||
data = 'The quick Brown fox Jumped over The lazy Dog'.split()
|
||||
s(data, cmp=compare, key=str.lower)
|
||||
yield test
|
||||
|
|
@ -329,7 +329,7 @@ class Traceback(list):
|
|||
originates if appropriate, None if no recursion occurred
|
||||
"""
|
||||
cache = {}
|
||||
for i, entry in py.builtin.enumerate(self):
|
||||
for i, entry in enumerate(self):
|
||||
key = entry.frame.code.path, entry.lineno
|
||||
#print "checking for recursion at", key
|
||||
l = cache.setdefault(key, [])
|
||||
|
@ -555,7 +555,7 @@ class FormattedExcinfo(object):
|
|||
last = traceback[-1]
|
||||
entries = []
|
||||
extraline = None
|
||||
for index, entry in py.builtin.enumerate(traceback):
|
||||
for index, entry in enumerate(traceback):
|
||||
einfo = (last == entry) and excinfo or None
|
||||
reprentry = self.repr_traceback_entry(entry, einfo)
|
||||
entries.append(reprentry)
|
||||
|
|
|
@ -706,7 +706,7 @@ class SvnWCCommandPath(common.PathBase):
|
|||
result = []
|
||||
blamelines = out.splitlines()
|
||||
reallines = py.path.svnurl(self.url).readlines()
|
||||
for i, (blameline, line) in py.builtin.enumerate(
|
||||
for i, (blameline, line) in enumerate(
|
||||
zip(blamelines, reallines)):
|
||||
m = rex_blame.match(blameline)
|
||||
if not m:
|
||||
|
|
|
@ -160,7 +160,7 @@ class Paragraph(AbstractNode):
|
|||
def __init__(self, *args, **kwargs):
|
||||
# make shortcut
|
||||
args = list(args)
|
||||
for num, arg in py.builtin.enumerate(args):
|
||||
for num, arg in enumerate(args):
|
||||
if isinstance(arg, str):
|
||||
args[num] = Text(arg)
|
||||
super(Paragraph, self).__init__(*args, **kwargs)
|
||||
|
@ -255,7 +255,7 @@ class LiteralBlock(AbstractText):
|
|||
if not self._text.strip():
|
||||
return ''
|
||||
text = self.escape(self._text).split('\n')
|
||||
for i, line in py.builtin.enumerate(text):
|
||||
for i, line in enumerate(text):
|
||||
if line.strip():
|
||||
text[i] = ' %s' % (line,)
|
||||
return self.start + '\n'.join(text)
|
||||
|
|
|
@ -88,7 +88,7 @@ class HookRecorder:
|
|||
return l
|
||||
|
||||
def popcall(self, name):
|
||||
for i, call in py.builtin.enumerate(self.calls):
|
||||
for i, call in enumerate(self.calls):
|
||||
if call._name == name:
|
||||
del self.calls[i]
|
||||
return call
|
||||
|
|
|
@ -102,7 +102,7 @@ class WarningsRecorder:
|
|||
|
||||
def pop(self, cls=Warning):
|
||||
""" pop the first recorded warning, raise exception if not exists."""
|
||||
for i, w in py.builtin.enumerate(self.list):
|
||||
for i, w in enumerate(self.list):
|
||||
if issubclass(w.category, cls):
|
||||
return self.list.pop(i)
|
||||
__tracebackhide__ = True
|
||||
|
|
|
@ -54,7 +54,7 @@ def deindent(s, sep='\n'):
|
|||
leastspaces = spaces
|
||||
if leastspaces == -1:
|
||||
return s
|
||||
for i, line in py.builtin.enumerate(lines):
|
||||
for i, line in enumerate(lines):
|
||||
if not line.strip():
|
||||
lines[i] = ''
|
||||
else:
|
||||
|
@ -256,7 +256,7 @@ class LinkCheckerMaker(py.test.collect.Collector):
|
|||
path = self.fspath
|
||||
# generating functions + args as single tests
|
||||
timeout = self.config.getvalue("urlcheck_timeout")
|
||||
for lineno, line in py.builtin.enumerate(path.readlines()):
|
||||
for lineno, line in enumerate(path.readlines()):
|
||||
line = line.strip()
|
||||
if line.startswith('.. _'):
|
||||
if line.startswith('.. _`'):
|
||||
|
|
|
@ -244,7 +244,7 @@ class TerminalReporter:
|
|||
plugins.append(name)
|
||||
plugins = ", ".join(plugins)
|
||||
self.write_line("active plugins: %s" %(plugins,))
|
||||
for i, testarg in py.builtin.enumerate(self.config.args):
|
||||
for i, testarg in enumerate(self.config.args):
|
||||
self.write_line("test object %d: %s" %(i+1, testarg))
|
||||
|
||||
def pytest_sessionfinish(self, exitstatus, __multicall__):
|
||||
|
|
|
@ -287,7 +287,7 @@ class Generator(FunctionMixin, PyCollectorMixin, py.test.collect.Collector):
|
|||
self.config._setupstate.prepare(self)
|
||||
l = []
|
||||
seen = {}
|
||||
for i, x in py.builtin.enumerate(self.obj()):
|
||||
for i, x in enumerate(self.obj()):
|
||||
name, call, args = self.getcallargs(x)
|
||||
if not callable(call):
|
||||
raise TypeError("%r yielded non callable test %r" %(self.obj, call,))
|
||||
|
|
Loading…
Reference in New Issue