inkstitch/lib/utils/io.py

41 wiersze
1.1 KiB
Python
Czysty Zwykły widok Historia

2021-03-12 04:17:19 +00:00
# Authors: see git history
#
# Copyright (c) 2010 Authors
# Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details.
import os
import sys
from io import StringIO
def save_stderr():
# GTK likes to spam stderr, which inkscape will show in a dialog.
with open(os.devnull, 'w') as null:
sys.stderr_dup = os.dup(sys.stderr.fileno())
sys.real_stderr = os.fdopen(sys.stderr_dup, 'w', encoding='utf-8')
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.
2018-08-22 00:32:50 +00:00
2018-06-14 00:10:22 +00:00
def save_stdout():
with open(os.devnull, 'w') as null:
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()
2018-06-14 00:10:22 +00:00
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