Author: Brian Kearns <bdkea...@gmail.com> Branch: stdlib-2.7.8 Changeset: r73023:e58e1cf2c0f2 Date: 2014-08-24 11:35 -0400 http://bitbucket.org/pypy/pypy/changeset/e58e1cf2c0f2/
Log: cleanup operator mapping/sequence tests diff --git a/pypy/module/operator/app_operator.py b/pypy/module/operator/app_operator.py --- a/pypy/module/operator/app_operator.py +++ b/pypy/module/operator/app_operator.py @@ -5,8 +5,10 @@ equivalent to x+y. ''' from __pypy__ import builtinify +import types -def countOf(a,b): + +def countOf(a,b): 'countOf(a, b) -- Return the number of times b occurs in a.' count = 0 for x in a: @@ -37,11 +39,11 @@ index += 1 raise ValueError, 'sequence.index(x): x not in sequence' -# XXX the following is approximative def isMappingType(obj,): 'isMappingType(a) -- Return True if a has a mapping type, False otherwise.' - # XXX this is fragile and approximative anyway - return hasattr(obj, '__getitem__') and hasattr(obj, 'keys') + if isinstance(obj, types.InstanceType): + return hasattr(obj, '__getitem__') + return hasattr(obj, '__getitem__') and not hasattr(obj, '__getslice__') def isNumberType(obj,): 'isNumberType(a) -- Return True if a has a numeric type, False otherwise.' @@ -49,7 +51,9 @@ def isSequenceType(obj,): 'isSequenceType(a) -- Return True if a has a sequence type, False otherwise.' - return hasattr(obj, '__getitem__') and not hasattr(obj, 'keys') + if isinstance(obj, dict): + return False + return hasattr(obj, '__getitem__') def repeat(obj, num): 'repeat(a, b) -- Return a * b, where a is a sequence, and b is an integer.' diff --git a/pypy/module/operator/test/test_operator.py b/pypy/module/operator/test/test_operator.py --- a/pypy/module/operator/test/test_operator.py +++ b/pypy/module/operator/test/test_operator.py @@ -140,6 +140,35 @@ assert operator.repeat(a, 0) == [] raises(TypeError, operator.repeat, 6, 7) + def test_isMappingType(self): + import operator + assert not operator.isMappingType([]) + assert operator.isMappingType(dict()) + class M: + def __getitem__(self, key): + return 42 + assert operator.isMappingType(M()) + del M.__getitem__ + assert not operator.isMappingType(M()) + class M(object): + def __getitem__(self, key): + return 42 + assert operator.isMappingType(M()) + del M.__getitem__ + assert not operator.isMappingType(M()) + class M: + def __getitem__(self, key): + return 42 + def __getslice__(self, key): + return 42 + assert operator.isMappingType(M()) + class M(object): + def __getitem__(self, key): + return 42 + def __getslice__(self, key): + return 42 + assert not operator.isMappingType(M()) + def test_isSequenceType(self): import operator _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit