ยปCore Development>Code coverage>Lib/test/test_format.py

# Python code coverage for Lib/test/test_format.py

#countcontent
1n/afrom test.support import verbose, TestFailed
2n/aimport locale
3n/aimport sys
4n/aimport test.support as support
5n/aimport unittest
6n/a
7n/amaxsize = support.MAX_Py_ssize_t
8n/a
9n/a# test string formatting operator (I am not sure if this is being tested
10n/a# elsewhere but, surely, some of the given cases are *not* tested because
11n/a# they crash python)
12n/a# test on bytes object as well
13n/a
14n/adef testformat(formatstr, args, output=None, limit=None, overflowok=False):
15n/a if verbose:
16n/a if output:
17n/a print("{!a} % {!a} =? {!a} ...".format(formatstr, args, output),
18n/a end=' ')
19n/a else:
20n/a print("{!a} % {!a} works? ...".format(formatstr, args), end=' ')
21n/a try:
22n/a result = formatstr % args
23n/a except OverflowError:
24n/a if not overflowok:
25n/a raise
26n/a if verbose:
27n/a print('overflow (this is fine)')
28n/a else:
29n/a if output and limit is None and result != output:
30n/a if verbose:
31n/a print('no')
32n/a raise AssertionError("%r %% %r == %r != %r" %
33n/a (formatstr, args, result, output))
34n/a # when 'limit' is specified, it determines how many characters
35n/a # must match exactly; lengths must always match.
36n/a # ex: limit=5, '12345678' matches '12345___'
37n/a # (mainly for floating point format tests for which an exact match
38n/a # can't be guaranteed due to rounding and representation errors)
39n/a elif output and limit is not None and (
40n/a len(result)!=len(output) or result[:limit]!=output[:limit]):
41n/a if verbose:
42n/a print('no')
43n/a print("%s %% %s == %s != %s" % \
44n/a (repr(formatstr), repr(args), repr(result), repr(output)))
45n/a else:
46n/a if verbose:
47n/a print('yes')
48n/a
49n/adef testcommon(formatstr, args, output=None, limit=None, overflowok=False):
50n/a # if formatstr is a str, test str, bytes, and bytearray;
51n/a # otherwise, test bytes and bytearry
52n/a if isinstance(formatstr, str):
53n/a testformat(formatstr, args, output, limit, overflowok)
54n/a b_format = formatstr.encode('ascii')
55n/a else:
56n/a b_format = formatstr
57n/a ba_format = bytearray(b_format)
58n/a b_args = []
59n/a if not isinstance(args, tuple):
60n/a args = (args, )
61n/a b_args = tuple(args)
62n/a if output is None:
63n/a b_output = ba_output = None
64n/a else:
65n/a if isinstance(output, str):
66n/a b_output = output.encode('ascii')
67n/a else:
68n/a b_output = output
69n/a ba_output = bytearray(b_output)
70n/a testformat(b_format, b_args, b_output, limit, overflowok)
71n/a testformat(ba_format, b_args, ba_output, limit, overflowok)
72n/a
73n/a
74n/aclass FormatTest(unittest.TestCase):
75n/a
76n/a def test_common_format(self):
77n/a # test the format identifiers that work the same across
78n/a # str, bytes, and bytearrays (integer, float, oct, hex)
79n/a testcommon("%.1d", (1,), "1")
80n/a testcommon("%.*d", (sys.maxsize,1), overflowok=True) # expect overflow
81n/a testcommon("%.100d", (1,), '00000000000000000000000000000000000000'
82n/a '000000000000000000000000000000000000000000000000000000'
83n/a '00000001', overflowok=True)
84n/a testcommon("%#.117x", (1,), '0x00000000000000000000000000000000000'
85n/a '000000000000000000000000000000000000000000000000000000'
86n/a '0000000000000000000000000001',
87n/a overflowok=True)
88n/a testcommon("%#.118x", (1,), '0x00000000000000000000000000000000000'
89n/a '000000000000000000000000000000000000000000000000000000'
90n/a '00000000000000000000000000001',
91n/a overflowok=True)
92n/a
93n/a testcommon("%f", (1.0,), "1.000000")
94n/a # these are trying to test the limits of the internal magic-number-length
95n/a # formatting buffer, if that number changes then these tests are less
96n/a # effective
97n/a testcommon("%#.*g", (109, -1.e+49/3.))
98n/a testcommon("%#.*g", (110, -1.e+49/3.))
99n/a testcommon("%#.*g", (110, -1.e+100/3.))
100n/a # test some ridiculously large precision, expect overflow
101n/a testcommon('%12.*f', (123456, 1.0))
102n/a
103n/a # check for internal overflow validation on length of precision
104n/a # these tests should no longer cause overflow in Python
105n/a # 2.7/3.1 and later.
106n/a testcommon("%#.*g", (110, -1.e+100/3.))
107n/a testcommon("%#.*G", (110, -1.e+100/3.))
108n/a testcommon("%#.*f", (110, -1.e+100/3.))
109n/a testcommon("%#.*F", (110, -1.e+100/3.))
110n/a # Formatting of integers. Overflow is not ok
111n/a testcommon("%x", 10, "a")
112n/a testcommon("%x", 100000000000, "174876e800")
113n/a testcommon("%o", 10, "12")
114n/a testcommon("%o", 100000000000, "1351035564000")
115n/a testcommon("%d", 10, "10")
116n/a testcommon("%d", 100000000000, "100000000000")
117n/a
118n/a big = 123456789012345678901234567890
119n/a testcommon("%d", big, "123456789012345678901234567890")
120n/a testcommon("%d", -big, "-123456789012345678901234567890")
121n/a testcommon("%5d", -big, "-123456789012345678901234567890")
122n/a testcommon("%31d", -big, "-123456789012345678901234567890")
123n/a testcommon("%32d", -big, " -123456789012345678901234567890")
124n/a testcommon("%-32d", -big, "-123456789012345678901234567890 ")
125n/a testcommon("%032d", -big, "-0123456789012345678901234567890")
126n/a testcommon("%-032d", -big, "-123456789012345678901234567890 ")
127n/a testcommon("%034d", -big, "-000123456789012345678901234567890")
128n/a testcommon("%034d", big, "0000123456789012345678901234567890")
129n/a testcommon("%0+34d", big, "+000123456789012345678901234567890")
130n/a testcommon("%+34d", big, " +123456789012345678901234567890")
131n/a testcommon("%34d", big, " 123456789012345678901234567890")
132n/a testcommon("%.2d", big, "123456789012345678901234567890")
133n/a testcommon("%.30d", big, "123456789012345678901234567890")
134n/a testcommon("%.31d", big, "0123456789012345678901234567890")
135n/a testcommon("%32.31d", big, " 0123456789012345678901234567890")
136n/a testcommon("%d", float(big), "123456________________________", 6)
137n/a
138n/a big = 0x1234567890abcdef12345 # 21 hex digits
139n/a testcommon("%x", big, "1234567890abcdef12345")
140n/a testcommon("%x", -big, "-1234567890abcdef12345")
141n/a testcommon("%5x", -big, "-1234567890abcdef12345")
142n/a testcommon("%22x", -big, "-1234567890abcdef12345")
143n/a testcommon("%23x", -big, " -1234567890abcdef12345")
144n/a testcommon("%-23x", -big, "-1234567890abcdef12345 ")
145n/a testcommon("%023x", -big, "-01234567890abcdef12345")
146n/a testcommon("%-023x", -big, "-1234567890abcdef12345 ")
147n/a testcommon("%025x", -big, "-0001234567890abcdef12345")
148n/a testcommon("%025x", big, "00001234567890abcdef12345")
149n/a testcommon("%0+25x", big, "+0001234567890abcdef12345")
150n/a testcommon("%+25x", big, " +1234567890abcdef12345")
151n/a testcommon("%25x", big, " 1234567890abcdef12345")
152n/a testcommon("%.2x", big, "1234567890abcdef12345")
153n/a testcommon("%.21x", big, "1234567890abcdef12345")
154n/a testcommon("%.22x", big, "01234567890abcdef12345")
155n/a testcommon("%23.22x", big, " 01234567890abcdef12345")
156n/a testcommon("%-23.22x", big, "01234567890abcdef12345 ")
157n/a testcommon("%X", big, "1234567890ABCDEF12345")
158n/a testcommon("%#X", big, "0X1234567890ABCDEF12345")
159n/a testcommon("%#x", big, "0x1234567890abcdef12345")
160n/a testcommon("%#x", -big, "-0x1234567890abcdef12345")
161n/a testcommon("%#27x", big, " 0x1234567890abcdef12345")
162n/a testcommon("%#-27x", big, "0x1234567890abcdef12345 ")
163n/a testcommon("%#027x", big, "0x00001234567890abcdef12345")
164n/a testcommon("%#.23x", big, "0x001234567890abcdef12345")
165n/a testcommon("%#.23x", -big, "-0x001234567890abcdef12345")
166n/a testcommon("%#27.23x", big, " 0x001234567890abcdef12345")
167n/a testcommon("%#-27.23x", big, "0x001234567890abcdef12345 ")
168n/a testcommon("%#027.23x", big, "0x00001234567890abcdef12345")
169n/a testcommon("%#+.23x", big, "+0x001234567890abcdef12345")
170n/a testcommon("%# .23x", big, " 0x001234567890abcdef12345")
171n/a testcommon("%#+.23X", big, "+0X001234567890ABCDEF12345")
172n/a # next one gets two leading zeroes from precision, and another from the
173n/a # 0 flag and the width
174n/a testcommon("%#+027.23X", big, "+0X0001234567890ABCDEF12345")
175n/a testcommon("%# 027.23X", big, " 0X0001234567890ABCDEF12345")
176n/a # same, except no 0 flag
177n/a testcommon("%#+27.23X", big, " +0X001234567890ABCDEF12345")
178n/a testcommon("%#-+27.23x", big, "+0x001234567890abcdef12345 ")
179n/a testcommon("%#- 27.23x", big, " 0x001234567890abcdef12345 ")
180n/a
181n/a big = 0o12345670123456701234567012345670 # 32 octal digits
182n/a testcommon("%o", big, "12345670123456701234567012345670")
183n/a testcommon("%o", -big, "-12345670123456701234567012345670")
184n/a testcommon("%5o", -big, "-12345670123456701234567012345670")
185n/a testcommon("%33o", -big, "-12345670123456701234567012345670")
186n/a testcommon("%34o", -big, " -12345670123456701234567012345670")
187n/a testcommon("%-34o", -big, "-12345670123456701234567012345670 ")
188n/a testcommon("%034o", -big, "-012345670123456701234567012345670")
189n/a testcommon("%-034o", -big, "-12345670123456701234567012345670 ")
190n/a testcommon("%036o", -big, "-00012345670123456701234567012345670")
191n/a testcommon("%036o", big, "000012345670123456701234567012345670")
192n/a testcommon("%0+36o", big, "+00012345670123456701234567012345670")
193n/a testcommon("%+36o", big, " +12345670123456701234567012345670")
194n/a testcommon("%36o", big, " 12345670123456701234567012345670")
195n/a testcommon("%.2o", big, "12345670123456701234567012345670")
196n/a testcommon("%.32o", big, "12345670123456701234567012345670")
197n/a testcommon("%.33o", big, "012345670123456701234567012345670")
198n/a testcommon("%34.33o", big, " 012345670123456701234567012345670")
199n/a testcommon("%-34.33o", big, "012345670123456701234567012345670 ")
200n/a testcommon("%o", big, "12345670123456701234567012345670")
201n/a testcommon("%#o", big, "0o12345670123456701234567012345670")
202n/a testcommon("%#o", -big, "-0o12345670123456701234567012345670")
203n/a testcommon("%#38o", big, " 0o12345670123456701234567012345670")
204n/a testcommon("%#-38o", big, "0o12345670123456701234567012345670 ")
205n/a testcommon("%#038o", big, "0o000012345670123456701234567012345670")
206n/a testcommon("%#.34o", big, "0o0012345670123456701234567012345670")
207n/a testcommon("%#.34o", -big, "-0o0012345670123456701234567012345670")
208n/a testcommon("%#38.34o", big, " 0o0012345670123456701234567012345670")
209n/a testcommon("%#-38.34o", big, "0o0012345670123456701234567012345670 ")
210n/a testcommon("%#038.34o", big, "0o000012345670123456701234567012345670")
211n/a testcommon("%#+.34o", big, "+0o0012345670123456701234567012345670")
212n/a testcommon("%# .34o", big, " 0o0012345670123456701234567012345670")
213n/a testcommon("%#+38.34o", big, " +0o0012345670123456701234567012345670")
214n/a testcommon("%#-+38.34o", big, "+0o0012345670123456701234567012345670 ")
215n/a testcommon("%#- 38.34o", big, " 0o0012345670123456701234567012345670 ")
216n/a testcommon("%#+038.34o", big, "+0o00012345670123456701234567012345670")
217n/a testcommon("%# 038.34o", big, " 0o00012345670123456701234567012345670")
218n/a # next one gets one leading zero from precision
219n/a testcommon("%.33o", big, "012345670123456701234567012345670")
220n/a # base marker added in spite of leading zero (different to Python 2)
221n/a testcommon("%#.33o", big, "0o012345670123456701234567012345670")
222n/a # reduce precision, and base marker is always added
223n/a testcommon("%#.32o", big, "0o12345670123456701234567012345670")
224n/a # one leading zero from precision, plus two from "0" flag & width
225n/a testcommon("%035.33o", big, "00012345670123456701234567012345670")
226n/a # base marker shouldn't change the size
227n/a testcommon("%0#35.33o", big, "0o012345670123456701234567012345670")
228n/a
229n/a # Some small ints, in both Python int and flavors).
230n/a testcommon("%d", 42, "42")
231n/a testcommon("%d", -42, "-42")
232n/a testcommon("%d", 42.0, "42")
233n/a testcommon("%#x", 1, "0x1")
234n/a testcommon("%#X", 1, "0X1")
235n/a testcommon("%#o", 1, "0o1")
236n/a testcommon("%#o", 0, "0o0")
237n/a testcommon("%o", 0, "0")
238n/a testcommon("%d", 0, "0")
239n/a testcommon("%#x", 0, "0x0")
240n/a testcommon("%#X", 0, "0X0")
241n/a testcommon("%x", 0x42, "42")
242n/a testcommon("%x", -0x42, "-42")
243n/a testcommon("%o", 0o42, "42")
244n/a testcommon("%o", -0o42, "-42")
245n/a # alternate float formatting
246n/a testcommon('%g', 1.1, '1.1')
247n/a testcommon('%#g', 1.1, '1.10000')
248n/a
249n/a def test_str_format(self):
250n/a testformat("%r", "\u0378", "'\\u0378'") # non printable
251n/a testformat("%a", "\u0378", "'\\u0378'") # non printable
252n/a testformat("%r", "\u0374", "'\u0374'") # printable
253n/a testformat("%a", "\u0374", "'\\u0374'") # printable
254n/a
255n/a # Test exception for unknown format characters, etc.
256n/a if verbose:
257n/a print('Testing exceptions')
258n/a def test_exc(formatstr, args, exception, excmsg):
259n/a try:
260n/a testformat(formatstr, args)
261n/a except exception as exc:
262n/a if str(exc) == excmsg:
263n/a if verbose:
264n/a print("yes")
265n/a else:
266n/a if verbose: print('no')
267n/a print('Unexpected ', exception, ':', repr(str(exc)))
268n/a except:
269n/a if verbose: print('no')
270n/a print('Unexpected exception')
271n/a raise
272n/a else:
273n/a raise TestFailed('did not get expected exception: %s' % excmsg)
274n/a test_exc('abc %b', 1, ValueError,
275n/a "unsupported format character 'b' (0x62) at index 5")
276n/a #test_exc(unicode('abc %\u3000','raw-unicode-escape'), 1, ValueError,
277n/a # "unsupported format character '?' (0x3000) at index 5")
278n/a test_exc('%d', '1', TypeError, "%d format: a number is required, not str")
279n/a test_exc('%x', '1', TypeError, "%x format: an integer is required, not str")
280n/a test_exc('%x', 3.14, TypeError, "%x format: an integer is required, not float")
281n/a test_exc('%g', '1', TypeError, "must be real number, not str")
282n/a test_exc('no format', '1', TypeError,
283n/a "not all arguments converted during string formatting")
284n/a test_exc('%c', -1, OverflowError, "%c arg not in range(0x110000)")
285n/a test_exc('%c', sys.maxunicode+1, OverflowError,
286n/a "%c arg not in range(0x110000)")
287n/a #test_exc('%c', 2**128, OverflowError, "%c arg not in range(0x110000)")
288n/a test_exc('%c', 3.14, TypeError, "%c requires int or char")
289n/a test_exc('%c', 'ab', TypeError, "%c requires int or char")
290n/a test_exc('%c', b'x', TypeError, "%c requires int or char")
291n/a
292n/a if maxsize == 2**31-1:
293n/a # crashes 2.2.1 and earlier:
294n/a try:
295n/a "%*d"%(maxsize, -127)
296n/a except MemoryError:
297n/a pass
298n/a else:
299n/a raise TestFailed('"%*d"%(maxsize, -127) should fail')
300n/a
301n/a def test_bytes_and_bytearray_format(self):
302n/a # %c will insert a single byte, either from an int in range(256), or
303n/a # from a bytes argument of length 1, not from a str.
304n/a testcommon(b"%c", 7, b"\x07")
305n/a testcommon(b"%c", b"Z", b"Z")
306n/a testcommon(b"%c", bytearray(b"Z"), b"Z")
307n/a testcommon(b"%5c", 65, b" A")
308n/a testcommon(b"%-5c", 65, b"A ")
309n/a # %b will insert a series of bytes, either from a type that supports
310n/a # the Py_buffer protocol, or something that has a __bytes__ method
311n/a class FakeBytes(object):
312n/a def __bytes__(self):
313n/a return b'123'
314n/a fb = FakeBytes()
315n/a testcommon(b"%b", b"abc", b"abc")
316n/a testcommon(b"%b", bytearray(b"def"), b"def")
317n/a testcommon(b"%b", fb, b"123")
318n/a # # %s is an alias for %b -- should only be used for Py2/3 code
319n/a testcommon(b"%s", b"abc", b"abc")
320n/a testcommon(b"%s", bytearray(b"def"), b"def")
321n/a testcommon(b"%s", fb, b"123")
322n/a # %a will give the equivalent of
323n/a # repr(some_obj).encode('ascii', 'backslashreplace')
324n/a testcommon(b"%a", 3.14, b"3.14")
325n/a testcommon(b"%a", b"ghi", b"b'ghi'")
326n/a testcommon(b"%a", "jkl", b"'jkl'")
327n/a testcommon(b"%a", "\u0544", b"'\\u0544'")
328n/a # %r is an alias for %a
329n/a testcommon(b"%r", 3.14, b"3.14")
330n/a testcommon(b"%r", b"ghi", b"b'ghi'")
331n/a testcommon(b"%r", "jkl", b"'jkl'")
332n/a testcommon(b"%r", "\u0544", b"'\\u0544'")
333n/a
334n/a # Test exception for unknown format characters, etc.
335n/a if verbose:
336n/a print('Testing exceptions')
337n/a def test_exc(formatstr, args, exception, excmsg):
338n/a try:
339n/a testformat(formatstr, args)
340n/a except exception as exc:
341n/a if str(exc) == excmsg:
342n/a if verbose:
343n/a print("yes")
344n/a else:
345n/a if verbose: print('no')
346n/a print('Unexpected ', exception, ':', repr(str(exc)))
347n/a except:
348n/a if verbose: print('no')
349n/a print('Unexpected exception')
350n/a raise
351n/a else:
352n/a raise TestFailed('did not get expected exception: %s' % excmsg)
353n/a test_exc(b'%d', '1', TypeError,
354n/a "%d format: a number is required, not str")
355n/a test_exc(b'%d', b'1', TypeError,
356n/a "%d format: a number is required, not bytes")
357n/a test_exc(b'%x', 3.14, TypeError,
358n/a "%x format: an integer is required, not float")
359n/a test_exc(b'%g', '1', TypeError, "float argument required, not str")
360n/a test_exc(b'%g', b'1', TypeError, "float argument required, not bytes")
361n/a test_exc(b'no format', 7, TypeError,
362n/a "not all arguments converted during bytes formatting")
363n/a test_exc(b'no format', b'1', TypeError,
364n/a "not all arguments converted during bytes formatting")
365n/a test_exc(b'no format', bytearray(b'1'), TypeError,
366n/a "not all arguments converted during bytes formatting")
367n/a test_exc(b"%c", -1, OverflowError,
368n/a "%c arg not in range(256)")
369n/a test_exc(b"%c", 256, OverflowError,
370n/a "%c arg not in range(256)")
371n/a test_exc(b"%c", 2**128, OverflowError,
372n/a "%c arg not in range(256)")
373n/a test_exc(b"%c", b"Za", TypeError,
374n/a "%c requires an integer in range(256) or a single byte")
375n/a test_exc(b"%c", "Y", TypeError,
376n/a "%c requires an integer in range(256) or a single byte")
377n/a test_exc(b"%c", 3.14, TypeError,
378n/a "%c requires an integer in range(256) or a single byte")
379n/a test_exc(b"%b", "Xc", TypeError,
380n/a "%b requires bytes, or an object that implements __bytes__, not 'str'")
381n/a test_exc(b"%s", "Wd", TypeError,
382n/a "%b requires bytes, or an object that implements __bytes__, not 'str'")
383n/a
384n/a if maxsize == 2**31-1:
385n/a # crashes 2.2.1 and earlier:
386n/a try:
387n/a "%*d"%(maxsize, -127)
388n/a except MemoryError:
389n/a pass
390n/a else:
391n/a raise TestFailed('"%*d"%(maxsize, -127) should fail')
392n/a
393n/a def test_nul(self):
394n/a # test the null character
395n/a testcommon("a\0b", (), 'a\0b')
396n/a testcommon("a%cb", (0,), 'a\0b')
397n/a testformat("a%sb", ('c\0d',), 'ac\0db')
398n/a testcommon(b"a%sb", (b'c\0d',), b'ac\0db')
399n/a
400n/a def test_non_ascii(self):
401n/a testformat("\u20ac=%f", (1.0,), "\u20ac=1.000000")
402n/a
403n/a self.assertEqual(format("abc", "\u2007<5"), "abc\u2007\u2007")
404n/a self.assertEqual(format(123, "\u2007<5"), "123\u2007\u2007")
405n/a self.assertEqual(format(12.3, "\u2007<6"), "12.3\u2007\u2007")
406n/a self.assertEqual(format(0j, "\u2007<4"), "0j\u2007\u2007")
407n/a self.assertEqual(format(1+2j, "\u2007<8"), "(1+2j)\u2007\u2007")
408n/a
409n/a self.assertEqual(format("abc", "\u2007>5"), "\u2007\u2007abc")
410n/a self.assertEqual(format(123, "\u2007>5"), "\u2007\u2007123")
411n/a self.assertEqual(format(12.3, "\u2007>6"), "\u2007\u200712.3")
412n/a self.assertEqual(format(1+2j, "\u2007>8"), "\u2007\u2007(1+2j)")
413n/a self.assertEqual(format(0j, "\u2007>4"), "\u2007\u20070j")
414n/a
415n/a self.assertEqual(format("abc", "\u2007^5"), "\u2007abc\u2007")
416n/a self.assertEqual(format(123, "\u2007^5"), "\u2007123\u2007")
417n/a self.assertEqual(format(12.3, "\u2007^6"), "\u200712.3\u2007")
418n/a self.assertEqual(format(1+2j, "\u2007^8"), "\u2007(1+2j)\u2007")
419n/a self.assertEqual(format(0j, "\u2007^4"), "\u20070j\u2007")
420n/a
421n/a def test_locale(self):
422n/a try:
423n/a oldloc = locale.setlocale(locale.LC_ALL)
424n/a locale.setlocale(locale.LC_ALL, '')
425n/a except locale.Error as err:
426n/a self.skipTest("Cannot set locale: {}".format(err))
427n/a try:
428n/a localeconv = locale.localeconv()
429n/a sep = localeconv['thousands_sep']
430n/a point = localeconv['decimal_point']
431n/a
432n/a text = format(123456789, "n")
433n/a self.assertIn(sep, text)
434n/a self.assertEqual(text.replace(sep, ''), '123456789')
435n/a
436n/a text = format(1234.5, "n")
437n/a self.assertIn(sep, text)
438n/a self.assertIn(point, text)
439n/a self.assertEqual(text.replace(sep, ''), '1234' + point + '5')
440n/a finally:
441n/a locale.setlocale(locale.LC_ALL, oldloc)
442n/a
443n/a @support.cpython_only
444n/a def test_optimisations(self):
445n/a text = "abcde" # 5 characters
446n/a
447n/a self.assertIs("%s" % text, text)
448n/a self.assertIs("%.5s" % text, text)
449n/a self.assertIs("%.10s" % text, text)
450n/a self.assertIs("%1s" % text, text)
451n/a self.assertIs("%5s" % text, text)
452n/a
453n/a self.assertIs("{0}".format(text), text)
454n/a self.assertIs("{0:s}".format(text), text)
455n/a self.assertIs("{0:.5s}".format(text), text)
456n/a self.assertIs("{0:.10s}".format(text), text)
457n/a self.assertIs("{0:1s}".format(text), text)
458n/a self.assertIs("{0:5s}".format(text), text)
459n/a
460n/a self.assertIs(text % (), text)
461n/a self.assertIs(text.format(), text)
462n/a
463n/a def test_precision(self):
464n/a f = 1.2
465n/a self.assertEqual(format(f, ".0f"), "1")
466n/a self.assertEqual(format(f, ".3f"), "1.200")
467n/a with self.assertRaises(ValueError) as cm:
468n/a format(f, ".%sf" % (sys.maxsize + 1))
469n/a
470n/a c = complex(f)
471n/a self.assertEqual(format(c, ".0f"), "1+0j")
472n/a self.assertEqual(format(c, ".3f"), "1.200+0.000j")
473n/a with self.assertRaises(ValueError) as cm:
474n/a format(c, ".%sf" % (sys.maxsize + 1))
475n/a
476n/a @support.cpython_only
477n/a def test_precision_c_limits(self):
478n/a from _testcapi import INT_MAX
479n/a
480n/a f = 1.2
481n/a with self.assertRaises(ValueError) as cm:
482n/a format(f, ".%sf" % (INT_MAX + 1))
483n/a
484n/a c = complex(f)
485n/a with self.assertRaises(ValueError) as cm:
486n/a format(c, ".%sf" % (INT_MAX + 1))
487n/a
488n/a
489n/aif __name__ == "__main__":
490n/a unittest.main()