File: stdoutcapture.py

package info (click to toggle)
pypy3 7.3.19%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 212,236 kB
  • sloc: python: 2,098,316; ansic: 540,565; sh: 21,462; asm: 14,419; cpp: 4,451; makefile: 4,209; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 12; awk: 4
file content (75 lines) | stat: -rw-r--r-- 2,465 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"""
A quick hack to capture stdout/stderr.
"""
from __future__ import print_function

import os, sys


class Capture:

    def __init__(self, mixed_out_err = False):
        "Start capture of the Unix-level stdout and stderr."
        if (sys.platform == 'win32' or # os.tmpfile fails, cpython issue #2232
            not hasattr(os, 'tmpfile') or
            not hasattr(os, 'dup') or
            not hasattr(os, 'dup2') or
            not hasattr(os, 'fdopen')):
            self.dummy = 1
        else:
            self.dummy = 0
            # make new stdout/stderr files if needed
            self.localoutfd = os.dup(1)
            self.localerrfd = os.dup(2)
            if hasattr(sys.stdout, 'fileno') and sys.stdout.fileno() == 1:
                self.saved_stdout = sys.stdout
                sys.stdout = os.fdopen(self.localoutfd, 'w', 1)
            else:
                self.saved_stdout = None
            if hasattr(sys.stderr, 'fileno') and sys.stderr.fileno() == 2:
                self.saved_stderr = sys.stderr
                sys.stderr = os.fdopen(self.localerrfd, 'w', 0)
            else:
                self.saved_stderr = None
            self.tmpout = os.tmpfile()
            if mixed_out_err:
                self.tmperr = self.tmpout
            else:
                self.tmperr = os.tmpfile()
            os.dup2(self.tmpout.fileno(), 1)
            os.dup2(self.tmperr.fileno(), 2)

    def done(self):
        "End capture and return the captured text (stdoutfile, stderrfile)."
        if self.dummy:
            import cStringIO
            return cStringIO.StringIO(), cStringIO.StringIO()
        else:
            os.dup2(self.localoutfd, 1)
            os.dup2(self.localerrfd, 2)
            if self.saved_stdout is not None:
                f = sys.stdout
                sys.stdout = self.saved_stdout
                f.close()
            else:
                os.close(self.localoutfd)
            if self.saved_stderr is not None:
                f = sys.stderr
                sys.stderr = self.saved_stderr
                f.close()
            else:
                os.close(self.localerrfd)
            self.tmpout.seek(0)
            self.tmperr.seek(0)
            return self.tmpout, self.tmperr


if __name__ == '__main__':
    # test
    c = Capture()
    try:
        os.system('echo hello')
    finally:
        fout, ferr = c.done()
    print('Output:', `fout.read()`)
    print('Error:', `ferr.read()`)