Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_string.py @ 87690:66c1c9f32567 default tip
Add use_weakref option to functools.lru_cache
The use_weakref option makes lru_cache create weak references to the function
arguments when creating the cache key, which prevents them from staying in the
memory forever even after all other references to them are gone.
This is a proof of concept for http://bugs.python.org/issue19859
| author | Radomir Dopieralski <devel@sheep.art.pl> |
|---|---|
| date | Tue, 03 Dec 2013 16:02:48 +0100 |
| parents | 4feb10457c13 |
| children |
| rev | line source |
|---|---|
|
28009
fe10ece8ba68
Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents:
27963
diff
changeset
|
1 import unittest, string |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
43144
diff
changeset
|
2 from test import support |
|
28009
fe10ece8ba68
Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents:
27963
diff
changeset
|
3 |
|
33246
03440272028c
SF Patch #1007087: Return new string for single subclass joins (Bug #1001011)
Raymond Hettinger <python@rcn.com>
parents:
28917
diff
changeset
|
4 |
|
28009
fe10ece8ba68
Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents:
27963
diff
changeset
|
5 class ModuleTest(unittest.TestCase): |
|
fe10ece8ba68
Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents:
27963
diff
changeset
|
6 |
|
fe10ece8ba68
Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents:
27963
diff
changeset
|
7 def test_attrs(self): |
|
fe10ece8ba68
Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents:
27963
diff
changeset
|
8 string.whitespace |
|
42585
b623777f22b0
Remove string.{letters,lowercase,uppercase}.
Martin v. Löwis <martin@v.loewis.de>
parents:
41480
diff
changeset
|
9 string.ascii_lowercase |
|
b623777f22b0
Remove string.{letters,lowercase,uppercase}.
Martin v. Löwis <martin@v.loewis.de>
parents:
41480
diff
changeset
|
10 string.ascii_uppercase |
|
b623777f22b0
Remove string.{letters,lowercase,uppercase}.
Martin v. Löwis <martin@v.loewis.de>
parents:
41480
diff
changeset
|
11 string.ascii_letters |
|
28009
fe10ece8ba68
Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents:
27963
diff
changeset
|
12 string.digits |
|
fe10ece8ba68
Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents:
27963
diff
changeset
|
13 string.hexdigits |
|
fe10ece8ba68
Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents:
27963
diff
changeset
|
14 string.octdigits |
|
fe10ece8ba68
Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents:
27963
diff
changeset
|
15 string.punctuation |
|
fe10ece8ba68
Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents:
27963
diff
changeset
|
16 string.printable |
|
fe10ece8ba68
Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents:
27963
diff
changeset
|
17 |
|
56231
bf9bef70f7e8
Merged revisions 75072 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
53094
diff
changeset
|
18 def test_capwords(self): |
|
bf9bef70f7e8
Merged revisions 75072 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
53094
diff
changeset
|
19 self.assertEqual(string.capwords('abc def ghi'), 'Abc Def Ghi') |
|
bf9bef70f7e8
Merged revisions 75072 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
53094
diff
changeset
|
20 self.assertEqual(string.capwords('abc\tdef\nghi'), 'Abc Def Ghi') |
|
bf9bef70f7e8
Merged revisions 75072 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
53094
diff
changeset
|
21 self.assertEqual(string.capwords('abc\t def \nghi'), 'Abc Def Ghi') |
|
bf9bef70f7e8
Merged revisions 75072 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
53094
diff
changeset
|
22 self.assertEqual(string.capwords('ABC DEF GHI'), 'Abc Def Ghi') |
|
bf9bef70f7e8
Merged revisions 75072 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
53094
diff
changeset
|
23 self.assertEqual(string.capwords('ABC-DEF-GHI', '-'), 'Abc-Def-Ghi') |
|
bf9bef70f7e8
Merged revisions 75072 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
53094
diff
changeset
|
24 self.assertEqual(string.capwords('ABC-def DEF-ghi GHI'), 'Abc-def Def-ghi Ghi') |
|
56233
f3c595214abc
Merged revisions 75074 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56231
diff
changeset
|
25 self.assertEqual(string.capwords(' aBc DeF '), 'Abc Def') |
|
f3c595214abc
Merged revisions 75074 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56231
diff
changeset
|
26 self.assertEqual(string.capwords('\taBc\tDeF\t'), 'Abc Def') |
|
f3c595214abc
Merged revisions 75074 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56231
diff
changeset
|
27 self.assertEqual(string.capwords('\taBc\tDeF\t', '\t'), '\tAbc\tDef\t') |
|
56231
bf9bef70f7e8
Merged revisions 75072 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
53094
diff
changeset
|
28 |
|
70547
f1a213d034af
Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents:
68611
diff
changeset
|
29 def test_basic_formatter(self): |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
42585
diff
changeset
|
30 fmt = string.Formatter() |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
42585
diff
changeset
|
31 self.assertEqual(fmt.format("foo"), "foo") |
|
42875
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42817
diff
changeset
|
32 self.assertEqual(fmt.format("foo{0}", "bar"), "foobar") |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42817
diff
changeset
|
33 self.assertEqual(fmt.format("foo{1}{0}-{1}", "bar", 6), "foo6bar-6") |
|
70547
f1a213d034af
Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents:
68611
diff
changeset
|
34 |
|
78658
24b449a77e88
#13579: teach string.Formatter about 'a'.
R David Murray <rdmurray@bitdance.com>
parents:
68610
diff
changeset
|
35 def test_conversion_specifiers(self): |
|
24b449a77e88
#13579: teach string.Formatter about 'a'.
R David Murray <rdmurray@bitdance.com>
parents:
68610
diff
changeset
|
36 fmt = string.Formatter() |
|
24b449a77e88
#13579: teach string.Formatter about 'a'.
R David Murray <rdmurray@bitdance.com>
parents:
68610
diff
changeset
|
37 self.assertEqual(fmt.format("-{arg!r}-", arg='test'), "-'test'-") |
|
24b449a77e88
#13579: teach string.Formatter about 'a'.
R David Murray <rdmurray@bitdance.com>
parents:
68610
diff
changeset
|
38 self.assertEqual(fmt.format("{0!s}", 'test'), 'test') |
|
24b449a77e88
#13579: teach string.Formatter about 'a'.
R David Murray <rdmurray@bitdance.com>
parents:
68610
diff
changeset
|
39 self.assertRaises(ValueError, fmt.format, "{0!h}", 'test') |
|
24b449a77e88
#13579: teach string.Formatter about 'a'.
R David Murray <rdmurray@bitdance.com>
parents:
68610
diff
changeset
|
40 # issue13579 |
|
24b449a77e88
#13579: teach string.Formatter about 'a'.
R David Murray <rdmurray@bitdance.com>
parents:
68610
diff
changeset
|
41 self.assertEqual(fmt.format("{0!a}", 42), '42') |
|
24b449a77e88
#13579: teach string.Formatter about 'a'.
R David Murray <rdmurray@bitdance.com>
parents:
68610
diff
changeset
|
42 self.assertEqual(fmt.format("{0!a}", string.ascii_letters), |
|
24b449a77e88
#13579: teach string.Formatter about 'a'.
R David Murray <rdmurray@bitdance.com>
parents:
68610
diff
changeset
|
43 "'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'") |
|
24b449a77e88
#13579: teach string.Formatter about 'a'.
R David Murray <rdmurray@bitdance.com>
parents:
68610
diff
changeset
|
44 self.assertEqual(fmt.format("{0!a}", chr(255)), "'\\xff'") |
|
24b449a77e88
#13579: teach string.Formatter about 'a'.
R David Murray <rdmurray@bitdance.com>
parents:
68610
diff
changeset
|
45 self.assertEqual(fmt.format("{0!a}", chr(256)), "'\\u0100'") |
|
24b449a77e88
#13579: teach string.Formatter about 'a'.
R David Murray <rdmurray@bitdance.com>
parents:
68610
diff
changeset
|
46 |
|
70547
f1a213d034af
Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents:
68611
diff
changeset
|
47 def test_name_lookup(self): |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
42585
diff
changeset
|
48 fmt = string.Formatter() |
|
70547
f1a213d034af
Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents:
68611
diff
changeset
|
49 class AnyAttr: |
|
f1a213d034af
Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents:
68611
diff
changeset
|
50 def __getattr__(self, attr): |
|
f1a213d034af
Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents:
68611
diff
changeset
|
51 return attr |
|
f1a213d034af
Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents:
68611
diff
changeset
|
52 x = AnyAttr() |
|
f1a213d034af
Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents:
68611
diff
changeset
|
53 self.assertEqual(fmt.format("{0.lumber}{0.jack}", x), 'lumberjack') |
|
f1a213d034af
Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents:
68611
diff
changeset
|
54 with self.assertRaises(AttributeError): |
|
f1a213d034af
Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents:
68611
diff
changeset
|
55 fmt.format("{0.lumber}{0.jack}", '') |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
42585
diff
changeset
|
56 |
|
70547
f1a213d034af
Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents:
68611
diff
changeset
|
57 def test_index_lookup(self): |
|
f1a213d034af
Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents:
68611
diff
changeset
|
58 fmt = string.Formatter() |
|
f1a213d034af
Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents:
68611
diff
changeset
|
59 lookup = ["eggs", "and", "spam"] |
|
f1a213d034af
Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents:
68611
diff
changeset
|
60 self.assertEqual(fmt.format("{0[2]}{0[0]}", lookup), 'spameggs') |
|
f1a213d034af
Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents:
68611
diff
changeset
|
61 with self.assertRaises(IndexError): |
|
f1a213d034af
Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents:
68611
diff
changeset
|
62 fmt.format("{0[2]}{0[0]}", []) |
|
f1a213d034af
Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents:
68611
diff
changeset
|
63 with self.assertRaises(KeyError): |
|
f1a213d034af
Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents:
68611
diff
changeset
|
64 fmt.format("{0[2]}{0[0]}", {}) |
|
42875
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42817
diff
changeset
|
65 |
|
70547
f1a213d034af
Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents:
68611
diff
changeset
|
66 def test_override_get_value(self): |
|
42875
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42817
diff
changeset
|
67 class NamespaceFormatter(string.Formatter): |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42817
diff
changeset
|
68 def __init__(self, namespace={}): |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42817
diff
changeset
|
69 string.Formatter.__init__(self) |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42817
diff
changeset
|
70 self.namespace = namespace |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42817
diff
changeset
|
71 |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42817
diff
changeset
|
72 def get_value(self, key, args, kwds): |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42817
diff
changeset
|
73 if isinstance(key, str): |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42817
diff
changeset
|
74 try: |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42817
diff
changeset
|
75 # Check explicitly passed arguments first |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42817
diff
changeset
|
76 return kwds[key] |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42817
diff
changeset
|
77 except KeyError: |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42817
diff
changeset
|
78 return self.namespace[key] |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42817
diff
changeset
|
79 else: |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42817
diff
changeset
|
80 string.Formatter.get_value(key, args, kwds) |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42817
diff
changeset
|
81 |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42817
diff
changeset
|
82 fmt = NamespaceFormatter({'greeting':'hello'}) |
|
5fd65e250843
PEP 3101: Completed string.Formatter class. Reimplemented field_name to object transformation.
Eric Smith <eric@trueblade.com>
parents:
42817
diff
changeset
|
83 self.assertEqual(fmt.format("{greeting}, world!"), 'hello, world!') |
|
42817
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
42585
diff
changeset
|
84 |
|
5355337709e1
Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents:
42585
diff
changeset
|
85 |
|
70547
f1a213d034af
Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents:
68611
diff
changeset
|
86 def test_override_format_field(self): |
|
43110
cdb7b9f82cc4
Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents:
42875
diff
changeset
|
87 class CallFormatter(string.Formatter): |
|
cdb7b9f82cc4
Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents:
42875
diff
changeset
|
88 def format_field(self, value, format_spec): |
|
cdb7b9f82cc4
Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents:
42875
diff
changeset
|
89 return format(value(), format_spec) |
|
cdb7b9f82cc4
Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents:
42875
diff
changeset
|
90 |
|
cdb7b9f82cc4
Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents:
42875
diff
changeset
|
91 fmt = CallFormatter() |
|
cdb7b9f82cc4
Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents:
42875
diff
changeset
|
92 self.assertEqual(fmt.format('*{0}*', lambda : 'result'), '*result*') |
|
cdb7b9f82cc4
Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents:
42875
diff
changeset
|
93 |
|
cdb7b9f82cc4
Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents:
42875
diff
changeset
|
94 |
|
70547
f1a213d034af
Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents:
68611
diff
changeset
|
95 def test_override_convert_field(self): |
|
43110
cdb7b9f82cc4
Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents:
42875
diff
changeset
|
96 class XFormatter(string.Formatter): |
|
cdb7b9f82cc4
Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents:
42875
diff
changeset
|
97 def convert_field(self, value, conversion): |
|
cdb7b9f82cc4
Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents:
42875
diff
changeset
|
98 if conversion == 'x': |
|
cdb7b9f82cc4
Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents:
42875
diff
changeset
|
99 return None |
|
70547
f1a213d034af
Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents:
68611
diff
changeset
|
100 return super().convert_field(value, conversion) |
|
43110
cdb7b9f82cc4
Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents:
42875
diff
changeset
|
101 |
|
cdb7b9f82cc4
Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents:
42875
diff
changeset
|
102 fmt = XFormatter() |
|
cdb7b9f82cc4
Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents:
42875
diff
changeset
|
103 self.assertEqual(fmt.format("{0!r}:{0!x}", 'foo', 'foo'), "'foo':None") |
|
cdb7b9f82cc4
Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents:
42875
diff
changeset
|
104 |
|
cdb7b9f82cc4
Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents:
42875
diff
changeset
|
105 |
|
70547
f1a213d034af
Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents:
68611
diff
changeset
|
106 def test_override_parse(self): |
|
43110
cdb7b9f82cc4
Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents:
42875
diff
changeset
|
107 class BarFormatter(string.Formatter): |
|
cdb7b9f82cc4
Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents:
42875
diff
changeset
|
108 # returns an iterable that contains tuples of the form: |
|
cdb7b9f82cc4
Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents:
42875
diff
changeset
|
109 # (literal_text, field_name, format_spec, conversion) |
|
cdb7b9f82cc4
Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents:
42875
diff
changeset
|
110 def parse(self, format_string): |
|
cdb7b9f82cc4
Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents:
42875
diff
changeset
|
111 for field in format_string.split('|'): |
|
cdb7b9f82cc4
Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents:
42875
diff
changeset
|
112 if field[0] == '+': |
|
cdb7b9f82cc4
Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents:
42875
diff
changeset
|
113 # it's markup |
|
cdb7b9f82cc4
Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents:
42875
diff
changeset
|
114 field_name, _, format_spec = field[1:].partition(':') |
|
cdb7b9f82cc4
Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents:
42875
diff
changeset
|
115 yield '', field_name, format_spec, None |
|
cdb7b9f82cc4
Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents:
42875
diff
changeset
|
116 else: |
|
cdb7b9f82cc4
Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents:
42875
diff
changeset
|
117 yield field, None, None, None |
|
cdb7b9f82cc4
Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents:
42875
diff
changeset
|
118 |
|
cdb7b9f82cc4
Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents:
42875
diff
changeset
|
119 fmt = BarFormatter() |
|
cdb7b9f82cc4
Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents:
42875
diff
changeset
|
120 self.assertEqual(fmt.format('*|+0:^10s|*', 'foo'), '* foo *') |
|
cdb7b9f82cc4
Added test cases for string.Formatter subclassing.
Eric Smith <eric@trueblade.com>
parents:
42875
diff
changeset
|
121 |
|
70547
f1a213d034af
Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents:
68611
diff
changeset
|
122 def test_check_unused_args(self): |
|
43112
90c715bc7d30
Changed signature of string.Formatter.get_field, per suggestion by
Eric Smith <eric@trueblade.com>
parents:
43110
diff
changeset
|
123 class CheckAllUsedFormatter(string.Formatter): |
|
90c715bc7d30
Changed signature of string.Formatter.get_field, per suggestion by
Eric Smith <eric@trueblade.com>
parents:
43110
diff
changeset
|
124 def check_unused_args(self, used_args, args, kwargs): |
|
68488
0991b40e895d
#11515: fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
56233
diff
changeset
|
125 # Track which arguments actually got used |
|
43112
90c715bc7d30
Changed signature of string.Formatter.get_field, per suggestion by
Eric Smith <eric@trueblade.com>
parents:
43110
diff
changeset
|
126 unused_args = set(kwargs.keys()) |
|
90c715bc7d30
Changed signature of string.Formatter.get_field, per suggestion by
Eric Smith <eric@trueblade.com>
parents:
43110
diff
changeset
|
127 unused_args.update(range(0, len(args))) |
|
90c715bc7d30
Changed signature of string.Formatter.get_field, per suggestion by
Eric Smith <eric@trueblade.com>
parents:
43110
diff
changeset
|
128 |
|
90c715bc7d30
Changed signature of string.Formatter.get_field, per suggestion by
Eric Smith <eric@trueblade.com>
parents:
43110
diff
changeset
|
129 for arg in used_args: |
|
90c715bc7d30
Changed signature of string.Formatter.get_field, per suggestion by
Eric Smith <eric@trueblade.com>
parents:
43110
diff
changeset
|
130 unused_args.remove(arg) |
|
90c715bc7d30
Changed signature of string.Formatter.get_field, per suggestion by
Eric Smith <eric@trueblade.com>
parents:
43110
diff
changeset
|
131 |
|
90c715bc7d30
Changed signature of string.Formatter.get_field, per suggestion by
Eric Smith <eric@trueblade.com>
parents:
43110
diff
changeset
|
132 if unused_args: |
|
90c715bc7d30
Changed signature of string.Formatter.get_field, per suggestion by
Eric Smith <eric@trueblade.com>
parents:
43110
diff
changeset
|
133 raise ValueError("unused arguments") |
|
90c715bc7d30
Changed signature of string.Formatter.get_field, per suggestion by
Eric Smith <eric@trueblade.com>
parents:
43110
diff
changeset
|
134 |
|
90c715bc7d30
Changed signature of string.Formatter.get_field, per suggestion by
Eric Smith <eric@trueblade.com>
parents:
43110
diff
changeset
|
135 fmt = CheckAllUsedFormatter() |
|
90c715bc7d30
Changed signature of string.Formatter.get_field, per suggestion by
Eric Smith <eric@trueblade.com>
parents:
43110
diff
changeset
|
136 self.assertEqual(fmt.format("{0}", 10), "10") |
|
90c715bc7d30
Changed signature of string.Formatter.get_field, per suggestion by
Eric Smith <eric@trueblade.com>
parents:
43110
diff
changeset
|
137 self.assertEqual(fmt.format("{0}{i}", 10, i=100), "10100") |
|
90c715bc7d30
Changed signature of string.Formatter.get_field, per suggestion by
Eric Smith <eric@trueblade.com>
parents:
43110
diff
changeset
|
138 self.assertEqual(fmt.format("{0}{i}{1}", 10, 20, i=100), "1010020") |
|
90c715bc7d30
Changed signature of string.Formatter.get_field, per suggestion by
Eric Smith <eric@trueblade.com>
parents:
43110
diff
changeset
|
139 self.assertRaises(ValueError, fmt.format, "{0}{i}{1}", 10, 20, i=100, j=0) |
|
90c715bc7d30
Changed signature of string.Formatter.get_field, per suggestion by
Eric Smith <eric@trueblade.com>
parents:
43110
diff
changeset
|
140 self.assertRaises(ValueError, fmt.format, "{0}", 10, 20) |
|
90c715bc7d30
Changed signature of string.Formatter.get_field, per suggestion by
Eric Smith <eric@trueblade.com>
parents:
43110
diff
changeset
|
141 self.assertRaises(ValueError, fmt.format, "{0}", 10, 20, i=100) |
|
90c715bc7d30
Changed signature of string.Formatter.get_field, per suggestion by
Eric Smith <eric@trueblade.com>
parents:
43110
diff
changeset
|
142 self.assertRaises(ValueError, fmt.format, "{i}", 10, 20, i=100) |
|
90c715bc7d30
Changed signature of string.Formatter.get_field, per suggestion by
Eric Smith <eric@trueblade.com>
parents:
43110
diff
changeset
|
143 |
|
70547
f1a213d034af
Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents:
68611
diff
changeset
|
144 def test_vformat_recursion_limit(self): |
|
f1a213d034af
Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents:
68611
diff
changeset
|
145 fmt = string.Formatter() |
|
f1a213d034af
Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents:
68611
diff
changeset
|
146 args = () |
|
f1a213d034af
Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents:
68611
diff
changeset
|
147 kwargs = dict(i=100) |
|
f1a213d034af
Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents:
68611
diff
changeset
|
148 with self.assertRaises(ValueError) as err: |
|
f1a213d034af
Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents:
68611
diff
changeset
|
149 fmt._vformat("{i}", args, kwargs, set(), -1) |
|
f1a213d034af
Tidy up the additional string module tests added at the Pycon sprints (closes #11505)
Nick Coghlan <ncoghlan@gmail.com>
parents:
68611
diff
changeset
|
150 self.assertIn("recursion", str(err.exception)) |
|
68610
cdd667e02fe3
Backport improved test coverage for string.py
Nick Coghlan <ncoghlan@gmail.com>
parents:
68488
diff
changeset
|
151 |
|
cdd667e02fe3
Backport improved test coverage for string.py
Nick Coghlan <ncoghlan@gmail.com>
parents:
68488
diff
changeset
|
152 |
|
28009
fe10ece8ba68
Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents:
27963
diff
changeset
|
153 def test_main(): |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
43144
diff
changeset
|
154 support.run_unittest(ModuleTest) |
|
28009
fe10ece8ba68
Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents:
27963
diff
changeset
|
155 |
|
fe10ece8ba68
Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents:
27963
diff
changeset
|
156 if __name__ == "__main__": |
|
fe10ece8ba68
Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents:
27963
diff
changeset
|
157 test_main() |
