From: <pj...@us...> - 2009-05-06 05:25:24
|
Revision: 6302 http://jython.svn.sourceforge.net/jython/?rev=6302&view=rev Author: pjenvey Date: 2009-05-06 05:25:18 +0000 (Wed, 06 May 2009) Log Message: ----------- o have API methods call into final exposed methods, simplify issuperset, coding standards o enable set's test_weakref Modified Paths: -------------- trunk/jython/Lib/test/test_set.py trunk/jython/src/org/python/core/BaseSet.java Modified: trunk/jython/Lib/test/test_set.py =================================================================== --- trunk/jython/Lib/test/test_set.py 2009-05-06 05:20:34 UTC (rev 6301) +++ trunk/jython/Lib/test/test_set.py 2009-05-06 05:25:18 UTC (rev 6302) @@ -1,5 +1,6 @@ import unittest from test import test_support +from test_weakref import extra_collect from weakref import proxy import operator import copy @@ -227,7 +228,7 @@ # Create a nest of cycles to exercise overall ref count check class A: pass - s = set([A() for i in xrange(1000)]) + s = set(A() for i in xrange(1000)) for elem in s: elem.cycle = s elem.sub = elem @@ -278,25 +279,25 @@ fo.close() os.remove(test_support.TESTFN) -# XXX: tests cpython internals (caches key hashes) -# def test_do_not_rehash_dict_keys(self): -# n = 10 -# d = dict.fromkeys(map(HashCountingInt, xrange(n))) -# self.assertEqual(sum([elem.hash_count for elem in d]), n) -# s = self.thetype(d) -# self.assertEqual(sum([elem.hash_count for elem in d]), n) -# s.difference(d) -# self.assertEqual(sum([elem.hash_count for elem in d]), n) -# if hasattr(s, 'symmetric_difference_update'): -# s.symmetric_difference_update(d) -# self.assertEqual(sum([elem.hash_count for elem in d]), n) -# d2 = dict.fromkeys(set(d)) -# self.assertEqual(sum([elem.hash_count for elem in d]), n) -# d3 = dict.fromkeys(frozenset(d)) -# self.assertEqual(sum([elem.hash_count for elem in d]), n) -# d3 = dict.fromkeys(frozenset(d), 123) -# self.assertEqual(sum([elem.hash_count for elem in d]), n) -# self.assertEqual(d3, dict.fromkeys(d, 123)) + # XXX: Tests CPython internals (caches key hashes) + def _test_do_not_rehash_dict_keys(self): + n = 10 + d = dict.fromkeys(map(HashCountingInt, xrange(n))) + self.assertEqual(sum(elem.hash_count for elem in d), n) + s = self.thetype(d) + self.assertEqual(sum(elem.hash_count for elem in d), n) + s.difference(d) + self.assertEqual(sum(elem.hash_count for elem in d), n) + if hasattr(s, 'symmetric_difference_update'): + s.symmetric_difference_update(d) + self.assertEqual(sum(elem.hash_count for elem in d), n) + d2 = dict.fromkeys(set(d)) + self.assertEqual(sum(elem.hash_count for elem in d), n) + d3 = dict.fromkeys(frozenset(d)) + self.assertEqual(sum(elem.hash_count for elem in d), n) + d3 = dict.fromkeys(frozenset(d), 123) + self.assertEqual(sum(elem.hash_count for elem in d), n) + self.assertEqual(d3, dict.fromkeys(d, 123)) class TestSet(TestJointOps): thetype = set @@ -478,13 +479,13 @@ t ^= t self.assertEqual(t, self.thetype()) -# XXX: CPython gc-specific -# def test_weakref(self): -# s = self.thetype('gallahad') -# p = proxy(s) -# self.assertEqual(str(p), str(s)) -# s = None -# self.assertRaises(ReferenceError, str, p) + def test_weakref(self): + s = self.thetype('gallahad') + p = proxy(s) + self.assertEqual(str(p), str(s)) + s = None + extra_collect() + self.assertRaises(ReferenceError, str, p) # C API test only available in a debug build if hasattr(set, "test_c_api"): @@ -560,15 +561,15 @@ f = self.thetype('abcdcda') self.assertEqual(hash(f), hash(f)) -# XXX: tied to cpython's hash implementation -# def test_hash_effectiveness(self): -# n = 13 -# hashvalues = set() -# addhashvalue = hashvalues.add -# elemmasks = [(i+1, 1<<i) for i in range(n)] -# for i in xrange(2**n): -# addhashvalue(hash(frozenset([e for e, m in elemmasks if m&i]))) -# self.assertEqual(len(hashvalues), 2**n) + # XXX: tied to CPython's hash implementation + def _test_hash_effectiveness(self): + n = 13 + hashvalues = set() + addhashvalue = hashvalues.add + elemmasks = [(i+1, 1<<i) for i in range(n)] + for i in xrange(2**n): + addhashvalue(hash(frozenset([e for e, m in elemmasks if m&i]))) + self.assertEqual(len(hashvalues), 2**n) class FrozenSetSubclass(frozenset): pass @@ -683,11 +684,12 @@ def test_iteration(self): for v in self.set: self.assert_(v in self.values) -# XXX: jython does not use length_hint -# setiter = iter(self.set) -# # note: __length_hint__ is an internal undocumented API, -# # don't rely on it in your own programs -# self.assertEqual(setiter.__length_hint__(), len(self.set)) + # XXX: jython does not use length_hint + if not test_support.is_jython: + setiter = iter(self.set) + # note: __length_hint__ is an internal undocumented API, + # don't rely on it in your own programs + self.assertEqual(setiter.__length_hint__(), len(self.set)) def test_pickling(self): p = pickle.dumps(self.set) Modified: trunk/jython/src/org/python/core/BaseSet.java =================================================================== --- trunk/jython/src/org/python/core/BaseSet.java 2009-05-06 05:20:34 UTC (rev 6301) +++ trunk/jython/src/org/python/core/BaseSet.java 2009-05-06 05:25:18 UTC (rev 6302) @@ -1,3 +1,4 @@ +/* Copyright (c) Jython Developers */ package org.python.core; import java.lang.reflect.Array; @@ -48,8 +49,7 @@ * The union of <code>this</code> with <code>other</code>. <p/> <br/> (I.e. all elements * that are in either set) * - * @param other - * A <code>BaseSet</code> instance. + * @param other A <code>BaseSet</code> instance. * @return The union of the two sets as a new set. */ public PyObject __or__(PyObject other) { @@ -100,7 +100,7 @@ if (!(other instanceof BaseSet)) { throw Py.TypeError("Not Implemented"); } - return difference(other); + return baseset_difference(other); } public PyObject difference(PyObject other) { @@ -108,15 +108,15 @@ } final PyObject baseset_difference(PyObject other) { - BaseSet bs = (other instanceof BaseSet) ? (BaseSet)other : new PySet(other); + BaseSet bs = other instanceof BaseSet ? (BaseSet)other : new PySet(other); Set<PyObject> set = bs._set; BaseSet o = BaseSet.makeNewSet(getType()); + for (PyObject p : _set) { if (!set.contains(p)) { o._set.add(p); } } - return o; } @@ -137,7 +137,7 @@ if (!(other instanceof BaseSet)) { throw Py.TypeError("Not Implemented"); } - return symmetric_difference(other); + return baseset_symmetric_difference(other); } public PyObject symmetric_difference(PyObject other) { @@ -145,7 +145,7 @@ } final PyObject baseset_symmetric_difference(PyObject other) { - BaseSet bs = (other instanceof BaseSet) ? (BaseSet)other : new PySet(other); + BaseSet bs = other instanceof BaseSet ? (BaseSet)other : new PySet(other); BaseSet o = BaseSet.makeNewSet(getType()); for (PyObject p : _set) { if (!bs._set.contains(p)) { @@ -201,13 +201,13 @@ final PyObject baseset___iter__() { return new PyIterator() { - private int size = _set.size(); + private int size = size(); private Iterator<PyObject> iterator = _set.iterator(); @Override public PyObject __iternext__() { - if (_set.size() != size) { + if (size != size()) { throw Py.RuntimeError("set changed size during iteration"); } if (iterator.hasNext()) { @@ -255,7 +255,7 @@ } final PyObject baseset___ne__(PyObject other) { - if(other instanceof BaseSet) { + if (other instanceof BaseSet) { return Py.newBoolean(!_set.equals(((BaseSet)other)._set)); } return Py.True; @@ -285,8 +285,7 @@ final PyObject baseset___lt__(PyObject other) { BaseSet bs = _binary_sanity_check(other); - return Py.newBoolean(__len__() < bs.__len__() - && baseset_issubset(other).__nonzero__()); + return Py.newBoolean(size() < bs.size() && baseset_issubset(other).__nonzero__()); } public PyObject __gt__(PyObject other) { @@ -295,8 +294,7 @@ final PyObject baseset___gt__(PyObject other) { BaseSet bs = _binary_sanity_check(other); - return Py.newBoolean(__len__() > bs.__len__() - && baseset_issuperset(other).__nonzero__()); + return Py.newBoolean(size() > bs.size() && baseset_issuperset(other).__nonzero__()); } /** @@ -326,11 +324,11 @@ final PyObject baseset_intersection(PyObject other) { PyObject little, big; - if(!(other instanceof BaseSet)) { + if (!(other instanceof BaseSet)) { other = new PySet(other); } - if (__len__() <= __builtin__.len(other)) { + if (size() <= __builtin__.len(other)) { little = this; big = other; } else { @@ -348,8 +346,8 @@ } final PyObject baseset_issubset(PyObject other) { - BaseSet bs = (other instanceof BaseSet) ? (BaseSet)other : new PySet(other); - if (__len__() > bs.__len__()) { + BaseSet bs = other instanceof BaseSet ? (BaseSet)other : new PySet(other); + if (size() > bs.size()) { return Py.False; } for (Object p : _set) { @@ -361,16 +359,8 @@ } final PyObject baseset_issuperset(PyObject other) { - BaseSet bs = (other instanceof BaseSet) ? (BaseSet)other : new PySet(other); - if (__len__() < bs.__len__()) { - return Py.False; - } - for (Object p : bs._set) { - if (!_set.contains(p)) { - return Py.False; - } - } - return Py.True; + BaseSet bs = other instanceof BaseSet ? (BaseSet)other : new PySet(other); + return bs.baseset_issubset(this); } public String toString() { @@ -414,10 +404,8 @@ * can override, say, __hash__ and all of a sudden you can't assume that a non-PyFrozenSet is * unhashable anymore. * - * @param pye - * The exception thrown from a hashable operation. - * @param value - * The object which was unhashable. + * @param pye The exception thrown from a hashable operation. + * @param value The object which was unhashable. * @return A PyFrozenSet if appropriate, otherwise the pye is rethrown */ protected final PyFrozenSet asFrozen(PyException pye, PyObject value) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |