| 1 |
import pytest |
| 2 |
|
| 3 |
class filelike(): |
| 4 |
def __init__(self): |
| 5 |
self.written = '' |
| 6 |
self.flushed = 0 |
| 7 |
def write(self, str): |
| 8 |
self.written += str |
| 9 |
def flush(self): |
| 10 |
self.flushed += 1 |
| 11 |
|
| 12 |
def test_print_flush(): |
| 13 |
|
| 14 |
|
| 15 |
f = filelike() |
| 16 |
print(1, file=f, end='', flush=True) |
| 17 |
print(2, file=f, end='', flush=True) |
| 18 |
print(3, file=f, flush=False) |
| 19 |
assert f.written == '123\n' |
| 20 |
assert f.flushed == 2 |
| 21 |
|
| 22 |
|
| 23 |
class noflush(): |
| 24 |
def write(self, str): |
| 25 |
pass |
| 26 |
def flush(self): |
| 27 |
raise RuntimeError |
| 28 |
with pytest.raises(RuntimeError): |
| 29 |
print(1, file=noflush(), flush=True) |
| 30 |
|
| 31 |
def test_end_None(): |
| 32 |
f = filelike() |
| 33 |
print(1, file=f, end=None) |
| 34 |
assert f.written == '1\n' |
| 35 |
|
| 36 |
def test_sep_None(): |
| 37 |
f = filelike() |
| 38 |
print(1, 2, 3, file=f, sep=None) |
| 39 |
assert f.written == '1 2 3\n' |