Author: Antonio Cuni <[email protected]>
Branch: identity-dict-strategy
Changeset: r45775:be0b34777be3
Date: 2011-07-20 12:37 +0200
http://bitbucket.org/pypy/pypy/changeset/be0b34777be3/
Log: move the strategy to identitydict.py
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
@@ -1,9 +1,7 @@
import py, sys
-from pypy.tool.sourcetools import func_with_new_name
from pypy.objspace.std.model import registerimplementation, W_Object
from pypy.objspace.std.register_all import register_all
from pypy.objspace.std.settype import set_typedef as settypedef
-from pypy.objspace.std import identitydict
from pypy.interpreter import gateway
from pypy.interpreter.argument import Signature
from pypy.interpreter.error import OperationError, operationerrfmt
@@ -184,6 +182,7 @@
w_dict.dstorage = storage
def switch_to_identity_strategy(self, w_dict):
+ from pypy.objspace.std.identitydict import IdentityDictStrategy
strategy = self.space.fromcache(IdentityDictStrategy)
storage = strategy.get_empty_storage()
w_dict.strategy = strategy
@@ -416,53 +415,6 @@
return self.unerase(w_dict.dstorage).keys()
-class IdentityDictStrategy(AbstractTypedStrategy, DictStrategy):
- """
- Strategy for custom instances which compares by identity (i.e., the
- default unless you override __hash__, __eq__ or __cmp__). The storage is
- just a normal RPython dict, which has already the correct by-identity
- semantics.
- """
-
- _erase_tuple, _unerase_tuple = rerased.new_erasing_pair("identitydict")
- _erase_tuple = staticmethod(_erase_tuple)
- _unerase_tuple = staticmethod(_unerase_tuple)
-
- def wrap(self, unwrapped):
- return unwrapped
-
- def unwrap(self, wrapped):
- return wrapped
-
- def erase(self, d):
- current_version = identitydict.get_global_version(self.space)
- return self._erase_tuple((current_version, d))
-
- def unerase(self, dstorage):
- version, d = self._unerase_tuple(dstorage)
- return d
-
- def get_current_version(self, dstorage):
- version, d = self._unerase_tuple(dstorage)
- return version
-
- def get_empty_storage(self):
- return self.erase({})
-
- def is_correct_type(self, w_obj):
- w_type = self.space.type(w_obj)
- return w_type.compares_by_identity()
-
- def _never_equal_to(self, w_lookup_type):
- return False
-
- def iter(self, w_dict):
- return IdentityDictIteratorImplementation(self.space, self, w_dict)
-
- def keys(self, w_dict):
- return self.unerase(w_dict.dstorage).keys()
-
-
class StringDictStrategy(AbstractTypedStrategy, DictStrategy):
erase, unerase = rerased.new_erasing_pair("string")
@@ -574,14 +526,6 @@
else:
return None, None
-
-class IdentityDictIteratorImplementation(IteratorImplementation):
- __init__ = func_with_new_name(
- ObjectIteratorImplementation.__init__.im_func, '__init__')
-
- next_entry = func_with_new_name(
- ObjectIteratorImplementation.next_entry.im_func, 'next_entry')
-
init_signature = Signature(['seq_or_map'], None, 'kwargs')
init_defaults = [None]
diff --git a/pypy/objspace/std/identitydict.py
b/pypy/objspace/std/identitydict.py
--- a/pypy/objspace/std/identitydict.py
+++ b/pypy/objspace/std/identitydict.py
@@ -1,3 +1,9 @@
+from pypy.rlib import rerased
+from pypy.objspace.std.dictmultiobject import (AbstractTypedStrategy,
+ DictStrategy,
+ IteratorImplementation)
+
+
# a global (per-space) version counter to track live instances which "compare
# by identity" (i.e., whose __eq__, __cmp__ and __hash__ are the default
# ones). The idea is to track only classes for which we checked the
@@ -25,3 +31,68 @@
def get(self):
return self._version
+
+
+
+## ----------------------------------------------------------------------------
+## dict strategy (see dict_multiobject.py)
+
+# this strategy is selected by EmptyDictStrategy.switch_to_correct_strategy
+class IdentityDictStrategy(AbstractTypedStrategy, DictStrategy):
+ """
+ Strategy for custom instances which compares by identity (i.e., the
+ default unless you override __hash__, __eq__ or __cmp__). The storage is
+ just a normal RPython dict, which has already the correct by-identity
+ semantics.
+ """
+
+ _erase_tuple, _unerase_tuple = rerased.new_erasing_pair("identitydict")
+ _erase_tuple = staticmethod(_erase_tuple)
+ _unerase_tuple = staticmethod(_unerase_tuple)
+
+ def wrap(self, unwrapped):
+ return unwrapped
+
+ def unwrap(self, wrapped):
+ return wrapped
+
+ def erase(self, d):
+ current_version = get_global_version(self.space)
+ return self._erase_tuple((current_version, d))
+
+ def unerase(self, dstorage):
+ version, d = self._unerase_tuple(dstorage)
+ return d
+
+ def get_current_version(self, dstorage):
+ version, d = self._unerase_tuple(dstorage)
+ return version
+
+ def get_empty_storage(self):
+ return self.erase({})
+
+ def is_correct_type(self, w_obj):
+ w_type = self.space.type(w_obj)
+ return w_type.compares_by_identity()
+
+ def _never_equal_to(self, w_lookup_type):
+ return False
+
+ def iter(self, w_dict):
+ return IdentityDictIteratorImplementation(self.space, self, w_dict)
+
+ def keys(self, w_dict):
+ return self.unerase(w_dict.dstorage).keys()
+
+
+class IdentityDictIteratorImplementation(IteratorImplementation):
+ def __init__(self, space, strategy, dictimplementation):
+ IteratorImplementation.__init__(self, space, dictimplementation)
+ self.iterator =
strategy.unerase(dictimplementation.dstorage).iteritems()
+
+ def next_entry(self):
+ # note that this 'for' loop only runs once, at most
+ for w_key, w_value in self.iterator:
+ return w_key, w_value
+ else:
+ return None, None
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit