Author: Armin Rigo <[email protected]>
Branch: py3.5
Changeset: r88282:254669bd5f21
Date: 2016-11-09 18:10 +0100
http://bitbucket.org/pypy/pypy/changeset/254669bd5f21/
Log: Small tweaks until the tests pass on pypy too
diff --git a/pypy/objspace/std/formatting.py b/pypy/objspace/std/formatting.py
--- a/pypy/objspace/std/formatting.py
+++ b/pypy/objspace/std/formatting.py
@@ -213,7 +213,7 @@
if self.peekchr() == '.':
self.forward()
- self.prec = self.peel_num('prec', INT_MAX)
+ self.prec = self.peel_num('precision', INT_MAX)
if self.prec < 0:
self.prec = 0 # this can happen: '%.*f' % (-5, 3)
else:
@@ -259,7 +259,7 @@
w_value = self.nextinputvalue()
if name == 'width':
return space.int_w(w_value)
- elif name == 'prec':
+ elif name == 'precision':
return space.c_int_w(w_value)
else:
assert False
@@ -460,18 +460,12 @@
def fmt_c(self, w_value):
self.prec = -1 # just because
space = self.space
- if space.isinstance_w(w_value, space.w_str):
- s = space.str_w(w_value)
- if len(s) != 1:
- raise oefmt(space.w_TypeError, "%c requires int or char")
- self.std_wp(s)
- elif space.isinstance_w(w_value, space.w_unicode):
- if not do_unicode:
- raise NeedUnicodeFormattingError
- ustr = space.unicode_w(w_value)
- if len(ustr) != 1:
- raise oefmt(space.w_TypeError, "%c requires int or
unichar")
- self.std_wp(ustr)
+ try:
+ w_value = space.index(w_value)
+ except OperationError as e:
+ if e.async(space):
+ raise
+ # otherwise, eats all exceptions, like CPython
else:
n = space.int_w(w_value)
if do_unicode:
@@ -488,6 +482,23 @@
raise oefmt(space.w_OverflowError,
"character code not in range(256)")
self.std_wp(s)
+ return
+ if space.isinstance_w(w_value, space.w_str):
+ s = space.str_w(w_value)
+ if len(s) == 1:
+ self.std_wp(s)
+ return
+ elif space.isinstance_w(w_value, space.w_unicode):
+ if not do_unicode:
+ raise NeedUnicodeFormattingError
+ ustr = space.unicode_w(w_value)
+ if len(ustr) == 1:
+ self.std_wp(ustr)
+ return
+ if do_unicode:
+ raise oefmt(space.w_TypeError, "%c requires int or char")
+ else:
+ raise oefmt(space.w_TypeError, "%c requires int or single
byte")
def fmt_b(self, w_value):
space = self.space
@@ -581,18 +592,22 @@
# make sure that w_value is a wrapped integer
return space.int(w_value)
+def maybe_index(space, w_value):
+ return space.index(w_value)
+
def maybe_float(space, w_value):
# make sure that w_value is a wrapped float
return space.float(w_value)
-def format_num_helper_generator(fmt, digits):
+def format_num_helper_generator(fmt, digits, decoder=maybe_int,
+ expect_text="a number"):
def format_num_helper(space, w_value):
try:
- w_value = maybe_int(space, w_value)
+ w_value = decoder(space, w_value)
except OperationError:
raise oefmt(space.w_TypeError,
- "%s format: a number is required, not %T",
- fmt, w_value)
+ "%s format: %s is required, not %T",
+ fmt, expect_text, w_value)
try:
value = space.int_w(w_value)
return fmt % (value,)
@@ -605,5 +620,7 @@
'base%d_num_helper' % len(digits))
int_num_helper = format_num_helper_generator('%d', '0123456789')
-oct_num_helper = format_num_helper_generator('%o', '01234567')
-hex_num_helper = format_num_helper_generator('%x', '0123456789abcdef')
+oct_num_helper = format_num_helper_generator('%o', '01234567',
+ decoder=maybe_index, expect_text="an integer")
+hex_num_helper = format_num_helper_generator('%x', '0123456789abcdef',
+ decoder=maybe_index, expect_text="an integer")
diff --git a/pypy/objspace/std/test/test_stringformat.py
b/pypy/objspace/std/test/test_stringformat.py
--- a/pypy/objspace/std/test/test_stringformat.py
+++ b/pypy/objspace/std/test/test_stringformat.py
@@ -78,10 +78,14 @@
raises(TypeError, '%d'.__mod__, s)
def test_format_float(self):
- f = 23.456
- assert '23' == '%d' % f
+ f = -23.456
+ assert '-23' == '%d' % f
+ assert '-23' == '%i' % f
+ assert '-23' == '%u' % f
raises(TypeError, "'%x' % f")
- assert '23.456' == '%s' % f
+ raises(TypeError, "'%X' % f")
+ raises(TypeError, "'%o' % f")
+ assert '-23.456' == '%s' % f
# for 'r' use a float that has an exact decimal rep:
g = 23.125
assert '23.125' == '%r' % g
@@ -194,6 +198,7 @@
exc = raises(TypeError, "%x".__mod__, IndexFails())
expected = "%x format: an integer is required, not IndexFails"
assert str(exc.value) == expected
+ raises(TypeError, "%c".__mod__, IndexFails())
def test_formatting_huge_precision(self):
prec = 2**31
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit