[pypy-svn] r75181 - in pypy/branch/fast-forward/pypy/objspace/std: . test
benjamin at codespeak.net
benjamin at codespeak.net
Mon Jun 7 20:16:25 CEST 2010
Author: benjamin
Date: Mon Jun 7 20:16:23 2010
New Revision: 75181
Modified:
pypy/branch/fast-forward/pypy/objspace/std/default.py
pypy/branch/fast-forward/pypy/objspace/std/objecttype.py
pypy/branch/fast-forward/pypy/objspace/std/test/test_obj.py
Log:
add default __format__ to object
Modified: pypy/branch/fast-forward/pypy/objspace/std/default.py
==============================================================================
--- pypy/branch/fast-forward/pypy/objspace/std/default.py (original)
+++ pypy/branch/fast-forward/pypy/objspace/std/default.py Mon Jun 7 20:16:23 2010
@@ -45,14 +45,4 @@
raise OperationError(space.w_TypeError,
typed_unwrap_error_msg(space, "integer", w_obj))
-def format__ANY_ANY(space, w_obj, w_format_spec):
- if space.isinstance_w(w_format_spec, space.w_unicode):
- w_as_str = space.unicode(w_obj)
- elif space.isinstance_w(w_format_spec, space.w_str):
- w_as_str = space.str(w_obj)
- else:
- msg = "format_spec must be a string"
- raise OperationError(space.w_TypeError, space.wrap(msg))
- return space.format(w_as_str)
-
register_all(vars())
Modified: pypy/branch/fast-forward/pypy/objspace/std/objecttype.py
==============================================================================
--- pypy/branch/fast-forward/pypy/objspace/std/objecttype.py (original)
+++ pypy/branch/fast-forward/pypy/objspace/std/objecttype.py Mon Jun 7 20:16:23 2010
@@ -1,6 +1,7 @@
from pypy.interpreter.error import OperationError, operationerrfmt
from pypy.interpreter.typedef import GetSetProperty, default_identity_hash
from pypy.interpreter import gateway
+from pypy.interpreter.baseobjspace import ObjSpace
from pypy.objspace.descroperation import Object
from pypy.objspace.std.stdtypedef import StdTypeDef, no_hash_descr
from pypy.objspace.std.register_all import register_all
@@ -89,6 +90,17 @@
return space.call(w_reduce, space.newtuple([]))
return descr__reduce__(space, w_obj, proto)
+def descr___format__(space, w_obj, w_format_spec):
+ if space.isinstance_w(w_format_spec, space.w_unicode):
+ w_as_str = space.call_function(space.w_unicode, w_obj)
+ elif space.isinstance_w(w_format_spec, space.w_str):
+ w_as_str = space.str(w_obj)
+ else:
+ msg = "format_spec must be a string"
+ raise OperationError(space.w_TypeError, space.wrap(msg))
+ return space.format(w_as_str, w_format_spec)
+
+
app = gateway.applevel(r'''
def reduce_1(obj, proto):
import copy_reg
@@ -177,6 +189,8 @@
unwrap_spec=[gateway.ObjSpace,gateway.W_Root,int]),
__reduce__ = gateway.interp2app(descr__reduce__,
unwrap_spec=[gateway.ObjSpace,gateway.W_Root,int]),
+ __format__ = gateway.interp2app(descr___format__, unwrap_spec=[ObjSpace,
+ gateway.W_Root, gateway.W_Root]),
__init__ = gateway.interp2app(descr__init__,
unwrap_spec=[gateway.ObjSpace,gateway.W_Root,gateway.Arguments]),
)
Modified: pypy/branch/fast-forward/pypy/objspace/std/test/test_obj.py
==============================================================================
--- pypy/branch/fast-forward/pypy/objspace/std/test/test_obj.py (original)
+++ pypy/branch/fast-forward/pypy/objspace/std/test/test_obj.py Mon Jun 7 20:16:23 2010
@@ -46,3 +46,20 @@
return object.__reduce__(self) + (':-)',)
s = X().__reduce__()
assert s[-1] == ':-)'
+
+ def test_default_format(self):
+ class x(object):
+ def __str__(self):
+ return "Pickle"
+ def __unicode__(self):
+ return u"Cheese"
+ res = format(x())
+ assert res == "Pickle"
+ assert isinstance(res, str)
+ res = format(x(), u"")
+ assert res == u"Cheese"
+ assert isinstance(res, unicode)
+ del x.__unicode__
+ res = format(x(), u"")
+ assert res == u"Pickle"
+ assert isinstance(res, unicode)
More information about the Pypy-commit
mailing list