Belle II Software  release-05-01-25
_override_print.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 """
5 This is a small module to replace the builtin print function with one that
6 flushes by default.
7 
8 This is necessary as we mix a lot of C++ and python output and this means we
9 need to flush the output frequently otherwise the order of lines is totally
10 garbled
11 
12 It overrides the function in the builtins module so this will propagate to
13 everywhere. We do this in a extra module to be able to check the python version
14 on `import basf2`.
15 """
16 
17 import builtins
18 
19 
20 def flush_print(*args, **argk):
21  """print function which flushes by default"""
22  argk.setdefault("flush", True)
23  builtins._print(*args, **argk)
24 
25 
26 # remember original print function
27 builtins._print = print
28 # and replace it with the flushing one
29 builtins.print = flush_print