Author: Philip Jenvey <[email protected]>
Branch:
Changeset: r70730:281b05c23a4b
Date: 2014-04-17 16:27 -0700
http://bitbucket.org/pypy/pypy/changeset/281b05c23a4b/
Log: minor cleanup/refactoring for the sake of py3k
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
@@ -3,15 +3,18 @@
indirection is introduced to make the version tag change less often.
"""
+from rpython.rlib import jit, rerased
+
from pypy.interpreter.baseobjspace import W_Root
-from pypy.objspace.std.dictmultiobject import create_iterator_classes
-from pypy.objspace.std.dictmultiobject import DictStrategy,
_never_equal_to_string
-from pypy.objspace.std.dictmultiobject import ObjectDictStrategy
-from rpython.rlib import jit, rerased
+from pypy.objspace.std.dictmultiobject import (
+ DictStrategy, ObjectDictStrategy, _never_equal_to_string,
+ create_iterator_classes)
+
class VersionTag(object):
pass
+
class ModuleCell(W_Root):
def __init__(self, w_value=None):
self.w_value = w_value
@@ -19,11 +22,17 @@
def __repr__(self):
return "<ModuleCell: %s>" % (self.w_value, )
+
def unwrap_cell(w_value):
if isinstance(w_value, ModuleCell):
return w_value.w_value
return w_value
+
+def _wrapkey(space, key):
+ return space.wrap(key)
+
+
class ModuleDictStrategy(DictStrategy):
erase, unerase = rerased.new_erasing_pair("modulecell")
@@ -55,7 +64,7 @@
def setitem(self, w_dict, w_key, w_value):
space = self.space
if space.is_w(space.type(w_key), space.w_str):
- self.setitem_str(w_dict, self.space.str_w(w_key), w_value)
+ self.setitem_str(w_dict, space.str_w(w_key), w_value)
else:
self.switch_to_object_strategy(w_dict)
w_dict.setitem(w_key, w_value)
@@ -66,8 +75,8 @@
cell.w_value = w_value
return
if cell is not None:
- # If the new value and the current value are the same, don't
create a
- # level of indirection, or mutate the version.
+ # If the new value and the current value are the same, don't
+ # create a level of indirection, or mutate the version.
if self.space.is_w(w_value, cell):
return
w_value = ModuleCell(w_value)
@@ -121,8 +130,8 @@
return w_dict.getitem(w_key)
def getitem_str(self, w_dict, key):
- w_res = self.getdictvalue_no_unwrapping(w_dict, key)
- return unwrap_cell(w_res)
+ cell = self.getdictvalue_no_unwrapping(w_dict, key)
+ return unwrap_cell(cell)
def w_keys(self, w_dict):
space = self.space
@@ -136,37 +145,43 @@
def items(self, w_dict):
space = self.space
iterator = self.unerase(w_dict.dstorage).iteritems
- return [space.newtuple([space.wrap(key), unwrap_cell(cell)])
- for key, cell in iterator()]
+ return [space.newtuple([_wrapkey(space, key), unwrap_cell(cell)])
+ for key, cell in iterator()]
def clear(self, w_dict):
self.unerase(w_dict.dstorage).clear()
self.mutated()
def popitem(self, w_dict):
+ space = self.space
d = self.unerase(w_dict.dstorage)
- key, w_value = d.popitem()
+ key, cell = d.popitem()
self.mutated()
- return self.space.wrap(key), unwrap_cell(w_value)
+ return _wrapkey(space, key), unwrap_cell(cell)
def switch_to_object_strategy(self, w_dict):
+ space = self.space
d = self.unerase(w_dict.dstorage)
- strategy = self.space.fromcache(ObjectDictStrategy)
+ strategy = space.fromcache(ObjectDictStrategy)
d_new = strategy.unerase(strategy.get_empty_storage())
for key, cell in d.iteritems():
- d_new[self.space.wrap(key)] = unwrap_cell(cell)
+ d_new[_wrapkey(space, key)] = unwrap_cell(cell)
w_dict.strategy = strategy
w_dict.dstorage = strategy.erase(d_new)
def getiterkeys(self, w_dict):
return self.unerase(w_dict.dstorage).iterkeys()
+
def getitervalues(self, w_dict):
return self.unerase(w_dict.dstorage).itervalues()
+
def getiteritems(self, w_dict):
return self.unerase(w_dict.dstorage).iteritems()
- def wrapkey(space, key):
- return space.wrap(key)
+
+ wrapkey = _wrapkey
+
def wrapvalue(space, value):
return unwrap_cell(value)
+
create_iterator_classes(ModuleDictStrategy)
diff --git a/pypy/objspace/std/kwargsdict.py b/pypy/objspace/std/kwargsdict.py
--- a/pypy/objspace/std/kwargsdict.py
+++ b/pypy/objspace/std/kwargsdict.py
@@ -1,12 +1,19 @@
-## ----------------------------------------------------------------------------
-## dict strategy (see dictmultiobject.py)
+"""dict implementation specialized for keyword argument dicts.
-from rpython.rlib import rerased, jit
+Based on two lists containing unwrapped key value pairs.
+"""
+
+from rpython.rlib import jit, rerased
+
from pypy.objspace.std.dictmultiobject import (
BytesDictStrategy, DictStrategy, EmptyDictStrategy, ObjectDictStrategy,
create_iterator_classes)
+def _wrapkey(space, key):
+ return space.wrap(key)
+
+
class EmptyKwargsDictStrategy(EmptyDictStrategy):
def switch_to_bytes_strategy(self, w_dict):
strategy = self.space.fromcache(KwargsDictStrategy)
@@ -21,7 +28,7 @@
unerase = staticmethod(unerase)
def wrap(self, key):
- return self.space.wrap(key)
+ return _wrapkey(self.space, key)
def unwrap(self, wrapped):
return self.space.str_w(wrapped)
@@ -117,16 +124,14 @@
def items(self, w_dict):
space = self.space
keys, values_w = self.unerase(w_dict.dstorage)
- result = []
- for i in range(len(keys)):
- result.append(space.newtuple([self.wrap(keys[i]), values_w[i]]))
- return result
+ return [space.newtuple([self.wrap(keys[i]), values_w[i]])
+ for i in range(len(keys))]
def popitem(self, w_dict):
keys, values_w = self.unerase(w_dict.dstorage)
key = keys.pop()
w_value = values_w.pop()
- return (self.wrap(key), w_value)
+ return self.wrap(key), w_value
def clear(self, w_dict):
w_dict.dstorage = self.get_empty_storage()
@@ -164,17 +169,15 @@
keys = self.unerase(w_dict.dstorage)[0]
return iter(range(len(keys)))
- def wrapkey(space, key):
- return space.wrap(key)
+ wrapkey = _wrapkey
def next_item(self):
strategy = self.strategy
assert isinstance(strategy, KwargsDictStrategy)
for i in self.iterator:
- keys, values_w = strategy.unerase(
- self.dictimplementation.dstorage)
- return self.space.wrap(keys[i]), values_w[i]
+ keys, values_w = strategy.unerase(self.dictimplementation.dstorage)
+ return _wrapkey(self.space, keys[i]), values_w[i]
else:
return None, None
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit