[pypy-commit] benchmarks default: check track_memory capability with own process, so we don't have to run it with root anymore
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: Changeset: r183:59184f41478d Date: 2012-07-09 18:18 +0200 http://bitbucket.org/pypy/benchmarks/changeset/59184f41478d/ Log:check track_memory capability with own process, so we don't have to run it with root anymore diff --git a/unladen_swallow/perf.py b/unladen_swallow/perf.py --- a/unladen_swallow/perf.py +++ b/unladen_swallow/perf.py @@ -281,7 +281,8 @@ pass try: -_ReadSmapsFile(pid=1) +import os +_ReadSmapsFile(pid=os.getpid()) except IOError: pass else: ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy default: test speed optimization for set.update when updating a set of obejcts with another set containing objects
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: Changeset: r55722:9f7e8c8ce987 Date: 2012-06-20 12:16 +0200 http://bitbucket.org/pypy/pypy/changeset/9f7e8c8ce987/ Log:test speed optimization for set.update when updating a set of obejcts with another set containing objects diff --git a/pypy/objspace/std/test/test_setobject.py b/pypy/objspace/std/test/test_setobject.py --- a/pypy/objspace/std/test/test_setobject.py +++ b/pypy/objspace/std/test/test_setobject.py @@ -425,6 +425,8 @@ s1 = set() s1.update(set('abcd')) assert s1 == set('abcd') +s1 = set([1, 2.0, 3]) +s1.update(set([3, 4, 5.0])) def test_recursive_repr(self): class A(object): ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy default: issue 1183: added additional test
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: Changeset: r55716:f7988abae5ec Date: 2012-06-19 13:14 +0200 http://bitbucket.org/pypy/pypy/changeset/f7988abae5ec/ Log:issue 1183: added additional test diff --git a/pypy/objspace/std/test/test_setobject.py b/pypy/objspace/std/test/test_setobject.py --- a/pypy/objspace/std/test/test_setobject.py +++ b/pypy/objspace/std/test/test_setobject.py @@ -648,7 +648,14 @@ c = a - b c.remove(2) assert c == set([1, 3]) -assert a == set([1,2, 3]) +assert a == set([1, 2, 3]) + +a = set([1,2,3]) +b = set([a, b, c]) +c = a - b +c.remove(2) +assert c == set([1, 3]) +assert a == set([1, 2, 3]) def test_intersection_update(self): s = set([1,2,3,4,7]) ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy default: merge
Author: l.diekmann Branch: Changeset: r55718:75dcb8759f38 Date: 2012-06-19 18:20 +0200 http://bitbucket.org/pypy/pypy/changeset/75dcb8759f38/ Log:merge diff --git a/pypy/objspace/std/test/test_setobject.py b/pypy/objspace/std/test/test_setobject.py --- a/pypy/objspace/std/test/test_setobject.py +++ b/pypy/objspace/std/test/test_setobject.py @@ -648,7 +648,14 @@ c = a - b c.remove(2) assert c == set([1, 3]) -assert a == set([1,2, 3]) +assert a == set([1, 2, 3]) + +a = set([1,2,3]) +b = set([a, b, c]) +c = a - b +c.remove(2) +assert c == set([1, 3]) +assert a == set([1, 2, 3]) def test_intersection_update(self): s = set([1,2,3,4,7]) ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy type-specialized-instances: fixed OverflowError for type-specialized instances
Author: l.diekmann Branch: type-specialized-instances Changeset: r52013:ed1dbd45c349 Date: 2012-02-01 12:48 + http://bitbucket.org/pypy/pypy/changeset/ed1dbd45c349/ Log:fixed OverflowError for type-specialized instances diff --git a/pypy/objspace/std/mapdict.py b/pypy/objspace/std/mapdict.py --- a/pypy/objspace/std/mapdict.py +++ b/pypy/objspace/std/mapdict.py @@ -29,26 +29,29 @@ self.terminator = terminator def read(self, obj, selector): -attr = self.findmap(selector) # index = self.index(selector) +attr = self.findmap(selector) if attr is None: return self.terminator._read_terminator(obj, selector) -return attr.read_attr(obj) #obj._mapdict_read_storage(index) +return attr.read_attr(obj) def write(self, obj, selector, w_value): from pypy.interpreter.error import OperationError -attr = self.findmap(selector) # index = self.index(selector) +attr = self.findmap(selector) if attr is None: return self.terminator._write_terminator(obj, selector, w_value) try: -attr.write_attr(obj, w_value) #obj._mapdict_write_storage(index, w_value) +attr.write_attr(obj, w_value) except OperationError, e: if not e.match(self.space, self.space.w_TypeError): raise -firstattr = obj._get_mapdict_map() -firstattr.delete(obj, selector) -firstattr.add_attr(obj, selector, w_value) +self._replace(obj, selector, w_value) return True +def _replace(self, obj, selector, w_value): +firstattr = obj._get_mapdict_map() +firstattr.delete(obj, selector) +firstattr.add_attr(obj, selector, w_value) + def delete(self, obj, selector): return None @@ -362,6 +365,9 @@ return self.space.wrap(value) def write_attr(self, obj, w_value): +if not is_taggable_int(self.space, w_value): +self._replace(obj, self.selector, w_value) +return erased = self.erase_item(self.space.int_w(w_value)) obj._mapdict_write_storage(self.position, erased) diff --git a/pypy/objspace/std/test/test_mapdict.py b/pypy/objspace/std/test/test_mapdict.py --- a/pypy/objspace/std/test/test_mapdict.py +++ b/pypy/objspace/std/test/test_mapdict.py @@ -776,6 +776,16 @@ assert a.x == 5 +def test_too_large_int(self): +class A(object): +def __init__(self): +self.x = 1 + +a = A() +a.x = 1234567890L + +assert a.x == 1234567890L + class AppTestWithMapDictAndCounters(object): def setup_class(cls): from pypy.interpreter import gateway ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy type-specialized-instances: these lines are now unnecessary
Author: l.diekmann Branch: type-specialized-instances Changeset: r52014:ce9d7cbdddfb Date: 2012-02-01 12:56 + http://bitbucket.org/pypy/pypy/changeset/ce9d7cbdddfb/ Log:these lines are now unnecessary diff --git a/pypy/objspace/std/mapdict.py b/pypy/objspace/std/mapdict.py --- a/pypy/objspace/std/mapdict.py +++ b/pypy/objspace/std/mapdict.py @@ -39,12 +39,7 @@ attr = self.findmap(selector) if attr is None: return self.terminator._write_terminator(obj, selector, w_value) -try: -attr.write_attr(obj, w_value) -except OperationError, e: -if not e.match(self.space, self.space.w_TypeError): -raise -self._replace(obj, selector, w_value) +attr.write_attr(obj, w_value) return True def _replace(self, obj, selector, w_value): ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy default: decided that a cached empty list is too dangerous and that is is better to live
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: Changeset: r51908:3607ba0bb92d Date: 2012-01-28 13:08 +0100 http://bitbucket.org/pypy/pypy/changeset/3607ba0bb92d/ Log:decided that a cached empty list is too dangerous and that is is better to live with a sligthly slower empty list creation than with unexpected behaviour if we are not careful enough with the getitems method diff --git a/pypy/objspace/std/listobject.py b/pypy/objspace/std/listobject.py --- a/pypy/objspace/std/listobject.py +++ b/pypy/objspace/std/listobject.py @@ -347,8 +347,6 @@ def __init__(self, space): ListStrategy.__init__(self, space) -# cache an empty list that is used whenever getitems is called (i.e. sorting) -self.cached_emptylist_w = [] def init_from_list_w(self, w_list, list_w): assert len(list_w) == 0 @@ -376,10 +374,10 @@ def getslice(self, w_list, start, stop, step, length): # will never be called because the empty list case is already caught in # getslice__List_ANY_ANY and getitem__List_Slice -return W_ListObject(self.space, self.cached_emptylist_w) +return W_ListObject(self.space, []) def getitems(self, w_list): -return self.cached_emptylist_w +return [] def getitems_copy(self, w_list): return [] ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy default: test and fix for cpyext/listobject.py: PyList_GET_SIZE and PyList_SetItem
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: Changeset: r51907:a384864c5d6c Date: 2012-01-28 12:39 +0100 http://bitbucket.org/pypy/pypy/changeset/a384864c5d6c/ Log:test and fix for cpyext/listobject.py: PyList_GET_SIZE and PyList_SetItem diff --git a/pypy/module/cpyext/listobject.py b/pypy/module/cpyext/listobject.py --- a/pypy/module/cpyext/listobject.py +++ b/pypy/module/cpyext/listobject.py @@ -32,11 +32,10 @@ Py_DecRef(space, w_item) if not isinstance(w_list, W_ListObject): PyErr_BadInternalCall(space) -wrappeditems = w_list.getitems() -if index 0 or index = len(wrappeditems): +if index 0 or index = w_list.length(): raise OperationError(space.w_IndexError, space.wrap( list assignment index out of range)) -wrappeditems[index] = w_item +w_list.setitem(index, w_item) return 0 @cpython_api([PyObject, Py_ssize_t], PyObject) @@ -74,7 +73,7 @@ Macro form of PyList_Size() without error checking. assert isinstance(w_list, W_ListObject) -return len(w_list.getitems()) +return w_list.length() @cpython_api([PyObject], Py_ssize_t, error=-1) diff --git a/pypy/module/cpyext/test/test_listobject.py b/pypy/module/cpyext/test/test_listobject.py --- a/pypy/module/cpyext/test/test_listobject.py +++ b/pypy/module/cpyext/test/test_listobject.py @@ -128,3 +128,6 @@ module.setslice(l, None) assert l == [0, 4, 5] +l = [1, 2, 3] +module.setlistitem(l,0) +assert l == [None, 2, 3] ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: little changes to make the jit inline more stuff and optimize the trace
Author: l.diekmann Branch: set-strategies Changeset: r51289:93d68d35cc81 Date: 2012-01-12 17:23 + http://bitbucket.org/pypy/pypy/changeset/93d68d35cc81/ Log:little changes to make the jit inline more stuff and optimize the trace diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -943,8 +943,9 @@ w_set.sstorage = strategy.get_empty_storage() return -#XXX check ints and strings at once +_pick_correct_strategy(space, w_set, iterable_w) +def _pick_correct_strategy(space, w_set, iterable_w): # check for integers for w_item in iterable_w: if type(w_item) is not W_IntObject: diff --git a/pypy/objspace/std/stringobject.py b/pypy/objspace/std/stringobject.py --- a/pypy/objspace/std/stringobject.py +++ b/pypy/objspace/std/stringobject.py @@ -61,7 +61,12 @@ return plain_str2unicode(space, w_self._value) def listview_str(w_self): -return [s for s in w_self._value] +return _create_list_from_string(w_self._value) + +def _create_list_from_string(value): +# need this helper function to allow the jit to look inside and inline +# listview_str +return [s for s in value] registerimplementation(W_StringObject) ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: added tests for optimized jit output with merged strategy implementations (lists, sets, strings)
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r51290:a81b07b0d748 Date: 2012-01-12 18:27 +0100 http://bitbucket.org/pypy/pypy/changeset/a81b07b0d748/ Log:added tests for optimized jit output with merged strategy implementations (lists, sets, strings) diff --git a/pypy/module/pypyjit/test_pypy_c/test_containers.py b/pypy/module/pypyjit/test_pypy_c/test_containers.py --- a/pypy/module/pypyjit/test_pypy_c/test_containers.py +++ b/pypy/module/pypyjit/test_pypy_c/test_containers.py @@ -128,3 +128,82 @@ loop, = log.loops_by_filename(self.filepath) ops = loop.ops_by_id('look') assert 'call' not in log.opnames(ops) + +#XXX the following tests only work with strategies enabled + +def test_should_not_create_intobject_with_sets(self): +def main(n): +i = 0 +s = set() +while i n: +s.add(i) +i += 1 +log = self.run(main, [1000]) +assert log.result == main(1000) +loop, = log.loops_by_filename(self.filepath) +opnames = log.opnames(loop.allops()) +assert opnames.count('new_with_vtable') == 0 + +def test_should_not_create_stringobject_with_sets(self): +def main(n): +i = 0 +s = set() +while i n: +s.add(str(i)) +i += 1 +log = self.run(main, [1000]) +assert log.result == main(1000) +loop, = log.loops_by_filename(self.filepath) +opnames = log.opnames(loop.allops()) +assert opnames.count('new_with_vtable') == 0 + +def test_should_not_create_intobject_with_lists(self): +def main(n): +i = 0 +l = [] +while i n: +l.append(i) +i += 1 +log = self.run(main, [1000]) +assert log.result == main(1000) +loop, = log.loops_by_filename(self.filepath) +opnames = log.opnames(loop.allops()) +assert opnames.count('new_with_vtable') == 0 + +def test_should_not_create_stringobject_with_lists(self): +def main(n): +i = 0 +l = [] +while i n: +l.append(str(i)) +i += 1 +log = self.run(main, [1000]) +assert log.result == main(1000) +loop, = log.loops_by_filename(self.filepath) +opnames = log.opnames(loop.allops()) +assert opnames.count('new_with_vtable') == 0 + +def test_optimized_create_list_from_string(self): +def main(n): +i = 0 +l = [] +while i n: +l = list(abc * i) +i += 1 +log = self.run(main, [1000]) +assert log.result == main(1000) +loop, = log.loops_by_filename(self.filepath) +opnames = log.opnames(loop.allops()) +assert opnames.count('new_with_vtable') == 0 + +def test_optimized_create_set_from_list(self): +def main(n): +i = 0 +while i n: +s = set([1,2,3]) +i += 1 +log = self.run(main, [1000]) +assert log.result == main(1000) +loop, = log.loops_by_filename(self.filepath) +opnames = log.opnames(loop.allops()) +assert opnames.count('new_with_vtable') == 0 ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: merge
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r51291:a4bba3dd3493 Date: 2012-01-12 18:29 +0100 http://bitbucket.org/pypy/pypy/changeset/a4bba3dd3493/ Log:merge diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -943,8 +943,9 @@ w_set.sstorage = strategy.get_empty_storage() return -#XXX check ints and strings at once +_pick_correct_strategy(space, w_set, iterable_w) +def _pick_correct_strategy(space, w_set, iterable_w): # check for integers for w_item in iterable_w: if type(w_item) is not W_IntObject: diff --git a/pypy/objspace/std/stringobject.py b/pypy/objspace/std/stringobject.py --- a/pypy/objspace/std/stringobject.py +++ b/pypy/objspace/std/stringobject.py @@ -61,7 +61,12 @@ return plain_str2unicode(space, w_self._value) def listview_str(w_self): -return [s for s in w_self._value] +return _create_list_from_string(w_self._value) + +def _create_list_from_string(value): +# need this helper function to allow the jit to look inside and inline +# listview_str +return [s for s in value] registerimplementation(W_StringObject) ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: added listview_str/int for setobjects to later create lists from sets without wrapping/unwrapping the elements
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r51219:764907052fed Date: 2012-01-10 17:43 +0100 http://bitbucket.org/pypy/pypy/changeset/764907052fed/ Log:added listview_str/int for setobjects to later create lists from sets without wrapping/unwrapping the elements diff --git a/pypy/objspace/std/objspace.py b/pypy/objspace/std/objspace.py --- a/pypy/objspace/std/objspace.py +++ b/pypy/objspace/std/objspace.py @@ -438,11 +438,15 @@ def listview_str(self, w_obj): if isinstance(w_obj, W_ListObject): return w_obj.getitems_str() +if isinstance(w_obj, W_SetObject): +return w_obj.listview_str() return None def listview_int(self, w_obj): if isinstance(w_obj, W_ListObject): return w_obj.getitems_int() +if isinstance(w_obj, W_SetObject): +return w_obj.listview_int() return None def sliceindices(self, w_slice, w_length): diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -63,6 +63,7 @@ # _ strategy methods + def clear(self): Removes all elements from the set. self.strategy.clear(self) @@ -87,6 +88,14 @@ Returns a dict with all elements of the set. Needed only for switching to ObjectSetStrategy. return self.strategy.getdict_w(self) +def listview_str(self): + If this is a string set return its contents as a list of uwnrapped strings. Otherwise return None. +return self.strategy.listview_str(self) + +def listview_int(self): + If this is an int set return its contents as a list of uwnrapped ints. Otherwise return None. +return self.strategy.listview_int(self) + def get_storage_copy(self): Returns a copy of the storage. Needed when we want to clone all elements from one set and put them into another. @@ -189,6 +198,12 @@ Returns an empty storage (erased) object. Used to initialize an empty set. raise NotImplementedError +def listview_str(self, w_set): +return None + +def listview_int(self, w_set): +return None + #def erase(self, storage): #raise NotImplementedError @@ -694,6 +709,9 @@ def get_empty_dict(self): return {} +def listview_str(self, w_set): +return self.unerase(w_set.sstorage).keys() + def is_correct_type(self, w_key): return type(w_key) is W_StringObject @@ -724,6 +742,9 @@ def get_empty_dict(self): return {} +def listview_int(self, w_set): +return self.unerase(w_set.sstorage).keys() + def is_correct_type(self, w_key): from pypy.objspace.std.intobject import W_IntObject return type(w_key) is W_IntObject diff --git a/pypy/objspace/std/test/test_setobject.py b/pypy/objspace/std/test/test_setobject.py --- a/pypy/objspace/std/test/test_setobject.py +++ b/pypy/objspace/std/test/test_setobject.py @@ -123,6 +123,19 @@ # changed cached object, need to change it back for other tests to pass intstr.get_storage_from_list = tmp_func +def test_listview_str_int_on_set(self): +w = self.space.wrap + +w_a = W_SetObject(self.space) +_initialize_set(self.space, w_a, w(abcdefg)) +assert sorted(self.space.listview_str(w_a)) == list(abcdefg) +assert self.space.listview_int(w_a) is None + +w_b = W_SetObject(self.space) +_initialize_set(self.space, w_b, self.space.newlist([w(1),w(2),w(3),w(4),w(5)])) +assert sorted(self.space.listview_int(w_b)) == [1,2,3,4,5] +assert self.space.listview_str(w_b) is None + class AppTestAppSetTest: def setup_class(self): ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: added fastpath for initialization of lists with iterables using int- or stringstrategy
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r51221:29acb5e48ac9 Date: 2012-01-11 14:09 +0100 http://bitbucket.org/pypy/pypy/changeset/29acb5e48ac9/ Log:added fastpath for initialization of lists with iterables using int- or stringstrategy diff --git a/pypy/objspace/std/listobject.py b/pypy/objspace/std/listobject.py --- a/pypy/objspace/std/listobject.py +++ b/pypy/objspace/std/listobject.py @@ -1042,6 +1042,21 @@ elif isinstance(w_iterable, W_TupleObject): W_ListObject(space, w_iterable.wrappeditems[:]).copy_into(w_list) return + +intlist = space.listview_int(w_iterable) +if intlist is not None: +w_list.strategy = strategy = space.fromcache(IntegerListStrategy) + # need to copy because intlist can share with w_iterable +w_list.lstorage = strategy.erase(intlist[:]) +return + +strlist = space.listview_str(w_iterable) +if strlist is not None: +w_list.strategy = strategy = space.fromcache(StringListStrategy) + # need to copy because intlist can share with w_iterable +w_list.lstorage = strategy.erase(strlist[:]) +return + w_list.__init__(space, []) # xxx special hack for speed from pypy.interpreter.generator import GeneratorIterator diff --git a/pypy/objspace/std/test/test_liststrategies.py b/pypy/objspace/std/test/test_liststrategies.py --- a/pypy/objspace/std/test/test_liststrategies.py +++ b/pypy/objspace/std/test/test_liststrategies.py @@ -463,6 +463,34 @@ w_res = listobject.list_pop__List_ANY(space, w_l, space.w_None) # does not crash assert space.unwrap(w_res) == 3 +def test_create_list_from_set(self): +from pypy.objspace.std.setobject import W_SetObject +from pypy.objspace.std.setobject import _initialize_set + +space = self.space +w = space.wrap + +w_l = W_ListObject(space, [space.wrap(1), space.wrap(2), space.wrap(3)]) + +w_set = W_SetObject(self.space) +_initialize_set(self.space, w_set, w_l) +w_set.iter = None # make sure fast path is used + +w_l2 = W_ListObject(space, []) +space.call_method(w_l2, __init__, w_set) + +w_l2.sort(False) +assert space.eq_w(w_l, w_l2) + +w_l = W_ListObject(space, [space.wrap(a), space.wrap(b), space.wrap(c)]) +_initialize_set(self.space, w_set, w_l) + +space.call_method(w_l2, __init__, w_set) + +w_l2.sort(False) +assert space.eq_w(w_l, w_l2) + + class TestW_ListStrategiesDisabled: def setup_class(cls): ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: (cfbolz, l.diekmann): restructure some code: the speed hack in FastListIterator
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r51220:95d8ecd1711f Date: 2012-01-10 18:15 +0100 http://bitbucket.org/pypy/pypy/changeset/95d8ecd1711f/ Log:(cfbolz, l.diekmann): restructure some code: the speed hack in FastListIterator is no longer there, so we don't need to use extend here. Also, why was the generator path hidden in _init_from_iterable? diff --git a/pypy/objspace/std/iterobject.py b/pypy/objspace/std/iterobject.py --- a/pypy/objspace/std/iterobject.py +++ b/pypy/objspace/std/iterobject.py @@ -29,9 +29,8 @@ class W_SeqIterObject(W_AbstractSeqIterObject): Sequence iterator implementation for general sequences. -class W_FastListIterObject(W_AbstractSeqIterObject): -Sequence iterator specialized for lists, accessing -directly their RPython-level list of wrapped objects. +class W_FastListIterObject(W_AbstractSeqIterObject): # XXX still needed +Sequence iterator specialized for lists. class W_FastTupleIterObject(W_AbstractSeqIterObject): diff --git a/pypy/objspace/std/listobject.py b/pypy/objspace/std/listobject.py --- a/pypy/objspace/std/listobject.py +++ b/pypy/objspace/std/listobject.py @@ -1035,26 +1035,26 @@ # this is on the silly side w_iterable, = __args__.parse_obj( None, 'list', init_signature, init_defaults) -w_list.__init__(space, []) if w_iterable is not None: -# unfortunately this is duplicating space.unpackiterable to avoid -# assigning a new RPython list to 'wrappeditems', which defeats the -# W_FastListIterObject optimization. if isinstance(w_iterable, W_ListObject): -w_list.extend(w_iterable) +w_iterable.copy_into(w_list) +return elif isinstance(w_iterable, W_TupleObject): -w_list.extend(W_ListObject(space, w_iterable.wrappeditems[:])) -else: -_init_from_iterable(space, w_list, w_iterable) +W_ListObject(space, w_iterable.wrappeditems[:]).copy_into(w_list) +return +w_list.__init__(space, []) +# xxx special hack for speed +from pypy.interpreter.generator import GeneratorIterator +if isinstance(w_iterable, GeneratorIterator): +w_iterable.unpack_into_w(w_list) +return +# /xxx +_init_from_iterable(space, w_list, w_iterable) +else: +w_list.__init__(space, []) def _init_from_iterable(space, w_list, w_iterable): # in its own function to make the JIT look into init__List -# xxx special hack for speed -from pypy.interpreter.generator import GeneratorIterator -if isinstance(w_iterable, GeneratorIterator): -w_iterable.unpack_into_w(w_list) -return -# /xxx w_iterator = space.iter(w_iterable) while True: try: ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: (cfbolz, l.diekmann) added fastpath for dict.fromkeys with iterable using stringstrategy
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r51222:b8be45d7d460 Date: 2012-01-11 14:24 +0100 http://bitbucket.org/pypy/pypy/changeset/b8be45d7d460/ Log:(cfbolz, l.diekmann) added fastpath for dict.fromkeys with iterable using stringstrategy diff --git a/pypy/objspace/std/dicttype.py b/pypy/objspace/std/dicttype.py --- a/pypy/objspace/std/dicttype.py +++ b/pypy/objspace/std/dicttype.py @@ -62,8 +62,14 @@ w_fill = space.w_None if space.is_w(w_type, space.w_dict): w_dict = W_DictMultiObject.allocate_and_init_instance(space, w_type) -for w_key in space.listview(w_keys): -w_dict.setitem(w_key, w_fill) + +strlist = space.listview_str(w_keys) +if strlist is not None: +for key in strlist: +w_dict.setitem_str(key, w_fill) +else: +for w_key in space.listview(w_keys): +w_dict.setitem(w_key, w_fill) else: w_dict = space.call_function(w_type) for w_key in space.listview(w_keys): diff --git a/pypy/objspace/std/test/test_dictmultiobject.py b/pypy/objspace/std/test/test_dictmultiobject.py --- a/pypy/objspace/std/test/test_dictmultiobject.py +++ b/pypy/objspace/std/test/test_dictmultiobject.py @@ -131,6 +131,16 @@ assert self.space.eq_w(space.call_function(get, w(33)), w(None)) assert self.space.eq_w(space.call_function(get, w(33), w(44)), w(44)) +def test_fromkeys_fastpath(self): +space = self.space +w = space.wrap + +w_l = self.space.newlist([w(a),w(b)]) +w_l.getitems = None +w_d = space.call_method(space.w_dict, fromkeys, w_l) + +assert space.eq_w(w_d.getitem_str(a), space.w_None) +assert space.eq_w(w_d.getitem_str(b), space.w_None) class AppTest_DictObject: def setup_class(cls): ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: (cfbolz, l.diekmann) implemented listview_str on dicts
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r51224:b1a065c4225d Date: 2012-01-11 14:40 +0100 http://bitbucket.org/pypy/pypy/changeset/b1a065c4225d/ Log:(cfbolz, l.diekmann) implemented listview_str on dicts diff --git a/pypy/objspace/std/dictmultiobject.py b/pypy/objspace/std/dictmultiobject.py --- a/pypy/objspace/std/dictmultiobject.py +++ b/pypy/objspace/std/dictmultiobject.py @@ -91,7 +91,7 @@ getitem_str delitem length \ clear keys values \ items iter setdefault \ -popitem.split() +popitem listview_str.split() def make_method(method): def f(self, *args): @@ -148,6 +148,9 @@ w_dict.strategy = strategy w_dict.dstorage = storage +def listview_str(self, w_dict): +return None + class EmptyDictStrategy(DictStrategy): @@ -457,6 +460,9 @@ assert key is not None return self.unerase(w_dict.dstorage).get(key, None) +def listview_str(self, w_dict): +return self.unerase(w_dict.dstorage).keys() + def iter(self, w_dict): return StrIteratorImplementation(self.space, self, w_dict) @@ -676,6 +682,7 @@ return space.newlist(w_self.items()) def dict_keys__DictMulti(space, w_self): +#XXX add fastpath for strategies here return space.newlist(w_self.keys()) def dict_values__DictMulti(space, w_self): diff --git a/pypy/objspace/std/objspace.py b/pypy/objspace/std/objspace.py --- a/pypy/objspace/std/objspace.py +++ b/pypy/objspace/std/objspace.py @@ -440,6 +440,8 @@ return w_obj.getitems_str() if isinstance(w_obj, W_SetObject): return w_obj.listview_str() +if isinstance(w_obj, W_DictMultiObject): +return w_obj.listview_str() return None def listview_int(self, w_obj): diff --git a/pypy/objspace/std/test/test_dictmultiobject.py b/pypy/objspace/std/test/test_dictmultiobject.py --- a/pypy/objspace/std/test/test_dictmultiobject.py +++ b/pypy/objspace/std/test/test_dictmultiobject.py @@ -142,6 +142,14 @@ assert space.eq_w(w_d.getitem_str(a), space.w_None) assert space.eq_w(w_d.getitem_str(b), space.w_None) +def test_listview_str_dict(self): +w = self.space.wrap + +w_d = self.space.newdict() +w_d.initialize_content([(w(a), w(1)), (w(b), w(2))]) + +assert self.space.listview_str(w_d) == [a, b] + class AppTest_DictObject: def setup_class(cls): cls.w_on_pypy = cls.space.wrap(__pypy__ in sys.builtin_module_names) ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: added listview tests for listobject
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r51226:243af83be4d9 Date: 2012-01-11 14:49 +0100 http://bitbucket.org/pypy/pypy/changeset/243af83be4d9/ Log:added listview tests for listobject diff --git a/pypy/objspace/std/test/test_liststrategies.py b/pypy/objspace/std/test/test_liststrategies.py --- a/pypy/objspace/std/test/test_liststrategies.py +++ b/pypy/objspace/std/test/test_liststrategies.py @@ -491,6 +491,16 @@ assert space.eq_w(w_l, w_l2) +def test_listview_str_list(self): +space = self.space +w_l = W_ListObject(space, [space.wrap(a), space.wrap(b)]) +assert self.space.listview_str(w_l) == [a, b] + +def test_listview_int_list(self): +space = self.space +w_l = W_ListObject(space, [space.wrap(1), space.wrap(2), space.wrap(3)]) +assert self.space.listview_int(w_l) == [1, 2, 3] + class TestW_ListStrategiesDisabled: def setup_class(cls): ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: (cfbolz, l.diekmann): added listview_str for strings
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r51227:44c5a3419379 Date: 2012-01-11 14:55 +0100 http://bitbucket.org/pypy/pypy/changeset/44c5a3419379/ Log:(cfbolz, l.diekmann): added listview_str for strings diff --git a/pypy/objspace/std/objspace.py b/pypy/objspace/std/objspace.py --- a/pypy/objspace/std/objspace.py +++ b/pypy/objspace/std/objspace.py @@ -442,6 +442,8 @@ return w_obj.listview_str() if isinstance(w_obj, W_DictMultiObject): return w_obj.listview_str() +if isinstance(w_obj, W_StringObject): +return w_obj.listview_str() return None def listview_int(self, w_obj): diff --git a/pypy/objspace/std/stringobject.py b/pypy/objspace/std/stringobject.py --- a/pypy/objspace/std/stringobject.py +++ b/pypy/objspace/std/stringobject.py @@ -60,6 +60,9 @@ from pypy.objspace.std.unicodetype import plain_str2unicode return plain_str2unicode(space, w_self._value) +def listview_str(w_self): +return list(w_self._value) + registerimplementation(W_StringObject) W_StringObject.EMPTY = W_StringObject('') diff --git a/pypy/objspace/std/test/test_stringobject.py b/pypy/objspace/std/test/test_stringobject.py --- a/pypy/objspace/std/test/test_stringobject.py +++ b/pypy/objspace/std/test/test_stringobject.py @@ -85,6 +85,10 @@ w_slice = space.newslice(w(1), w_None, w(2)) assert self.space.eq_w(space.getitem(w_str, w_slice), w('el')) +def test_listview_str(self): +w_str = self.space.wrap('abcd') +assert self.space.listview_str(w_str) == list(abcd) + class AppTestStringObject: def test_format_wrongchar(self): ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: (cfbolz, l.diekmann): added fastpath for dict.keys if keys are strings
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r51230:a26b3141a0d4 Date: 2012-01-11 15:55 +0100 http://bitbucket.org/pypy/pypy/changeset/a26b3141a0d4/ Log:(cfbolz, l.diekmann): added fastpath for dict.keys if keys are strings diff --git a/pypy/objspace/std/celldict.py b/pypy/objspace/std/celldict.py --- a/pypy/objspace/std/celldict.py +++ b/pypy/objspace/std/celldict.py @@ -127,10 +127,10 @@ def iter(self, w_dict): return ModuleDictIteratorImplementation(self.space, self, w_dict) -def keys(self, w_dict): +def w_keys(self, w_dict): space = self.space -iterator = self.unerase(w_dict.dstorage).iteritems -return [space.wrap(key) for key, cell in iterator()] +l = self.unerase(w_dict.dstorage).keys() +return space.newlist_str(l) def values(self, w_dict): iterator = self.unerase(w_dict.dstorage).itervalues diff --git a/pypy/objspace/std/dictmultiobject.py b/pypy/objspace/std/dictmultiobject.py --- a/pypy/objspace/std/dictmultiobject.py +++ b/pypy/objspace/std/dictmultiobject.py @@ -89,7 +89,7 @@ def _add_indirections(): dict_methods = setitem setitem_str getitem \ getitem_str delitem length \ -clear keys values \ +clear w_keys values \ items iter setdefault \ popitem listview_str listview_int.split() @@ -112,7 +112,7 @@ def get_empty_storage(self): raise NotImplementedError -def keys(self, w_dict): +def w_keys(self, w_dict): iterator = self.iter(w_dict) result = [] while 1: @@ -120,7 +120,7 @@ if w_key is not None: result.append(w_key) else: -return result +return self.space.newlist(result) def values(self, w_dict): iterator = self.iter(w_dict) @@ -364,8 +364,9 @@ self.switch_to_object_strategy(w_dict) return w_dict.getitem(w_key) -def keys(self, w_dict): -return [self.wrap(key) for key in self.unerase(w_dict.dstorage).iterkeys()] +def w_keys(self, w_dict): +l = [self.wrap(key) for key in self.unerase(w_dict.dstorage).iterkeys()] +return self.space.newlist(l) def values(self, w_dict): return self.unerase(w_dict.dstorage).values() @@ -418,8 +419,8 @@ def iter(self, w_dict): return ObjectIteratorImplementation(self.space, self, w_dict) -def keys(self, w_dict): -return self.unerase(w_dict.dstorage).keys() +def w_keys(self, w_dict): +return self.space.newlist(self.unerase(w_dict.dstorage).keys()) class StringDictStrategy(AbstractTypedStrategy, DictStrategy): @@ -468,6 +469,9 @@ def iter(self, w_dict): return StrIteratorImplementation(self.space, self, w_dict) +def w_keys(self, w_dict): +return self.space.newlist_str(self.listview_str(w_dict)) + class _WrappedIteratorMixin(object): _mixin_ = True @@ -533,6 +537,11 @@ def listview_int(self, w_dict): return self.unerase(w_dict.dstorage).keys() +def w_keys(self, w_dict): +# XXX there is no space.newlist_int yet +space = self.space +return space.call_function(space.w_list, w_dict) + class IntIteratorImplementation(_WrappedIteratorMixin, IteratorImplementation): pass @@ -687,8 +696,7 @@ return space.newlist(w_self.items()) def dict_keys__DictMulti(space, w_self): -#XXX add fastpath for strategies here -return space.newlist(w_self.keys()) +return w_self.w_keys() def dict_values__DictMulti(space, w_self): return space.newlist(w_self.values()) diff --git a/pypy/objspace/std/dictproxyobject.py b/pypy/objspace/std/dictproxyobject.py --- a/pypy/objspace/std/dictproxyobject.py +++ b/pypy/objspace/std/dictproxyobject.py @@ -75,7 +75,7 @@ def keys(self, w_dict): space = self.space -return [space.wrap(key) for key in self.unerase(w_dict.dstorage).dict_w.iterkeys()] +return space.newlist_str(self.unerase(w_dict.dstorage).dict_w.keys()) def values(self, w_dict): return [unwrap_cell(self.space, w_value) for w_value in self.unerase(w_dict.dstorage).dict_w.itervalues()] diff --git a/pypy/objspace/std/mapdict.py b/pypy/objspace/std/mapdict.py --- a/pypy/objspace/std/mapdict.py +++ b/pypy/objspace/std/mapdict.py @@ -694,6 +694,8 @@ self.delitem(w_dict, w_key) return (w_key, w_value) +# XXX could implement a more efficient w_keys based on space.newlist_str + def materialize_r_dict(space, obj, dict_w): map = obj._get_mapdict_map() new_obj = map.materialize_r_dict(space, obj, dict_w) diff --git a/pypy/objspace/std/test/test_dictmultiobject.py b/pypy/objspace/std/test/test_dictmultiobject.py --- a/pypy/objspace/std/test/test_dictmultiobject.py +++
[pypy-commit] pypy set-strategies: fixed tests: stringobject has now a listview_str method, too
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r51229:37da0bb1707e Date: 2012-01-11 15:54 +0100 http://bitbucket.org/pypy/pypy/changeset/37da0bb1707e/ Log:fixed tests: stringobject has now a listview_str method, too diff --git a/pypy/objspace/std/test/test_liststrategies.py b/pypy/objspace/std/test/test_liststrategies.py --- a/pypy/objspace/std/test/test_liststrategies.py +++ b/pypy/objspace/std/test/test_liststrategies.py @@ -420,7 +420,7 @@ def test_listview_str(self): space = self.space -assert space.listview_str(space.wrap(a)) is None +assert space.listview_str(space.wrap(a)) == [a] w_l = self.space.newlist([self.space.wrap('a'), self.space.wrap('b')]) assert space.listview_str(w_l) == [a, b] ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: fixed test: show that listview_str returns None for other objects
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r51231:aae0411e2217 Date: 2012-01-11 16:07 +0100 http://bitbucket.org/pypy/pypy/changeset/aae0411e2217/ Log:fixed test: show that listview_str returns None for other objects diff --git a/pypy/objspace/std/test/test_liststrategies.py b/pypy/objspace/std/test/test_liststrategies.py --- a/pypy/objspace/std/test/test_liststrategies.py +++ b/pypy/objspace/std/test/test_liststrategies.py @@ -420,7 +420,7 @@ def test_listview_str(self): space = self.space -assert space.listview_str(space.wrap(a)) == [a] +assert space.listview_str(space.wrap(1)) == None w_l = self.space.newlist([self.space.wrap('a'), self.space.wrap('b')]) assert space.listview_str(w_l) == [a, b] ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: fixes
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r51232:7578dd73ccf8 Date: 2012-01-11 16:53 +0100 http://bitbucket.org/pypy/pypy/changeset/7578dd73ccf8/ Log:fixes diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -923,14 +923,14 @@ return stringlist = space.listview_str(w_iterable) -if stringlist != None: +if stringlist is not None: strategy = space.fromcache(StringSetStrategy) w_set.strategy = strategy w_set.sstorage = strategy.get_storage_from_unwrapped_list(stringlist) return intlist = space.listview_int(w_iterable) -if intlist != None: +if intlist is not None: strategy = space.fromcache(IntegerSetStrategy) w_set.strategy = strategy w_set.sstorage = strategy.get_storage_from_unwrapped_list(intlist) diff --git a/pypy/objspace/std/stringobject.py b/pypy/objspace/std/stringobject.py --- a/pypy/objspace/std/stringobject.py +++ b/pypy/objspace/std/stringobject.py @@ -61,7 +61,7 @@ return plain_str2unicode(space, w_self._value) def listview_str(w_self): -return list(w_self._value) +return [s for s in w_self._value] registerimplementation(W_StringObject) ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: optimization fix
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r51211:498b6ee337e9 Date: 2012-01-10 17:22 +0100 http://bitbucket.org/pypy/pypy/changeset/498b6ee337e9/ Log:optimization fix diff --git a/pypy/objspace/std/listobject.py b/pypy/objspace/std/listobject.py --- a/pypy/objspace/std/listobject.py +++ b/pypy/objspace/std/listobject.py @@ -691,6 +691,7 @@ for i in l: if i == obj: return True +return False return ListStrategy.contains(self, w_list, w_obj) def length(self, w_list): ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: merged set- with liststrategies. when initializing a set with lists they can copy the storage and strategy from that list without wrapping the storages content
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r50751:b0d872ae3261 Date: 2011-12-20 13:42 +0100 http://bitbucket.org/pypy/pypy/changeset/b0d872ae3261/ Log:merged set- with liststrategies. when initializing a set with lists they can copy the storage and strategy from that list without wrapping the storages content diff --git a/pypy/objspace/std/listobject.py b/pypy/objspace/std/listobject.py --- a/pypy/objspace/std/listobject.py +++ b/pypy/objspace/std/listobject.py @@ -508,6 +508,11 @@ def getitems_copy(self, w_list): return self._getitems_range(w_list, True) +getitems_wrapped = getitems_copy + +def getitems_unwrapped(self, w_list): +return self._getitems_range(w_list, False) + def getstorage_copy(self, w_list): # tuple is unmutable return w_list.lstorage @@ -698,6 +703,11 @@ def getitems_copy(self, w_list): return [self.wrap(item) for item in self.unerase(w_list.lstorage)] +getitems_wrapped = getitems_copy + +def getitems_unwrapped(self, w_list): +return self.unerase(w_list.lstorage) + @jit.unroll_safe def getitems_unroll(self, w_list): return [self.wrap(item) for item in self.unerase(w_list.lstorage)] @@ -926,6 +936,8 @@ def getitems(self, w_list): return self.unerase(w_list.lstorage) +getitems_wrapped = getitems + class IntegerListStrategy(AbstractUnwrappedStrategy, ListStrategy): _none_value = 0 _applevel_repr = int diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -13,6 +13,8 @@ from pypy.objspace.std.listobject import W_ListObject from pypy.objspace.std.intobject import W_IntObject from pypy.objspace.std.stringobject import W_StringObject +from pypy.objspace.std.listobject import IntegerListStrategy, StringListStrategy,\ +EmptyListStrategy, RangeListStrategy, ObjectListStrategy, FloatListStrategy class W_BaseSetObject(W_Object): typedef = None @@ -280,6 +282,9 @@ def get_empty_storage(self): return self.erase(None) +def get_storage_from_w_list(self, w_list): +return self.get_empty_storage() + def is_correct_type(self, w_key): return False @@ -384,6 +389,14 @@ setdata[self.unwrap(w_item)] = None return self.erase(setdata) +def get_storage_from_w_list(self, w_list): +items = w_list.strategy.getitems_unwrapped(w_list) + +setdata = self.get_empty_dict() +for item in items: +setdata[item] = None +return self.erase(setdata) + def length(self, w_set): return len(self.unerase(w_set.sstorage)) @@ -746,6 +759,14 @@ def get_empty_storage(self): return self.erase(self.get_empty_dict()) +def get_storage_from_w_list(self, w_list): +items = w_list.strategy.getitems_wrapped(w_list) + +setdata = self.get_empty_dict() +for item in items: +setdata[item] = None +return self.erase(setdata) + def get_empty_dict(self): return newset(self.space) @@ -883,6 +904,22 @@ def newset(space): return r_dict(space.eq_w, space.hash_w, force_non_null=True) +_strategy_map = { +EmptyListStrategy: EmptySetStrategy, +IntegerListStrategy: IntegerSetStrategy, +RangeListStrategy: IntegerSetStrategy, +StringListStrategy: StringSetStrategy, +FloatListStrategy: ObjectSetStrategy, +ObjectListStrategy: ObjectSetStrategy +} + +def set_strategy_and_setdata_from_listobject(space, w_set, w_list): +strategy_class = _strategy_map[w_list.strategy.__class__] +strategy = space.fromcache(strategy_class) + +w_set.sstorage = strategy.get_storage_from_w_list(w_list) +w_set.strategy = strategy + def set_strategy_and_setdata(space, w_set, w_iterable): from pypy.objspace.std.intobject import W_IntObject if w_iterable is None : @@ -895,6 +932,10 @@ w_set.sstorage = w_iterable.get_storage_copy() return +if isinstance(w_iterable, W_ListObject): +set_strategy_and_setdata_from_listobject(space, w_set, w_iterable) +return + iterable_w = space.listview(w_iterable) if len(iterable_w) == 0: diff --git a/pypy/objspace/std/test/test_setobject.py b/pypy/objspace/std/test/test_setobject.py --- a/pypy/objspace/std/test/test_setobject.py +++ b/pypy/objspace/std/test/test_setobject.py @@ -8,7 +8,7 @@ is not too wrong. import py.test -from pypy.objspace.std.setobject import W_SetObject, W_FrozensetObject +from pypy.objspace.std.setobject import W_SetObject, W_FrozensetObject, IntegerSetStrategy from pypy.objspace.std.setobject import _initialize_set from pypy.objspace.std.setobject import newset from pypy.objspace.std.setobject import and__Set_Set @@ -83,6 +83,45 @@ result = set_intersection__Set(space, a, [d,c,b])
[pypy-commit] pypy set-strategies: better approach for merging sets with lists
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r50760:01dbcc06249a Date: 2011-12-20 16:19 +0100 http://bitbucket.org/pypy/pypy/changeset/01dbcc06249a/ Log:better approach for merging sets with lists diff --git a/pypy/objspace/std/listobject.py b/pypy/objspace/std/listobject.py --- a/pypy/objspace/std/listobject.py +++ b/pypy/objspace/std/listobject.py @@ -193,6 +193,11 @@ Return the items in the list as unwrapped strings. If the list does not use the list strategy, return None. return self.strategy.getitems_str(self) + +def getitems_int(self): + Return the items in the list as unwrapped strings. If the list does +not use the list strategy, return None. +return self.strategy.getitems_int(self) # ___ @@ -292,6 +297,9 @@ def getitems_str(self, w_list): return None +def getitems_int(self, w_list): +return None + def getstorage_copy(self, w_list): raise NotImplementedError @@ -502,17 +510,15 @@ raise IndexError return start + i * step +def getitems_int(self, w_list): +return self._getitems_range(w_list, False) + def getitem(self, w_list, i): return self.wrap(self._getitem_unwrapped(w_list, i)) def getitems_copy(self, w_list): return self._getitems_range(w_list, True) -getitems_wrapped = getitems_copy - -def getitems_unwrapped(self, w_list): -return self._getitems_range(w_list, False) - def getstorage_copy(self, w_list): # tuple is unmutable return w_list.lstorage @@ -703,11 +709,6 @@ def getitems_copy(self, w_list): return [self.wrap(item) for item in self.unerase(w_list.lstorage)] -getitems_wrapped = getitems_copy - -def getitems_unwrapped(self, w_list): -return self.unerase(w_list.lstorage) - @jit.unroll_safe def getitems_unroll(self, w_list): return [self.wrap(item) for item in self.unerase(w_list.lstorage)] @@ -936,8 +937,6 @@ def getitems(self, w_list): return self.unerase(w_list.lstorage) -getitems_wrapped = getitems - class IntegerListStrategy(AbstractUnwrappedStrategy, ListStrategy): _none_value = 0 _applevel_repr = int @@ -965,6 +964,9 @@ if reverse: l.reverse() +def getitems_int(self, w_list): +return self.unerase(w_list.lstorage) + class FloatListStrategy(AbstractUnwrappedStrategy, ListStrategy): _none_value = 0.0 _applevel_repr = float @@ -1022,7 +1024,6 @@ def getitems_str(self, w_list): return self.unerase(w_list.lstorage) - # ___ init_signature = Signature(['sequence'], None, None) diff --git a/pypy/objspace/std/objspace.py b/pypy/objspace/std/objspace.py --- a/pypy/objspace/std/objspace.py +++ b/pypy/objspace/std/objspace.py @@ -440,6 +440,11 @@ return w_obj.getitems_str() return None +def listview_int(self, w_obj): +if isinstance(w_obj, W_ListObject): +return w_obj.getitems_int() +return None + def sliceindices(self, w_slice, w_length): if isinstance(w_slice, W_SliceObject): a, b, c = w_slice.indices3(self, self.int_w(w_length)) diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -13,8 +13,6 @@ from pypy.objspace.std.listobject import W_ListObject from pypy.objspace.std.intobject import W_IntObject from pypy.objspace.std.stringobject import W_StringObject -from pypy.objspace.std.listobject import IntegerListStrategy, StringListStrategy,\ -EmptyListStrategy, RangeListStrategy, ObjectListStrategy, FloatListStrategy class W_BaseSetObject(W_Object): typedef = None @@ -282,9 +280,6 @@ def get_empty_storage(self): return self.erase(None) -def get_storage_from_w_list(self, w_list): -return self.get_empty_storage() - def is_correct_type(self, w_key): return False @@ -389,9 +384,7 @@ setdata[self.unwrap(w_item)] = None return self.erase(setdata) -def get_storage_from_w_list(self, w_list): -items = w_list.strategy.getitems_unwrapped(w_list) - +def get_storage_from_unwrapped_list(self, items): setdata = self.get_empty_dict() for item in items: setdata[item] = None @@ -759,14 +752,6 @@ def get_empty_storage(self): return self.erase(self.get_empty_dict()) -def get_storage_from_w_list(self, w_list): -items = w_list.strategy.getitems_wrapped(w_list) - -setdata = self.get_empty_dict() -for item in items: -setdata[item] = None -return self.erase(setdata) - def get_empty_dict(self): return newset(self.space) @@
[pypy-commit] pypy default: merge
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: Changeset: r50311:6acad4874739 Date: 2011-12-08 17:47 +0100 http://bitbucket.org/pypy/pypy/changeset/6acad4874739/ Log:merge diff --git a/pypy/jit/backend/llsupport/regalloc.py b/pypy/jit/backend/llsupport/regalloc.py --- a/pypy/jit/backend/llsupport/regalloc.py +++ b/pypy/jit/backend/llsupport/regalloc.py @@ -23,9 +23,12 @@ return self.frame_bindings.get(box, None) def loc(self, box): -res = self.get(box) -if res is not None: -return res +try: +return self.frame_bindings[box] +except KeyError: +return self.get_new_loc(box) + +def get_new_loc(self, box): size = self.frame_size(box.type) self.frame_depth += ((-self.frame_depth) (size-1)) # ^^^ frame_depth is rounded up to a multiple of 'size', assuming @@ -67,8 +70,17 @@ self.position = -1 self.frame_manager = frame_manager self.assembler = assembler +self.hint_frame_locations = {} # {Box: StackLoc} +self.freed_frame_locations = {}# {StackLoc: None} + +def is_still_alive(self, v): +# Check if 'v' is alive at the current position. +# Return False if the last usage is strictly before. +return self.longevity[v][1] = self.position def stays_alive(self, v): +# Check if 'v' stays alive after the current position. +# Return False if the last usage is before or at position. return self.longevity[v][1] self.position def next_instruction(self, incr=1): @@ -84,11 +96,16 @@ point for all variables that might be in registers. self._check_type(v) -if isinstance(v, Const) or v not in self.reg_bindings: +if isinstance(v, Const): return if v not in self.longevity or self.longevity[v][1] = self.position: -self.free_regs.append(self.reg_bindings[v]) -del self.reg_bindings[v] +if v in self.reg_bindings: +self.free_regs.append(self.reg_bindings[v]) +del self.reg_bindings[v] +if self.frame_manager is not None: +if v in self.frame_manager.frame_bindings: +loc = self.frame_manager.frame_bindings[v] +self.freed_frame_locations[loc] = None def possibly_free_vars(self, vars): Same as 'possibly_free_var', but for all v in vars. @@ -160,6 +177,23 @@ self.reg_bindings[v] = loc return loc +def _frame_loc(self, v): +# first check if it's already in the frame_manager +try: +return self.frame_manager.frame_bindings[v] +except KeyError: +pass +# check if we have a hint for this box +if v in self.hint_frame_locations: +# if we do, check that the hinted location is known to be free +loc = self.hint_frame_locations[v] +if loc in self.freed_frame_locations: +del self.freed_frame_locations[loc] +self.frame_manager.frame_bindings[v] = loc +return loc +# no valid hint. make up a new free location +return self.frame_manager.get_new_loc(v) + def _spill_var(self, v, forbidden_vars, selected_reg, need_lower_byte=False): v_to_spill = self._pick_variable_to_spill(v, forbidden_vars, @@ -167,7 +201,7 @@ loc = self.reg_bindings[v_to_spill] del self.reg_bindings[v_to_spill] if self.frame_manager.get(v_to_spill) is None: -newloc = self.frame_manager.loc(v_to_spill) +newloc = self._frame_loc(v_to_spill) self.assembler.regalloc_mov(loc, newloc) return loc @@ -244,7 +278,7 @@ except KeyError: if box in self.bindings_to_frame_reg: return self.frame_reg -return self.frame_manager.loc(box) +return self._frame_loc(box) def return_constant(self, v, forbidden_vars=[], selected_reg=None): Return the location of the constant v. If 'selected_reg' is @@ -292,7 +326,7 @@ self.reg_bindings[v] = loc self.assembler.regalloc_mov(prev_loc, loc) else: -loc = self.frame_manager.loc(v) +loc = self._frame_loc(v) self.assembler.regalloc_mov(prev_loc, loc) def force_result_in_reg(self, result_v, v, forbidden_vars=[]): @@ -311,7 +345,7 @@ self.reg_bindings[result_v] = loc return loc if v not in self.reg_bindings: -prev_loc = self.frame_manager.loc(v) +prev_loc = self._frame_loc(v) loc = self.force_allocate_reg(v, forbidden_vars) self.assembler.regalloc_mov(prev_loc, loc) assert v in self.reg_bindings @@ -331,7 +365,7 @@ def _sync_var(self, v):
[pypy-commit] pypy default: (l.diekmann, cfbolz): Be more careful about unrolling getitems. also make one less copy
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: Changeset: r50310:f19d06a5972d Date: 2011-12-08 17:27 +0100 http://bitbucket.org/pypy/pypy/changeset/f19d06a5972d/ Log:(l.diekmann, cfbolz): Be more careful about unrolling getitems. also make one less copy diff --git a/pypy/objspace/std/listobject.py b/pypy/objspace/std/listobject.py --- a/pypy/objspace/std/listobject.py +++ b/pypy/objspace/std/listobject.py @@ -9,8 +9,9 @@ from pypy.interpreter import gateway, baseobjspace from pypy.rlib.objectmodel import instantiate, specialize from pypy.rlib.listsort import make_timsort_class -from pypy.rlib import rerased, jit +from pypy.rlib import rerased, jit, debug from pypy.interpreter.argument import Signature +from pypy.tool.sourcetools import func_with_new_name UNROLL_CUTOFF = 5 @@ -170,6 +171,19 @@ share with the storage, if possible. return self.strategy.getitems(self) +def getitems_fixedsize(self): +Returns a fixed-size list of all items after wrapping them. +l = self.strategy.getitems_fixedsize(self) +debug.make_sure_not_resized(l) +return l + +def getitems_unroll(self): +Returns a fixed-size list of all items after wrapping them. The JIT +will fully unroll this function. +l = self.strategy.getitems_unroll(self) +debug.make_sure_not_resized(l) +return l + def getitems_copy(self): Returns a copy of all items in the list. Same as getitems except for ObjectListStrategy. @@ -366,6 +380,8 @@ def getitems_copy(self, w_list): return [] +getitems_fixedsize = func_with_new_name(getitems_copy, getitems_fixedsize) +getitems_unroll = getitems_fixedsize def getstorage_copy(self, w_list): return self.erase(None) @@ -496,7 +512,6 @@ # tuple is unmutable return w_list.lstorage - @specialize.arg(2) def _getitems_range(self, w_list, wrap_items): l = self.unerase(w_list.lstorage) @@ -519,6 +534,13 @@ return r +@jit.dont_look_inside +def getitems_fixedsize(self, w_list): +return self._getitems_range_unroll(w_list, True) +def getitems_unroll(self, w_list): +return self._getitems_range_unroll(w_list, True) +_getitems_range_unroll = jit.unroll_safe(func_with_new_name(_getitems_range, _getitems_range_unroll)) + def getslice(self, w_list, start, stop, step, length): v = self.unerase(w_list.lstorage) old_start = v[0] @@ -676,6 +698,13 @@ def getitems_copy(self, w_list): return [self.wrap(item) for item in self.unerase(w_list.lstorage)] +@jit.unroll_safe +def getitems_unroll(self, w_list): +return [self.wrap(item) for item in self.unerase(w_list.lstorage)] +@jit.dont_look_inside +def getitems_fixedsize(self, w_list): +return self.getitems_unroll(w_list) + def getstorage_copy(self, w_list): items = self.unerase(w_list.lstorage)[:] return self.erase(items) diff --git a/pypy/objspace/std/objspace.py b/pypy/objspace/std/objspace.py --- a/pypy/objspace/std/objspace.py +++ b/pypy/objspace/std/objspace.py @@ -408,8 +408,10 @@ if isinstance(w_obj, W_TupleObject): t = w_obj.wrappeditems elif isinstance(w_obj, W_ListObject): -# XXX this can copy twice -t = w_obj.getitems()[:] +if unroll: +t = w_obj.getitems_unroll() +else: +t = w_obj.getitems_fixedsize() else: if unroll: return make_sure_not_resized(ObjSpace.unpackiterable_unroll( diff --git a/pypy/objspace/std/test/test_listobject.py b/pypy/objspace/std/test/test_listobject.py --- a/pypy/objspace/std/test/test_listobject.py +++ b/pypy/objspace/std/test/test_listobject.py @@ -48,6 +48,46 @@ for i in range(7): assert self.space.eq_w(l[i], l2[i]) +def test_getitems_fixedsize(self): +w = self.space.wrap +from pypy.objspace.std.listobject import make_range_list +rangelist = make_range_list(self.space, 1,1,7) +emptylist = W_ListObject(self.space, []) +intlist = W_ListObject(self.space, [w(1),w(2),w(3),w(4),w(5),w(6),w(7)]) +strlist = W_ListObject(self.space, [w('1'),w('2'),w('3'),w('4'),w('5'),w('6'),w('7')]) +floatlist = W_ListObject(self.space, [w(1.0),w(2.0),w(3.0),w(4.0),w(5.0),w(6.0),w(7.0)]) +objlist = W_ListObject(self.space, [w(1),w('2'),w(3.0),w(4),w(5),w(6),w(7)]) + +emptylist_copy = emptylist.getitems_fixedsize() +assert emptylist_copy == [] + +rangelist_copy = rangelist.getitems_fixedsize() +intlist_copy = intlist.getitems_fixedsize() +strlist_copy = strlist.getitems_fixedsize() +floatlist_copy = floatlist.getitems_fixedsize() +objlist_copy = objlist.getitems_fixedsize() +for i in range(7): +
[pypy-commit] pypy type-specialized-instances: fixes for type-specialized-attributes
Author: l.diekmann Branch: type-specialized-instances Changeset: r50312:e481a093e056 Date: 2011-12-08 16:35 + http://bitbucket.org/pypy/pypy/changeset/e481a093e056/ Log:fixes for type-specialized-attributes diff --git a/pypy/objspace/std/mapdict.py b/pypy/objspace/std/mapdict.py --- a/pypy/objspace/std/mapdict.py +++ b/pypy/objspace/std/mapdict.py @@ -134,7 +134,7 @@ @jit.elidable def _get_new_attr(self, name, index, attrclass): -key = name, index, attrclass +key = name, index, attrclass.key_for_attr_cache cache = self.cache_attrs if cache is None: cache = self.cache_attrs = {} @@ -334,6 +334,7 @@ return PlainAttribute %s %s %r % (self.selector, self.position, self.back) class PlainAttribute(AbstractStoredAttribute): +key_for_attr_cache = 0 erase_item, unerase_item = rerased.new_erasing_pair(mapdict storage object item) erase_item = staticmethod(erase_item) @@ -349,6 +350,7 @@ obj._mapdict_write_storage(self.position, erased) class IntAttribute(AbstractStoredAttribute): +key_for_attr_cache = 1 erase_item, unerase_item = rerased.erase_int, rerased.unerase_int erase_item = staticmethod(erase_item) ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy type-specialized-instances: some fixes for type-specialized-attributes
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: type-specialized-instances Changeset: r50207:940ed396c2d9 Date: 2011-12-06 14:34 +0100 http://bitbucket.org/pypy/pypy/changeset/940ed396c2d9/ Log:some fixes for type-specialized-attributes diff --git a/pypy/objspace/std/mapdict.py b/pypy/objspace/std/mapdict.py --- a/pypy/objspace/std/mapdict.py +++ b/pypy/objspace/std/mapdict.py @@ -42,7 +42,9 @@ try: attr.write_attr(obj, w_value) #obj._mapdict_write_storage(index, w_value) except OperationError: -firstattr = obj.map +if not e.match(self.space, self.space.w_TypeError): +raise +firstattr = obj._get_mapdict_map() firstattr.delete(obj, selector) firstattr.add_attr(obj, selector, w_value) return True @@ -158,7 +160,7 @@ oldattr._size_estimate = size_est if attr.length() obj._mapdict_storage_length(): # note that attr.size_estimate() is always at least attr.length() -new_storage = [None] * attr.size_estimate() +new_storage = [PlainAttribute.erase_item(None)] * attr.size_estimate() for i in range(obj._mapdict_storage_length()): new_storage[i] = obj._mapdict_read_storage(i) obj._set_mapdict_storage_and_map(new_storage, attr) @@ -375,6 +377,7 @@ attrclass = PlainAttribute if is_taggable_int(space, w_value): attrclass = IntAttribute + return attrclass def _become(w_obj, new_obj): @@ -517,7 +520,7 @@ def _init_empty(self, map): from pypy.rlib.debug import make_sure_not_resized self.map = map -self.storage = make_sure_not_resized([None] * map.size_estimate()) +self.storage = make_sure_not_resized([PlainAttribute.erase_item(None)] * map.size_estimate()) def _mapdict_read_storage(self, index): assert index = 0 @@ -918,7 +921,7 @@ version_tag) if w_method is None or isinstance(w_method, TypeCell): return -_fill_cache(pycode, nameindex, map, version_tag, -1, w_method) +_fill_cache(pycode, nameindex, map, version_tag, None, w_method) # XXX fix me: if a function contains a loop with both LOAD_ATTR and # XXX LOOKUP_METHOD on the same attribute name, it keeps trashing and ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy type-specialized-instances: oups
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: type-specialized-instances Changeset: r50209:0c894ad8e9b5 Date: 2011-12-06 14:47 +0100 http://bitbucket.org/pypy/pypy/changeset/0c894ad8e9b5/ Log:oups diff --git a/pypy/objspace/std/mapdict.py b/pypy/objspace/std/mapdict.py --- a/pypy/objspace/std/mapdict.py +++ b/pypy/objspace/std/mapdict.py @@ -41,7 +41,7 @@ return self.terminator._write_terminator(obj, selector, w_value) try: attr.write_attr(obj, w_value) #obj._mapdict_write_storage(index, w_value) -except OperationError: +except OperationError, e: if not e.match(self.space, self.space.w_TypeError): raise firstattr = obj._get_mapdict_map() ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy type-specialized-instances: fixed mapdict tests
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: type-specialized-instances Changeset: r50169:591a337d364f Date: 2011-12-05 12:32 +0100 http://bitbucket.org/pypy/pypy/changeset/591a337d364f/ Log:fixed mapdict tests diff --git a/pypy/objspace/std/test/test_mapdict.py b/pypy/objspace/std/test/test_mapdict.py --- a/pypy/objspace/std/test/test_mapdict.py +++ b/pypy/objspace/std/test/test_mapdict.py @@ -25,17 +25,17 @@ class typedef: hasdict = False -def erase_storage_items(items): -return [IntAttribute.erase_item(item) for item in items] +def erase_storage_items(items, eraser=PlainAttribute): +return [eraser.erase_item(item) for item in items] -def unerase_storage_items(storage, uneraser=IntAttribute): +def unerase_storage_items(storage, uneraser=PlainAttribute): return [uneraser.unerase_item(item) for item in storage] def test_plain_attribute(): w_cls = class -aa = IntAttribute((b, DICT), -IntAttribute((a, DICT), +aa = PlainAttribute((b, DICT), +PlainAttribute((a, DICT), Terminator(space, w_cls))) assert aa.space is space assert aa.terminator.w_cls is w_cls @@ -81,7 +81,8 @@ def test_add_attribute(): cls = Class() obj = cls.instantiate() -obj.setdictvalue(space, a, 10) +obj.setdictvalue(space, a, space.wrap(10)) +print obj.map assert unerase_storage_items(obj.storage) == [10] assert obj.getdictvalue(space, a) == 10 assert obj.getdictvalue(space, b) is None @@ -159,9 +160,7 @@ assert obj.getdictvalue(space, a) == 50 assert obj.getdictvalue(space, b) == 60 assert obj.getdictvalue(space, c) == 70 -#assert unerase_storage_items(obj.storage) == [50, 60, 70, lifeline1] -assert unerase_storage_items(obj.storage[:-1], IntAttribute) == [50, 60, 70] -assert unerase_storage_items(obj.storage[-1:], PlainAttribute) == [lifeline1] +assert unerase_storage_items(obj.storage) == [50, 60, 70, lifeline1] assert obj.getweakref() is lifeline1 obj2 = c.instantiate() @@ -169,9 +168,7 @@ obj2.setdictvalue(space, b, 160) obj2.setdictvalue(space, c, 170) obj2.setweakref(space, lifeline2) -#assert unerase_storage_items(obj2.storage) == [150, 160, 170, lifeline2] -assert unerase_storage_items(obj2.storage[:-1], IntAttribute) == [150, 160, 170] -assert unerase_storage_items(obj2.storage[-1:], PlainAttribute) == [lifeline2] +assert unerase_storage_items(obj2.storage) == [150, 160, 170, lifeline2] assert obj2.getweakref() is lifeline2 assert obj2.map is obj.map @@ -282,9 +279,7 @@ assert flag materialize_r_dict(space, obj, d) assert d == {a: 5, b: 6, c: 7} -#assert unerase_storage_items(obj.storage) == [50, 60, 70, w_d] -assert unerase_storage_items(obj.storage[:-1], IntAttribute) == [50, 60, 70] -assert unerase_storage_items(obj.storage[-1:], PlainAttribute) == [w_d] +assert unerase_storage_items(obj.storage) == [50, 60, 70, w_d] def test_size_prediction(): @@ -766,12 +761,21 @@ def test_delete_slot(self): class A(object): __slots__ = ['x'] - + a = A() a.x = 42 del a.x raises(AttributeError, a.x) +def test_subclassed_int(self): +class Integer(int): +pass + +a = Integer() +a.x = 5 + +assert a.x == 5 + class AppTestWithMapDictAndCounters(object): def setup_class(cls): from pypy.interpreter import gateway ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy default: added interp_magic method to get the strategy of a list on the application level
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: Changeset: r50032:497907d5d515 Date: 2011-12-01 11:50 +0100 http://bitbucket.org/pypy/pypy/changeset/497907d5d515/ Log:added interp_magic method to get the strategy of a list on the application level diff --git a/pypy/module/__pypy__/__init__.py b/pypy/module/__pypy__/__init__.py --- a/pypy/module/__pypy__/__init__.py +++ b/pypy/module/__pypy__/__init__.py @@ -27,6 +27,7 @@ 'builtinify': 'interp_magic.builtinify', 'lookup_special': 'interp_magic.lookup_special', 'do_what_I_mean': 'interp_magic.do_what_I_mean', +'list_strategy' : 'interp_magic.list_strategy', } submodules = { diff --git a/pypy/module/__pypy__/interp_magic.py b/pypy/module/__pypy__/interp_magic.py --- a/pypy/module/__pypy__/interp_magic.py +++ b/pypy/module/__pypy__/interp_magic.py @@ -4,7 +4,7 @@ from pypy.rlib.objectmodel import we_are_translated from pypy.objspace.std.typeobject import MethodCache from pypy.objspace.std.mapdict import IndexCache - +from pypy.objspace.std.listobject import W_ListObject, IntegerListStrategy, StringListStrategy, FloatListStrategy, RangeListStrategy, EmptyListStrategy, ObjectListStrategy def internal_repr(space, w_object): return space.wrap('%r' % (w_object,)) @@ -73,3 +73,9 @@ def do_what_I_mean(space): return space.wrap(42) + +def list_strategy(space, w_list): +str_type = None +if isinstance(w_list, W_ListObject): +str_type = w_list.strategy._type +return space.wrap(str_type) diff --git a/pypy/module/__pypy__/test/test_special.py b/pypy/module/__pypy__/test/test_special.py --- a/pypy/module/__pypy__/test/test_special.py +++ b/pypy/module/__pypy__/test/test_special.py @@ -5,7 +5,7 @@ def setup_class(cls): if option.runappdirect: py.test.skip(does not make sense on pypy-c) -cls.space = gettestobjspace(**{objspace.usemodules.select: False}) +cls.space = gettestobjspace(**{objspace.usemodules.select: False, objspace.std.withrangelist: True}) def test__isfake(self): from __pypy__ import isfake @@ -54,3 +54,20 @@ from __pypy__ import do_what_I_mean x = do_what_I_mean() assert x == 42 + +def test_list_strategy(self): +from __pypy__ import list_strategy +l = [1,2,3] +assert list_strategy(l) == int +l = [a,b,c] +assert list_strategy(l) == str +l = [1.1,2.2,3.3] +assert list_strategy(l) == float +l = range(3) +assert list_strategy(l) == range +l = [1,b,3] +assert list_strategy(l) == object +l = [] +assert list_strategy(l) == empty +o = 5 +assert list_strategy(o) == None diff --git a/pypy/objspace/std/listobject.py b/pypy/objspace/std/listobject.py --- a/pypy/objspace/std/listobject.py +++ b/pypy/objspace/std/listobject.py @@ -326,6 +326,8 @@ to the added item. W_Lists do not switch back to EmptyListStrategy when becoming empty again. +_type = empty + def __init__(self, space): ListStrategy.__init__(self, space) # cache an empty list that is used whenever getitems is called (i.e. sorting) @@ -426,6 +428,8 @@ On any operation destroying the range (inserting, appending non-ints) the strategy is switched to IntegerListStrategy. +_type = range + def switch_to_integer_strategy(self, w_list): items = self._getitems_range(w_list, False) strategy = w_list.strategy = self.space.fromcache(IntegerListStrategy) @@ -864,6 +868,7 @@ class ObjectListStrategy(AbstractUnwrappedStrategy, ListStrategy): _none_value = None +_type = object def unwrap(self, w_obj): return w_obj @@ -892,6 +897,7 @@ class IntegerListStrategy(AbstractUnwrappedStrategy, ListStrategy): _none_value = 0 +_type = int def wrap(self, intval): return self.space.wrap(intval) @@ -918,6 +924,7 @@ class FloatListStrategy(AbstractUnwrappedStrategy, ListStrategy): _none_value = 0.0 +_type = float def wrap(self, floatval): return self.space.wrap(floatval) @@ -944,6 +951,7 @@ class StringListStrategy(AbstractUnwrappedStrategy, ListStrategy): _none_value = None +_type = str def wrap(self, stringval): return self.space.wrap(stringval) ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy default: added floatstrategy for lists
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: Changeset: r50010:748818c5a463 Date: 2011-11-30 15:03 +0100 http://bitbucket.org/pypy/pypy/changeset/748818c5a463/ Log:added floatstrategy for lists diff --git a/pypy/objspace/std/listobject.py b/pypy/objspace/std/listobject.py --- a/pypy/objspace/std/listobject.py +++ b/pypy/objspace/std/listobject.py @@ -50,6 +50,13 @@ else: return space.fromcache(StringListStrategy) +# check for floats +for w_obj in list_w: +if not is_W_FloatObject(w_obj): +break +else: +return space.fromcache(FloatListStrategy) + return space.fromcache(ObjectListStrategy) def is_W_IntObject(w_object): @@ -60,7 +67,9 @@ from pypy.objspace.std.stringobject import W_StringObject return type(w_object) is W_StringObject - +def is_W_FloatObject(w_object): +from pypy.objspace.std.floatobject import W_FloatObject +return type(w_object) is W_FloatObject class W_ListObject(W_AbstractListObject): from pypy.objspace.std.listtype import list_typedef as typedef @@ -364,6 +373,8 @@ strategy = self.space.fromcache(IntegerListStrategy) elif is_W_StringObject(w_item): strategy = self.space.fromcache(StringListStrategy) +elif is_W_FloatObject(w_item): +strategy = self.space.fromcache(FloatListStrategy) else: strategy = self.space.fromcache(ObjectListStrategy) @@ -905,6 +916,32 @@ if reverse: l.reverse() +class FloatListStrategy(AbstractUnwrappedStrategy, ListStrategy): +_none_value = 0.0 + +def wrap(self, floatval): +return self.space.wrap(floatval) + +def unwrap(self, w_float): +return self.space.float_w(w_float) + +erase, unerase = rerased.new_erasing_pair(float) +erase = staticmethod(erase) +unerase = staticmethod(unerase) + +def is_correct_type(self, w_obj): +return is_W_FloatObject(w_obj) + +def list_is_correct_type(self, w_list): +return w_list.strategy is self.space.fromcache(FloatListStrategy) + +def sort(self, w_list, reverse): +l = self.unerase(w_list.lstorage) +sorter = FloatSort(l, len(l)) +sorter.sort() +if reverse: +l.reverse() + class StringListStrategy(AbstractUnwrappedStrategy, ListStrategy): _none_value = None @@ -934,6 +971,7 @@ def getitems_str(self, w_list): return self.unerase(w_list.lstorage) + # ___ init_signature = Signature(['sequence'], None, None) @@ -1282,6 +1320,7 @@ TimSort = make_timsort_class() IntBaseTimSort = make_timsort_class() +FloatBaseTimSort = make_timsort_class() StringBaseTimSort = make_timsort_class() class KeyContainer(baseobjspace.W_Root): @@ -1302,6 +1341,10 @@ def lt(self, a, b): return a b +class FloatSort(FloatBaseTimSort): +def lt(self, a, b): +return a b + class StringSort(StringBaseTimSort): def lt(self, a, b): return a b diff --git a/pypy/objspace/std/test/test_liststrategies.py b/pypy/objspace/std/test/test_liststrategies.py --- a/pypy/objspace/std/test/test_liststrategies.py +++ b/pypy/objspace/std/test/test_liststrategies.py @@ -1,4 +1,4 @@ -from pypy.objspace.std.listobject import W_ListObject, EmptyListStrategy, ObjectListStrategy, IntegerListStrategy, StringListStrategy, RangeListStrategy, make_range_list +from pypy.objspace.std.listobject import W_ListObject, EmptyListStrategy, ObjectListStrategy, IntegerListStrategy, FloatListStrategy, StringListStrategy, RangeListStrategy, make_range_list from pypy.objspace.std import listobject from pypy.objspace.std.test.test_listobject import TestW_ListObject @@ -15,7 +15,7 @@ def test_empty_to_any(self): l = W_ListObject(self.space, []) assert isinstance(l.strategy, EmptyListStrategy) -l.append(self.space.wrap(1.)) +l.append(self.space.wrap((1,3))) assert isinstance(l.strategy, ObjectListStrategy) l = W_ListObject(self.space, []) @@ -28,6 +28,11 @@ l.append(self.space.wrap('a')) assert isinstance(l.strategy, StringListStrategy) +l = W_ListObject(self.space, []) +assert isinstance(l.strategy, EmptyListStrategy) +l.append(self.space.wrap(1.2)) +assert isinstance(l.strategy, FloatListStrategy) + def test_int_to_any(self): l = W_ListObject(self.space, [self.space.wrap(1),self.space.wrap(2),self.space.wrap(3)]) assert isinstance(l.strategy, IntegerListStrategy) @@ -44,6 +49,14 @@ l.append(self.space.wrap(3)) assert isinstance(l.strategy, ObjectListStrategy) +def test_float_to_any(self): +l = W_ListObject(self.space, [self.space.wrap(1.1),self.space.wrap(2.2),self.space.wrap(3.3)]) +assert isinstance(l.strategy, FloatListStrategy) +l.append(self.space.wrap(4.4)) +
[pypy-commit] pypy default: merge
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: Changeset: r50011:6d12edf0639c Date: 2011-11-30 15:14 +0100 http://bitbucket.org/pypy/pypy/changeset/6d12edf0639c/ Log:merge diff --git a/pypy/annotation/specialize.py b/pypy/annotation/specialize.py --- a/pypy/annotation/specialize.py +++ b/pypy/annotation/specialize.py @@ -36,9 +36,7 @@ newtup = SpaceOperation('newtuple', starargs, argscopy[-1]) newstartblock.operations.append(newtup) newstartblock.closeblock(Link(argscopy, graph.startblock)) -graph.startblock.isstartblock = False graph.startblock = newstartblock -newstartblock.isstartblock = True argnames = argnames + ['.star%d' % i for i in range(nb_extra_args)] graph.signature = Signature(argnames) # note that we can mostly ignore defaults: if nb_extra_args 0, diff --git a/pypy/jit/metainterp/warmspot.py b/pypy/jit/metainterp/warmspot.py --- a/pypy/jit/metainterp/warmspot.py +++ b/pypy/jit/metainterp/warmspot.py @@ -255,10 +255,8 @@ s_binding = self.translator.annotator.binding jd._portal_args_s = [s_binding(v) for v in args] graph = copygraph(graph) -graph.startblock.isstartblock = False [jmpp] = find_jit_merge_points([graph]) graph.startblock = support.split_before_jit_merge_point(*jmpp) -graph.startblock.isstartblock = True # a crash in the following checkgraph() means that you forgot # to list some variable in greens=[] or reds=[] in JitDriver, # or that a jit_merge_point() takes a constant as an argument. diff --git a/pypy/objspace/flow/model.py b/pypy/objspace/flow/model.py --- a/pypy/objspace/flow/model.py +++ b/pypy/objspace/flow/model.py @@ -38,7 +38,6 @@ def __init__(self, name, startblock, return_var=None): self.name= name# function name (possibly mangled already) self.startblock = startblock -self.startblock.isstartblock = True # build default returnblock self.returnblock = Block([return_var or Variable()]) self.returnblock.operations = () @@ -171,11 +170,10 @@ class Block(object): -__slots__ = isstartblock inputargs operations exitswitch +__slots__ = inputargs operations exitswitch exits blockcolor.split() def __init__(self, inputargs): -self.isstartblock = False self.inputargs = list(inputargs) # mixed list of variable/const XXX self.operations = [] # list of SpaceOperation(s) self.exitswitch = None# a variable or @@ -452,7 +450,6 @@ newblock.closeblock(*newlinks) newstartblock = blockmap[graph.startblock] -newstartblock.isstartblock = True newgraph = FunctionGraph(graph.name, newstartblock) newgraph.returnblock = blockmap[graph.returnblock] newgraph.exceptblock = blockmap[graph.exceptblock] @@ -490,7 +487,6 @@ for block in graph.iterblocks(): -assert bool(block.isstartblock) == (block is graph.startblock) assert type(block.exits) is tuple, ( block.exits is a %s (closeblock() or recloseblock() missing?) % (type(block.exits).__name__,)) diff --git a/pypy/objspace/flow/test/test_checkgraph.py b/pypy/objspace/flow/test/test_checkgraph.py --- a/pypy/objspace/flow/test/test_checkgraph.py +++ b/pypy/objspace/flow/test/test_checkgraph.py @@ -13,20 +13,6 @@ py.test.raises(AssertionError, checkgraph, g) -def test_nostartblock(): -g = FunctionGraph(g, Block([])) -g.startblock.closeblock(Link([Constant(1)], g.returnblock)) -g.startblock.isstartblock = False -py.test.raises(AssertionError, checkgraph, g) - -def test_twostartblocks(): -g = FunctionGraph(g, Block([])) -b = Block([]) -b.isstartblock = True -g.startblock.closeblock(Link([], b)) -b.closeblock(Link([Constant(1)], g.returnblock)) -py.test.raises(AssertionError, checkgraph, g) - def test_exitlessblocknotexitblock(): g = FunctionGraph(g, Block([])) py.test.raises(AssertionError, checkgraph, g) diff --git a/pypy/rpython/memory/gctransform/asmgcroot.py b/pypy/rpython/memory/gctransform/asmgcroot.py --- a/pypy/rpython/memory/gctransform/asmgcroot.py +++ b/pypy/rpython/memory/gctransform/asmgcroot.py @@ -92,7 +92,6 @@ # make a copy of the graph that will reload the values graph2 = copygraph(fnptr._obj.graph) block2 = graph2.startblock -block2.isstartblock = False block1 = Block([]) reloadedvars = [] for v, c_p in zip(block2.inputargs, sra): @@ -109,7 +108,6 @@ [w], v)) reloadedvars.append(v) block1.closeblock(Link(reloadedvars, block2)) -block1.isstartblock = True graph2.startblock = block1 FUNC2 = lltype.FuncType([],
[pypy-commit] pypy list-strategies: added more tests for so far uncovered code in listobject.setslice
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: list-strategies Changeset: r49688:47318b7c592d Date: 2011-11-23 15:19 +0100 http://bitbucket.org/pypy/pypy/changeset/47318b7c592d/ Log:added more tests for so far uncovered code in listobject.setslice diff --git a/pypy/objspace/std/listobject.py b/pypy/objspace/std/listobject.py --- a/pypy/objspace/std/listobject.py +++ b/pypy/objspace/std/listobject.py @@ -774,7 +774,6 @@ # self.unerase is valid for both of them other_items = self.unerase(w_other.lstorage) if other_items is items: -XXX # untested paths if step 0: # Always copy starting from the right to avoid # having to make a shallow copy in the case where diff --git a/pypy/objspace/std/test/test_listobject.py b/pypy/objspace/std/test/test_listobject.py --- a/pypy/objspace/std/test/test_listobject.py +++ b/pypy/objspace/std/test/test_listobject.py @@ -779,6 +779,32 @@ l[::3] = ('a', 'b') assert l == ['a', 1, 2, 'b', 4, 5] +def test_setslice_with_self(self): +l = [1,2,3,4] +l[:] = l +assert l == [1,2,3,4] + +l = [1,2,3,4] +l[0:2] = l +assert l == [1,2,3,4,3,4] + +l = [1,2,3,4] +l[0:2] = l +assert l == [1,2,3,4,3,4] + +l = [1,2,3,4,5,6,7,8,9,10] +raises(ValueError, l[5::-1] = l) + +l = [1,2,3,4,5,6,7,8,9,10] +raises(ValueError, l[::2] = l) + +l = [1,2,3,4,5,6,7,8,9,10] +l[5:] = l +assert l == [1,2,3,4,5,1,2,3,4,5,6,7,8,9,10] + +l = [1,2,3,4,5,6] +l[::-1] = l +assert l == [6,5,4,3,2,1] def test_recursive_repr(self): l = [] ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy type-specialized-instances: added tests and fixes for overwriting attributes with another type and for untaggable ints
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: type-specialized-instances Changeset: r49664:c31774e1542e Date: 2011-11-22 16:10 +0100 http://bitbucket.org/pypy/pypy/changeset/c31774e1542e/ Log:added tests and fixes for overwriting attributes with another type and for untaggable ints diff --git a/pypy/objspace/std/mapdict.py b/pypy/objspace/std/mapdict.py --- a/pypy/objspace/std/mapdict.py +++ b/pypy/objspace/std/mapdict.py @@ -35,10 +35,16 @@ return attr.read_attr(obj) #obj._mapdict_read_storage(index) def write(self, obj, selector, w_value): +from pypy.interpreter.error import OperationError attr = self.findmap(selector) # index = self.index(selector) if attr is None: return self.terminator._write_terminator(obj, selector, w_value) -attr.write_attr(obj, w_value) #obj._mapdict_write_storage(index, w_value) +try: +attr.write_attr(obj, w_value) #obj._mapdict_write_storage(index, w_value) +except OperationError: +firstattr = obj.map +firstattr.delete(obj, selector) +firstattr.add_attr(obj, selector, w_value) return True def delete(self, obj, selector): @@ -355,13 +361,20 @@ erased = self.erase_item(self.space.int_w(w_value)) obj._mapdict_write_storage(self.position, erased) +def is_taggable_int(space, w_value): +from pypy.objspace.std.intobject import W_IntObject +if type(w_value) is W_IntObject: +try: +IntAttribute.erase_item(space.int_w(w_value)) +return True +except OverflowError: +pass +return False + def get_attrclass_from_value(space, w_value): attrclass = PlainAttribute -try: -if space.is_w(space.type(w_value), space.w_int): -attrclass = IntAttribute -except AttributeError: -pass +if is_taggable_int(space, w_value): +attrclass = IntAttribute return attrclass def _become(w_obj, new_obj): diff --git a/pypy/objspace/std/test/test_mapdict.py b/pypy/objspace/std/test/test_mapdict.py --- a/pypy/objspace/std/test/test_mapdict.py +++ b/pypy/objspace/std/test/test_mapdict.py @@ -365,6 +365,51 @@ assert isinstance(obj1.map, PlainAttribute) assert space.eq_w(obj1.getdictvalue(space, y), space.wrap(str)) +def test_overwrite_attribute_with_another_type(self): +space = self.space +cls = Class(sp=space) +obj1 = cls.instantiate() + +obj1.setdictvalue(space, x, space.wrap(1)) +assert isinstance(obj1.map, IntAttribute) +assert space.eq_w(obj1.getdictvalue(space, x), space.wrap(1)) + +obj1.setdictvalue(space, x, space.wrap(a)) +assert isinstance(obj1.map, PlainAttribute) +assert space.eq_w(obj1.getdictvalue(space, x), space.wrap(a)) + +def test_overwrite_attribute_with_another_type2(self): +space = self.space +cls = Class(sp=space) +obj1 = cls.instantiate() + +obj1.setdictvalue(space, x, space.wrap(1)) +assert isinstance(obj1.map, IntAttribute) +assert space.eq_w(obj1.getdictvalue(space, x), space.wrap(1)) + +obj1.setdictvalue(space, y, space.wrap(2)) +assert isinstance(obj1.map.back, IntAttribute) +assert space.eq_w(obj1.getdictvalue(space, y), space.wrap(2)) + +# overwrite 'x' with new type +obj1.setdictvalue(space, x, space.wrap(a)) +assert isinstance(obj1.map, PlainAttribute) +assert space.eq_w(obj1.getdictvalue(space, x), space.wrap(a)) + +# check if 'y' is still reachable +assert isinstance(obj1.map.back, IntAttribute) +assert space.eq_w(obj1.getdictvalue(space, y), space.wrap(2)) + +def test_int_does_not_fit(self): +import sys +space = self.space +cls = Class(sp=space) +obj1 = cls.instantiate() + +obj1.setdictvalue(space, x, space.wrap(sys.maxint)) +assert isinstance(obj1.map, PlainAttribute) +assert space.eq_w(obj1.getdictvalue(space, x), space.wrap(sys.maxint)) + # ___ # dict tests ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy type-specialized-instances: reverted last fix, found a new (and better) one
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: type-specialized-instances Changeset: r49604:118048845a8d Date: 2011-11-21 11:41 +0100 http://bitbucket.org/pypy/pypy/changeset/118048845a8d/ Log:reverted last fix, found a new (and better) one diff --git a/pypy/objspace/std/mapdict.py b/pypy/objspace/std/mapdict.py --- a/pypy/objspace/std/mapdict.py +++ b/pypy/objspace/std/mapdict.py @@ -100,8 +100,7 @@ def _findmap(self, selector): while isinstance(self, AbstractStoredAttribute): -# XXX is this the right fix? -if selector == self.selector[:2]: +if selector == self.selector: return self self = self.back return None @@ -133,7 +132,7 @@ cache = self.cache_attrs = {} attr = cache.get(key, None) if attr is None: -attr = attrclass(key, self) +attr = attrclass((name, index), self) cache[key] = attr return attr @@ -278,12 +277,11 @@ self._size_estimate = self.length() * NUM_DIGITS_POW2 def _copy_attr(self, obj, new_obj): -#XXX this the right fix? -w_value = self.read(obj, self.selector[:2]) +w_value = self.read(obj, self.selector) new_obj._get_mapdict_map().add_attr(new_obj, self.selector, w_value) def delete(self, obj, selector): -if selector == self.selector[:2]: +if selector == self.selector: # ok, attribute is deleted return self.back.copy(obj) new_obj = self.back.delete(obj, selector) diff --git a/pypy/objspace/std/test/test_mapdict.py b/pypy/objspace/std/test/test_mapdict.py --- a/pypy/objspace/std/test/test_mapdict.py +++ b/pypy/objspace/std/test/test_mapdict.py @@ -34,8 +34,8 @@ def test_plain_attribute(): w_cls = class -aa = IntAttribute((b, DICT, IntAttribute), -IntAttribute((a, DICT, IntAttribute), +aa = IntAttribute((b, DICT), +IntAttribute((a, DICT), Terminator(space, w_cls))) assert aa.space is space assert aa.terminator.w_cls is w_cls ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy type-specialized-instances: fixed some tests where storage items are erased. there is no general erasing pair anymore so we need to use the methods from the correct AbstractAttribut
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: type-specialized-instances Changeset: r49624:90348184732d Date: 2011-11-21 16:44 +0100 http://bitbucket.org/pypy/pypy/changeset/90348184732d/ Log:fixed some tests where storage items are erased. there is no general erasing pair anymore so we need to use the methods from the correct AbstractAttribute to (un)erase diff --git a/pypy/objspace/std/test/test_mapdict.py b/pypy/objspace/std/test/test_mapdict.py --- a/pypy/objspace/std/test/test_mapdict.py +++ b/pypy/objspace/std/test/test_mapdict.py @@ -159,6 +159,7 @@ assert obj.getdictvalue(space, a) == 50 assert obj.getdictvalue(space, b) == 60 assert obj.getdictvalue(space, c) == 70 +#assert unerase_storage_items(obj.storage) == [50, 60, 70, lifeline1] assert unerase_storage_items(obj.storage[:-1], IntAttribute) == [50, 60, 70] assert unerase_storage_items(obj.storage[-1:], PlainAttribute) == [lifeline1] assert obj.getweakref() is lifeline1 @@ -168,7 +169,9 @@ obj2.setdictvalue(space, b, 160) obj2.setdictvalue(space, c, 170) obj2.setweakref(space, lifeline2) -assert unerase_storage_items(obj2.storage) == [150, 160, 170, lifeline2] +#assert unerase_storage_items(obj2.storage) == [150, 160, 170, lifeline2] +assert unerase_storage_items(obj2.storage[:-1], IntAttribute) == [150, 160, 170] +assert unerase_storage_items(obj2.storage[-1:], PlainAttribute) == [lifeline2] assert obj2.getweakref() is lifeline2 assert obj2.map is obj.map @@ -279,7 +282,9 @@ assert flag materialize_r_dict(space, obj, d) assert d == {a: 5, b: 6, c: 7} -assert unerase_storage_items(obj.storage) == [50, 60, 70, w_d] +#assert unerase_storage_items(obj.storage) == [50, 60, 70, w_d] +assert unerase_storage_items(obj.storage[:-1], IntAttribute) == [50, 60, 70] +assert unerase_storage_items(obj.storage[-1:], PlainAttribute) == [w_d] def test_size_prediction(): @@ -463,12 +468,12 @@ obj = objectcls() obj.user_setup(space, cls) obj.setdictvalue(space, a, w1) -assert unerase_item(obj._value0) is w1 +assert PlainAttribute.unerase_item(obj._value0) is w1 assert obj.getdictvalue(space, a) is w1 assert obj.getdictvalue(space, b) is None assert obj.getdictvalue(space, c) is None obj.setdictvalue(space, a, w2) -assert unerase_item(obj._value0) is w2 +assert PlainAttribute.unerase_item(obj._value0) is w2 assert obj.getdictvalue(space, a) == w2 assert obj.getdictvalue(space, b) is None assert obj.getdictvalue(space, c) is None @@ -486,7 +491,7 @@ res = obj.deldictvalue(space, a) assert res -assert unerase_item(obj._value0) is w4 +assert PlainAttribute.unerase_item(obj._value0) is w4 assert obj.getdictvalue(space, a) is None assert obj.getdictvalue(space, b) is w4 assert obj.getdictvalue(space, c) is None ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy type-specialized-instances: introduced IntAttribute
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: type-specialized-instances Changeset: r49522:b16fead0c3f3 Date: 2011-11-16 18:24 +0100 http://bitbucket.org/pypy/pypy/changeset/b16fead0c3f3/ Log:introduced IntAttribute diff --git a/pypy/objspace/std/mapdict.py b/pypy/objspace/std/mapdict.py --- a/pypy/objspace/std/mapdict.py +++ b/pypy/objspace/std/mapdict.py @@ -125,15 +125,15 @@ return None @jit.elidable -def _get_new_attr(self, name, index): -selector = name, index +def _get_new_attr(self, name, index, attrclass): +key = name, index, attrclass cache = self.cache_attrs if cache is None: cache = self.cache_attrs = {} -attr = cache.get(selector, None) +attr = cache.get(key, None) if attr is None: -attr = PlainAttribute(selector, self) -cache[selector] = attr +attr = attrclass(key, self) +cache[key] = attr return attr @jit.look_inside_iff(lambda self, obj, selector, w_value: @@ -141,8 +141,9 @@ jit.isconstant(selector[0]) and jit.isconstant(selector[1])) def add_attr(self, obj, selector, w_value): +attrclass = get_attrclass_from_value(self.space, w_value) # grumble, jit needs this -attr = self._get_new_attr(selector[0], selector[1]) +attr = self._get_new_attr(selector[0], selector[1], attrclass) oldattr = obj._get_mapdict_map() if not jit.we_are_jitted(): size_est = (oldattr._size_estimate + attr.size_estimate() @@ -264,8 +265,10 @@ terminator = terminator.devolved_dict_terminator return Terminator.set_terminator(self, obj, terminator) -class PlainAttribute(AbstractAttribute): +class AbstractStoredAttribute(AbstractAttribute): + _immutable_fields_ = ['selector', 'position', 'back'] + def __init__(self, selector, back): AbstractAttribute.__init__(self, back.space, back.terminator) self.selector = selector @@ -277,17 +280,6 @@ w_value = self.read(obj, self.selector) new_obj._get_mapdict_map().add_attr(new_obj, self.selector, w_value) -def read_attr(self, obj): -# XXX do the unerasing (and wrapping) here -erased = obj._mapdict_read_storage(self.position) -w_value = unerase_item(erased) -return w_value - -def write_attr(self, obj, w_value): -# XXX do the unerasing (and unwrapping) here -erased = erase_item(w_value) -obj._mapdict_write_storage(self.position, erased) - def delete(self, obj, selector): if selector == self.selector: # ok, attribute is deleted @@ -333,6 +325,41 @@ def __repr__(self): return PlainAttribute %s %s %r % (self.selector, self.position, self.back) +class PlainAttribute(AbstractStoredAttribute): + +erase_item, unerase_item = rerased.new_erasing_pair(mapdict storage object item) +erase_item = staticmethod(erase_item) +unerase_item = staticmethod(unerase_item) + +def read_attr(self, obj): +erased = obj._mapdict_read_storage(self.position) +w_value = self.unerase_item(erased) +return w_value + +def write_attr(self, obj, w_value): +erased = self.erase_item(w_value) +obj._mapdict_write_storage(self.position, erased) + +class IntAttribute(AbstractStoredAttribute): + +erase_item, unerase_item = rerased.erase_int, rerased.unerase_int +erase_item = staticmethod(erase_item) +unerase_item = staticmethod(unerase_item) + +def read_attr(self, obj): +erased = obj._mapdict_read_storage(self.position) +value = self.unerase_item(erased) +return self.space.wrap(value) + +def write_attr(self, obj, w_value): +erased = self.erase_item(self.space.int_w(w_value)) +obj._mapdict_write_storage(self.position, erased) + +def get_attrclass_from_value(space, w_value): +if space.is_w(space.type(w_value), space.w_int): +return IntAttribute +return PlainAttribute + def _become(w_obj, new_obj): # this is like the _become method, really, but we cannot use that due to # RPython reasons @@ -524,7 +551,6 @@ memo_get_subclass_of_correct_size._annspecialcase_ = specialize:memo _subclass_cache = {} -erase_item, unerase_item = rerased.new_erasing_pair(mapdict storage item) erase_list, unerase_list = rerased.new_erasing_pair(mapdict storage list) def _make_subclass_size_n(supercls, n): diff --git a/pypy/objspace/std/test/test_mapdict.py b/pypy/objspace/std/test/test_mapdict.py --- a/pypy/objspace/std/test/test_mapdict.py +++ b/pypy/objspace/std/test/test_mapdict.py @@ -5,12 +5,14 @@ space = FakeSpace() class Class(object): -def __init__(self, hasdict=True): +def __init__(self, hasdict=True, sp=None): self.hasdict = True +if sp is None: +sp = space if hasdict: -
[pypy-commit] pypy type-specialized-instances: fix: former PlainAttribute is now AbstractStoredAttribute
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: type-specialized-instances Changeset: r49524:51f420b3af95 Date: 2011-11-16 20:40 +0100 http://bitbucket.org/pypy/pypy/changeset/51f420b3af95/ Log:fix: former PlainAttribute is now AbstractStoredAttribute diff --git a/pypy/objspace/std/mapdict.py b/pypy/objspace/std/mapdict.py --- a/pypy/objspace/std/mapdict.py +++ b/pypy/objspace/std/mapdict.py @@ -99,7 +99,7 @@ return attr def _findmap(self, selector): -while isinstance(self, PlainAttribute): +while isinstance(self, AbstractStoredAttribute): if selector == self.selector: return self self = self.back ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy type-specialized-instances: removed some old prints
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: type-specialized-instances Changeset: r49523:26d4cedd131b Date: 2011-11-16 20:39 +0100 http://bitbucket.org/pypy/pypy/changeset/26d4cedd131b/ Log:removed some old prints diff --git a/pypy/objspace/std/test/test_mapdict.py b/pypy/objspace/std/test/test_mapdict.py --- a/pypy/objspace/std/test/test_mapdict.py +++ b/pypy/objspace/std/test/test_mapdict.py @@ -311,12 +311,12 @@ cls = Class(sp=space) obj1 = cls.instantiate() obj1.setdictvalue(space, x, space.wrap(1)) -#assert space.eq_w(obj1.getdictvalue(space, x), space.wrap(1)) +assert space.eq_w(obj1.getdictvalue(space, x), space.wrap(1)) obj2 = cls.instantiate() w_str = space.wrap(string) obj2.setdictvalue(space, x, w_str) -#assert space.eq_w(obj1.getdictvalue(space, x), w_str) +assert space.eq_w(obj2.getdictvalue(space, x), w_str) assert obj1.map is not obj2.map assert isinstance(obj1.map, IntAttribute) @@ -501,9 +501,7 @@ a.x = 42 assert a.x == 42 -print read once assert a.x == 42 -print read twice def test_simple(self): class A(object): @@ -727,7 +725,6 @@ INVALID_CACHE_ENTRY.failure_counter = 0 # w_res = space.call_function(w_func) -print w_res assert space.eq_w(w_res, space.wrap(42)) # entry = w_code._mapdict_caches[nameindex] @@ -758,14 +755,9 @@ def f(): return a.x # -print 1 assert a.x == 42 -print 2 assert a.x == 42 -print 3 -print first check res = self.check(f, 'x') -print second check assert res == (1, 0, 0) res = self.check(f, 'x') assert res == (0, 1, 0) ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy type-specialized-instances: fixed read with new selector (still not sure if this is the right fix)
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: type-specialized-instances Changeset: r49525:ca3e806187f6 Date: 2011-11-18 14:41 +0100 http://bitbucket.org/pypy/pypy/changeset/ca3e806187f6/ Log:fixed read with new selector (still not sure if this is the right fix) diff --git a/pypy/objspace/std/mapdict.py b/pypy/objspace/std/mapdict.py --- a/pypy/objspace/std/mapdict.py +++ b/pypy/objspace/std/mapdict.py @@ -100,7 +100,8 @@ def _findmap(self, selector): while isinstance(self, AbstractStoredAttribute): -if selector == self.selector: +# XXX is this the right fix? +if selector == self.selector[:2]: return self self = self.back return None @@ -277,11 +278,12 @@ self._size_estimate = self.length() * NUM_DIGITS_POW2 def _copy_attr(self, obj, new_obj): -w_value = self.read(obj, self.selector) +#XXX this the right fix? +w_value = self.read(obj, self.selector[:2]) new_obj._get_mapdict_map().add_attr(new_obj, self.selector, w_value) def delete(self, obj, selector): -if selector == self.selector: +if selector == self.selector[:2]: # ok, attribute is deleted return self.back.copy(obj) new_obj = self.back.delete(obj, selector) ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy type-specialized-instances: started fixing tests to work with new selector
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: type-specialized-instances Changeset: r49526:6f9cd7e5bdeb Date: 2011-11-18 14:42 +0100 http://bitbucket.org/pypy/pypy/changeset/6f9cd7e5bdeb/ Log:started fixing tests to work with new selector diff --git a/pypy/objspace/std/test/test_mapdict.py b/pypy/objspace/std/test/test_mapdict.py --- a/pypy/objspace/std/test/test_mapdict.py +++ b/pypy/objspace/std/test/test_mapdict.py @@ -28,14 +28,14 @@ def erase_storage_items(items): return [IntAttribute.erase_item(item) for item in items] -def unerase_storage_items(storage): -return [IntAttribute.unerase_item(item) for item in storage] +def unerase_storage_items(storage, uneraser=IntAttribute): +return [uneraser.unerase_item(item) for item in storage] def test_plain_attribute(): w_cls = class -aa = PlainAttribute((b, DICT), -PlainAttribute((a, DICT), +aa = IntAttribute((b, DICT, IntAttribute), +IntAttribute((a, DICT, IntAttribute), Terminator(space, w_cls))) assert aa.space is space assert aa.terminator.w_cls is w_cls @@ -115,8 +115,10 @@ obj.setdictvalue(space, a, 50) obj.setdictvalue(space, b, 60) obj.setdictvalue(space, c, 70) +print obj.storage assert unerase_storage_items(obj.storage) == [50, 60, 70] res = obj.deldictvalue(space, dattr) +print obj.storage assert res s = [50, 60, 70] del s[i] @@ -159,7 +161,8 @@ assert obj.getdictvalue(space, a) == 50 assert obj.getdictvalue(space, b) == 60 assert obj.getdictvalue(space, c) == 70 -assert unerase_storage_items(obj.storage) == [50, 60, 70, lifeline1] +assert unerase_storage_items(obj.storage[:-1], IntAttribute) == [50, 60, 70] +assert unerase_storage_items(obj.storage[-1:], PlainAttribute) == [lifeline1] assert obj.getweakref() is lifeline1 obj2 = c.instantiate() @@ -323,6 +326,7 @@ obj3 = cls.instantiate() obj3.setdictvalue(space, x, space.wrap(5)) +assert space.eq_w(obj3.getdictvalue(space, x), space.wrap(5)) assert obj1.map is obj3.map @@ -336,15 +340,27 @@ obj1 = cls.instantiate() obj1.setdictvalue(space, x, space.wrap(1)) obj1.setdictvalue(space, y, space.wrap(2)) +assert space.eq_w(obj1.getdictvalue(space, x), space.wrap(1)) +assert space.eq_w(obj1.getdictvalue(space, y), space.wrap(2)) + +obj2 = cls.instantiate() +obj2.setdictvalue(space, x, space.wrap(5)) # this is shared +obj2.setdictvalue(space, y, space.wrap(str)) # this not +assert space.eq_w(obj2.getdictvalue(space, x), space.wrap(5)) +assert space.eq_w(obj2.getdictvalue(space, y), space.wrap(str)) def test_switch_attribute_types(self): space = self.space cls = Class(sp=space) obj1 = cls.instantiate() + obj1.setdictvalue(space, x, space.wrap(1)) assert isinstance(obj1.map, IntAttribute) +assert space.eq_w(obj1.getdictvalue(space, x), space.wrap(1)) + obj1.setdictvalue(space, y, space.wrap(str)) assert isinstance(obj1.map, PlainAttribute) +assert space.eq_w(obj1.getdictvalue(space, y), space.wrap(str)) # ___ # dict tests ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy type-specialized-instances: removed some old debug prints
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: type-specialized-instances Changeset: r49527:c76cccda3d75 Date: 2011-11-18 14:43 +0100 http://bitbucket.org/pypy/pypy/changeset/c76cccda3d75/ Log:removed some old debug prints diff --git a/pypy/objspace/std/test/test_mapdict.py b/pypy/objspace/std/test/test_mapdict.py --- a/pypy/objspace/std/test/test_mapdict.py +++ b/pypy/objspace/std/test/test_mapdict.py @@ -115,10 +115,8 @@ obj.setdictvalue(space, a, 50) obj.setdictvalue(space, b, 60) obj.setdictvalue(space, c, 70) -print obj.storage assert unerase_storage_items(obj.storage) == [50, 60, 70] res = obj.deldictvalue(space, dattr) -print obj.storage assert res s = [50, 60, 70] del s[i] ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy type-specialized-instances: do not use index anymore to read attributes. in future the attributes manage (un)erasing and (un)wrapping of their values themselves
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: type-specialized-instances Changeset: r49472:9348749a851e Date: 2011-11-15 14:04 +0100 http://bitbucket.org/pypy/pypy/changeset/9348749a851e/ Log:do not use index anymore to read attributes. in future the attributes manage (un)erasing and (un)wrapping of their values themselves diff --git a/pypy/objspace/std/mapdict.py b/pypy/objspace/std/mapdict.py --- a/pypy/objspace/std/mapdict.py +++ b/pypy/objspace/std/mapdict.py @@ -29,42 +29,42 @@ self.terminator = terminator def read(self, obj, selector): -index = self.index(selector) -if index 0: +attr = self.findmap(selector) # index = self.index(selector) +if attr is None: return self.terminator._read_terminator(obj, selector) -return obj._mapdict_read_storage(index) +return attr.read_attr(obj) #obj._mapdict_read_storage(index) def write(self, obj, selector, w_value): -index = self.index(selector) -if index 0: +attr = self.findmap(selector) # index = self.index(selector) +if attr is None: return self.terminator._write_terminator(obj, selector, w_value) -obj._mapdict_write_storage(index, w_value) +attr.write_attr(obj, w_value) #obj._mapdict_write_storage(index, w_value) return True def delete(self, obj, selector): return None -def index(self, selector): +def findmap(self, selector): if jit.we_are_jitted(): # hack for the jit: # the _index method is pure too, but its argument is never # constant, because it is always a new tuple -return self._index_jit_pure(selector[0], selector[1]) +return self._findmap_jit_pure(selector[0], selector[1]) else: -return self._index_indirection(selector) +return self._findmap_indirection(selector) @jit.elidable -def _index_jit_pure(self, name, index): -return self._index_indirection((name, index)) +def _findmap_jit_pure(self, name, index): +return self._findmap_indirection((name, index)) @jit.dont_look_inside -def _index_indirection(self, selector): +def _findmap_indirection(self, selector): if (self.space.config.objspace.std.withmethodcache): -return self._index_cache(selector) -return self._index(selector) +return self._findmap_cache(selector) +return self._findmap(selector) @jit.dont_look_inside -def _index_cache(self, selector): +def _findmap_cache(self, selector): space = self.space cache = space.fromcache(IndexCache) SHIFT2 = r_uint.BITS - space.config.objspace.std.methodcachesizeexp @@ -80,26 +80,31 @@ if cached_attr is self: cached_selector = cache.selectors[index_hash] if cached_selector == selector: -index = cache.indices[index_hash] +attr = cache.cachedattrs[index_hash] if space.config.objspace.std.withmethodcachecounter: name = selector[0] cache.hits[name] = cache.hits.get(name, 0) + 1 -return index -index = self._index(selector) +# XXX return the correct Attribute here +return attr +attr = self._findmap(selector) +if attr is None: +index = -1 +else: +index = attr.position cache.attrs[index_hash] = self cache.selectors[index_hash] = selector -cache.indices[index_hash] = index +cache.cachedattrs[index_hash] = attr if space.config.objspace.std.withmethodcachecounter: name = selector[0] cache.misses[name] = cache.misses.get(name, 0) + 1 -return index +return attr -def _index(self, selector): +def _findmap(self, selector): while isinstance(self, PlainAttribute): if selector == self.selector: -return self.position +return self self = self.back -return -1 +return None def copy(self, obj): raise NotImplementedError(abstract base class) @@ -273,6 +278,14 @@ w_value = self.read(obj, self.selector) new_obj._get_mapdict_map().add_attr(new_obj, self.selector, w_value) +def read_attr(self, obj): +# XXX do the unerasing (and wrapping) here +return obj._mapdict_read_storage(self.position) + +def write_attr(self, obj, w_value): +# XXX do the unerasing (and unwrapping) here +obj._mapdict_write_storage(self.position, w_value) + def delete(self, obj, selector): if selector == self.selector: # ok, attribute is deleted @@ -330,7 +343,7 @@ self.attrs = [None] * SIZE self._empty_selector = (None, INVALID)
[pypy-commit] pypy type-specialized-instances: read attributes only through Attribute class. fixed tests
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: type-specialized-instances Changeset: r49473:c905b06f965f Date: 2011-11-16 14:17 +0100 http://bitbucket.org/pypy/pypy/changeset/c905b06f965f/ Log:read attributes only through Attribute class. fixed tests diff --git a/pypy/objspace/std/mapdict.py b/pypy/objspace/std/mapdict.py --- a/pypy/objspace/std/mapdict.py +++ b/pypy/objspace/std/mapdict.py @@ -84,7 +84,6 @@ if space.config.objspace.std.withmethodcachecounter: name = selector[0] cache.hits[name] = cache.hits.get(name, 0) + 1 -# XXX return the correct Attribute here return attr attr = self._findmap(selector) if attr is None: @@ -160,7 +159,7 @@ # the order is important here: first change the map, then the storage, # for the benefit of the special subclasses obj._set_mapdict_map(attr) -obj._mapdict_write_storage(attr.position, w_value) +attr.write_attr(obj, w_value) #obj._mapdict_write_storage(attr.position, w_value) def materialize_r_dict(self, space, obj, dict_w): raise NotImplementedError(abstract base class) @@ -280,11 +279,14 @@ def read_attr(self, obj): # XXX do the unerasing (and wrapping) here -return obj._mapdict_read_storage(self.position) +erased = obj._mapdict_read_storage(self.position) +w_value = unerase_item(erased) +return w_value def write_attr(self, obj, w_value): # XXX do the unerasing (and unwrapping) here -obj._mapdict_write_storage(self.position, w_value) +erased = erase_item(w_value) +obj._mapdict_write_storage(self.position, erased) def delete(self, obj, selector): if selector == self.selector: @@ -317,7 +319,7 @@ new_obj = self.back.materialize_r_dict(space, obj, dict_w) if self.selector[1] == DICT: w_attr = space.wrap(self.selector[0]) -dict_w[w_attr] = obj._mapdict_read_storage(self.position) +dict_w[w_attr] = self.read_attr(obj) else: self._copy_attr(obj, new_obj) return new_obj @@ -550,20 +552,19 @@ for i in rangenmin1: if index == i: erased = getattr(self, _value%s % i) -return unerase_item(erased) +return erased if self._has_storage_list(): return self._mapdict_get_storage_list()[index - nmin1] erased = getattr(self, _value%s % nmin1) -return unerase_item(erased) +return erased -def _mapdict_write_storage(self, index, value): -erased = erase_item(value) +def _mapdict_write_storage(self, index, erased): for i in rangenmin1: if index == i: setattr(self, _value%s % i, erased) return if self._has_storage_list(): -self._mapdict_get_storage_list()[index - nmin1] = value +self._mapdict_get_storage_list()[index - nmin1] = erased return setattr(self, _value%s % nmin1, erased) @@ -577,21 +578,23 @@ len_storage = len(storage) for i in rangenmin1: if i len_storage: -erased = erase_item(storage[i]) +erased = storage[i] else: +# XXX later: use correct erase method from attribute erased = erase_item(None) setattr(self, _value%s % i, erased) has_storage_list = self._has_storage_list() if len_storage n: assert not has_storage_list +# XXX later: use correct erase method from attribute erased = erase_item(None) elif len_storage == n: assert not has_storage_list -erased = erase_item(storage[nmin1]) +erased = storage[nmin1] elif not has_storage_list: # storage is longer than self.map.length() only due to # overallocation -erased = erase_item(storage[nmin1]) +erased = storage[nmin1] # in theory, we should be ultra-paranoid and check all entries, # but checking just one should catch most problems anyway: assert storage[n] is None @@ -771,14 +774,14 @@ pycode._mapdict_caches = [INVALID_CACHE_ENTRY] * num_entries @jit.dont_look_inside -def _fill_cache(pycode, nameindex, map, version_tag, index, w_method=None): +def _fill_cache(pycode, nameindex, map, version_tag, attr, w_method=None): entry = pycode._mapdict_caches[nameindex] if entry is INVALID_CACHE_ENTRY: entry = CacheEntry() pycode._mapdict_caches[nameindex] = entry
[pypy-commit] pypy set-strategies: First basic implementation of strategies for SetObjects
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49136:46455b9b0a9d Date: 2011-04-30 11:35 +0200 http://bitbucket.org/pypy/pypy/changeset/46455b9b0a9d/ Log:First basic implementation of strategies for SetObjects diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -8,6 +8,19 @@ from pypy.interpreter.function import Defaults from pypy.objspace.std.settype import set_typedef as settypedef from pypy.objspace.std.frozensettype import frozenset_typedef as frozensettypedef +from pypy.rlib import rerased +from pypy.rlib.objectmodel import instantiate + +def get_strategy_from_setdata(space, setdata): +from pypy.objspace.std.intobject import W_IntObject + +keys_w = setdata.keys() +for item_w in setdata.keys(): +if type(item_w) is not W_IntObject: +break; +if item_w is keys_w[-1]: +return space.fromcache(IntegerSetStrategy) +return space.fromcache(ObjectSetStrategy) class W_BaseSetObject(W_Object): typedef = None @@ -21,18 +34,19 @@ return True return False - def __init__(w_self, space, setdata): Initialize the set by taking ownership of 'setdata'. assert setdata is not None -w_self.setdata = setdata +w_self.strategy = get_strategy_from_setdata(space, setdata) +w_self.strategy.init_from_setdata(w_self, setdata) def __repr__(w_self): representation for debugging purposes -reprlist = [repr(w_item) for w_item in w_self.setdata.keys()] +reprlist = [repr(w_item) for w_item in w_self.getkeys()] return %s(%s) % (w_self.__class__.__name__, ', '.join(reprlist)) def _newobj(w_self, space, rdict_w=None): +print _newobj Make a new set or frozenset by taking ownership of 'rdict_w'. #return space.call(space.type(w_self),W_SetIterObject(rdict_w)) objtype = type(w_self) @@ -51,6 +65,38 @@ def setweakref(self, space, weakreflifeline): self._lifeline_ = weakreflifeline +# _ strategy methods + +def clear(self): +self.strategy.clear(self) + +def copy(self): +return self.strategy.copy(self) + +def length(self): +return self.strategy.length(self) + +def add(self, w_key): +self.strategy.add(self, w_key) + +def getkeys(self): +return self.strategy.getkeys(self) + +def intersect(self, w_other): +return self.strategy.intersect(self, w_other) + +def intersect_multiple(self, others_w): +return self.strategy.intersect_multiple(self, others_w) + +def update(self, w_other): +self.strategy.update(self, w_other) + +def has_key(self, w_key): +return self.strategy.has_key(self, w_key) + +def equals(self, w_other): +return self.strategy.equals(self, w_other) + class W_SetObject(W_BaseSetObject): from pypy.objspace.std.settype import set_typedef as typedef @@ -62,6 +108,151 @@ registerimplementation(W_SetObject) registerimplementation(W_FrozensetObject) +class SetStrategy(object): +def __init__(self, space): +self.space = space + +def init_from_setdata(self, w_set, setdata): +raise NotImplementedError + +def init_from_w_iterable(self, w_set, setdata): +raise NotImplementedError + +def length(self, w_set): +raise NotImplementedError + +class AbstractUnwrappedSetStrategy(object): +__mixin__ = True + +def init_from_setdata(self, w_set, setdata): +#XXX this copies again (see: make_setdata_from_w_iterable) +#XXX cannot store int into r_dict +d = newset(self.space) +for item_w in setdata.keys(): +d[self.unwrap(item_w)] = None +w_set.sstorage = self.cast_to_void_star(d) + +def init_from_w_iterable(self, w_set, w_iterable=None): +keys = self.make_setdata_from_w_iterable(w_iterable) +w_set.sstorage = self.cast_to_void_star(keys) + +def make_setdata_from_w_iterable(self, w_iterable): +Return a new r_dict with the content of w_iterable. +if isinstance(w_iterable, W_BaseSetObject): +return self.cast_from_void_star(w_set.sstorage).copy() +data = newset(self.space) +if w_iterable is not None: +for w_item in self.space.listview(w_iterable): +data[self.unwrap(w_item)] = None +return data + +def length(self, w_set): +return len(self.cast_from_void_star(w_set.sstorage)) + +def clear(self, w_set): +self.cast_from_void_star(w_set.sstorage).clear() + +def copy(self, w_set): +print w_set +d = self.cast_from_void_star(w_set.sstorage).copy() +print d +#XXX make it faster by using from_storage_and_strategy +clone = instantiate(type(w_set)) +print
[pypy-commit] pypy set-strategies: All tests for setobject are working (but there is still untested code)
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49137:34fd0e9fa474 Date: 2011-05-01 16:19 +0200 http://bitbucket.org/pypy/pypy/changeset/34fd0e9fa474/ Log:All tests for setobject are working (but there is still untested code) diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -11,15 +11,25 @@ from pypy.rlib import rerased from pypy.rlib.objectmodel import instantiate -def get_strategy_from_setdata(space, setdata): +def get_strategy_from_w_iterable(space, w_iterable=None): from pypy.objspace.std.intobject import W_IntObject +#XXX what types for w_iterable are possible -keys_w = setdata.keys() -for item_w in setdata.keys(): +if isinstance(w_iterable, W_BaseSetObject): +return w_iterable.strategy + +if w_iterable is None: +#XXX becomes EmptySetStrategy later +return space.fromcache(ObjectSetStrategy) + +if not isinstance(w_iterable, list): +w_iterable = space.listview(w_iterable) +for item_w in w_iterable: if type(item_w) is not W_IntObject: break; -if item_w is keys_w[-1]: +if item_w is w_iterable[-1]: return space.fromcache(IntegerSetStrategy) + return space.fromcache(ObjectSetStrategy) class W_BaseSetObject(W_Object): @@ -37,8 +47,9 @@ def __init__(w_self, space, setdata): Initialize the set by taking ownership of 'setdata'. assert setdata is not None -w_self.strategy = get_strategy_from_setdata(space, setdata) -w_self.strategy.init_from_setdata(w_self, setdata) +w_self.space = space #XXX less memory without this indirection? +w_self.strategy = get_strategy_from_w_iterable(space, setdata.keys()) +w_self.strategy.init_from_setdata_w(w_self, setdata) def __repr__(w_self): representation for debugging purposes @@ -46,7 +57,6 @@ return %s(%s) % (w_self.__class__.__name__, ', '.join(reprlist)) def _newobj(w_self, space, rdict_w=None): -print _newobj Make a new set or frozenset by taking ownership of 'rdict_w'. #return space.call(space.type(w_self),W_SetIterObject(rdict_w)) objtype = type(w_self) @@ -62,9 +72,15 @@ _lifeline_ = None def getweakref(self): return self._lifeline_ + def setweakref(self, space, weakreflifeline): self._lifeline_ = weakreflifeline +def switch_to_object_strategy(self, space): +d = self.strategy.getdict_w(self) +self.strategy = space.fromcache(ObjectSetStrategy) +self.sstorage = self.strategy.cast_to_void_star(d) + # _ strategy methods def clear(self): @@ -79,15 +95,39 @@ def add(self, w_key): self.strategy.add(self, w_key) +def discard(self, w_item): +return self.strategy.discard(self, w_item) + +def delitem(self, w_item): +return self.strategy.delitem(self, w_item) + +def getdict_w(self): +return self.strategy.getdict_w(self) + def getkeys(self): return self.strategy.getkeys(self) +def difference(self, w_other): +return self.strategy.difference(self, w_other) + +def difference_update(self, w_other): +return self.strategy.difference_update(self, w_other) + def intersect(self, w_other): return self.strategy.intersect(self, w_other) def intersect_multiple(self, others_w): return self.strategy.intersect_multiple(self, others_w) +def intersect_multiple_update(self, others_w): +self.strategy.intersect_multiple_update(self, others_w) + +def issubset(self, w_other): +return self.strategy.issubset(self, w_other) + +def isdisjoint(self, w_other): +return self.strategy.isdisjoint(self, w_other) + def update(self, w_other): self.strategy.update(self, w_other) @@ -112,9 +152,6 @@ def __init__(self, space): self.space = space -def init_from_setdata(self, w_set, setdata): -raise NotImplementedError - def init_from_w_iterable(self, w_set, setdata): raise NotImplementedError @@ -124,23 +161,24 @@ class AbstractUnwrappedSetStrategy(object): __mixin__ = True -def init_from_setdata(self, w_set, setdata): -#XXX this copies again (see: make_setdata_from_w_iterable) -#XXX cannot store int into r_dict -d = newset(self.space) -for item_w in setdata.keys(): +def get_empty_storage(self): +raise NotImplementedError + +def init_from_w_iterable(self, w_set, w_iterable): +setdata = self.make_setdata_from_w_iterable(w_iterable) +w_set.sstorage = self.cast_to_void_star(setdata) + +def init_from_setdata_w(self, w_set, setdata_w): +d = self.get_empty_dict() +for item_w in setdata_w.keys():
[pypy-commit] pypy set-strategies: Cleaned up setobject.py
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49138:4f1baf0b12d1 Date: 2011-05-01 16:53 +0200 http://bitbucket.org/pypy/pypy/changeset/4f1baf0b12d1/ Log:Cleaned up setobject.py diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -234,6 +234,7 @@ return False except OperationError, e: #XXX is this ever tested? +assert False if not e.match(space, space.w_TypeError): raise return False @@ -464,38 +465,11 @@ w_frozen.strategy = w_obj.strategy w_frozen.sstorage = w_obj.sstorage return w_frozen -return W_FrozensetObject(space, - make_setdata_from_w_iterable(space, w_obj)) else: return None # helper functions for set operation on dicts -def _difference_dict(space, ld, rd): -result = newset(space) -for w_key in ld: -if w_key not in rd: -result[w_key] = None -return result - -def _difference_dict_update(space, ld, rd): -if ld is rd: -ld.clear() # for the case 'a.difference_update(a)' -else: -for w_key in rd: -try: -del ld[w_key] -except KeyError: -pass - -def _isdisjoint_dict(ld, rd): -if len(ld) len(rd): -ld, rd = rd, ld # loop over the smaller dict -for w_key in ld: -if w_key in rd: -return False -return True - def _symmetric_difference_dict(space, ld, rd): result = newset(space) for w_key in ld: @@ -568,11 +542,6 @@ result = w_left for w_other in others_w: result = result.difference(w_other) - #if isinstance(w_other, W_BaseSetObject): - #rd = w_other.setdata # optimization only - #else: - #rd = make_setdata_from_w_iterable(space, w_other) - #result = _difference_dict(space, result, rd) return result frozenset_difference__Frozenset = set_difference__Set @@ -583,7 +552,6 @@ if isinstance(w_other, W_BaseSetObject): # optimization only w_left.difference_update(w_other) -#_difference_dict_update(space, ld, w_other.setdata) else: for w_key in space.listview(w_other): try: @@ -624,7 +592,6 @@ def ne__Set_Set(space, w_left, w_other): return space.wrap(not w_left.equals(w_other)) -return space.wrap(not _is_eq(w_left.setdata, w_other.setdata)) ne__Set_Frozenset = ne__Set_Set ne__Frozenset_Frozenset = ne__Set_Set @@ -662,9 +629,6 @@ if space.is_w(w_left, w_other): return space.w_True return space.wrap(w_left.issubset(w_other)) - -ld, rd = w_left.setdata, w_other.setdata -return space.wrap(_issubset_dict(ld, rd)) set_issubset__Set_Frozenset = set_issubset__Set_Set frozenset_issubset__Frozenset_Set = set_issubset__Set_Set @@ -691,8 +655,6 @@ return space.w_True return space.wrap(w_other.issubset(w_left)) -ld, rd = w_left.setdata, w_other.setdata -return space.wrap(_issubset_dict(rd, ld)) set_issuperset__Set_Frozenset = set_issuperset__Set_Set set_issuperset__Frozenset_Set = set_issuperset__Set_Set @@ -815,21 +777,6 @@ def _intersection_multiple(space, w_left, others_w): return w_left.intersect_multiple(others_w) -result = w_left.setdata -for w_other in others_w: -if isinstance(w_other, W_BaseSetObject): -# optimization only -#XXX test this -assert False -result = _intersection_dict(space, result, w_other.setdata) -else: -result2 = newset(space) -for w_key in space.listview(w_other): -if w_key in result: -result2[w_key] = None -result = result2 -return result - def set_intersection__Set(space, w_left, others_w): if len(others_w) == 0: return w_left.copy() @@ -841,8 +788,6 @@ def set_intersection_update__Set(space, w_left, others_w): w_left.intersect_multiple_update(others_w) return -result = _intersection_multiple(space, w_left, others_w) -w_left.setdata = result def inplace_and__Set_Set(space, w_left, w_other): ld, rd = w_left.setdata, w_other.setdata @@ -855,9 +800,6 @@ def set_isdisjoint__Set_Set(space, w_left, w_other): # optimization only (the general case works too) return space.newbool(w_left.isdisjoint(w_other)) -ld, rd = w_left.setdata, w_other.setdata -disjoint = _isdisjoint_dict(ld, rd) -return space.newbool(disjoint) set_isdisjoint__Set_Frozenset = set_isdisjoint__Set_Set set_isdisjoint__Frozenset_Frozenset = set_isdisjoint__Set_Set ___ pypy-commit mailing list pypy-commit@python.org
[pypy-commit] pypy set-strategies: test and fix for W_SetObject.pop()
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49142:8ade98db780b Date: 2011-05-01 17:35 +0200 http://bitbucket.org/pypy/pypy/changeset/8ade98db780b/ Log:test and fix for W_SetObject.pop() diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -208,7 +208,7 @@ w_set.add(w_key) def delitem(self, w_set, w_item): -# only used internally +# not a normal set operation; only used internally d = self.cast_from_void_star(w_set.sstorage) try: del d[self.unwrap(w_item)] @@ -729,12 +729,14 @@ return space.wrap(hash) def set_pop__Set(space, w_left): -for w_key in w_left.setdata: +#XXX move this to strategy so we don't have to +#wrap all items only to get the first one +for w_key in w_left.getkeys(): break else: raise OperationError(space.w_KeyError, space.wrap('pop from an empty set')) -del w_left.setdata[w_key] +w_left.delitem(w_key) return w_key def and__Set_Set(space, w_left, w_other): diff --git a/pypy/objspace/std/test/test_setobject.py b/pypy/objspace/std/test/test_setobject.py --- a/pypy/objspace/std/test/test_setobject.py +++ b/pypy/objspace/std/test/test_setobject.py @@ -88,6 +88,13 @@ raises(KeyError, a.remove(6)) +def test_pop(self): +a = set([1,2,3,4,5]) +for i in xrange(5): +a.pop() +assert a == set() +raises(KeyError, a.pop()) + def test_subtype(self): class subset(set):pass a = subset() ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: added test and fix for issubset and issuperset
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49144:b22d4b425150 Date: 2011-05-02 13:27 +0200 http://bitbucket.org/pypy/pypy/changeset/b22d4b425150/ Log:added test and fix for issubset and issuperset diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -322,6 +322,10 @@ w_set.sstorage = result.sstorage def issubset(self, w_set, w_other): +if not isinstance(w_other, W_BaseSetObject): +setdata = make_setdata_from_w_iterable(self.space, w_other) +w_other = w_set._newobj(self.space, setdata) + if w_set.length() w_other.length(): return False @@ -572,7 +576,7 @@ eq__Frozenset_Set = eq__Set_Set def eq__Set_settypedef(space, w_left, w_other): -#XXX what is faster: wrapping w_left or creating set from w_other +#XXX dont know how to test this rd = make_setdata_from_w_iterable(space, w_other) return space.wrap(_is_eq(w_left.setdata, rd)) @@ -635,8 +639,7 @@ if space.is_w(w_left, w_other): return space.w_True -ld, rd = w_left.setdata, make_setdata_from_w_iterable(space, w_other) -return space.wrap(_issubset_dict(ld, rd)) +return space.wrap(w_left.issubset(w_other)) frozenset_issubset__Frozenset_ANY = set_issubset__Set_ANY @@ -661,8 +664,11 @@ if space.is_w(w_left, w_other): return space.w_True -ld, rd = w_left.setdata, make_setdata_from_w_iterable(space, w_other) -return space.wrap(_issubset_dict(rd, ld)) +#XXX BAD +setdata = make_setdata_from_w_iterable(space, w_other) +w_other = w_left._newobj(space, setdata) + +return space.wrap(w_other.issubset(w_left)) frozenset_issuperset__Frozenset_ANY = set_issuperset__Set_ANY diff --git a/pypy/objspace/std/test/test_setobject.py b/pypy/objspace/std/test/test_setobject.py --- a/pypy/objspace/std/test/test_setobject.py +++ b/pypy/objspace/std/test/test_setobject.py @@ -85,6 +85,20 @@ a -= b assert a == set([2,3]) +def test_issubset(self): +a = set([1,2,3,4]) +b = set([2,3]) +assert b.issubset(a) +c = [1,2,3,4] +assert b.issubset(c) + +def test_issuperset(self): +a = set([1,2,3,4]) +b = set([2,3]) +assert a.issuperset(b) +c = [2,3] +assert a.issuperset(c) + def test_discard_remove(self): a = set([1,2,3,4,5]) a.remove(1) ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: added fix and tests for clear and __sub__
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49140:d6824feeab55 Date: 2011-05-01 17:15 +0200 http://bitbucket.org/pypy/pypy/changeset/d6824feeab55/ Log:added fix and tests for clear and __sub__ diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -524,12 +524,10 @@ return set_copy__Set(space, w_left) def set_clear__Set(space, w_left): -w_left.setdata.clear() +w_left.clear() def sub__Set_Set(space, w_left, w_other): -ld, rd = w_left.setdata, w_other.setdata -new_ld = _difference_dict(space, ld, rd) -return w_left._newobj(space, new_ld) +return w_left.difference(w_other) sub__Set_Frozenset = sub__Set_Set sub__Frozenset_Set = sub__Set_Set diff --git a/pypy/objspace/std/test/test_setobject.py b/pypy/objspace/std/test/test_setobject.py --- a/pypy/objspace/std/test/test_setobject.py +++ b/pypy/objspace/std/test/test_setobject.py @@ -68,6 +68,17 @@ a |= set([1,2,3]) assert a == b +def test_clear(self): +a = set([1,2,3]) +a.clear() +assert a == set() + +def test_sub(self): +a = set([1,2,3,4,5]) +b = set([2,3,4]) +a - b == [1,5] +a.__sub__(b) == [1,5] + def test_subtype(self): class subset(set):pass a = subset() ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: added test and fix for set(generator)
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49148:ffa5d9dadcfe Date: 2011-05-11 11:19 +0200 http://bitbucket.org/pypy/pypy/changeset/ffa5d9dadcfe/ Log:added test and fix for set(generator) diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -10,6 +10,8 @@ from pypy.objspace.std.frozensettype import frozenset_typedef as frozensettypedef from pypy.rlib import rerased from pypy.rlib.objectmodel import instantiate +from pypy.interpreter.generator import GeneratorIterator +from pypy.objspace.std.listobject import W_ListObject def get_strategy_from_w_iterable(space, w_iterable=None): from pypy.objspace.std.intobject import W_IntObject @@ -510,6 +512,8 @@ def _initialize_set(space, w_obj, w_iterable=None): w_obj.clear() if w_iterable is not None: +if isinstance(w_iterable, GeneratorIterator): +w_iterable = W_ListObject(space.listview(w_iterable)) w_obj.strategy = get_strategy_from_w_iterable(space, w_iterable) w_obj.strategy.init_from_w_iterable(w_obj, w_iterable) diff --git a/pypy/objspace/std/test/test_setobject.py b/pypy/objspace/std/test/test_setobject.py --- a/pypy/objspace/std/test/test_setobject.py +++ b/pypy/objspace/std/test/test_setobject.py @@ -59,6 +59,16 @@ c = a.union(b) assert c == set([1,2,3,4]) +def test_generator(self): +def foo(): +for i in [1,2,3,4,5]: +yield i +b = set(foo()) +assert b == set([1,2,3,4,5]) + +a = set(x for x in [1,2,3]) +assert a == set([1,2,3]) + def test_or(self): a = set([0,1,2]) b = a | set([1,2,3]) ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: another test for discard; cleaned up discard code
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49141:1e7b0dec4883 Date: 2011-05-01 17:25 +0200 http://bitbucket.org/pypy/pypy/changeset/1e7b0dec4883/ Log:another test for discard; cleaned up discard code diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -208,6 +208,7 @@ w_set.add(w_key) def delitem(self, w_set, w_item): +# only used internally d = self.cast_from_void_star(w_set.sstorage) try: del d[self.unwrap(w_item)] @@ -702,28 +703,6 @@ x = w_left.discard(w_item) return x -try: -del w_left.setdata[w_item] -return True -except KeyError: -return False -except OperationError, e: -if not e.match(space, space.w_TypeError): -raise -w_f = _convert_set_to_frozenset(space, w_item) -if w_f is None: -raise - -try: -del w_left.setdata[w_f] -return True -except KeyError: -return False -except OperationError, e: -if not e.match(space, space.w_TypeError): -raise -return False - def set_discard__Set_ANY(space, w_left, w_item): _discard_from_set(space, w_left, w_item) diff --git a/pypy/objspace/std/test/test_setobject.py b/pypy/objspace/std/test/test_setobject.py --- a/pypy/objspace/std/test/test_setobject.py +++ b/pypy/objspace/std/test/test_setobject.py @@ -79,6 +79,15 @@ a - b == [1,5] a.__sub__(b) == [1,5] +def test_discard_remove(self): +a = set([1,2,3,4,5]) +a.remove(1) +assert a == set([2,3,4,5]) +a.discard(2) +assert a == set([3,4,5]) + +raises(KeyError, a.remove(6)) + def test_subtype(self): class subset(set):pass a = subset() ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: refactored initialisation of W_SetObject
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49149:d926be3f2432 Date: 2011-05-11 13:33 +0200 http://bitbucket.org/pypy/pypy/changeset/d926be3f2432/ Log:refactored initialisation of W_SetObject diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -14,6 +14,7 @@ from pypy.objspace.std.listobject import W_ListObject def get_strategy_from_w_iterable(space, w_iterable=None): +assert False from pypy.objspace.std.intobject import W_IntObject #XXX what types for w_iterable are possible @@ -50,8 +51,10 @@ Initialize the set by taking ownership of 'setdata'. assert setdata is not None w_self.space = space #XXX less memory without this indirection? -w_self.strategy = get_strategy_from_w_iterable(space, setdata.keys()) -w_self.strategy.init_from_setdata_w(w_self, setdata) +#XXX in case of ObjectStrategy we can reuse the setdata object +set_strategy_and_setdata(space, w_self, setdata.keys()) +#w_self.strategy = get_strategy_from_w_iterable(space, setdata.keys()) +#w_self.strategy.init_from_setdata_w(w_self, setdata) def __repr__(w_self): representation for debugging purposes @@ -185,6 +188,12 @@ d[self.unwrap(item_w)] = None w_set.sstorage = self.cast_to_void_star(d) +def get_storage_from_list(self, list_w): +setdata = self.get_empty_dict() +for w_item in list_w: +setdata[self.unwrap(w_item)] = None +return self.cast_to_void_star(setdata) + def make_setdata_from_w_iterable(self, w_iterable): Return a new r_dict with the content of w_iterable. if isinstance(w_iterable, W_BaseSetObject): @@ -437,6 +446,9 @@ cast_to_void_star = staticmethod(cast_to_void_star) cast_from_void_star = staticmethod(cast_from_void_star) +def get_empty_storage(self): +return self.cast_to_void_star(newset(self.space)) + def get_empty_dict(self): return newset(self.space) @@ -497,6 +509,34 @@ def newset(space): return r_dict(space.eq_w, space.hash_w) +def set_strategy_and_setdata(space, w_set, w_iterable): +from pypy.objspace.std.intobject import W_IntObject + +if w_iterable is None: +w_set.strategy = space.fromcache(ObjectSetStrategy) #XXX EmptySetStrategy +w_set.sstorage = w_set.strategy.get_empty_storage() +return + +if isinstance(w_iterable, W_BaseSetObject): +w_set.strategy = w_iterable.strategy +w_set.sstorage = w_iterable.sstorage +return + +if not isinstance(w_iterable, list): +w_iterable = space.listview(w_iterable) + +# check for integers +for item_w in w_iterable: +if type(item_w) is not W_IntObject: +break; +if item_w is w_iterable[:-1]: +w_set.strategy = space.fromcache(IntegerSetStrategy) +w_set.sstorage = w_set.strategy.get_storage_from_list(w_iterable) +return + +w_set.strategy = space.fromcache(ObjectSetStrategy) +w_set.sstorage = w_set.strategy.get_storage_from_list(w_iterable) + def make_setdata_from_w_iterable(space, w_iterable=None): #XXX remove this later Return a new r_dict with the content of w_iterable. @@ -511,6 +551,8 @@ def _initialize_set(space, w_obj, w_iterable=None): w_obj.clear() +set_strategy_and_setdata(space, w_obj, w_iterable) +return if w_iterable is not None: if isinstance(w_iterable, GeneratorIterator): w_iterable = W_ListObject(space.listview(w_iterable)) ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: replaced more make_setdata_from_w_iterbale by _newobj() and set_strategy_from_w_iterable()
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49151:fd3571e19e87 Date: 2011-05-11 16:29 +0200 http://bitbucket.org/pypy/pypy/changeset/fd3571e19e87/ Log:replaced more make_setdata_from_w_iterbale by _newobj() and set_strategy_from_w_iterable() diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -53,8 +53,6 @@ w_self.space = space #XXX less memory without this indirection? #XXX in case of ObjectStrategy we can reuse the setdata object set_strategy_and_setdata(space, w_self, setdata.keys()) -#w_self.strategy = get_strategy_from_w_iterable(space, setdata.keys()) -#w_self.strategy.init_from_setdata_w(w_self, setdata) def __repr__(w_self): representation for debugging purposes @@ -169,9 +167,6 @@ def __init__(self, space): self.space = space -def init_from_w_iterable(self, w_set, setdata): -raise NotImplementedError - def length(self, w_set): raise NotImplementedError @@ -181,10 +176,6 @@ def get_empty_storage(self): raise NotImplementedError -def init_from_w_iterable(self, w_set, w_iterable): -setdata = self.make_setdata_from_w_iterable(w_iterable) -w_set.sstorage = self.cast_to_void_star(setdata) - def init_from_setdata_w(self, w_set, setdata_w): d = self.get_empty_dict() for item_w in setdata_w.keys(): @@ -197,16 +188,6 @@ setdata[self.unwrap(w_item)] = None return self.cast_to_void_star(setdata) -def make_setdata_from_w_iterable(self, w_iterable): -Return a new r_dict with the content of w_iterable. -if isinstance(w_iterable, W_BaseSetObject): -return self.cast_from_void_star(w_set.sstorage).copy() -data = self.get_empty_dict() -if w_iterable is not None: -for w_item in self.space.listview(w_iterable): -data[self.unwrap(w_item)] = None -return data - def length(self, w_set): return len(self.cast_from_void_star(w_set.sstorage)) @@ -291,9 +272,10 @@ def difference(self, w_set, w_other): result = w_set._newobj(self.space, newset(self.space)) if not isinstance(w_other, W_BaseSetObject): -#XXX this is bad -setdata = make_setdata_from_w_iterable(self.space, w_other) -w_other = w_set._newobj(self.space, setdata) +w_temp = w_set._newobj(self.space, newset(self.space)) +set_strategy_and_setdata(self.space, w_temp, w_other) +w_other = w_temp +# lookup is faster when w_other is set for w_key in w_set.getkeys(): if not w_other.has_key(w_key): result.add(w_key) @@ -549,12 +531,6 @@ def _initialize_set(space, w_obj, w_iterable=None): w_obj.clear() set_strategy_and_setdata(space, w_obj, w_iterable) -return -if w_iterable is not None: -if isinstance(w_iterable, GeneratorIterator): -w_iterable = W_ListObject(space.listview(w_iterable)) -w_obj.strategy = get_strategy_from_w_iterable(space, w_iterable) -w_obj.strategy.init_from_w_iterable(w_obj, w_iterable) def _convert_set_to_frozenset(space, w_obj): #XXX can be optimized @@ -671,8 +647,8 @@ def eq__Set_settypedef(space, w_left, w_other): # tested in test_buildinshortcut.py #XXX do not make new setobject here -setdata = make_setdata_from_w_iterable(space, w_other) -w_other_as_set = w_left._newobj(space, setdata) +w_other_as_set = w_left._newobj(space, newset(space)) +set_strategy_and_setdata(space, w_other_as_set, w_other) return space.wrap(w_left.equals(w_other)) eq__Set_frozensettypedef = eq__Set_settypedef @@ -694,6 +670,7 @@ ne__Frozenset_Set = ne__Set_Set def ne__Set_settypedef(space, w_left, w_other): +#XXX this is not tested rd = make_setdata_from_w_iterable(space, w_other) return space.wrap(_is_eq(w_left.setdata, rd)) @@ -900,10 +877,10 @@ def set_symmetric_difference__Set_ANY(space, w_left, w_other): -#XXX deal with iterables withouth turning them into sets -setdata = make_setdata_from_w_iterable(space, w_other) -w_other_as_set = w_left._newobj(space, setdata) - +#XXX since we need to iterate over both objects, create set +#from w_other so looking up items is fast +w_other_as_set = w_left._newobj(space, newset(space)) +set_strategy_and_setdata(space, w_other_as_set, w_other) w_result = w_left.symmetric_difference(w_other_as_set) return w_result @@ -919,8 +896,8 @@ def set_symmetric_difference_update__Set_ANY(space, w_left, w_other): #XXX deal with iterables withouth turning them into sets -setdata = make_setdata_from_w_iterable(space, w_other) -w_other_as_set = w_left._newobj(space, setdata) +
[pypy-commit] pypy set-strategies: added from_storage_and_strategy function
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49152:1ae8d50ae922 Date: 2011-05-11 18:00 +0200 http://bitbucket.org/pypy/pypy/changeset/1ae8d50ae922/ Log:added from_storage_and_strategy function diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -59,6 +59,20 @@ reprlist = [repr(w_item) for w_item in w_self.getkeys()] return %s(%s) % (w_self.__class__.__name__, ', '.join(reprlist)) +def from_storage_and_strategy(w_self, storage, strategy): +objtype = type(w_self) +if objtype is W_SetObject: +obj = instantiate(W_SetObject) +elif objtype is W_FrozensetObject: +obj = instantiate(W_FrozensetObject) +else: +itemiterator = w_self.space.iter(W_SetIterObject(newset(w_self.space))) +obj = w_self.space.call_function(w_self.space.type(w_self),itemiterator) +obj.space = w_self.space +obj.strategy = strategy +obj.sstorage = storage +return obj + def _newobj(w_self, space, rdict_w=None): Make a new set or frozenset by taking ownership of 'rdict_w'. #return space.call(space.type(w_self),W_SetIterObject(rdict_w)) @@ -197,10 +211,9 @@ def copy(self, w_set): #XXX do not copy FrozenDict d = self.cast_from_void_star(w_set.sstorage) -#XXX make it faster by using from_storage_and_strategy -clone = w_set._newobj(self.space, newset(self.space)) -clone.strategy = w_set.strategy -clone.sstorage = self.cast_to_void_star(d.copy()) +strategy = w_set.strategy +storage = self.cast_to_void_star(d.copy()) +clone = w_set.from_storage_and_strategy(storage, strategy) return clone def add(self, w_set, w_key): ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: added test and fix for inplace_and
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49145:bf74909839b4 Date: 2011-05-02 13:43 +0200 http://bitbucket.org/pypy/pypy/changeset/bf74909839b4/ Log:added test and fix for inplace_and diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -116,6 +116,9 @@ def intersect(self, w_other): return self.strategy.intersect(self, w_other) +def intersect_update(self, w_other): +return self.strategy.intersect_update(self, w_other) + def intersect_multiple(self, others_w): return self.strategy.intersect_multiple(self, others_w) @@ -299,6 +302,22 @@ result.add(w_key) return result +def intersect_update(self, w_set, w_other): +if w_set.length() w_other.length(): +return w_other.intersect(w_set) + +setdata = newset(self.space) +items = self.cast_from_void_star(w_set.sstorage).keys() +for key in items: +w_key = self.wrap(key) +if w_other.has_key(w_key): +setdata[w_key] = None + +# do not switch strategy here if other items match +w_set.strategy = strategy = self.space.fromcache(ObjectSetStrategy) +w_set.sstorage = strategy.cast_to_void_star(setdata) +return w_set + def intersect_multiple(self, w_set, others_w): result = w_set for w_other in others_w: @@ -747,11 +766,6 @@ def and__Set_Set(space, w_left, w_other): new_set = w_left.intersect(w_other) return new_set -ld, rd = w_left.setdata, w_other.setdata -new_ld = _intersection_dict(space, ld, rd) -#XXX when both have same strategy, ini new set from storage -# therefore this must be moved to strategies -return w_left._newobj(space, new_ld) and__Set_Frozenset = and__Set_Set and__Frozenset_Set = and__Set_Set @@ -773,10 +787,7 @@ return def inplace_and__Set_Set(space, w_left, w_other): -ld, rd = w_left.setdata, w_other.setdata -new_ld = _intersection_dict(space, ld, rd) -w_left.setdata = new_ld -return w_left +return w_left.intersect_update(w_other) inplace_and__Set_Frozenset = inplace_and__Set_Set diff --git a/pypy/objspace/std/test/test_setobject.py b/pypy/objspace/std/test/test_setobject.py --- a/pypy/objspace/std/test/test_setobject.py +++ b/pypy/objspace/std/test/test_setobject.py @@ -99,6 +99,12 @@ c = [2,3] assert a.issuperset(c) +def test_inplace_and(test): +a = set([1,2,3,4]) +b = set([0,2,3,5,6]) +a = b +assert a == set([2,3]) + def test_discard_remove(self): a = set([1,2,3,4,5]) a.remove(1) ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: added fixes and tests for symmetric_difference[_update]
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49146:28ab4895a815 Date: 2011-05-10 11:59 +0200 http://bitbucket.org/pypy/pypy/changeset/28ab4895a815/ Log:added fixes and tests for symmetric_difference[_update] diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -113,6 +113,12 @@ def difference_update(self, w_other): return self.strategy.difference_update(self, w_other) +def symmetric_difference(self, w_other): +return self.strategy.symmetric_difference(self, w_other) + +def symmetric_difference_update(self, w_other): +return self.strategy.symmetric_difference_update(self, w_other) + def intersect(self, w_other): return self.strategy.intersect(self, w_other) @@ -289,6 +295,31 @@ except KeyError: pass +def symmetric_difference(self, w_set, w_other): +#XXX no wrapping when strategies are equal +result = w_set._newobj(self.space, newset(self.space)) +for w_key in w_set.getkeys(): +if not w_other.has_key(w_key): +result.add(w_key) +for w_key in w_other.getkeys(): +if not w_set.has_key(w_key): +result.add(w_key) +return result + +def symmetric_difference_update(self, w_set, w_other): +#XXX no wrapping when strategies are equal +newsetdata = newset(self.space) +for w_key in w_set.getkeys(): +if not w_other.has_key(w_key): +newsetdata[w_key] = None +for w_key in w_other.getkeys(): +if not w_set.has_key(w_key): +newsetdata[w_key] = None + +# do not switch strategy here if other items match +w_set.strategy = strategy = self.space.fromcache(ObjectSetStrategy) +w_set.sstorage = strategy.cast_to_void_star(newsetdata) + def intersect(self, w_set, w_other): if w_set.length() w_other.length(): return w_other.intersect(w_set) @@ -811,9 +842,8 @@ def set_symmetric_difference__Set_Set(space, w_left, w_other): # optimization only (the general case works too) -ld, rd = w_left.setdata, w_other.setdata -new_ld = _symmetric_difference_dict(space, ld, rd) -return w_left._newobj(space, new_ld) +w_result = w_left.symmetric_difference(w_other) +return w_result set_symmetric_difference__Set_Frozenset = set_symmetric_difference__Set_Set set_symmetric_difference__Frozenset_Set = set_symmetric_difference__Set_Set @@ -827,26 +857,28 @@ def set_symmetric_difference__Set_ANY(space, w_left, w_other): -ld, rd = w_left.setdata, make_setdata_from_w_iterable(space, w_other) -new_ld = _symmetric_difference_dict(space, ld, rd) -return w_left._newobj(space, new_ld) +#XXX deal with iterables withouth turning them into sets +setdata = make_setdata_from_w_iterable(space, w_other) +w_other_as_set = w_left._newobj(space, setdata) + +w_result = w_left.symmetric_difference(w_other_as_set) +return w_result frozenset_symmetric_difference__Frozenset_ANY = \ set_symmetric_difference__Set_ANY def set_symmetric_difference_update__Set_Set(space, w_left, w_other): # optimization only (the general case works too) -ld, rd = w_left.setdata, w_other.setdata -new_ld = _symmetric_difference_dict(space, ld, rd) -w_left.setdata = new_ld +w_left.symmetric_difference_update(w_other) set_symmetric_difference_update__Set_Frozenset = \ set_symmetric_difference_update__Set_Set def set_symmetric_difference_update__Set_ANY(space, w_left, w_other): -ld, rd = w_left.setdata, make_setdata_from_w_iterable(space, w_other) -new_ld = _symmetric_difference_dict(space, ld, rd) -w_left.setdata = new_ld +#XXX deal with iterables withouth turning them into sets +setdata = make_setdata_from_w_iterable(space, w_other) +w_other_as_set = w_left._newobj(space, setdata) +w_left.symmetric_difference_update(w_other_as_set) def inplace_xor__Set_Set(space, w_left, w_other): set_symmetric_difference_update__Set_Set(space, w_left, w_other) diff --git a/pypy/objspace/std/test/test_setobject.py b/pypy/objspace/std/test/test_setobject.py --- a/pypy/objspace/std/test/test_setobject.py +++ b/pypy/objspace/std/test/test_setobject.py @@ -121,6 +121,33 @@ assert a == set() raises(KeyError, a.pop()) +def test_symmetric_difference(self): +a = set([1,2,3]) +b = set([3,4,5]) +c = a.symmetric_difference(b) +assert c == set([1,2,4,5]) + +a = set([1,2,3]) +b = [3,4,5] +c = a.symmetric_difference(b) +assert c == set([1,2,4,5]) + +def test_symmetric_difference_update(self): +a = set([1,2,3]) +b = set([3,4,5]) +
[pypy-commit] pypy set-strategies: fixed eq__Set_settypedef
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49147:60ddcb62aeca Date: 2011-05-10 13:41 +0200 http://bitbucket.org/pypy/pypy/changeset/60ddcb62aeca/ Log:fixed eq__Set_settypedef diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -626,9 +626,11 @@ eq__Frozenset_Set = eq__Set_Set def eq__Set_settypedef(space, w_left, w_other): -#XXX dont know how to test this -rd = make_setdata_from_w_iterable(space, w_other) -return space.wrap(_is_eq(w_left.setdata, rd)) +# tested in test_buildinshortcut.py +#XXX do not make new setobject here +setdata = make_setdata_from_w_iterable(space, w_other) +w_other_as_set = w_left._newobj(space, setdata) +return space.wrap(w_left.equals(w_other)) eq__Set_frozensettypedef = eq__Set_settypedef eq__Frozenset_settypedef = eq__Set_settypedef ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: added intelligent way to treat the different strategies in W_SetObject.difference
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49163:a9c59d68f3ac Date: 2011-05-18 15:42 +0200 http://bitbucket.org/pypy/pypy/changeset/a9c59d68f3ac/ Log:added intelligent way to treat the different strategies in W_SetObject.difference diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -391,34 +391,50 @@ return True def difference(self, w_set, w_other): -#XXX return clone if other is Empty -result = w_set._newobj(self.space, None) if not isinstance(w_other, W_BaseSetObject): w_other = w_set._newobj(self.space, w_other) -# lookup is faster when w_other is set -for w_key in w_set.getkeys(): -if not w_other.has_key(w_key): -result.add(w_key) + +if w_other.strategy is self.space.fromcache(ObjectSetStrategy): +return self.difference_wrapped(w_set, w_other) + +if w_set.strategy is not w_other.strategy: +return w_set.copy() + +return self.difference_unwrapped(w_set, w_other) + +def difference_wrapped(self, w_set, w_other): +result = w_set._newobj(self.space, None) +w_iter = self.space.iter(w_set) +while True: +try: +w_item = self.space.next(w_iter) +if not w_other.has_key(w_key): +result.add(w_key) +except OperationError, e: +if not e.match(self.space, self.space.w_StopIteration): +raise +return +return result + +def difference_unwrapped(self, w_set, w_other): +if not isinstance(w_other, W_BaseSetObject): +w_other = w_set._newobj(self.space, w_other) +iterator = self.cast_from_void_star(w_set.sstorage).iterkeys() +other_dict = self.cast_from_void_star(w_other.sstorage) +result_dict = self.get_empty_dict() +for key in iterator: +if key not in other_dict: +result_dict[key] = None +result = w_set._newobj(self.space, None) +result.strategy = self +result.sstorage = self.cast_to_void_star(result_dict) return result def difference_update(self, w_set, w_other): -if w_other.strategy is EmptySetStrategy: -return -if w_set is w_other: -w_set.clear() # for the case 'a.difference_update(a)' -else: -w_iter = self.space.iter(w_other) -while True: -try: -w_item = self.space.next(w_iter) -try: -self.delitem(w_set, w_item) -except KeyError: -pass -except OperationError, e: -if not e.match(self.space, self.space.w_StopIteration): -raise -return +#XXX this way we unnecessarily create a new set +result = self.difference(w_set, w_other) +w_set.strategy = result.strategy +w_set.sstorage = result.sstorage def symmetric_difference(self, w_set, w_other): #XXX no wrapping when strategies are equal ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: fixed bug in issuperset, more tests, some optimization
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49155:14b4c0d3850a Date: 2011-05-13 15:42 +0200 http://bitbucket.org/pypy/pypy/changeset/14b4c0d3850a/ Log:fixed bug in issuperset, more tests, some optimization diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -478,12 +478,15 @@ w_set.sstorage = result.sstorage def issuperset(self, w_set, w_other): -#XXX other is empty is always True -if w_set.length() self.space.unwrap(self.space.len(w_other)): -return False -for w_key in self.space.unpackiterable(w_other): -if not w_set.has_key(w_key): -return False +#XXX always True if other is empty +w_iter = self.space.iter(w_other) +while True: +try: +w_item = self.space.next(w_iter) +if not w_set.has_key(w_item): +return False +except OperationError: +return True return True def isdisjoint(self, w_set, w_other): @@ -818,6 +821,8 @@ # optimization only (the general case works too) if space.is_w(w_left, w_other): return space.w_True +if w_left.length() w_other.length(): +return space.w_False return space.wrap(w_other.issuperset(w_left)) set_issubset__Set_Frozenset = set_issubset__Set_Set @@ -829,6 +834,9 @@ return space.w_True w_other_as_set = w_left._newobj(space, w_other) + +if w_left.length() w_other_as_set.length(): +return space.w_False return space.wrap(w_other_as_set.issuperset(w_left)) frozenset_issubset__Frozenset_ANY = set_issubset__Set_ANY @@ -842,6 +850,8 @@ # optimization only (the general case works too) if space.is_w(w_left, w_other): return space.w_True +if w_left.length() w_other.length(): +return space.w_False return space.wrap(w_left.issuperset(w_other)) set_issuperset__Set_Frozenset = set_issuperset__Set_Set diff --git a/pypy/objspace/std/test/test_setobject.py b/pypy/objspace/std/test/test_setobject.py --- a/pypy/objspace/std/test/test_setobject.py +++ b/pypy/objspace/std/test/test_setobject.py @@ -115,6 +115,10 @@ c = [2,3] assert a.issuperset(c) +c = [1,1,1,1,1] +assert a.issuperset(c) +assert set([1,1,1,1,1]).issubset(a) + def test_inplace_and(test): a = set([1,2,3,4]) b = set([0,2,3,5,6]) @@ -518,3 +522,10 @@ assert e.isdisjoint(e) == True assert e.isdisjoint(x) == True assert x.isdisjoint(e) == True + + +def test_super_with_generator(self): +def foo(): +for i in [1,2,3]: +yield i +set([1,2,3,4,5]).issuperset(foo()) ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: another way of creating a frozen set
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49166:35fb3d7fec2a Date: 2011-05-18 17:28 +0200 http://bitbucket.org/pypy/pypy/changeset/35fb3d7fec2a/ Log:another way of creating a frozen set diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -713,7 +713,7 @@ def _convert_set_to_frozenset(space, w_obj): #XXX can be optimized if space.is_true(space.isinstance(w_obj, space.w_set)): -w_frozen = instantiate(W_FrozensetObject) +w_frozen = W_FrozensetObject(space, None) w_frozen.strategy = w_obj.strategy w_frozen.sstorage = w_obj.sstorage return w_frozen ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: fixed EmptySetStrategy.issuperset
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49156:64ffc4b0905b Date: 2011-05-13 15:51 +0200 http://bitbucket.org/pypy/pypy/changeset/64ffc4b0905b/ Log:fixed EmptySetStrategy.issuperset diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -261,7 +261,9 @@ return True def issuperset(self, w_set, w_other): -if self.space.unwrap(self.space.len(w_other)) == 0: +if isinstance(w_other, W_BaseSetObject) and w_other.strategy is EmptySetStrategy: +return True +elif len(self.space.unpackiterable(w_other)) == 0: return True return False diff --git a/pypy/objspace/std/test/test_setobject.py b/pypy/objspace/std/test/test_setobject.py --- a/pypy/objspace/std/test/test_setobject.py +++ b/pypy/objspace/std/test/test_setobject.py @@ -509,12 +509,16 @@ assert e.issuperset(x) == False assert x.issuperset(e) == True +assert e.issuperset(set()) +assert e.issuperset([]) + def test_empty_issubset(self): e = set() x = set([1,2,3]) assert e.issubset(e) == True assert e.issubset(x) == True assert x.issubset(e) == False +assert e.issubset([]) def test_empty_isdisjoint(self): e = set() ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: be sure that w_obj is setobject
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49167:3f70c38813f3 Date: 2011-05-18 17:43 +0200 http://bitbucket.org/pypy/pypy/changeset/3f70c38813f3/ Log:be sure that w_obj is setobject diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -713,6 +713,8 @@ def _convert_set_to_frozenset(space, w_obj): #XXX can be optimized if space.is_true(space.isinstance(w_obj, space.w_set)): +assert isinstance(w_obj, W_SetObject) +#XXX better instantiate? w_frozen = W_FrozensetObject(space, None) w_frozen.strategy = w_obj.strategy w_frozen.sstorage = w_obj.sstorage ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: added test for user generated subclass of setobject
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49168:d23ca90396d5 Date: 2011-05-18 18:24 +0200 http://bitbucket.org/pypy/pypy/changeset/d23ca90396d5/ Log:added test for user generated subclass of setobject diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -65,8 +65,7 @@ elif objtype is W_FrozensetObject: obj = instantiate(W_FrozensetObject) else: -itemiterator = w_self.space.iter(W_SetIterObject(newset(w_self.space))) -obj = w_self.space.call_function(w_self.space.type(w_self),itemiterator) +obj = w_self.space.call_function(w_self.space.type(w_self), None) obj.space = w_self.space obj.strategy = strategy obj.sstorage = storage @@ -81,8 +80,7 @@ elif objtype is W_FrozensetObject: obj = W_FrozensetObject(space, w_iterable) else: -itemiterator = space.iter(W_SetIterObject(w_iterable)) -obj = space.call_function(space.type(w_self), itemiterator) +obj = space.call_function(space.type(w_self), w_iterable) return obj _lifeline_ = None diff --git a/pypy/objspace/std/test/test_setobject.py b/pypy/objspace/std/test/test_setobject.py --- a/pypy/objspace/std/test/test_setobject.py +++ b/pypy/objspace/std/test/test_setobject.py @@ -75,6 +75,16 @@ a = set(x for x in [1,2,3]) assert a == set([1,2,3]) +def test_generator2(self): +def foo(): +for i in [1,2,3]: +yield i +class A(set): +pass +a = A([1,2,3,4,5]) +b = a.difference(foo()) +assert b == set([4,5]) + def test_or(self): a = set([0,1,2]) b = a | set([1,2,3]) ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: some more optimization
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49157:1ca516864d70 Date: 2011-05-13 17:29 +0200 http://bitbucket.org/pypy/pypy/changeset/1ca516864d70/ Log:some more optimization diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -393,15 +393,23 @@ return result def difference_update(self, w_set, w_other): -#XXX do nothing if other is empty +if w_other.strategy is EmptySetStrategy: +return if w_set is w_other: w_set.clear() # for the case 'a.difference_update(a)' else: -for w_key in w_other.getkeys(): +w_iter = self.space.iter(w_other) +while True: try: -self.delitem(w_set, w_key) -except KeyError: -pass +w_item = self.space.next(w_iter) +try: +self.delitem(w_set, w_item) +except KeyError: +pass +except OperationError, e: +if not e.match(self.space, self.space.w_StopIteration): +raise +return def symmetric_difference(self, w_set, w_other): #XXX no wrapping when strategies are equal @@ -487,12 +495,15 @@ w_item = self.space.next(w_iter) if not w_set.has_key(w_item): return False -except OperationError: +except OperationError, e: +if not e.match(self.space, self.space.w_StopIteration): +raise return True return True def isdisjoint(self, w_set, w_other): -#XXX always True if other is empty +if w_other.length() == 0: +return True if w_set.length() w_other.length(): return w_other.isdisjoint(w_set) ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: space not necessary here?
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49170:d711192077c7 Date: 2011-05-20 15:18 +0200 http://bitbucket.org/pypy/pypy/changeset/d711192077c7/ Log:space not necessary here? diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -66,7 +66,6 @@ obj = instantiate(W_FrozensetObject) else: obj = w_self.space.call_function(w_self.space.type(w_self), None) -obj.space = w_self.space obj.strategy = strategy obj.sstorage = storage return obj ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: added different method for symmetric_difference_update when strategies match
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49158:e4d6683b7917 Date: 2011-05-17 13:39 +0200 http://bitbucket.org/pypy/pypy/changeset/e4d6683b7917/ Log:added different method for symmetric_difference_update when strategies match diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -422,7 +422,23 @@ result.add(w_key) return result +def symmetric_difference_update_match(self, w_set, w_other): +d_new = self.get_empty_dict() +d_this = self.cast_from_void_star(w_set.sstorage) +d_other = self.cast_from_void_star(w_other.sstorage) +for key in d_other.keys(): +if not key in d_this: +d_new[key] = None +for key in d_this.keys(): +if not key in d_other: +d_new[key] = None + +w_set.sstorage = self.cast_to_void_star(d_new) + def symmetric_difference_update(self, w_set, w_other): +if w_set.strategy is w_other.strategy: +self.symmetric_difference_update_match(w_set, w_other) +return #XXX no wrapping when strategies are equal newsetdata = newset(self.space) for w_key in w_set.getkeys(): ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: this is the same but hopefully it will satisfy the annotator
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49171:10a1be5db44b Date: 2011-05-20 15:39 +0200 http://bitbucket.org/pypy/pypy/changeset/10a1be5db44b/ Log:this is the same but hopefully it will satisfy the annotator diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -663,7 +663,7 @@ if w_iterable is None : w_set.strategy = space.fromcache(EmptySetStrategy) -w_set.sstorage = w_set.strategy.cast_to_void_star(None)#w_set.strategy.get_empty_storage() +w_set.sstorage = w_set.strategy.get_empty_storage() return if isinstance(w_iterable, W_BaseSetObject): @@ -677,7 +677,7 @@ if len(w_iterable) == 0: w_set.strategy = space.fromcache(EmptySetStrategy) -w_set.sstorage = w_set.strategy.cast_to_void_star(None) +w_set.sstorage = w_set.strategy.get_empty_storage() return # check for integers ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: fixed bug in determination of strategy
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49159:39f1615703a2 Date: 2011-05-17 13:40 +0200 http://bitbucket.org/pypy/pypy/changeset/39f1615703a2/ Log:fixed bug in determination of strategy diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -658,7 +658,7 @@ for item_w in w_iterable: if type(item_w) is not W_IntObject: break; -if item_w is w_iterable[:-1]: +if item_w is w_iterable[-1]: w_set.strategy = space.fromcache(IntegerSetStrategy) w_set.sstorage = w_set.strategy.get_storage_from_list(w_iterable) return ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: EmptySet.add() switches to correct strategy now
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49161:28e84214560e Date: 2011-05-17 13:46 +0200 http://bitbucket.org/pypy/pypy/changeset/28e84214560e/ Log:EmptySet.add() switches to correct strategy now diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -212,8 +212,13 @@ return clone def add(self, w_set, w_key): -#XXX switch to correct strategy later -w_set.switch_to_object_strategy(self.space) +from pypy.objspace.std.intobject import W_IntObject +if type(w_key) is W_IntObject: +w_set.strategy = self.space.fromcache(IntegerSetStrategy) +else: +w_set.strategy = self.space.fromcache(ObjectSetStrategy) + +w_set.sstorage = w_set.strategy.get_empty_storage() w_set.add(w_key) def delitem(self, w_set, w_item): @@ -551,6 +556,9 @@ cast_to_void_star = staticmethod(cast_to_void_star) cast_from_void_star = staticmethod(cast_from_void_star) +def get_empty_storage(self): +return self.cast_to_void_star({}) + def get_empty_dict(self): return {} ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: tell annotator that this obj must be a set
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49173:a0feb9250ca0 Date: 2011-05-20 16:09 +0200 http://bitbucket.org/pypy/pypy/changeset/a0feb9250ca0/ Log:tell annotator that this obj must be a set diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -66,6 +66,7 @@ obj = instantiate(W_FrozensetObject) else: obj = w_self.space.call_function(w_self.space.type(w_self), None) +assert isinstance(obj, W_BaseSetObject) obj.strategy = strategy obj.sstorage = storage return obj ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: fixed ne__Set_settypedef
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49164:7cfd17778080 Date: 2011-05-18 16:54 +0200 http://bitbucket.org/pypy/pypy/changeset/7cfd17778080/ Log:fixed ne__Set_settypedef diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -848,8 +848,8 @@ def ne__Set_settypedef(space, w_left, w_other): #XXX this is not tested -rd = make_setdata_from_w_iterable(space, w_other) -return space.wrap(_is_eq(w_left.setdata, rd)) +w_other_as_set = w_left._newobj(space, w_other) +return space.wrap(w_left.equals(w_other)) ne__Set_frozensettypedef = ne__Set_settypedef ne__Frozenset_settypedef = ne__Set_settypedef ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: was not rpython
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49175:ca26985e470d Date: 2011-05-24 11:17 +0200 http://bitbucket.org/pypy/pypy/changeset/ca26985e470d/ Log:was not rpython diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -292,12 +292,6 @@ def get_empty_storage(self): raise NotImplementedError -def init_from_setdata_w(self, w_set, setdata_w): -d = self.get_empty_dict() -for item_w in setdata_w.keys(): -d[self.unwrap(item_w)] = None -w_set.sstorage = self.cast_to_void_star(d) - def get_storage_from_list(self, list_w): setdata = self.get_empty_dict() for w_item in list_w: @@ -377,8 +371,11 @@ return keys_w def has_key(self, w_set, w_key): -dict_w = self.cast_from_void_star(w_set.sstorage) -return self.unwrap(w_key) in dict_w +if not self.is_correct_type(w_key): +#XXX switch object strategy, test +return False +d = self.cast_from_void_star(w_set.sstorage) +return self.unwrap(w_key) in d def equals(self, w_set, w_other): if w_set.length() != w_other.length(): @@ -587,7 +584,7 @@ return type(w_key) is W_IntObject def unwrap(self, w_item): -return self.space.unwrap(w_item) +return self.space.int_w(w_item) def wrap(self, item): return self.space.wrap(item) ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: fixed _mixin_
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49165:190fda089ccf Date: 2011-05-18 17:15 +0200 http://bitbucket.org/pypy/pypy/changeset/190fda089ccf/ Log:fixed _mixin_ diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -288,7 +288,7 @@ w_set.update(w_other) class AbstractUnwrappedSetStrategy(object): -__mixin__ = True +_mixin_ = True def get_empty_storage(self): raise NotImplementedError ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: fix and tests for fakeints in instrategy
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49177:1e8aabff9f2a Date: 2011-05-24 15:24 +0200 http://bitbucket.org/pypy/pypy/changeset/1e8aabff9f2a/ Log:fix and tests for fakeints in instrategy diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -329,6 +329,7 @@ raise def discard(self, w_set, w_item): +from pypy.objspace.std.dictmultiobject import _is_sane_hash d = self.cast_from_void_star(w_set.sstorage) try: del d[self.unwrap(w_item)] @@ -336,13 +337,28 @@ except KeyError: return False except OperationError, e: +# raise any error except TypeError if not e.match(self.space, self.space.w_TypeError): raise +# if error is TypeError and w_item is not None, Int, String, Bool or Float +# (i.e. FakeObject) switch to object strategy and discard again +if (not _is_sane_hash(self.space, w_item) and +self is not self.space.fromcache(ObjectSetStrategy)): +w_set.switch_to_object_strategy(self.space) +return w_set.discard(w_item) +# else we have two cases: +# - w_item is as set: then we convert it to frozenset and check again +# - type doesn't match (string in intstrategy): then we raise (cause w_f is none) w_f = _convert_set_to_frozenset(self.space, w_item) if w_f is None: raise + +# if w_item is a set and we are not in ObjectSetStrategy we are finished here +if not self.space.fromcache(ObjectSetStrategy): +return False + try: -del d[w_f] +del d[w_f] # XXX nonsense in intstrategy return True except KeyError: return False @@ -595,7 +611,7 @@ cast_from_void_star = staticmethod(cast_from_void_star) def get_empty_storage(self): -return self.cast_to_void_star(newset(self.space)) +return self.cast_to_void_star(self.get_empty_dict()) def get_empty_dict(self): return newset(self.space) diff --git a/pypy/objspace/std/test/test_setobject.py b/pypy/objspace/std/test/test_setobject.py --- a/pypy/objspace/std/test/test_setobject.py +++ b/pypy/objspace/std/test/test_setobject.py @@ -545,3 +545,36 @@ for i in [1,2,3]: yield i set([1,2,3,4,5]).issuperset(foo()) + + +def test_fakeint_intstrategy(self): +class FakeInt(object): +def __init__(self, value): +self.value = value +def __hash__(self): +return hash(self.value) + +def __eq__(self, other): +if other == self.value: +return True +return False + +f1 = FakeInt(4) +assert f1 == 4 +assert hash(f1) == hash(4) + +# test with object strategy +s = set([1, 2, 'three', 'four']) +s.discard(FakeInt(2)) +assert s == set([1, 'three', 'four']) +s.remove(FakeInt(1)) +assert s == set(['three', 'four']) +raises(KeyError, s.remove, FakeInt(16)) + +# test with int strategy +s = set([1,2,3,4]) +s.discard(FakeInt(4)) +assert s == set([1,2,3]) +s.remove(FakeInt(3)) +assert s == set([1,2]) +raises(KeyError, s.remove, FakeInt(16)) ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: not needed anymore
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49183:0a8c1ba28319 Date: 2011-05-27 14:41 +0200 http://bitbucket.org/pypy/pypy/changeset/0a8c1ba28319/ Log:not needed anymore diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -13,28 +13,6 @@ from pypy.interpreter.generator import GeneratorIterator from pypy.objspace.std.listobject import W_ListObject -def get_strategy_from_w_iterable(space, w_iterable=None): -assert False -from pypy.objspace.std.intobject import W_IntObject -#XXX what types for w_iterable are possible - -if isinstance(w_iterable, W_BaseSetObject): -return w_iterable.strategy - -if w_iterable is None: -#XXX becomes EmptySetStrategy later -return space.fromcache(ObjectSetStrategy) - -if not isinstance(w_iterable, list): -w_iterable = space.listview(w_iterable) -for item_w in w_iterable: -if type(item_w) is not W_IntObject: -break; -if item_w is w_iterable[-1]: -return space.fromcache(IntegerSetStrategy) - -return space.fromcache(ObjectSetStrategy) - class W_BaseSetObject(W_Object): typedef = None ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: fix and test for fakeobject in has_key
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49178:7bc2b4077184 Date: 2011-05-24 15:56 +0200 http://bitbucket.org/pypy/pypy/changeset/7bc2b4077184/ Log:fix and test for fakeobject in has_key diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -387,8 +387,11 @@ return keys_w def has_key(self, w_set, w_key): +from pypy.objspace.std.dictmultiobject import _is_sane_hash if not self.is_correct_type(w_key): -#XXX switch object strategy, test +if not _is_sane_hash(self.space, w_key): +w_set.switch_to_object_strategy(self.space) +return w_set.has_key(w_key) return False d = self.cast_from_void_star(w_set.sstorage) return self.unwrap(w_key) in d diff --git a/pypy/objspace/std/test/test_setobject.py b/pypy/objspace/std/test/test_setobject.py --- a/pypy/objspace/std/test/test_setobject.py +++ b/pypy/objspace/std/test/test_setobject.py @@ -578,3 +578,20 @@ s.remove(FakeInt(3)) assert s == set([1,2]) raises(KeyError, s.remove, FakeInt(16)) + + +def test_fakeobject_and_has_key(test): +class FakeInt(object): +def __init__(self, value): +self.value = value +def __hash__(self): +return hash(self.value) + +def __eq__(self, other): +if other == self.value: +return True +return False + +s = set([1,2,3,4,5]) +assert 5 in s +assert FakeInt(5) in s ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: Altough the if-part will never be executed in IntegerSetStrategy, the annotator doesn't know what type d is. It could be an int-dict and then d[w_key], where w_key i
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49181:95966fc24e8c Date: 2011-05-27 11:53 +0200 http://bitbucket.org/pypy/pypy/changeset/95966fc24e8c/ Log:Altough the if-part will never be executed in IntegerSetStrategy, the annotator doesn't know what type d is. It could be an int-dict and then d[w_key], where w_key is always a wrapped object because of the getkeys()-method, would degenerate this object to an integer. diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -539,18 +539,19 @@ return True def update(self, w_set, w_other): -d = self.cast_from_void_star(w_set.sstorage) if w_set.strategy is self.space.fromcache(ObjectSetStrategy): +d_obj = self.cast_from_void_star(w_set.sstorage) other_w = w_other.getkeys() -#XXX better solution!? for w_key in other_w: -d[w_key] = None +d_obj[w_key] = None return elif w_set.strategy is w_other.strategy: +d_int = self.cast_from_void_star(w_set.sstorage) other = self.cast_from_void_star(w_other.sstorage) -d.update(other) +d_int.update(other) return + w_set.switch_to_object_strategy(self.space) w_set.update(w_other) ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: fix in EmptySetStrategy.issuperset
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49184:13f5685e273c Date: 2011-05-27 14:51 +0200 http://bitbucket.org/pypy/pypy/changeset/13f5685e273c/ Log:fix in EmptySetStrategy.issuperset diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -247,7 +247,8 @@ return True def issuperset(self, w_set, w_other): -if isinstance(w_other, W_BaseSetObject) and w_other.strategy is EmptySetStrategy: +if (isinstance(w_other, W_BaseSetObject) and +w_other.strategy is self.space.fromcache(EmptySetStrategy)): return True elif len(self.space.unpackiterable(w_other)) == 0: return True ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: obviuosly d_obj still could be an int-dict
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49182:d8f16ee35e9b Date: 2011-05-27 14:27 +0200 http://bitbucket.org/pypy/pypy/changeset/d8f16ee35e9b/ Log:obviuosly d_obj still could be an int-dict diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -543,7 +543,7 @@ d_obj = self.cast_from_void_star(w_set.sstorage) other_w = w_other.getkeys() for w_key in other_w: -d_obj[w_key] = None +d_obj[self.unwrap(w_key)] = None return elif w_set.strategy is w_other.strategy: ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: implemented new iteratorimplementation (similar to dictmultiobject)
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49185:6e5ed22d0735 Date: 2011-06-08 11:28 +0200 http://bitbucket.org/pypy/pypy/changeset/6e5ed22d0735/ Log:implemented new iteratorimplementation (similar to dictmultiobject) diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -149,6 +149,9 @@ def equals(self, w_other): return self.strategy.equals(self, w_other) +def iter(self): +return self.strategy.iter(self) + class W_SetObject(W_BaseSetObject): from pypy.objspace.std.settype import set_typedef as typedef @@ -265,6 +268,9 @@ w_set.switch_to_object_strategy(self.space) w_set.update(w_other) +def iter(self, w_set): +return EmptyIteratorImplementation(self.space, w_set) + class AbstractUnwrappedSetStrategy(object): _mixin_ = True @@ -555,6 +561,9 @@ def wrap(self, item): return self.space.wrap(item) +def iter(self, w_set): +return IntegerIteratorImplementation(self.space, self, w_set) + class ObjectSetStrategy(AbstractUnwrappedSetStrategy, SetStrategy): cast_to_void_star, cast_from_void_star = rerased.new_erasing_pair(object) cast_to_void_star = staticmethod(cast_to_void_star) @@ -575,20 +584,79 @@ def wrap(self, item): return item +def iter(self, w_set): +return RDictIteratorImplementation(self.space, self, w_set) + +class IteratorImplementation(object): +def __init__(self, space, implementation): +self.space = space +self.dictimplementation = implementation +self.len = implementation.length() +self.pos = 0 + +def next(self): +if self.dictimplementation is None: +return None, None +if self.len != self.dictimplementation.length(): +self.len = -1 # Make this error state sticky +raise OperationError(self.space.w_RuntimeError, + self.space.wrap(dictionary changed size during iteration)) +# look for the next entry +if self.pos self.len: +result = self.next_entry() +self.pos += 1 +return result +# no more entries +self.dictimplementation = None +return None, None + +def next_entry(self): + Purely abstract method + +raise NotImplementedError + +def length(self): +if self.dictimplementation is not None: +return self.len - self.pos +return 0 + +class EmptyIteratorImplementation(IteratorImplementation): +def next(self): +return (None, None) + +class IntegerIteratorImplementation(IteratorImplementation): +#XXX same implementation in dictmultiobject on dictstrategy-branch +def __init__(self, space, strategy, dictimplementation): +IteratorImplementation.__init__(self, space, dictimplementation) +d = strategy.cast_from_void_star(dictimplementation.sstorage) +self.iterator = d.iteritems() + +def next_entry(self): +# note that this 'for' loop only runs once, at most +for w_key, w_value in self.iterator: +return self.space.wrap(w_key), w_value +else: +return None, None + +class RDictIteratorImplementation(IteratorImplementation): +def __init__(self, space, strategy, dictimplementation): +IteratorImplementation.__init__(self, space, dictimplementation) +d = strategy.cast_from_void_star(dictimplementation.sstorage) +self.iterator = d.iteritems() + +def next_entry(self): +# note that this 'for' loop only runs once, at most +for item in self.iterator: +return item +else: +return None, None + class W_SetIterObject(W_Object): from pypy.objspace.std.settype import setiter_typedef as typedef -def __init__(w_self, setdata): -w_self.content = content = setdata -w_self.len = len(content) -w_self.pos = 0 -w_self.iterator = iter(w_self.content) - -def next_entry(w_self): -for w_key in w_self.iterator: -return w_key -else: -return None +def __init__(w_self, space, iterimplementation): +w_self.space = space +w_self.iterimplementation = iterimplementation registerimplementation(W_SetIterObject) @@ -596,19 +664,10 @@ return w_setiter def next__SetIterObject(space, w_setiter): -content = w_setiter.content -if content is not None: -if w_setiter.len != len(content): -w_setiter.len = -1 # Make this error state sticky -raise OperationError(space.w_RuntimeError, - space.wrap(Set changed size during iteration)) -# look for the next entry -w_result = w_setiter.next_entry() -if w_result is not None: -w_setiter.pos += 1 -
[pypy-commit] pypy set-strategies: _newobj moved to W_SetObject and W_FrozenSetObject
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49190:167cc1b5687a Date: 2011-07-19 14:06 +0200 http://bitbucket.org/pypy/pypy/changeset/167cc1b5687a/ Log:_newobj moved to W_SetObject and W_FrozenSetObject diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -48,19 +48,6 @@ obj.sstorage = storage return obj -def _newobj(w_self, space, w_iterable): -Make a new set or frozenset by taking ownership of 'rdict_w'. -#return space.call(space.type(w_self),W_SetIterObject(rdict_w)) -objtype = type(w_self) -if objtype is W_SetObject: -obj = W_SetObject(space, w_iterable) -elif objtype is W_FrozensetObject: -obj = W_FrozensetObject(space, w_iterable) -else: -obj = space.call_function(space.type(w_self), w_iterable) -assert isinstance(obj, W_BaseSetObject) -return obj - _lifeline_ = None def getweakref(self): return self._lifeline_ ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: argument must be None to create a new empty set
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49188:67d070d04ba6 Date: 2011-07-19 14:02 +0200 http://bitbucket.org/pypy/pypy/changeset/67d070d04ba6/ Log:argument must be None to create a new empty set diff --git a/pypy/objspace/std/objspace.py b/pypy/objspace/std/objspace.py --- a/pypy/objspace/std/objspace.py +++ b/pypy/objspace/std/objspace.py @@ -310,7 +310,7 @@ def newset(self): from pypy.objspace.std.setobject import newset -return W_SetObject(self, newset(self)) +return W_SetObject(self, None) def newslice(self, w_start, w_end, w_step): return W_SliceObject(w_start, w_end, w_step) ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: FakeInt is needed for this test class but setup_class is overwritten
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49192:142e4c1b492d Date: 2011-07-22 16:15 +0200 http://bitbucket.org/pypy/pypy/changeset/142e4c1b492d/ Log:FakeInt is needed for this test class but setup_class is overwritten diff --git a/pypy/objspace/std/test/test_builtinshortcut.py b/pypy/objspace/std/test/test_builtinshortcut.py --- a/pypy/objspace/std/test/test_builtinshortcut.py +++ b/pypy/objspace/std/test/test_builtinshortcut.py @@ -85,6 +85,20 @@ def setup_class(cls): from pypy import conftest cls.space = conftest.gettestobjspace(**WITH_BUILTINSHORTCUT) +w_fakeint = cls.space.appexec([], (): +class FakeInt(object): +def __init__(self, value): +self.value = value +def __hash__(self): +return hash(self.value) + +def __eq__(self, other): +if other == self.value: +return True +return False +return FakeInt +) +cls.w_FakeInt = w_fakeint class AppTestString(test_stringobject.AppTestStringObject): def setup_class(cls): ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: need to use StopItertion to check for last element in list
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49195:fc1ddf33f169 Date: 2011-07-28 13:31 +0200 http://bitbucket.org/pypy/pypy/changeset/fc1ddf33f169/ Log:need to use StopItertion to check for last element in list diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -11,6 +11,7 @@ from pypy.rlib.objectmodel import instantiate from pypy.interpreter.generator import GeneratorIterator from pypy.objspace.std.listobject import W_ListObject +from pypy.objspace.std.intobject import W_IntObject class W_BaseSetObject(W_Object): typedef = None @@ -208,7 +209,6 @@ return clone def add(self, w_set, w_key): -from pypy.objspace.std.intobject import W_IntObject if type(w_key) is W_IntObject: w_set.strategy = self.space.fromcache(IntegerSetStrategy) else: @@ -722,11 +722,13 @@ return # check for integers -for item_w in w_iterable: -if type(item_w) is not W_IntObject: -break; -#XXX wont work for [1, two, three, 1] use StopIteration instead -if item_w is w_iterable[-1]: +iterator = iter(w_iterable) +while True: +try: +item_w = iterator.next() +if type(item_w) is not W_IntObject: +break; +except StopIteration: w_set.strategy = space.fromcache(IntegerSetStrategy) w_set.sstorage = w_set.strategy.get_storage_from_list(w_iterable) return ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: implemented popitem on W_SetObject
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49196:8a7f58f9e061 Date: 2011-07-28 14:04 +0200 http://bitbucket.org/pypy/pypy/changeset/8a7f58f9e061/ Log:implemented popitem on W_SetObject diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -136,6 +136,9 @@ def iter(self): return self.strategy.iter(self) +def popitem(self): +return self.strategy.popitem(self) + class W_SetObject(W_BaseSetObject): from pypy.objspace.std.settype import set_typedef as typedef @@ -288,6 +291,10 @@ def iter(self, w_set): return EmptyIteratorImplementation(self.space, w_set) +def popitem(self, w_set): +raise OperationError(self.space.w_KeyError, +self.space.wrap('pop from an empty set')) + class AbstractUnwrappedSetStrategy(object): _mixin_ = True @@ -557,6 +564,16 @@ w_set.switch_to_object_strategy(self.space) w_set.update(w_other) +def popitem(self, w_set): +storage = self.cast_from_void_star(w_set.sstorage) +try: +result = storage.popitem() +except KeyError: +# strategy may still be the same even if dict is empty +raise OperationError(self.space.w_KeyError, +self.space.wrap('pop from an empty set')) +return self.wrap(result) + class IntegerSetStrategy(AbstractUnwrappedSetStrategy, SetStrategy): cast_to_void_star, cast_from_void_star = rerased.new_erasing_pair(integer) cast_to_void_star = staticmethod(cast_to_void_star) @@ -1030,6 +1047,7 @@ #XXX move this to strategy so we don't have to #wrap all items only to get the first one #XXX use popitem +return w_left.popitem() for w_key in w_left.getkeys(): break else: diff --git a/pypy/objspace/std/test/test_setobject.py b/pypy/objspace/std/test/test_setobject.py --- a/pypy/objspace/std/test/test_setobject.py +++ b/pypy/objspace/std/test/test_setobject.py @@ -165,6 +165,9 @@ raises(KeyError, a.remove(6)) def test_pop(self): +b = set() +raises(KeyError, b.pop()) + a = set([1,2,3,4,5]) for i in xrange(5): a.pop() ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: removed/chnaged old comments
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49198:534d51292ce2 Date: 2011-08-23 12:01 +0200 http://bitbucket.org/pypy/pypy/changeset/534d51292ce2/ Log:removed/chnaged old comments diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -1085,9 +1085,7 @@ set_isdisjoint__Frozenset_Set = set_isdisjoint__Set_Set def set_isdisjoint__Set_ANY(space, w_left, w_other): -#XXX maybe checking if type fits strategy first (before comparing) speeds this up a bit -#since this will be used in many other functions - general function for that -# if w_left.strategy != w_other.strategy = return w_False +#XXX may be optimized when other strategies are added for w_key in space.listview(w_other): if w_left.has_key(w_key): return space.w_False @@ -1112,8 +1110,6 @@ def set_symmetric_difference__Set_ANY(space, w_left, w_other): -#XXX since we need to iterate over both objects, create set -#from w_other so looking up items is fast w_other_as_set = w_left._newobj(space, w_other) w_result = w_left.symmetric_difference(w_other_as_set) return w_result ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: refactored symmetric_difference for sets
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49205:3e0b4ff1c77a Date: 2011-10-04 13:53 +0200 http://bitbucket.org/pypy/pypy/changeset/3e0b4ff1c77a/ Log:refactored symmetric_difference for sets diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -457,23 +457,21 @@ strategy = self.space.fromcache(ObjectSetStrategy) return strategy.cast_to_void_star(newsetdata) -def symmetric_difference(self, w_set, w_other): +def _symmetric_difference_base(self, w_set, w_other): if w_set.strategy is w_other.strategy: strategy = w_set.strategy storage = self._symmetric_difference_unwrapped(w_set, w_other) else: strategy = self.space.fromcache(ObjectSetStrategy) storage = self._symmetric_difference_wrapped(w_set, w_other) +return storage, strategy + +def symmetric_difference(self, w_set, w_other): +storage, strategy = self._symmetric_difference_base(w_set, w_other) return w_set.from_storage_and_strategy(storage, strategy) def symmetric_difference_update(self, w_set, w_other): -if w_set.strategy is w_other.strategy: -strategy = w_set.strategy -storage = self._symmetric_difference_unwrapped(w_set, w_other) -else: -strategy = self.space.fromcache(ObjectSetStrategy) -storage = self._symmetric_difference_wrapped(w_set, w_other) - +storage, strategy = self._symmetric_difference_base(w_set, w_other) w_set.strategy = strategy w_set.sstorage = storage ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: differentiation between set types happens in W_SetObject and W_FrozenSetObject (more OO)
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49191:e87c1f05838a Date: 2011-07-22 11:26 +0200 http://bitbucket.org/pypy/pypy/changeset/e87c1f05838a/ Log:differentiation between set types happens in W_SetObject and W_FrozenSetObject (more OO) diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -37,12 +37,7 @@ def from_storage_and_strategy(w_self, storage, strategy): objtype = type(w_self) -if objtype is W_SetObject: -obj = instantiate(W_SetObject) -elif objtype is W_FrozensetObject: -obj = instantiate(W_FrozensetObject) -else: -obj = w_self.space.call_function(w_self.space.type(w_self), None) +obj = w_self._newobj(w_self.space, None) assert isinstance(obj, W_BaseSetObject) obj.strategy = strategy obj.sstorage = storage ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: added tests and fix for unhashable items in combination with EmptySetStrategy
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49193:b07c4ba0f7ba Date: 2011-07-22 16:15 +0200 http://bitbucket.org/pypy/pypy/changeset/b07c4ba0f7ba/ Log:added tests and fix for unhashable items in combination with EmptySetStrategy diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -178,6 +178,17 @@ cast_to_void_star = staticmethod(cast_to_void_star) cast_from_void_star = staticmethod(cast_from_void_star) +def check_for_unhashable_objects(self, w_iterable): +w_iterator = self.space.iter(w_iterable) +while True: +try: +elem = self.space.next(w_iterator) +self.space.hash(elem) +except OperationError, e: +if not e.match(self.space, self.space.w_StopIteration): +raise +break + def get_empty_storage(self): return self.cast_to_void_star(None) @@ -230,22 +241,27 @@ return False def difference(self, w_set, w_other): +self.check_for_unhashable_objects(w_other) return w_set.copy() def difference_update(self, w_set, w_other): -pass +self.check_for_unhashable_objects(w_other) def intersect(self, w_set, w_other): +self.check_for_unhashable_objects(w_other) return w_set.copy() def intersect_update(self, w_set, w_other): +self.check_for_unhashable_objects(w_other) return w_set.copy() -def intersect_multiple(self, w_set, w_other): +def intersect_multiple(self, w_set, others_w): +self.intersect_multiple_update(w_set, others_w) return w_set.copy() -def intersect_multiple_update(self, w_set, w_other): -pass +def intersect_multiple_update(self, w_set, others_w): +for w_other in others_w: +self.intersect(w_set, w_other) def isdisjoint(self, w_set, w_other): return True @@ -828,6 +844,7 @@ w_left.difference_update(w_other) else: for w_key in space.listview(w_other): +space.hash(w_key) w_left.delitem(w_key) def inplace_sub__Set_Set(space, w_left, w_other): diff --git a/pypy/objspace/std/test/test_setobject.py b/pypy/objspace/std/test/test_setobject.py --- a/pypy/objspace/std/test/test_setobject.py +++ b/pypy/objspace/std/test/test_setobject.py @@ -599,6 +599,15 @@ assert e.isdisjoint(x) == True assert x.isdisjoint(e) == True +def test_empty_typeerror(self): +s = set() +raises(TypeError, s.difference, [[]]) +raises(TypeError, s.difference_update, [[]]) +raises(TypeError, s.intersection, [[]]) +raises(TypeError, s.intersection_update, [[]]) +raises(TypeError, s.symmetric_difference, [[]]) +raises(TypeError, s.symmetric_difference_update, [[]]) +raises(TypeError, s.update, [[]]) def test_super_with_generator(self): def foo(): ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: frozenset does not need to be copied
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49206:8592d5651c05 Date: 2011-10-10 11:10 +0200 http://bitbucket.org/pypy/pypy/changeset/8592d5651c05/ Log:frozenset does not need to be copied diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -28,7 +28,6 @@ def __init__(w_self, space, w_iterable=None): Initialize the set by taking ownership of 'setdata'. w_self.space = space #XXX less memory without this indirection? -#XXX in case of ObjectStrategy we can reuse the setdata object set_strategy_and_setdata(space, w_self, w_iterable) def __repr__(w_self): @@ -314,10 +313,12 @@ w_set.switch_to_empty_strategy() def copy(self, w_set): -#XXX do not copy FrozenDict -d = self.cast_from_void_star(w_set.sstorage) strategy = w_set.strategy -storage = self.cast_to_void_star(d.copy()) +if isinstance(w_set, W_FrozensetObject): +storage = w_set.sstorage +else: +d = self.cast_from_void_star(w_set.sstorage) +storage = self.cast_to_void_star(d.copy()) clone = w_set.from_storage_and_strategy(storage, strategy) return clone ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: make_setdata_from_w_iterable is not needed anymore
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49194:6a2ef1ad6abe Date: 2011-07-28 11:41 +0200 http://bitbucket.org/pypy/pypy/changeset/6a2ef1ad6abe/ Log:make_setdata_from_w_iterable is not needed anymore diff --git a/pypy/objspace/std/frozensettype.py b/pypy/objspace/std/frozensettype.py --- a/pypy/objspace/std/frozensettype.py +++ b/pypy/objspace/std/frozensettype.py @@ -39,7 +39,6 @@ def descr__frozenset__new__(space, w_frozensettype, w_iterable=gateway.NoneNotWrapped): from pypy.objspace.std.setobject import W_FrozensetObject -from pypy.objspace.std.setobject import make_setdata_from_w_iterable if (space.is_w(w_frozensettype, space.w_frozenset) and w_iterable is not None and type(w_iterable) is W_FrozensetObject): return w_iterable diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -725,6 +725,7 @@ for item_w in w_iterable: if type(item_w) is not W_IntObject: break; +#XXX wont work for [1, two, three, 1] use StopIteration instead if item_w is w_iterable[-1]: w_set.strategy = space.fromcache(IntegerSetStrategy) w_set.sstorage = w_set.strategy.get_storage_from_list(w_iterable) @@ -733,18 +734,6 @@ w_set.strategy = space.fromcache(ObjectSetStrategy) w_set.sstorage = w_set.strategy.get_storage_from_list(w_iterable) -def make_setdata_from_w_iterable(space, w_iterable=None): -#XXX remove this later -Return a new r_dict with the content of w_iterable. -if isinstance(w_iterable, W_BaseSetObject): -#XXX is this bad or not? -return w_iterable.getdict_w() -data = newset(space) -if w_iterable is not None: -for w_item in space.listview(w_iterable): -data[w_item] = None -return data - def _initialize_set(space, w_obj, w_iterable=None): w_obj.clear() set_strategy_and_setdata(space, w_obj, w_iterable) @@ -1087,6 +1076,7 @@ def set_isdisjoint__Set_ANY(space, w_left, w_other): #XXX maybe checking if type fits strategy first (before comparing) speeds this up a bit #since this will be used in many other functions - general function for that +# if w_left.strategy != w_other.strategy = return w_False for w_key in space.listview(w_other): if w_left.has_key(w_key): return space.w_False diff --git a/pypy/objspace/std/test/test_setobject.py b/pypy/objspace/std/test/test_setobject.py --- a/pypy/objspace/std/test/test_setobject.py +++ b/pypy/objspace/std/test/test_setobject.py @@ -18,13 +18,6 @@ letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' -def make_setdata_from_w_iterable(space, w_iterable): -data = newset(space) -if w_iterable is not None: -for w_item in space.listview(w_iterable): -data[w_item] = None -return data - class W_SubSetObject(W_SetObject):pass class TestW_SetObject: ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: fixed recent popitem changes
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49197:b6937fff521d Date: 2011-08-23 11:34 +0200 http://bitbucket.org/pypy/pypy/changeset/b6937fff521d/ Log:fixed recent popitem changes diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -567,12 +567,13 @@ def popitem(self, w_set): storage = self.cast_from_void_star(w_set.sstorage) try: +# this returns a tuple because internally sets are dicts result = storage.popitem() except KeyError: # strategy may still be the same even if dict is empty raise OperationError(self.space.w_KeyError, self.space.wrap('pop from an empty set')) -return self.wrap(result) +return self.wrap(result[0]) class IntegerSetStrategy(AbstractUnwrappedSetStrategy, SetStrategy): cast_to_void_star, cast_from_void_star = rerased.new_erasing_pair(integer) @@ -1044,17 +1045,7 @@ return space.wrap(hash) def set_pop__Set(space, w_left): -#XXX move this to strategy so we don't have to -#wrap all items only to get the first one -#XXX use popitem return w_left.popitem() -for w_key in w_left.getkeys(): -break -else: -raise OperationError(space.w_KeyError, -space.wrap('pop from an empty set')) -w_left.delitem(w_key) -return w_key def and__Set_Set(space, w_left, w_other): new_set = w_left.intersect(w_other) diff --git a/pypy/objspace/std/test/test_setobject.py b/pypy/objspace/std/test/test_setobject.py --- a/pypy/objspace/std/test/test_setobject.py +++ b/pypy/objspace/std/test/test_setobject.py @@ -640,7 +640,7 @@ assert self.FakeInt(5) in s def test_fakeobject_and_pop(self): -s = set([1,2,3,self.FakeInt(4), 5]) +s = set([1,2,3,self.FakeInt(4),5]) assert s.pop() assert s.pop() assert s.pop() ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: removed unused methods
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49211:88edfa6d5641 Date: 2011-10-11 13:51 +0200 http://bitbucket.org/pypy/pypy/changeset/88edfa6d5641/ Log:removed unused methods diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -862,31 +862,6 @@ else: return None -# helper functions for set operation on dicts - -# XXX are these still needed? -def _symmetric_difference_dict(space, ld, rd): -result = newset(space) -for w_key in ld: -if w_key not in rd: -result[w_key] = None -for w_key in rd: -if w_key not in ld: -result[w_key] = None -return result - -def _issubset_dict(ldict, rdict): -if len(ldict) len(rdict): -return False - -for w_key in ldict: -if w_key not in rdict: -return False -return True - - -#end helper functions - def set_update__Set(space, w_left, others_w): Update a set with the union of itself and another. for w_other in others_w: ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: discard is not needed anymore
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49212:d7e380dfbd4c Date: 2011-10-11 13:54 +0200 http://bitbucket.org/pypy/pypy/changeset/d7e380dfbd4c/ Log:discard is not needed anymore diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -78,10 +78,6 @@ def add(self, w_key): self.strategy.add(self, w_key) -# XXX this appears unused? kill it -def discard(self, w_item): -return self.strategy.discard(self, w_item) - # XXX rename to remove, delitem is the name for the operation that does # del d[x] which does not work on sets def delitem(self, w_item): ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: removed old comment
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49200:3727073215e7 Date: 2011-08-23 14:03 +0200 http://bitbucket.org/pypy/pypy/changeset/3727073215e7/ Log:removed old comment diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -456,7 +456,6 @@ if w_set.strategy is w_other.strategy: self.symmetric_difference_update_match(w_set, w_other) return -#XXX no wrapping when strategies are equal newsetdata = newset(self.space) for w_key in w_set.getkeys(): if not w_other.has_key(w_key): ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: difference always expects w_other to be a set
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49216:2bec064b8288 Date: 2011-10-11 14:27 +0200 http://bitbucket.org/pypy/pypy/changeset/2bec064b8288/ Log:difference always expects w_other to be a set diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -244,10 +244,6 @@ return False def difference(self, w_set, w_other): -# XXX what is w_other here? a set or any wrapped object? -# if a set, the following line is unnecessary (sets contain only -# hashable objects). -self.check_for_unhashable_objects(w_other) return w_set.copy() def difference_update(self, w_set, w_other): @@ -896,9 +892,13 @@ def set_difference__Set(space, w_left, others_w): if len(others_w) == 0: return w_left.copy() -result = w_left +result = w_left.copy() for w_other in others_w: -result = result.difference(w_other) +if isinstance(w_other, W_BaseSetObject): +result.difference_update(w_other) +else: +w_other_as_set = w_left._newobj(space, w_other) +result.difference_update(w_other_as_set) return result frozenset_difference__Frozenset = set_difference__Set ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit
[pypy-commit] pypy set-strategies: to be consistent create a set and call difference_update here too
Author: Lukas Diekmann lukas.diekm...@uni-duesseldorf.de Branch: set-strategies Changeset: r49217:5676c0591355 Date: 2011-10-11 14:30 +0200 http://bitbucket.org/pypy/pypy/changeset/5676c0591355/ Log:to be consistent create a set and call difference_update here too diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py --- a/pypy/objspace/std/setobject.py +++ b/pypy/objspace/std/setobject.py @@ -910,9 +910,8 @@ # optimization only w_left.difference_update(w_other) else: -for w_key in space.listview(w_other): -space.hash(w_key) -w_left.remove(w_key) +w_other_as_set = w_left._newobj(space, w_other) +w_left.difference_update(w_other_as_set) def inplace_sub__Set_Set(space, w_left, w_other): w_left.difference_update(w_other) ___ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit