annotate Lib/test/test_print.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents 5341b30b1812
children f65d62473465
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
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
11 import sys
46107
5341b30b1812 Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45967
diff changeset
12 if sys.version_info[0] == 3:
45891
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
13 # 3.x
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
14 from io import StringIO
46107
5341b30b1812 Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45967
diff changeset
15 else:
45891
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
16 # 2.x
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
17 from StringIO import StringIO
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
18
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
19 NotDefined = object()
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
20
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
21 # 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
22 # sep, end, and file
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
23 # I use this machinery so that I'm not just passing default
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
24 # values to print, I'm eiher passing or not passing in the
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
25 # arguments
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
26 dispatch = {
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
27 (False, False, False):
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(*args),
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
29 (False, False, True):
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(file=file, *args),
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
31 (False, True, False):
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(end=end, *args),
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
33 (False, True, True):
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(end=end, file=file, *args),
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
35 (True, False, False):
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, *args),
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
37 (True, False, True):
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
38 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
39 (True, True, False):
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
40 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
41 (True, True, True):
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
42 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
43 }
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
44
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
45 # 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
46 class ClassWith__str__:
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
47 def __init__(self, x):
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
48 self.x = x
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
49 def __str__(self):
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
50 return self.x
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
51
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
52 class TestPrint(unittest.TestCase):
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
53 def check(self, expected, args,
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
54 sep=NotDefined, end=NotDefined, file=NotDefined):
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
55 # 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
56 # 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
57 # must match expected.
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
58
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
59 # 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
60 # are defined
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
61 fn = dispatch[(sep is not NotDefined,
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
62 end is not NotDefined,
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
63 file is not NotDefined)]
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
64
45921
b0b608e579cd Use test.test_support.captured_stdout instead of a custom contextmanager.
Eric Smith <eric@trueblade.com>
parents: 45891
diff changeset
65 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
66 fn(args, sep, end, file)
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
67
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
68 self.assertEqual(t.getvalue(), expected)
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
69
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
70 def test_print(self):
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
71 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
72 # 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
73 # file directed to a StringIO
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 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
76
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
77 # 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
78 o = StringIO()
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
79 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
80
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
81 # 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
82 self.assertEqual(o.getvalue(), expected)
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
83
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
84 x('\n', ())
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
85 x('a\n', ('a',))
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
86 x('None\n', (None,))
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
87 x('1 2\n', (1, 2))
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
88 x('1 2\n', (1, ' ', 2))
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
89 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
90 x('1 s', (1, 's'), end='')
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
91 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
92 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
93 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
94 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
95 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
96
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
97 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
98 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
99 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
100 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
101
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
102 x('*\n', (ClassWith__str__('*'),))
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
103 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
104
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
105 # 2.x unicode tests
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
106 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
107 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
108 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
109
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
110 # errors
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
111 self.assertRaises(TypeError, print, '', sep=3)
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
112 self.assertRaises(TypeError, print, '', end=3)
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
113 self.assertRaises(AttributeError, print, '', file='')
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
114
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
115 def test_main():
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
116 test_support.run_unittest(TestPrint)
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
117
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
118 if __name__ == "__main__":
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents:
diff changeset
119 test_main()