2
from io import StringIO
4
from test import support
8
# A dispatch table all 8 combinations of providing
10
# I use this machinery so that I'm not just passing default
11
# values to print, I'm either passing or not passing in the
14
(False, False, False):
15
lambda args, sep, end, file: print(*args),
17
lambda args, sep, end, file: print(file=file, *args),
19
lambda args, sep, end, file: print(end=end, *args),
21
lambda args, sep, end, file: print(end=end, file=file, *args),
23
lambda args, sep, end, file: print(sep=sep, *args),
25
lambda args, sep, end, file: print(sep=sep, file=file, *args),
27
lambda args, sep, end, file: print(sep=sep, end=end, *args),
29
lambda args, sep, end, file: print(sep=sep, end=end, file=file, *args),
33
# Class used to test __str__ and print
34
class ClassWith__str__:
35
def __init__(self, x):
42
class TestPrint(unittest.TestCase):
43
"""Test correct operation of the print function."""
45
def check(self, expected, args,
46
sep=NotDefined, end=NotDefined, file=NotDefined):
47
# Capture sys.stdout in a StringIO. Call print with args,
48
# and with sep, end, and file, if they're defined. Result
49
# must match expected.
51
# Look up the actual function to call, based on if sep, end,
52
# and file are defined.
53
fn = dispatch[(sep is not NotDefined,
54
end is not NotDefined,
55
file is not NotDefined)]
57
with support.captured_stdout() as t:
58
fn(args, sep, end, file)
60
self.assertEqual(t.getvalue(), expected)
63
def x(expected, args, sep=NotDefined, end=NotDefined):
64
# Run the test 2 ways: not using file, and using
65
# file directed to a StringIO.
67
self.check(expected, args, sep=sep, end=end)
69
# When writing to a file, stdout is expected to be empty
71
self.check('', args, sep=sep, end=end, file=o)
73
# And o will contain the expected output
74
self.assertEqual(o.getvalue(), expected)
80
x('1 2\n', (1, ' ', 2))
81
x('1*2\n', (1, 2), sep='*')
82
x('1 s', (1, 's'), end='')
83
x('a\nb\n', ('a', 'b'), sep='\n')
84
x('1.01', (1.0, 1), sep='', end='')
85
x('1*a*1.3+', (1, 'a', 1.3), sep='*', end='+')
86
x('a\n\nb\n', ('a\n', 'b'), sep='\n')
87
x('\0+ +\0\n', ('\0', ' ', '\0'), sep='+')
89
x('a\n b\n', ('a\n', 'b'))
90
x('a\n b\n', ('a\n', 'b'), sep=None)
91
x('a\n b\n', ('a\n', 'b'), end=None)
92
x('a\n b\n', ('a\n', 'b'), sep=None, end=None)
94
x('*\n', (ClassWith__str__('*'),))
95
x('abc 1\n', (ClassWith__str__('abc'), 1))
98
self.assertRaises(TypeError, print, '', sep=3)
99
self.assertRaises(TypeError, print, '', end=3)
100
self.assertRaises(AttributeError, print, '', file='')
102
def test_print_flush(self):
103
# operation of the flush flag
109
def write(self, str):
116
print(1, file=f, end='', flush=True)
117
print(2, file=f, end='', flush=True)
118
print(3, file=f, flush=False)
119
self.assertEqual(f.written, '123\n')
120
self.assertEqual(f.flushed, 2)
122
# ensure exceptions from flush are passed through
124
def write(self, str):
129
self.assertRaises(RuntimeError, print, 1, file=noflush(), flush=True)
131
if __name__ == "__main__":