[Python-3000-checkins] r57775 - in python/branches/py3k: Lib/test/test_builtin.py Lib/test/test_long.py Lib/test/test_unicode.py Objects/stringlib/formatter.h
eric.smith
python-3000-checkins at python.org
Fri Aug 31 00:23:08 CEST 2007
Author: eric.smith
Date: Fri Aug 31 00:23:08 2007
New Revision: 57775
Modified:
python/branches/py3k/Lib/test/test_builtin.py
python/branches/py3k/Lib/test/test_long.py
python/branches/py3k/Lib/test/test_unicode.py
python/branches/py3k/Objects/stringlib/formatter.h
Log:
Added format tests.
Fixed bug in alignment of negative numbers.
Whitespace normalization.
Modified: python/branches/py3k/Lib/test/test_builtin.py
==============================================================================
--- python/branches/py3k/Lib/test/test_builtin.py (original)
+++ python/branches/py3k/Lib/test/test_builtin.py Fri Aug 31 00:23:08 2007
@@ -540,6 +540,7 @@
self.assertEqual(format(3.1415e-104, ""), str(3.1415e-104))
self.assertEqual(format(-3.1415e-104, ""), str(-3.1415e-104))
self.assertEqual(format(object, ""), str(object))
+ self.assertEqual(format(None, ""), str(None))
# TypeError because self.__format__ returns the wrong type
self.assertRaises(TypeError, format, H(), "")
Modified: python/branches/py3k/Lib/test/test_long.py
==============================================================================
--- python/branches/py3k/Lib/test/test_long.py (original)
+++ python/branches/py3k/Lib/test/test_long.py Fri Aug 31 00:23:08 2007
@@ -497,13 +497,25 @@
self.assertEqual(format(123456789, 'd'), '123456789')
self.assertEqual(format(123456789, 'd'), '123456789')
+ # sign and aligning are interdependent
+ self.assertEqual(format(1, "-"), '1')
+ self.assertEqual(format(-1, "-"), '-1')
+ self.assertEqual(format(1, "-3"), ' 1')
+ self.assertEqual(format(-1, "-3"), ' -1')
+ self.assertEqual(format(1, "+3"), ' +1')
+ self.assertEqual(format(-1, "+3"), ' -1')
+ self.assertEqual(format(1, " 3"), ' 1')
+ self.assertEqual(format(-1, " 3"), ' -1')
+ self.assertEqual(format(1, " "), ' 1')
+ self.assertEqual(format(-1, " "), '-1')
+
# hex
self.assertEqual(format(3, "x"), "3")
self.assertEqual(format(3, "X"), "3")
self.assertEqual(format(1234, "x"), "4d2")
self.assertEqual(format(-1234, "x"), "-4d2")
self.assertEqual(format(1234, "8x"), " 4d2")
-# XXX fix self.assertEqual(format(-1234, "8x"), " -4d2")
+ self.assertEqual(format(-1234, "8x"), " -4d2")
self.assertEqual(format(1234, "x"), "4d2")
self.assertEqual(format(-1234, "x"), "-4d2")
self.assertEqual(format(-3, "x"), "-3")
@@ -530,7 +542,6 @@
# make sure these are errors
self.assertRaises(ValueError, format, 3, "1.3") # precision disallowed
- return
self.assertRaises(ValueError, format, 3, "+c") # sign not allowed
# with 'c'
self.assertRaises(ValueError, format, 3, "R") # bogus format type
Modified: python/branches/py3k/Lib/test/test_unicode.py
==============================================================================
--- python/branches/py3k/Lib/test/test_unicode.py (original)
+++ python/branches/py3k/Lib/test/test_unicode.py Fri Aug 31 00:23:08 2007
@@ -379,8 +379,6 @@
self.assertEqual("The year is {0.year}".format(d),
"The year is 2007")
- #"{0!r:20}".format("Hello")
-
# classes we'll use for testing
class C:
def __init__(self, x=100):
@@ -428,6 +426,10 @@
def __format__(self, format_spec):
return self.strftime(format_spec)
+ class J(int):
+ def __format__(self, format_spec):
+ return int.__format__(self * 2, format_spec)
+
self.assertEqual(''.format(), '')
self.assertEqual('abc'.format(), 'abc')
@@ -465,12 +467,6 @@
self.assertEqual('{0[1][0]}'.format(['abc', ['def']]), 'def')
self.assertEqual('{0[1][0].x}'.format(['abc', [D('def')]]), 'def')
- # I'm not sure if this should work, or if it's a problem if it does work
- #'{0[_{foo}]}'.format({'_FOO': 'abc'}, foo='FOO')
- #('{0[{foo}{bar}]}'.format({'FOOBAR': 'abc'}, foo='FOO', bar='BAR')
-
- # format specifiers for built in types
-
# strings
self.assertEqual('{0:.3s}'.format('abc'), 'abc')
self.assertEqual('{0:.3s}'.format('ab'), 'ab')
@@ -524,6 +520,10 @@
day=27)),
"date: 2007-08-27")
+ # test deriving from a builtin type and overriding __format__
+ self.assertEqual("{0}".format(J(10)), "20")
+
+
# string format specifiers
self.assertEqual('{0:}'.format('a'), 'a')
Modified: python/branches/py3k/Objects/stringlib/formatter.h
==============================================================================
--- python/branches/py3k/Objects/stringlib/formatter.h (original)
+++ python/branches/py3k/Objects/stringlib/formatter.h Fri Aug 31 00:23:08 2007
@@ -145,8 +145,7 @@
}
/* The special case for 0-padding (backwards compat) */
- if (format->fill_char == '\0' &&
- end-ptr >= 1 && ptr[0] == '0') {
+ if (format->fill_char == '\0' && end-ptr >= 1 && ptr[0] == '0') {
format->fill_char = '0';
if (format->align == '\0') {
format->align = '=';
@@ -298,9 +297,10 @@
r->n_lpadding = padding / 2;
r->n_rpadding = padding - r->n_lpadding;
}
- else
- /* must be '=' */
+ else if (format->align == '=')
r->n_spadding = padding;
+ else
+ r->n_lpadding = padding;
}
}
r->n_total = r->n_lpadding + r->n_lsign + r->n_spadding +
@@ -603,7 +603,7 @@
register Py_ssize_t i;
Py_ssize_t len = strlen(charbuffer);
for (i = len - 1; i >= 0; i--)
- buffer[i] = (Py_UNICODE) charbuffer[i];
+ buffer[i] = (Py_UNICODE) charbuffer[i];
return len;
}
@@ -641,14 +641,14 @@
/* Worst case length calc to ensure no buffer overrun:
'g' formats:
- fmt = %#.<prec>g
- buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp
- for any double rep.)
- len = 1 + prec + 1 + 2 + 5 = 9 + prec
+ fmt = %#.<prec>g
+ buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp
+ for any double rep.)
+ len = 1 + prec + 1 + 2 + 5 = 9 + prec
'f' formats:
- buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50)
- len = 1 + 50 + 1 + prec = 52 + prec
+ buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50)
+ len = 1 + 50 + 1 + prec = 52 + prec
If prec=0 the effective precision is 1 (the leading digit is
always given), therefore increase the length by one.
@@ -678,7 +678,7 @@
x = PyFloat_AsDouble(value);
if (x == -1.0 && PyErr_Occurred())
- goto done;
+ goto done;
if (type == '%') {
type = 'f';
@@ -687,9 +687,9 @@
}
if (precision < 0)
- precision = 6;
+ precision = 6;
if (type == 'f' && (fabs(x) / 1e25) >= 1e25)
- type = 'g';
+ type = 'g';
/* cast "type", because if we're in unicode we need to pass a
8-bit char. this is safe, because we've restricted what "type"
More information about the Python-3000-checkins
mailing list