|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Copyright (C) 2025 Nexedi SA and Contributors. |
| 3 | +# Kirill Smelkov <[email protected]> |
| 4 | +# |
| 5 | +# This program is free software: you can Use, Study, Modify and Redistribute |
| 6 | +# it under the terms of the GNU General Public License version 3, or (at your |
| 7 | +# option) any later version, as published by the Free Software Foundation. |
| 8 | +# |
| 9 | +# You can also Link and Combine this program with other software covered by |
| 10 | +# the terms of any of the Free Software licenses or any of the Open Source |
| 11 | +# Initiative approved licenses and Convey the resulting work. Corresponding |
| 12 | +# source of such a combination shall include the source code for all other |
| 13 | +# software used. |
| 14 | +# |
| 15 | +# This program is distributed WITHOUT ANY WARRANTY; without even the implied |
| 16 | +# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 17 | +# |
| 18 | +# See COPYING file for full licensing terms. |
| 19 | +# See https://www.nexedi.com/licensing for rationale and options. |
| 20 | +"""Program print_stdio_bufmode prints information about stdout/stderr buffering mode.""" |
| 21 | + |
| 22 | +from __future__ import print_function, absolute_import |
| 23 | + |
| 24 | +import sys, os |
| 25 | + |
| 26 | + |
| 27 | +def main(): |
| 28 | + null = os.open(os.devnull, os.O_WRONLY) |
| 29 | + |
| 30 | + def check(subj, ioobj): |
| 31 | + ioobj.write('%s: unbuffered if you see the next line; buffered otherwise\n' % subj) |
| 32 | + ioobj.flush() |
| 33 | + ioobj.write('%s: unbuffered' % subj) # NOTE: no \n to avoid flush even on line-bufferring |
| 34 | + os.close(ioobj.fileno()) |
| 35 | + os.dup2(null, ioobj.fileno()) # not to hit an error when ioobj is closed at the end |
| 36 | + |
| 37 | + check('stdout', sys.stdout) |
| 38 | + check('stderr', sys.stderr) |
| 39 | + |
| 40 | + |
| 41 | +if __name__ == '__main__': |
| 42 | + main() |
0 commit comments