[pypy-commit] pypy py3k: Fix test_newformat
amauryfa
noreply at buildbot.pypy.org
Tue Oct 18 00:37:59 CEST 2011
Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: py3k
Changeset: r48168:f12a2184cae5
Date: 2011-10-17 22:50 +0200
http://bitbucket.org/pypy/pypy/changeset/f12a2184cae5/
Log: Fix test_newformat
diff --git a/pypy/objspace/std/newformat.py b/pypy/objspace/std/newformat.py
--- a/pypy/objspace/std/newformat.py
+++ b/pypy/objspace/std/newformat.py
@@ -324,7 +324,7 @@
if recursive:
spec = self._build_string(spec_start, end, level)
w_rendered = self.space.format(w_obj, self.space.wrap(spec))
- unwrapper = "unicode_w" if self.is_unicode else "str_w"
+ unwrapper = "unicode_w" if self.is_unicode else "bytes_w"
to_interp = getattr(self.space, unwrapper)
return to_interp(w_rendered)
@@ -360,7 +360,7 @@
space.unicode_w(w_string))
return space.wrap(template.build(args))
else:
- template = str_template_formatter(space, space.str_w(w_string))
+ template = str_template_formatter(space, space.bytes_w(w_string))
return space.wrap(template.build(args))
@@ -534,8 +534,11 @@
w_msg = self.space.wrap(msg % (tp, self._type))
raise OperationError(self.space.w_ValueError, w_msg)
- def format_string(self, string):
+ def format_string(self, w_string):
space = self.space
+ if not space.isinstance_w(w_string, space.w_unicode):
+ w_string = space.str(w_string)
+ string = space.unicode_w(w_string)
if self._parse_spec("s", "<"):
return space.wrap(string)
if self._type != "s":
@@ -1133,21 +1136,12 @@
return Formatter
StrFormatter = make_formatting_class()
-UnicodeFormatter = make_formatting_class()
def unicode_formatter(space, spec):
return StrFormatter(space, True, spec)
-def str_formatter(space, spec):
- return UnicodeFormatter(space, False, spec)
-
-
@specialize.arg(2)
def run_formatter(space, w_format_spec, meth, *args):
- if space.isinstance_w(w_format_spec, space.w_unicode):
- formatter = unicode_formatter(space, space.unicode_w(w_format_spec))
- return getattr(formatter, meth)(*args)
- else:
- formatter = str_formatter(space, space.str_w(w_format_spec))
- return getattr(formatter, meth)(*args)
+ formatter = unicode_formatter(space, space.unicode_w(w_format_spec))
+ return getattr(formatter, meth)(*args)
diff --git a/pypy/objspace/std/stringobject.py b/pypy/objspace/std/stringobject.py
--- a/pypy/objspace/std/stringobject.py
+++ b/pypy/objspace/std/stringobject.py
@@ -988,13 +988,6 @@
encoding, errors = _get_encoding_and_errors(space, w_encoding, w_errors)
return decode_object(space, w_string, encoding, errors)
-def format__String_ANY(space, w_string, w_format_spec):
- if not space.isinstance_w(w_format_spec, space.w_str):
- w_format_spec = space.str(w_format_spec)
- spec = space.str_w(w_format_spec)
- formatter = newformat.str_formatter(space, spec)
- return formatter.format_string(w_string._value)
-
def buffer__String(space, w_string):
return space.wrap(StringBuffer(w_string._value))
diff --git a/pypy/objspace/std/test/test_newformat.py b/pypy/objspace/std/test/test_newformat.py
--- a/pypy/objspace/std/test/test_newformat.py
+++ b/pypy/objspace/std/test/test_newformat.py
@@ -179,9 +179,7 @@
return "32"
def __str__(self):
return "18"
- def __unicode__(self):
- return "42"
- assert self.s("{!s}").format(x()) == "42"
+ assert self.s("{!s}").format(x()) == "18"
assert self.s("{!r}").format(x()) == "32"
def test_non_latin1_key(self):
@@ -189,28 +187,11 @@
-class AppTestStringFormat(BaseStringFormatTests):
-
- def setup_class(cls):
- cls.w_s = cls.space.w_str
-
- def test_string_conversion(self):
- class x(object):
- def __repr__(self):
- return "32"
- def __str__(self):
- return "18"
- def __unicode__(self):
- return "42"
- assert self.s("{!s}").format(x()) == "18"
- assert self.s("{!r}").format(x()) == "32"
-
-
class BaseIntegralFormattingTest:
def test_simple(self):
assert format(self.i(2)) == "2"
- assert isinstance(format(self.i(2), u""), unicode)
+ assert isinstance(format(self.i(2), u""), str)
def test_invalid(self):
raises(ValueError, format, self.i(8), "s")
@@ -221,7 +202,7 @@
assert format(a, "c") == "a"
as_uni = format(a, u"c")
assert as_uni == u"a"
- assert isinstance(as_uni, unicode)
+ assert isinstance(as_uni, str)
raises(ValueError, format, a, "-c")
raises(ValueError, format, a, ",c")
assert format(a, "3c") == " a"
@@ -353,70 +334,74 @@
# undocumented API on string and unicode object, but used by string.py
def test_formatter_parser(self):
- l = list('abcd'._formatter_parser())
+ import _string
+ l = list(_string.formatter_parser('abcd'))
assert l == [('abcd', None, None, None)]
#
- l = list('ab{0}cd'._formatter_parser())
+ l = list(_string.formatter_parser('ab{0}cd'))
assert l == [('ab', '0', '', None), ('cd', None, None, None)]
#
- l = list('{0}cd'._formatter_parser())
+ l = list(_string.formatter_parser('{0}cd'))
assert l == [('', '0', '', None), ('cd', None, None, None)]
#
- l = list('ab{0}'._formatter_parser())
+ l = list(_string.formatter_parser('ab{0}'))
assert l == [('ab', '0', '', None)]
#
- l = list(''._formatter_parser())
+ l = list(_string.formatter_parser(''))
assert l == []
#
- l = list('{0:123}'._formatter_parser())
+ l = list(_string.formatter_parser('{0:123}'))
assert l == [('', '0', '123', None)]
#
- l = list('{0!x:123}'._formatter_parser())
+ l = list(_string.formatter_parser('{0!x:123}'))
assert l == [('', '0', '123', 'x')]
#
- l = list('{0!x:12{sdd}3}'._formatter_parser())
+ l = list(_string.formatter_parser('{0!x:12{sdd}3}'))
assert l == [('', '0', '12{sdd}3', 'x')]
def test_u_formatter_parser(self):
- l = list(u'{0!x:12{sdd}3}'._formatter_parser())
+ import _string
+ l = list(_string.formatter_parser('{0!x:12{sdd}3}'))
assert l == [(u'', u'0', u'12{sdd}3', u'x')]
for x in l[0]:
- assert isinstance(x, unicode)
+ assert isinstance(x, str)
def test_formatter_field_name_split(self):
- first, rest = ''._formatter_field_name_split()
+ import _string
+ first, rest = _string.formatter_field_name_split('')
assert first == ''
assert list(rest) == []
#
- first, rest = '31'._formatter_field_name_split()
+ first, rest = _string.formatter_field_name_split('31')
assert first == 31
assert list(rest) == []
#
- first, rest = 'foo'._formatter_field_name_split()
+ first, rest = _string.formatter_field_name_split('foo')
assert first == 'foo'
assert list(rest) == []
#
- first, rest = 'foo.bar'._formatter_field_name_split()
+ first, rest = _string.formatter_field_name_split('foo.bar')
assert first == 'foo'
assert list(rest) == [(True, 'bar')]
#
- first, rest = 'foo[123]'._formatter_field_name_split()
+ first, rest = _string.formatter_field_name_split('foo[123]')
assert first == 'foo'
assert list(rest) == [(False, 123)]
#
- first, rest = 'foo.baz[123].bok'._formatter_field_name_split()
+ first, rest = _string.formatter_field_name_split('foo.baz[123].bok')
assert first == 'foo'
assert list(rest) == [(True, 'baz'), (False, 123), (True, 'bok')]
#
- first, rest = 'foo.baz[hi].bok'._formatter_field_name_split()
+ first, rest = _string.formatter_field_name_split('foo.baz[hi].bok')
assert first == 'foo'
assert list(rest) == [(True, 'baz'), (False, 'hi'), (True, 'bok')]
def test_u_formatter_field_name_split(self):
- first, rest = u'foo.baz[hi].bok'._formatter_field_name_split()
+ import _string
+ first, rest = _string.formatter_field_name_split('foo.baz[hi].bok')
l = list(rest)
assert first == u'foo'
assert l == [(True, u'baz'), (False, u'hi'), (True, u'bok')]
- assert isinstance(first, unicode)
+ assert isinstance(first, str)
for x, y in l:
- assert isinstance(y, unicode)
+ assert isinstance(y, str)
diff --git a/pypy/objspace/std/unicodeobject.py b/pypy/objspace/std/unicodeobject.py
--- a/pypy/objspace/std/unicodeobject.py
+++ b/pypy/objspace/std/unicodeobject.py
@@ -827,14 +827,8 @@
def unicode_format__Unicode(space, w_unicode, __args__):
return newformat.format_method(space, w_unicode, __args__, True)
-def format__Unicode_ANY(space, w_unicode, w_format_spec):
- if not space.isinstance_w(w_format_spec, space.w_unicode):
- w_format_spec = space.call_function(space.w_unicode, w_format_spec)
- from pypy.objspace.std.unicodetype import unicode_from_object
- w_unicode = unicode_from_object(space, w_unicode)
- spec = space.unicode_w(w_format_spec)
- formatter = newformat.unicode_formatter(space, spec)
- return formatter.format_string(space.unicode_w(w_unicode))
+def format__Unicode_ANY(space, w_unicode, w_spec):
+ return newformat.run_formatter(space, w_spec, "format_string", w_unicode)
from pypy.objspace.std import unicodetype
More information about the pypy-commit
mailing list