[pypy-commit] pypy py3.3: add custom __repr__ to Cell
numerodix
noreply at buildbot.pypy.org
Sun Jul 27 11:56:18 CEST 2014
Author: Martin Matusiak <numerodix at gmail.com>
Branch: py3.3
Changeset: r72556:c799b96b7ec5
Date: 2014-07-27 11:45 +0200
http://bitbucket.org/pypy/pypy/changeset/c799b96b7ec5/
Log: add custom __repr__ to Cell
diff --git a/pypy/interpreter/nestedscope.py b/pypy/interpreter/nestedscope.py
--- a/pypy/interpreter/nestedscope.py
+++ b/pypy/interpreter/nestedscope.py
@@ -71,6 +71,14 @@
return "<%s(%s) at 0x%x>" % (self.__class__.__name__,
content, uid(self))
+ def descr__repr__(self, space):
+ if self.w_value is None:
+ content = "empty"
+ else:
+ content = "%s object at 0x%x" % (space.type(self.w_value).name, uid(self.w_value))
+ s = "<cell at 0x%x: %s>" % (uid(self), content)
+ return space.wrap(s.decode('utf-8'))
+
def descr__cell_contents(self, space):
try:
return self.get()
diff --git a/pypy/interpreter/test/test_nestedscope.py b/pypy/interpreter/test/test_nestedscope.py
--- a/pypy/interpreter/test/test_nestedscope.py
+++ b/pypy/interpreter/test/test_nestedscope.py
@@ -59,6 +59,28 @@
def test_lambda_in_genexpr(self):
assert [x() for x in (lambda: x for x in range(10))] == list(range(10))
+ def test_cell_repr(self):
+ import re
+ from reprlib import repr as r # Don't shadow builtin repr
+
+ def get_cell():
+ x = 42
+ def inner():
+ return x
+ return inner
+ x = get_cell().__closure__[0]
+ assert re.match(r'<cell at 0x[0-9A-Fa-f]+: int object at 0x[0-9A-Fa-f]+>', repr(x))
+ assert re.match(r'<cell at 0x.*\.\.\..*>', r(x))
+
+ def get_cell():
+ if False:
+ x = 42
+ def inner():
+ return x
+ return inner
+ x = get_cell().__closure__[0]
+ assert re.match(r'<cell at 0x[0-9A-Fa-f]+: empty>', repr(x))
+
def test_cell_contents(self):
def f(x):
def f(y):
diff --git a/pypy/interpreter/typedef.py b/pypy/interpreter/typedef.py
--- a/pypy/interpreter/typedef.py
+++ b/pypy/interpreter/typedef.py
@@ -944,6 +944,7 @@
__eq__ = interp2app(Cell.descr__eq__),
__hash__ = None,
__reduce__ = interp2app(Cell.descr__reduce__),
+ __repr__ = interp2app(Cell.descr__repr__),
__setstate__ = interp2app(Cell.descr__setstate__),
cell_contents= GetSetProperty(Cell.descr__cell_contents, cls=Cell),
)
More information about the pypy-commit
mailing list