2018-03-31 00:37:11 +00:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
from cStringIO import StringIO
|
|
|
|
|
2018-05-02 01:21:07 +00:00
|
|
|
|
2018-03-31 00:37:11 +00:00
|
|
|
def save_stderr():
|
|
|
|
# GTK likes to spam stderr, which inkscape will show in a dialog.
|
|
|
|
null = open(os.devnull, 'w')
|
|
|
|
sys.stderr_dup = os.dup(sys.stderr.fileno())
|
2018-06-14 00:10:22 +00:00
|
|
|
sys.real_stderr = os.fdopen(sys.stderr_dup, 'w')
|
2018-03-31 00:37:11 +00:00
|
|
|
os.dup2(null.fileno(), 2)
|
|
|
|
sys.stderr = StringIO()
|
|
|
|
|
|
|
|
|
|
|
|
def restore_stderr():
|
|
|
|
os.dup2(sys.stderr_dup, 2)
|
2018-06-14 00:10:22 +00:00
|
|
|
sys.real_stderr.write(sys.stderr.getvalue())
|
2018-06-14 00:45:51 +00:00
|
|
|
sys.stderr = sys.real_stderr
|
2018-06-14 00:10:22 +00:00
|
|
|
|
|
|
|
# It's probably possible to generalize this code, but when I tried,
|
|
|
|
# the result was incredibly unreadable.
|
|
|
|
def save_stdout():
|
|
|
|
null = open(os.devnull, 'w')
|
|
|
|
sys.stdout_dup = os.dup(sys.stdout.fileno())
|
|
|
|
sys.real_stdout = os.fdopen(sys.stdout_dup, 'w')
|
|
|
|
os.dup2(null.fileno(), 1)
|
|
|
|
sys.stdout = StringIO()
|
|
|
|
|
|
|
|
|
|
|
|
def restore_stdout():
|
|
|
|
os.dup2(sys.stdout_dup, 1)
|
|
|
|
sys.real_stdout.write(sys.stdout.getvalue())
|
2018-06-14 00:45:51 +00:00
|
|
|
sys.stdout = sys.real_stdout
|