From: <pj...@us...> - 2009-10-24 03:11:54
|
Revision: 6894 http://jython.svn.sourceforge.net/jython/?rev=6894&view=rev Author: pjenvey Date: 2009-10-24 03:11:46 +0000 (Sat, 24 Oct 2009) Log Message: ----------- improve isNumber/Mapping/SequenceType Modified Paths: -------------- trunk/jython/src/org/python/core/PyComplex.java trunk/jython/src/org/python/core/PyDictProxy.java trunk/jython/src/org/python/core/PyDictionary.java trunk/jython/src/org/python/core/PyFloat.java trunk/jython/src/org/python/core/PyInstance.java trunk/jython/src/org/python/core/PyInteger.java trunk/jython/src/org/python/core/PyLong.java trunk/jython/src/org/python/core/PyObject.java trunk/jython/src/org/python/core/PySequence.java trunk/jython/src/org/python/core/PyStringMap.java trunk/jython/src/org/python/modules/_collections/PyDeque.java Added Paths: ----------- trunk/jython/Lib/test/test_operator_jy.py Added: trunk/jython/Lib/test/test_operator_jy.py =================================================================== --- trunk/jython/Lib/test/test_operator_jy.py (rev 0) +++ trunk/jython/Lib/test/test_operator_jy.py 2009-10-24 03:11:46 UTC (rev 6894) @@ -0,0 +1,80 @@ +"""Misc operator module tests + +Made for Jython. +""" +import collections +import operator +import sys +import unittest +from test import test_support + +class OperatorTestCase(unittest.TestCase): + + class NewStyle(object): + pass + class OldStyle: + pass + class HasGetitem(object): + def __getitem__(self, name): + return 'foo' + class HasInt(object): + def __int__(self): + return 1 + class HasLong(object): + def __long__(self): + return 1 + class HasFloat(object): + def __float__(self): + return 1.0 + + # obj, isNumberType, isMappingType, isSequenceType + tests = ( + (type, False, False, False), + (type.__dict__, False, True, False), # dictproxy + (globals(), False, True, False), # stringmap + ({}, False, True, False), + ('', False, False, True), + (u'', False, False, True), + ([], False, False, True), + ((), False, False, True), + (xrange(5), False, False, True), + (set(), False, False, False), + (frozenset(), False, False, False), + (1, True, False, False), + (2L, True, False, False), + (3.0, True, False, False), + (4j, True, False, False), + (Exception(), False, False, True), + (collections.deque(), False, False, True), + (collections.defaultdict(), False, True, False), + (collections.namedtuple('test', 't'), False, False, False), + (NewStyle(), False, False, False), + (OldStyle(), not sys.platform.startswith('java'), False, False), + (HasGetitem(), False, True, True), + (HasInt(), True, False, False), + (HasFloat(), True, False, False), + ) + + def test_isNumberType(self): + for obj, isNumberType, _, _ in self.tests: + self.assert_istype(operator.isNumberType, obj, isNumberType) + + def test_isMappingType(self): + for obj, _, isMappingType, _ in self.tests: + self.assert_istype(operator.isMappingType, obj, isMappingType) + + def test_isSequenceType(self): + for obj, _, _, isSequenceType in self.tests: + self.assert_istype(operator.isSequenceType, obj, isSequenceType) + + def assert_istype(self, func, obj, result): + self.assertEqual(func(obj), result, '%s %s should be: %s' % + (type(obj), func.__name__, result)) + + +def test_main(): + test_support.run_unittest(OperatorTestCase) + + +if __name__ == "__main__": + test_main() Modified: trunk/jython/src/org/python/core/PyComplex.java =================================================================== --- trunk/jython/src/org/python/core/PyComplex.java 2009-10-24 01:44:57 UTC (rev 6893) +++ trunk/jython/src/org/python/core/PyComplex.java 2009-10-24 03:11:46 UTC (rev 6894) @@ -712,6 +712,8 @@ return complex___getnewargs__(); } - public boolean isMappingType() { return false; } - public boolean isSequenceType() { return false; } + @Override + public boolean isNumberType() { + return true; + } } Modified: trunk/jython/src/org/python/core/PyDictProxy.java =================================================================== --- trunk/jython/src/org/python/core/PyDictProxy.java 2009-10-24 01:44:57 UTC (rev 6893) +++ trunk/jython/src/org/python/core/PyDictProxy.java 2009-10-24 03:11:46 UTC (rev 6894) @@ -20,14 +20,17 @@ this.dict = dict; } + @Override public PyObject __iter__() { return dict.__iter__(); } + @Override public PyObject __finditem__(PyObject key) { return dict.__finditem__(key); } + @Override public int __len__() { return dict.__len__(); } @@ -87,6 +90,7 @@ return dict.invoke("copy"); } + @Override public int __cmp__(PyObject other) { return dictproxy___cmp__(other); } @@ -126,8 +130,19 @@ return dict.__ge__(other); } + @Override @ExposedMethod public PyString __str__() { return dict.__str__(); } + + @Override + public boolean isMappingType() { + return true; + } + + @Override + public boolean isSequenceType() { + return false; + } } Modified: trunk/jython/src/org/python/core/PyDictionary.java =================================================================== --- trunk/jython/src/org/python/core/PyDictionary.java 2009-10-24 01:44:57 UTC (rev 6893) +++ trunk/jython/src/org/python/core/PyDictionary.java 2009-10-24 03:11:46 UTC (rev 6894) @@ -676,6 +676,12 @@ throw Py.TypeError(String.format("unhashable type: '%.200s'", getType().fastGetName())); } + @Override + public boolean isMappingType() { + return true; + } + + @Override public boolean isSequenceType() { return false; } Modified: trunk/jython/src/org/python/core/PyFloat.java =================================================================== --- trunk/jython/src/org/python/core/PyFloat.java 2009-10-24 01:44:57 UTC (rev 6893) +++ trunk/jython/src/org/python/core/PyFloat.java 2009-10-24 03:11:46 UTC (rev 6894) @@ -628,6 +628,11 @@ return value; } + @Override + public boolean isNumberType() { + return true; + } + // standard singleton issues apply here to __getformat__/__setformat__, // but this is what Python demands @@ -681,8 +686,4 @@ } } } - - public boolean isMappingType() { return false; } - public boolean isSequenceType() { return false; } - } Modified: trunk/jython/src/org/python/core/PyInstance.java =================================================================== --- trunk/jython/src/org/python/core/PyInstance.java 2009-10-24 01:44:57 UTC (rev 6893) +++ trunk/jython/src/org/python/core/PyInstance.java 2009-10-24 03:11:46 UTC (rev 6894) @@ -174,6 +174,21 @@ } @Override + public boolean isNumberType() { + return __findattr__("__int__") != null || __findattr__("__float__") != null; + } + + @Override + public boolean isMappingType() { + return __findattr__("__getitem__") != null; + } + + @Override + public boolean isSequenceType() { + return __findattr__("__getitem__") != null; + } + + @Override public boolean isIndex() { return __findattr__("__index__") != null; } Modified: trunk/jython/src/org/python/core/PyInteger.java =================================================================== --- trunk/jython/src/org/python/core/PyInteger.java 2009-10-24 01:44:57 UTC (rev 6893) +++ trunk/jython/src/org/python/core/PyInteger.java 2009-10-24 03:11:46 UTC (rev 6894) @@ -825,6 +825,11 @@ } @Override + public boolean isNumberType() { + return true; + } + + @Override public boolean isIndex() { return true; } Modified: trunk/jython/src/org/python/core/PyLong.java =================================================================== --- trunk/jython/src/org/python/core/PyLong.java 2009-10-24 01:44:57 UTC (rev 6893) +++ trunk/jython/src/org/python/core/PyLong.java 2009-10-24 03:11:46 UTC (rev 6894) @@ -864,6 +864,11 @@ } @Override + public boolean isNumberType() { + return true; + } + + @Override public boolean isIndex() { return true; } Modified: trunk/jython/src/org/python/core/PyObject.java =================================================================== --- trunk/jython/src/org/python/core/PyObject.java 2009-10-24 01:44:57 UTC (rev 6893) +++ trunk/jython/src/org/python/core/PyObject.java 2009-10-24 03:11:46 UTC (rev 6894) @@ -523,20 +523,23 @@ return __call__(args, keywords); } - /* xxx fix these around */ - public boolean isCallable() { return getType().lookup("__call__") != null; } + public boolean isNumberType() { + PyType type = getType(); + return type.lookup("__int__") != null || type.lookup("__float__") != null; + } + public boolean isMappingType() { - return true; + PyType type = getType(); + return type.lookup("__getitem__") != null + && !(isSequenceType() && type.lookup("__getslice__") != null); } - public boolean isNumberType() { - return true; - } + public boolean isSequenceType() { - return true; + return getType().lookup("__getitem__") != null; } /** Modified: trunk/jython/src/org/python/core/PySequence.java =================================================================== --- trunk/jython/src/org/python/core/PySequence.java 2009-10-24 01:44:57 UTC (rev 6893) +++ trunk/jython/src/org/python/core/PySequence.java 2009-10-24 03:11:46 UTC (rev 6894) @@ -414,6 +414,11 @@ return null; } + @Override + public boolean isSequenceType() { + return true; + } + protected final SequenceIndexDelegate delegator = new SequenceIndexDelegate() { @Override Modified: trunk/jython/src/org/python/core/PyStringMap.java =================================================================== --- trunk/jython/src/org/python/core/PyStringMap.java 2009-10-24 01:44:57 UTC (rev 6893) +++ trunk/jython/src/org/python/core/PyStringMap.java 2009-10-24 03:11:46 UTC (rev 6894) @@ -435,6 +435,16 @@ return new ValuesIter(table.values()); } + @Override + public boolean isMappingType() { + return true; + } + + @Override + public boolean isSequenceType() { + return false; + } + private abstract class StringMapIter<T> extends PyIterator { protected final Iterator<T> iterator; Modified: trunk/jython/src/org/python/modules/_collections/PyDeque.java =================================================================== --- trunk/jython/src/org/python/modules/_collections/PyDeque.java 2009-10-24 01:44:57 UTC (rev 6893) +++ trunk/jython/src/org/python/modules/_collections/PyDeque.java 2009-10-24 03:11:46 UTC (rev 6894) @@ -474,6 +474,16 @@ return pd; } + @Override + public boolean isMappingType() { + return false; + } + + @Override + public boolean isSequenceType() { + return true; + } + private static class Node { private Node left; private Node right; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |