[Python-checkins] r69936 - in python/branches/release30-maint/Lib: _abcoll.py random.py test/test_collections.py
raymond.hettinger
python-checkins at python.org
Tue Feb 24 12:31:49 CET 2009
Author: raymond.hettinger
Date: Tue Feb 24 12:31:49 2009
New Revision: 69936
Log:
Backport r69934 and r69935:
register range() as a sequence
Use ABCs for input validation in random.sample()
Modified:
python/branches/release30-maint/Lib/_abcoll.py
python/branches/release30-maint/Lib/random.py
python/branches/release30-maint/Lib/test/test_collections.py
Modified: python/branches/release30-maint/Lib/_abcoll.py
==============================================================================
--- python/branches/release30-maint/Lib/_abcoll.py (original)
+++ python/branches/release30-maint/Lib/_abcoll.py Tue Feb 24 12:31:49 2009
@@ -546,6 +546,7 @@
Sequence.register(tuple)
Sequence.register(str)
+Sequence.register(range)
class ByteString(Sequence):
Modified: python/branches/release30-maint/Lib/random.py
==============================================================================
--- python/branches/release30-maint/Lib/random.py (original)
+++ python/branches/release30-maint/Lib/random.py Tue Feb 24 12:31:49 2009
@@ -43,6 +43,7 @@
from math import sqrt as _sqrt, acos as _acos, cos as _cos, sin as _sin
from os import urandom as _urandom
from binascii import hexlify as _hexlify
+import collections as _collections
__all__ = ["Random","seed","random","uniform","randint","choice","sample",
"randrange","shuffle","normalvariate","lognormvariate",
@@ -296,10 +297,10 @@
# preferred since the list takes less space than the
# set and it doesn't suffer from frequent reselections.
- if isinstance(population, (set, frozenset)):
+ if isinstance(population, _collections.Set):
population = tuple(population)
- if not hasattr(population, '__getitem__') or hasattr(population, 'keys'):
- raise TypeError("Population must be a sequence or set. For dicts, use dict.keys().")
+ if not isinstance(population, _collections.Sequence):
+ raise TypeError("Population must be a sequence or Set. For dicts, use list(d).")
random = self.random
n = len(population)
if not 0 <= k <= n:
Modified: python/branches/release30-maint/Lib/test/test_collections.py
==============================================================================
--- python/branches/release30-maint/Lib/test/test_collections.py (original)
+++ python/branches/release30-maint/Lib/test/test_collections.py Tue Feb 24 12:31:49 2009
@@ -393,6 +393,8 @@
for sample in [tuple, list, bytes, str]:
self.failUnless(isinstance(sample(), Sequence))
self.failUnless(issubclass(sample, Sequence))
+ self.failUnless(isinstance(range(10), Sequence))
+ self.failUnless(issubclass(range, Sequence))
self.failUnless(issubclass(str, Sequence))
self.validate_abstract_methods(Sequence, '__contains__', '__iter__', '__len__',
'__getitem__')
More information about the Python-checkins
mailing list