annotate Lib/test/test_print.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents cd8d3c2d3bce
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
45891
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
1 """Test correct operation of the print function.
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
2 """
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
3
45967
696225b5d555 Comment how 'from __future__ import print_function' operates in 3.0.
Eric Smith <eric@trueblade.com>
parents: 45921
diff changeset
4 # In 2.6, this gives us the behavior we want. In 3.0, it has
696225b5d555 Comment how 'from __future__ import print_function' operates in 3.0.
Eric Smith <eric@trueblade.com>
parents: 45921
diff changeset
5 # no function, but it still must parse correctly.
45891
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
6 from __future__ import print_function
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
7
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
8 import unittest
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
9 from test import test_support
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
10
55056
f65d62473465 Merged revisions 73776 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 46107
diff changeset
11 from StringIO import StringIO
45891
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
12
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
13 NotDefined = object()
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
14
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
15 # A dispatch table all 8 combinations of providing
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
16 # sep, end, and file
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
17 # I use this machinery so that I'm not just passing default
68588
cd8d3c2d3bce #11565: Fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58817
diff changeset
18 # values to print, I'm either passing or not passing in the
45891
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
19 # arguments
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
20 dispatch = {
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
21 (False, False, False):
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
22 lambda args, sep, end, file: print(*args),
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
23 (False, False, True):
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
24 lambda args, sep, end, file: print(file=file, *args),
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
25 (False, True, False):
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
26 lambda args, sep, end, file: print(end=end, *args),
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
27 (False, True, True):
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
28 lambda args, sep, end, file: print(end=end, file=file, *args),
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
29 (True, False, False):
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
30 lambda args, sep, end, file: print(sep=sep, *args),
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
31 (True, False, True):
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
32 lambda args, sep, end, file: print(sep=sep, file=file, *args),
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
33 (True, True, False):
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
34 lambda args, sep, end, file: print(sep=sep, end=end, *args),
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
35 (True, True, True):
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
36 lambda args, sep, end, file: print(sep=sep, end=end, file=file, *args),
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
37 }
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
38
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
39 # Class used to test __str__ and print
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
40 class ClassWith__str__:
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
41 def __init__(self, x):
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
42 self.x = x
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
43 def __str__(self):
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
44 return self.x
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
45
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
46 class TestPrint(unittest.TestCase):
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
47 def check(self, expected, args,
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
48 sep=NotDefined, end=NotDefined, file=NotDefined):
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
49 # Capture sys.stdout in a StringIO. Call print with args,
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
50 # and with sep, end, and file, if they're defined. Result
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
51 # must match expected.
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
52
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
53 # Look up the actual function to call, based on if sep, end, and file
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
54 # are defined
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
55 fn = dispatch[(sep is not NotDefined,
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
56 end is not NotDefined,
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
57 file is not NotDefined)]
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
58
45921
b0b608e579cd Use test.test_support.captured_stdout instead of a custom contextmanager.
Eric Smith <eric@trueblade.com>
parents: 45891
diff changeset
59 with test_support.captured_stdout() as t:
45891
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
60 fn(args, sep, end, file)
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
61
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
62 self.assertEqual(t.getvalue(), expected)
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
63
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
64 def test_print(self):
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
65 def x(expected, args, sep=NotDefined, end=NotDefined):
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
66 # Run the test 2 ways: not using file, and using
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
67 # file directed to a StringIO
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
68
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
69 self.check(expected, args, sep=sep, end=end)
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
70
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
71 # When writing to a file, stdout is expected to be empty
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
72 o = StringIO()
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
73 self.check('', args, sep=sep, end=end, file=o)
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
74
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
75 # And o will contain the expected output
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
76 self.assertEqual(o.getvalue(), expected)
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
77
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
78 x('\n', ())
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
79 x('a\n', ('a',))
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
80 x('None\n', (None,))
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
81 x('1 2\n', (1, 2))
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
82 x('1 2\n', (1, ' ', 2))
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
83 x('1*2\n', (1, 2), sep='*')
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
84 x('1 s', (1, 's'), end='')
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
85 x('a\nb\n', ('a', 'b'), sep='\n')
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
86 x('1.01', (1.0, 1), sep='', end='')
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
87 x('1*a*1.3+', (1, 'a', 1.3), sep='*', end='+')
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
88 x('a\n\nb\n', ('a\n', 'b'), sep='\n')
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
89 x('\0+ +\0\n', ('\0', ' ', '\0'), sep='+')
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
90
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
91 x('a\n b\n', ('a\n', 'b'))
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
92 x('a\n b\n', ('a\n', 'b'), sep=None)
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
93 x('a\n b\n', ('a\n', 'b'), end=None)
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
94 x('a\n b\n', ('a\n', 'b'), sep=None, end=None)
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
95
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
96 x('*\n', (ClassWith__str__('*'),))
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
97 x('abc 1\n', (ClassWith__str__('abc'), 1))
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
98
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
99 # 2.x unicode tests
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
100 x(u'1 2\n', ('1', u'2'))
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
101 x(u'u\1234\n', (u'u\1234',))
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
102 x(u' abc 1\n', (' ', ClassWith__str__(u'abc'), 1))
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
103
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
104 # errors
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
105 self.assertRaises(TypeError, print, '', sep=3)
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
106 self.assertRaises(TypeError, print, '', end=3)
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
107 self.assertRaises(AttributeError, print, '', file='')
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
108
55056
f65d62473465 Merged revisions 73776 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 46107
diff changeset
109 def test_mixed_args(self):
f65d62473465 Merged revisions 73776 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 46107
diff changeset
110 # If an unicode arg is passed, sep and end should be unicode, too.
f65d62473465 Merged revisions 73776 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 46107
diff changeset
111 class Recorder(object):
f65d62473465 Merged revisions 73776 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 46107
diff changeset
112
f65d62473465 Merged revisions 73776 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 46107
diff changeset
113 def __init__(self, must_be_unicode):
f65d62473465 Merged revisions 73776 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 46107
diff changeset
114 self.buf = []
f65d62473465 Merged revisions 73776 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 46107
diff changeset
115 self.force_unicode = must_be_unicode
f65d62473465 Merged revisions 73776 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 46107
diff changeset
116
f65d62473465 Merged revisions 73776 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 46107
diff changeset
117 def write(self, what):
f65d62473465 Merged revisions 73776 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 46107
diff changeset
118 if self.force_unicode and not isinstance(what, unicode):
f65d62473465 Merged revisions 73776 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 46107
diff changeset
119 raise AssertionError("{0!r} is not unicode".format(what))
f65d62473465 Merged revisions 73776 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 46107
diff changeset
120 self.buf.append(what)
f65d62473465 Merged revisions 73776 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 46107
diff changeset
121
f65d62473465 Merged revisions 73776 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 46107
diff changeset
122 buf = Recorder(True)
f65d62473465 Merged revisions 73776 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 46107
diff changeset
123 print(u'hi', file=buf)
f65d62473465 Merged revisions 73776 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 46107
diff changeset
124 self.assertEqual(u''.join(buf.buf), 'hi\n')
f65d62473465 Merged revisions 73776 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 46107
diff changeset
125 del buf.buf[:]
f65d62473465 Merged revisions 73776 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 46107
diff changeset
126 print(u'hi', u'nothing', file=buf)
f65d62473465 Merged revisions 73776 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 46107
diff changeset
127 self.assertEqual(u''.join(buf.buf), 'hi nothing\n')
f65d62473465 Merged revisions 73776 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 46107
diff changeset
128 buf = Recorder(False)
f65d62473465 Merged revisions 73776 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 46107
diff changeset
129 print('hi', 'bye', end=u'\n', file=buf)
58472
6ffcdafa0118 use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55056
diff changeset
130 self.assertIsInstance(buf.buf[1], unicode)
6ffcdafa0118 use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55056
diff changeset
131 self.assertIsInstance(buf.buf[3], unicode)
55056
f65d62473465 Merged revisions 73776 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 46107
diff changeset
132 del buf.buf[:]
f65d62473465 Merged revisions 73776 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 46107
diff changeset
133 print(sep=u'x', file=buf)
58472
6ffcdafa0118 use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55056
diff changeset
134 self.assertIsInstance(buf.buf[-1], unicode)
55056
f65d62473465 Merged revisions 73776 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 46107
diff changeset
135
f65d62473465 Merged revisions 73776 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 46107
diff changeset
136
45891
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
137 def test_main():
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
138 test_support.run_unittest(TestPrint)
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
139
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
140 if __name__ == "__main__":
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
141 test_main()