[pypy-svn] r26310 - in pypy/dist/pypy: interpreter interpreter/test module/_pickle_support
ericvrp at codespeak.net
ericvrp at codespeak.net
Tue Apr 25 10:51:22 CEST 2006
Author: ericvrp
Date: Tue Apr 25 10:51:14 2006
New Revision: 26310
Added:
pypy/dist/pypy/module/_pickle_support/
pypy/dist/pypy/module/_pickle_support/__init__.py (contents, props changed)
pypy/dist/pypy/module/_pickle_support/maker.py (contents, props changed)
Modified:
pypy/dist/pypy/interpreter/baseobjspace.py
pypy/dist/pypy/interpreter/nestedscope.py
pypy/dist/pypy/interpreter/test/test_pickle.py
pypy/dist/pypy/interpreter/typedef.py
Log:
(pedronis, ericvrp)
Added Cell pickling! We had to jump though a few loops but all-in-all it was not too hard.
Added Cell.__eq__ as well.
Modified: pypy/dist/pypy/interpreter/baseobjspace.py
==============================================================================
--- pypy/dist/pypy/interpreter/baseobjspace.py (original)
+++ pypy/dist/pypy/interpreter/baseobjspace.py Tue Apr 25 10:51:14 2006
@@ -193,6 +193,7 @@
modules.extend(['unicodedata', '_codecs',
'array', 'marshal', 'errno', 'math', '_sre'])
+ modules.append('_pickle_support')
if self.options.nofaking:
modules.append('posix')
Modified: pypy/dist/pypy/interpreter/nestedscope.py
==============================================================================
--- pypy/dist/pypy/interpreter/nestedscope.py (original)
+++ pypy/dist/pypy/interpreter/nestedscope.py Tue Apr 25 10:51:14 2006
@@ -27,7 +27,23 @@
if self.w_value is None:
raise ValueError, "delete() on an empty cell"
self.w_value = None
-
+
+ def descr__eq__(self, space, w_other):
+ other = space.interpclass_w(w_other)
+ if not isinstance(other, Cell):
+ return space.w_False
+ return space.eq(self.w_value, other.w_value)
+
+ def descr__reduce__(self, space):
+ cell_new = space.getbuiltinmodule('_pickle_support').get('cell_new')
+ if self.w_value is None: #when would this happen?
+ return space.newtuple([cell_new, space.newtuple([])])
+ return space.newtuple([cell_new, space.newtuple([]),
+ space.newtuple([self.w_value])])
+
+ def descr__setstate__(self, space, w_state):
+ self.w_value = space.getitem(w_state, space.wrap(0))
+
def __repr__(self):
""" representation for debugging purposes """
if self.w_value is None:
Modified: pypy/dist/pypy/interpreter/test/test_pickle.py
==============================================================================
--- pypy/dist/pypy/interpreter/test/test_pickle.py (original)
+++ pypy/dist/pypy/interpreter/test/test_pickle.py Tue Apr 25 10:51:14 2006
@@ -1,27 +1,27 @@
-import py
-import pickle
-
-def test_pickle_cell():
- py.test.skip("cell pickling is work in progress")
- def g():
- x = None
- def f():
- return x
- return f.func_closure[0]
- try:
+class AppTestInterpObjectPickling:
+
+ def test_pickle_cell(self):
+ import pickle
+ def g():
+ x = [42]
+ def f():
+ x[0] += 1
+ return x
+ return f.func_closure[0]
cell = g()
- pickle.dumps(cell)
- except IndexError, e:
- raise
+ pckl = pickle.dumps(g())
+ result = pickle.loads(pckl)
+ assert cell == result
+ assert not (cell != result)
-def test_pickle_generator():
- py.test.skip("generator pickling is work in progress")
- def giveme(n):
- x = 0
- while x < n:
- yield x
- generator = giveme(10)
- pickle.dumps(generator)
+ #def test_pickle_generator(self):
+ # import pickle
+ # def giveme(n):
+ # x = 0
+ # while x < n:
+ # yield x
+ # generator = giveme(10)
+ # print pickle.dumps(generator)
#TODO: test pickling of code objects
#TODO: test pickling of function objects
Modified: pypy/dist/pypy/interpreter/typedef.py
==============================================================================
--- pypy/dist/pypy/interpreter/typedef.py (original)
+++ pypy/dist/pypy/interpreter/typedef.py Tue Apr 25 10:51:14 2006
@@ -539,7 +539,15 @@
gi_frame = interp_attrproperty('frame', cls=GeneratorIterator),
)
-Cell.typedef = TypeDef("cell")
+Cell.typedef = TypeDef("cell",
+ __eq__ = interp2app(Cell.descr__eq__,
+ unwrap_spec=['self', ObjSpace, W_Root]),
+ __ne__ = descr_generic_ne,
+ __reduce__ = interp2app(Cell.descr__reduce__,
+ unwrap_spec=['self', ObjSpace]),
+ __setstate__ = interp2app(Cell.descr__setstate__,
+ unwrap_spec=['self', ObjSpace, W_Root]),
+)
Ellipsis.typedef = TypeDef("Ellipsis",
__repr__ = interp2app(Ellipsis.descr__repr__),
Added: pypy/dist/pypy/module/_pickle_support/__init__.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/module/_pickle_support/__init__.py Tue Apr 25 10:51:14 2006
@@ -0,0 +1,11 @@
+from pypy.interpreter.mixedmodule import MixedModule
+
+class Module(MixedModule):
+ """Built-in functions, exceptions, and other objects."""
+
+ appleveldefs = {
+ }
+
+ interpleveldefs = {
+ 'cell_new': 'maker.cell_new'
+ }
Added: pypy/dist/pypy/module/_pickle_support/maker.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/module/_pickle_support/maker.py Tue Apr 25 10:51:14 2006
@@ -0,0 +1,9 @@
+from pypy.interpreter.nestedscope import Cell
+
+#note: for now we don't use the actual value when creating the Cell.
+# (i.e. we assume it will be handled by __setstate__)
+# Stackless does use this so it might be needed here as well.
+
+def cell_new(space):
+ return space.wrap(Cell())
+#cell_new.unwrap_spec = [...]
More information about the Pypy-commit
mailing list