[pypy-svn] r4998 - in pypy/branch/src-newobjectmodel/pypy/objspace/std: . test
arigo at codespeak.net
arigo at codespeak.net
Sun Jun 6 14:36:44 CEST 2004
Author: arigo
Date: Sun Jun 6 14:36:43 2004
New Revision: 4998
Modified:
pypy/branch/src-newobjectmodel/pypy/objspace/std/dictobject.py
pypy/branch/src-newobjectmodel/pypy/objspace/std/objspace.py
pypy/branch/src-newobjectmodel/pypy/objspace/std/stdtypedef.py
pypy/branch/src-newobjectmodel/pypy/objspace/std/stringobject.py
pypy/branch/src-newobjectmodel/pypy/objspace/std/test/test_dictobject.py
Log:
Number of small random fixes.
Modified: pypy/branch/src-newobjectmodel/pypy/objspace/std/dictobject.py
==============================================================================
--- pypy/branch/src-newobjectmodel/pypy/objspace/std/dictobject.py (original)
+++ pypy/branch/src-newobjectmodel/pypy/objspace/std/dictobject.py Sun Jun 6 14:36:43 2004
@@ -9,80 +9,42 @@
from pypy.interpreter import gateway
from stringobject import W_StringObject
-class _NoValueInCell(object): pass
-
-class Cell(object):
- def __init__(self,w_value=_NoValueInCell):
- self.w_value = w_value
-
- def get(self):
- if self.is_empty():
- raise ValueError, "get() from an empty cell"
- return self.w_value
-
- def set(self,w_value):
- self.w_value = w_value
-
- def make_empty(self):
- if self.is_empty():
- raise ValueError, "make_empty() on an empty cell"
- self.w_value = _NoValueInCell
-
- def is_empty(self):
- return self.w_value is _NoValueInCell
-
- def __repr__(self):
- """ representation for debugging purposes """
- return "%s(%s)" % (self.__class__.__name__, self.w_value)
-
-
class W_DictObject(W_Object):
from pypy.objspace.std.dicttype import dict_typedef as typedef
def __init__(w_self, space, list_pairs_w):
W_Object.__init__(w_self, space)
- w_self.data = [ (w_key, space.unwrap(space.hash(w_key)), Cell(w_value))
+ w_self.data = [ [space.unwrap(space.hash(w_key)), w_key, w_value]
for w_key,w_value in list_pairs_w ]
def __repr__(w_self):
""" representation for debugging purposes """
return "%s(%s)" % (w_self.__class__.__name__, w_self.data)
- def non_empties(self):
- return [ (w_key, hash, cell) for w_key,hash,cell in self.data
- if not cell.is_empty()]
-
- def _cell(self,space,w_lookup):
- data = self.data
+ def lookup(self, w_lookup, create=False):
# this lookup is where most of the start-up time is consumed.
# Hashing helps a lot.
+ space = self.space
lookup_hash = space.unwrap(space.hash(w_lookup))
- for w_key, hash, cell in data:
- if lookup_hash == hash and space.is_true(space.eq(w_lookup, w_key)):
+ for cell in self.data:
+ if (cell[0] == lookup_hash and
+ space.is_true(space.eq(w_lookup, cell[1]))):
break
else:
- cell = Cell()
- data.append((w_lookup,lookup_hash,cell))
+ if not create:
+ raise OperationError(space.w_KeyError, w_lookup)
+ cell = [lookup_hash, w_lookup, None]
+ self.data.append(cell)
return cell
- def cell(self,space,w_lookup):
- return space.wrap(self._cell(space,w_lookup))
-
- def _appendcell(self, space, w_lookup, w_cell):
- # there should be no w_lookup entry already!
- data = self.data
- lookup_hash = space.unwrap(space.hash(w_lookup))
- cell = space.unwrap(w_cell)
- data.append((w_lookup, lookup_hash, cell))
-
registerimplementation(W_DictObject)
def unwrap__Dict(space, w_dict):
result = {}
- for w_key, hash, cell in w_dict.non_empties():
- result[space.unwrap(w_key)] = space.unwrap(cell.get())
+ for hash, w_key, w_value in w_dict.data:
+ result[space.unwrap(w_key)] = space.unwrap(w_value)
return result
def init__Dict(space, w_dict, w_args, w_kwds):
@@ -106,48 +68,28 @@
space.call_method(w_dict, 'update', w_kwds)
def getitem__Dict_ANY(space, w_dict, w_lookup):
- data = w_dict.non_empties()
- h = space.unwrap(space.hash(w_lookup))
- for w_key, hash, cell in data:
- if hash != h:
- continue
- if space.is_true(space.eq(w_lookup, w_key)):
- return cell.get()
- raise OperationError(space.w_KeyError, w_lookup)
+ return w_dict.lookup(w_lookup)[2]
def setitem__Dict_ANY_ANY(space, w_dict, w_newkey, w_newvalue):
- cell = w_dict._cell(space,w_newkey)
- cell.set(w_newvalue)
+ cell = w_dict.lookup(w_newkey, create=True)
+ cell[2] = w_newvalue
def delitem__Dict_ANY(space, w_dict, w_lookup):
- data = w_dict.non_empties()
- h = space.unwrap(space.hash(w_lookup))
- for w_key, hash, cell in data:
- if hash != h:
- continue
- if space.is_true(space.eq(w_lookup, w_key)):
- cell.make_empty()
- return
- raise OperationError(space.w_KeyError, w_lookup)
-
-def nonzero__Dict(space, w_dict):
- # this must be implemented in addition to len() for dictionaries
- # for infinite recursion reasons (is_true -> len -> call to len ->
- # checking for keywords -> is_true etc.)
- return space.newbool(not not w_dict.non_empties())
+ cell = w_dict.lookup(w_lookup)
+ # overwrite the cell with any other one removed from the dictionary
+ cell[:] = w_dict.data.pop()
def len__Dict(space, w_dict):
- return space.wrap(len(w_dict.non_empties()))
+ return space.wrap(len(w_dict.data))
def contains__Dict_ANY(space, w_dict, w_lookup):
- data = w_dict.non_empties()
- h = space.unwrap(space.hash(w_lookup))
- for w_key, hash, cell in data:
- if hash != h:
- continue
- if space.is_true(space.eq(w_lookup, w_key)):
- return space.w_True
- return space.w_False
+ try:
+ w_dict.lookup(w_lookup)
+ except OperationError:
+ # assert e.match(space, space.w_KeyError)
+ return space.w_False
+ else:
+ return space.w_True
dict_has_key__Dict_ANY = contains__Dict_ANY
@@ -160,30 +102,30 @@
if space.is_true(space.is_(w_left, w_right)):
return space.w_True
- dataleft = w_left.non_empties()
- dataright = w_right.non_empties()
+ dataleft = w_left.data
+ dataright = w_right.data
if len(dataleft) != len(dataright):
return space.w_False
- for w_key, hash, cell in dataleft:
+ for hash, w_key, w_value in dataleft:
try:
w_rightval = space.getitem(w_right, w_key)
except OperationError:
return space.w_False
- if not space.is_true(space.eq(cell.w_value, w_rightval)):
+ if not space.is_true(space.eq(w_value, w_rightval)):
return space.w_False
return space.w_True
def lt__Dict_Dict(space, w_left, w_right):
# Different sizes, no problem
- dataleft = w_left.non_empties()
- dataright = w_right.non_empties()
+ dataleft = w_left.data
+ dataright = w_right.data
if len(dataleft) < len(dataright):
return space.w_True
if len(dataleft) > len(dataright):
return space.w_False
# Same size
- for w_key, hash, cell in dataleft:
+ for hash, w_key, w_value in dataleft:
# This is incorrect, but we need to decide what comparisons on
# dictionaries of equal size actually means
# The Python language specification is silent on the subject
@@ -191,7 +133,7 @@
w_rightval = space.getitem(w_right, w_key)
except OperationError:
return space.w_True
- if space.is_true(space.lt(cell.w_value, w_rightval)):
+ if space.is_true(space.lt(w_value, w_rightval)):
return space.w_True
# The dictionaries are equal. This is correct.
return space.w_False
@@ -200,37 +142,31 @@
raise OperationError(space.w_TypeError,space.wrap("dict objects are unhashable"))
def dict_copy__Dict(space, w_self):
- return W_DictObject(space, [(w_key,cell.get())
- for w_key,hash,cell in
- w_self.non_empties()])
+ return W_DictObject(space, [(w_key,w_value)
+ for hash,w_key,w_value in w_self.data])
+
def dict_items__Dict(space, w_self):
- return space.newlist([ space.newtuple([w_key,cell.get()])
- for w_key,hash,cell in
- w_self.non_empties()])
+ return space.newlist([ space.newtuple([w_key,w_value])
+ for hash,w_key,w_value in w_self.data ])
def dict_keys__Dict(space, w_self):
return space.newlist([ w_key
- for w_key,hash,cell in
- w_self.non_empties()])
+ for hash,w_key,w_value in w_self.data ])
def dict_values__Dict(space, w_self):
- return space.newlist([ cell.get()
- for w_key,hash,cell in
- w_self.non_empties()])
+ return space.newlist([ w_value
+ for hash,w_key,w_value in w_self.data ])
def dict_clear__Dict(space, w_self):
w_self.data = []
-def dict_get__Dict_ANY_ANY(space, w_self, w_lookup, w_default):
- data = w_self.non_empties()
- h = space.unwrap(space.hash(w_lookup))
- for w_key, hash, cell in data:
- if h != hash:
- continue
- if space.is_true(space.eq(w_lookup, w_key)):
- return cell.get()
- return w_default
-
+def dict_get__Dict_ANY_ANY(space, w_dict, w_lookup, w_default):
+ try:
+ return w_dict.lookup(w_lookup)[2]
+ except OperationError:
+ # assert e.match(space, space.w_KeyError)
+ return w_default
+
# Now we only handle one implementation of dicts, this one.
# The fix is to move this to dicttype.py, and do a
# multimethod lookup mapping str to StdObjSpace.str
Modified: pypy/branch/src-newobjectmodel/pypy/objspace/std/objspace.py
==============================================================================
--- pypy/branch/src-newobjectmodel/pypy/objspace/std/objspace.py (original)
+++ pypy/branch/src-newobjectmodel/pypy/objspace/std/objspace.py Sun Jun 6 14:36:43 2004
@@ -323,18 +323,18 @@
# XXX a bit of hacking to gain more speed
#
if w_one is w_two:
- return self.newbool(1)
+ return self.w_True
if isinstance(w_one, W_CPythonObject):
if isinstance(w_two, W_CPythonObject):
if w_one.cpyobj is w_two.cpyobj:
- return self.newbool(1)
+ return self.w_True
return self.newbool(self.unwrap(w_one) is self.unwrap(w_two))
- return self.newbool(0)
+ return self.w_False
def is_true(self, w_obj):
# XXX don't look!
if isinstance(w_obj, W_DictObject):
- return not not w_obj.non_empties()
+ return not not w_obj.data
else:
return DescrOperation.is_true(self, w_obj)
Modified: pypy/branch/src-newobjectmodel/pypy/objspace/std/stdtypedef.py
==============================================================================
--- pypy/branch/src-newobjectmodel/pypy/objspace/std/stdtypedef.py (original)
+++ pypy/branch/src-newobjectmodel/pypy/objspace/std/stdtypedef.py Sun Jun 6 14:36:43 2004
@@ -47,6 +47,10 @@
# get all the sliced multimethods
multimethods = slicemultimethods(space.__class__, typedef)
for name, code in multimethods.items():
+ # compute the slice and ignore the multimethod if empty
+ if not code.computeslice(space):
+ continue
+ # create a Function around the sliced multimethod code
fn = function.Function(space, code, defs_w=code.getdefaults(space))
assert name not in rawdict, 'name clash: %s in %s_typedef' % (
name, typedef.name)
@@ -97,10 +101,6 @@
# import all multimethods defined directly on the type without slicing
for multimethod in typeclass.local_multimethods:
slicemultimethod(multimethod, None, result)
- # remove the empty slices
- for name, code in result.items():
- if code.slice().is_empty():
- del result[name]
return result
class MultimethodCode(eval.Code):
@@ -113,14 +113,25 @@
self.bound_position = bound_position
self.framecls = framecls
argnames = ['x%d'%(i+1) for i in range(multimethod.arity)]
- argnames.insert(0, argnames.pop(self.bound_position))
varargname = kwargname = None
if multimethod.extras.get('varargs', False):
varargname = 'args'
if multimethod.extras.get('keywords', False):
kwargname = 'keywords'
self.sig = argnames, varargname, kwargname
-
+
+ def computeslice(self, space):
+ if self.typeclass is None:
+ slice = self.basemultimethod
+ else:
+ slice = self.basemultimethod.slice(self.typeclass,
+ self.bound_position)
+ if slice.is_empty():
+ return False
+ else:
+ self.mm = slice.get(space)
+ return True
+
def signature(self):
return self.sig
@@ -128,24 +139,15 @@
return [space.wrap(x)
for x in self.basemultimethod.extras.get('defaults', ())]
- def slice(self):
- if self.typeclass is None:
- return self.basemultimethod
- else:
- return self.basemultimethod.slice(self.typeclass,
- self.bound_position)
-
def create_frame(self, space, w_globals, closure=None):
return self.framecls(space, self)
class MmFrame(eval.Frame):
def run(self):
"Call the multimethod, raising a TypeError if not implemented."
- mm = self.code.slice().get(self.space)
- args = self.fastlocals_w
- #print mm.multimethod.operatorsymbol, args
- #print
- w_result = mm(*args)
+ args = list(self.fastlocals_w)
+ args.insert(0, args.pop(self.code.bound_position))
+ w_result = self.code.mm(*args)
# we accept a real None from operations with no return value
if w_result is None:
w_result = self.space.w_None
@@ -154,10 +156,10 @@
class SpecialMmFrame(eval.Frame):
def run(self):
"Call the multimethods, possibly returning a NotImplemented."
- mm = self.code.slice().get(self.space)
- args = self.fastlocals_w
+ args = list(self.fastlocals_w)
+ args.insert(0, args.pop(self.code.bound_position))
try:
- return mm.perform_call(args)
+ return self.code.mm.perform_call(args)
except FailedToImplement, e:
if e.args:
raise OperationError(*e.args)
Modified: pypy/branch/src-newobjectmodel/pypy/objspace/std/stringobject.py
==============================================================================
--- pypy/branch/src-newobjectmodel/pypy/objspace/std/stringobject.py (original)
+++ pypy/branch/src-newobjectmodel/pypy/objspace/std/stringobject.py Sun Jun 6 14:36:43 2004
@@ -1017,6 +1017,10 @@
pieces.append(hex(int(value)))
elif c=='r':
pieces.append(repr(value))
+ elif c=='f':
+ pieces.append(str(float(value)))
+ elif c=='g':
+ pieces.append(str(value)) # XXX
else:
raise ValueError, "unsupported format character '%s' (%x) at index %d" % (
c, ord(c), i)
Modified: pypy/branch/src-newobjectmodel/pypy/objspace/std/test/test_dictobject.py
==============================================================================
--- pypy/branch/src-newobjectmodel/pypy/objspace/std/test/test_dictobject.py (original)
+++ pypy/branch/src-newobjectmodel/pypy/objspace/std/test/test_dictobject.py Sun Jun 6 14:36:43 2004
@@ -47,48 +47,48 @@
self.assertRaises_w(self.space.w_KeyError,
space.getitem,d,space.wrap('one'))
- def test_cell(self):
- space = self.space
- wk1 = space.wrap('key')
- d = W_DictObject(space, [])
- w_cell = d.cell(space,wk1)
- cell = space.unwrap(w_cell)
- self.failUnless(cell.is_empty())
- cell.set(space.wrap(1))
- self.assertEqual_w(space.getitem(d,wk1),space.wrap(1))
- wk2 = space.wrap('key2')
- space.setitem(d,wk2,space.wrap(2))
- cell = space.unwrap(d.cell(space,wk2))
- self.assertEqual_w(cell.get(),space.wrap(2))
-
- def test_empty_cell(self):
- space = self.space
- d = W_DictObject(space,
- [(space.wrap('colour'), space.wrap(0)),
- (space.wrap('of'), space.wrap(2)),
- (space.wrap('magic'), space.wrap(1))])
- w_cell = d.cell(space, space.wrap('of'))
- d2 = W_DictObject(space,
- [(space.wrap('colour'), space.wrap(0)),
- (space.wrap('magic'), space.wrap(1))])
- self.assertNotEqual_w(d, d2)
- space.delitem(d, space.wrap('of'))
- self.assertEqual_w(d, d2)
-
- def test_empty_cell2(self):
- space = self.space
- d = W_DictObject(space,
- [(space.wrap('colour'), space.wrap(0)),
- (space.wrap('of'), space.wrap(2)),
- (space.wrap('magic'), space.wrap(1))])
- w_cell = d.cell(space, space.wrap('of'))
- d2 = W_DictObject(space,
- [(space.wrap('colour'), space.wrap(0)),
- (space.wrap('magic'), space.wrap(1))])
- self.assertNotEqual_w(d, d2)
- cell = space.unwrap(w_cell)
- cell.make_empty()
- self.assertEqual_w(d, d2)
+## def test_cell(self):
+## space = self.space
+## wk1 = space.wrap('key')
+## d = W_DictObject(space, [])
+## w_cell = d.cell(space,wk1)
+## cell = space.unwrap(w_cell)
+## self.failUnless(cell.is_empty())
+## cell.set(space.wrap(1))
+## self.assertEqual_w(space.getitem(d,wk1),space.wrap(1))
+## wk2 = space.wrap('key2')
+## space.setitem(d,wk2,space.wrap(2))
+## cell = space.unwrap(d.cell(space,wk2))
+## self.assertEqual_w(cell.get(),space.wrap(2))
+
+## def test_empty_cell(self):
+## space = self.space
+## d = W_DictObject(space,
+## [(space.wrap('colour'), space.wrap(0)),
+## (space.wrap('of'), space.wrap(2)),
+## (space.wrap('magic'), space.wrap(1))])
+## w_cell = d.cell(space, space.wrap('of'))
+## d2 = W_DictObject(space,
+## [(space.wrap('colour'), space.wrap(0)),
+## (space.wrap('magic'), space.wrap(1))])
+## self.assertNotEqual_w(d, d2)
+## space.delitem(d, space.wrap('of'))
+## self.assertEqual_w(d, d2)
+
+## def test_empty_cell2(self):
+## space = self.space
+## d = W_DictObject(space,
+## [(space.wrap('colour'), space.wrap(0)),
+## (space.wrap('of'), space.wrap(2)),
+## (space.wrap('magic'), space.wrap(1))])
+## w_cell = d.cell(space, space.wrap('of'))
+## d2 = W_DictObject(space,
+## [(space.wrap('colour'), space.wrap(0)),
+## (space.wrap('magic'), space.wrap(1))])
+## self.assertNotEqual_w(d, d2)
+## cell = space.unwrap(w_cell)
+## cell.make_empty()
+## self.assertEqual_w(d, d2)
def test_wrap_dict(self):
More information about the Pypy-commit
mailing list