[pypy-svn] r8790 - in pypy/dist/pypy/module: . test
adim at codespeak.net
adim at codespeak.net
Tue Feb 1 17:24:43 CET 2005
Author: adim
Date: Tue Feb 1 17:24:43 2005
New Revision: 8790
Modified:
pypy/dist/pypy/module/__builtin__module.py
pypy/dist/pypy/module/test/test_builtin.py
Log:
made xrange a bit more CPython compatible
added keywords for the sorted() builtin (looks like I
forgot this one last week)
Modified: pypy/dist/pypy/module/__builtin__module.py
==============================================================================
--- pypy/dist/pypy/module/__builtin__module.py (original)
+++ pypy/dist/pypy/module/__builtin__module.py Tue Feb 1 17:24:43 2005
@@ -466,12 +466,21 @@
class xrange:
def __init__(self, start, stop=None, step=1):
- if stop is None:
+ if not isinstance(start, (int, long, float)):
+ raise TypeError('an integer is required')
+ start = int(start)
+ if stop is None:
self.start = 0
self.stop = start
else:
+ if not isinstance(stop, (int, long, float)):
+ raise TypeError('an integer is required')
+ stop = int(stop)
self.start = start
self.stop = stop
+ if not isinstance(step, (int, long, float)):
+ raise TypeError('an integer is required')
+ step = int(step)
if step == 0:
raise ValueError, 'xrange() step-argument (arg 3) must not be zero'
self.step = step
@@ -931,10 +940,10 @@
def __init__(self, object, offset=None, size=None):
raise NotImplementedError, "XXX nobody needs this anyway"
-def sorted(lst):
+def sorted(lst, cmp=None, key=None, reverse=None):
"sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list"
sorted_lst = list(lst)
- sorted_lst.sort()
+ sorted_lst.sort(cmp, key, reverse)
return sorted_lst
def reversed(iterable):
Modified: pypy/dist/pypy/module/test/test_builtin.py
==============================================================================
--- pypy/dist/pypy/module/test/test_builtin.py (original)
+++ pypy/dist/pypy/module/test/test_builtin.py Tue Feb 1 17:24:43 2005
@@ -132,6 +132,11 @@
assert x.stop == 10
assert x.step == 2
+ x = xrange(2.3, 10.5, 2.4)
+ assert x.start == 2
+ assert x.stop == 10
+ assert x.step == 2
+
raises(ValueError, xrange, 0, 1, 0)
def test_xrange_up(self):
@@ -159,10 +164,14 @@
def test_xrange_len(self):
x = xrange(33)
assert len(x) == 33
+ x = xrange(33.2)
+ assert len(x) == 33
x = xrange(33,0,-1)
assert len(x) == 33
x = xrange(33,0)
assert len(x) == 0
+ x = xrange(33,0.2)
+ assert len(x) == 0
x = xrange(0,33)
assert len(x) == 33
x = xrange(0,33,-1)
@@ -180,6 +189,15 @@
raises(IndexError, x.__getitem__, -18)
raises(TypeError, x.__getitem__, slice(0,3,1))
+ def test_xrange_bad_args(self):
+ raises(TypeError, xrange, '1')
+ raises(TypeError, xrange, None)
+ raises(TypeError, xrange, 3+2j)
+ raises(TypeError, xrange, 1, '1')
+ raises(TypeError, xrange, 1, 3+2j)
+ raises(TypeError, xrange, 1, 2, '1')
+ raises(TypeError, xrange, 1, 2, 3+2j)
+
def test_sorted(self):
l = []
sorted_l = sorted(l)
@@ -188,6 +206,15 @@
l = [1, 5, 2, 3]
sorted_l = sorted(l)
assert sorted_l == [1, 2, 3, 5]
+
+ def test_sorted_with_keywords(self):
+ l = ['a', 'C', 'b']
+ sorted_l = sorted(l, reverse = True)
+ assert sorted_l is not l
+ assert sorted_l == ['b', 'a', 'C']
+ sorted_l = sorted(l, reverse = True, key = lambda x: x.lower())
+ assert sorted_l is not l
+ assert sorted_l == ['C', 'b', 'a']
def test_reversed_simple_sequences(self):
l = range(5)
More information about the Pypy-commit
mailing list