4
4
import os
5
5
6
6
from .ansi import AnsiFore , AnsiBack , AnsiStyle , Style , BEL
7
- from .winterm import WinTerm , WinColor , WinStyle
7
+ from .winterm import enable_vt_processing , WinTerm , WinColor , WinStyle
8
8
from .win32 import windll , winapi_test
9
9
10
10
@@ -37,6 +37,12 @@ def __enter__(self, *args, **kwargs):
37
37
def __exit__ (self , * args , ** kwargs ):
38
38
return self .__wrapped .__exit__ (* args , ** kwargs )
39
39
40
+ def __setstate__ (self , state ):
41
+ self .__dict__ = state
42
+
43
+ def __getstate__ (self ):
44
+ return self .__dict__
45
+
40
46
def write (self , text ):
41
47
self .__convertor .write (text )
42
48
@@ -57,7 +63,9 @@ def closed(self):
57
63
stream = self .__wrapped
58
64
try :
59
65
return stream .closed
60
- except AttributeError :
66
+ # AttributeError in the case that the stream doesn't support being closed
67
+ # ValueError for the case that the stream has already been detached when atexit runs
68
+ except (AttributeError , ValueError ):
61
69
return True
62
70
63
71
@@ -86,15 +94,22 @@ def __init__(self, wrapped, convert=None, strip=None, autoreset=False):
86
94
# (e.g. Cygwin Terminal). In this case it's up to the terminal
87
95
# to support the ANSI codes.
88
96
conversion_supported = on_windows and winapi_test ()
97
+ try :
98
+ fd = wrapped .fileno ()
99
+ except Exception :
100
+ fd = - 1
101
+ system_has_native_ansi = not on_windows or enable_vt_processing (fd )
102
+ have_tty = not self .stream .closed and self .stream .isatty ()
103
+ need_conversion = conversion_supported and not system_has_native_ansi
89
104
90
105
# should we strip ANSI sequences from our output?
91
106
if strip is None :
92
- strip = conversion_supported or ( not self . stream . closed and not self . stream . isatty ())
107
+ strip = need_conversion or not have_tty
93
108
self .strip = strip
94
109
95
110
# should we should convert ANSI sequences into win32 calls?
96
111
if convert is None :
97
- convert = conversion_supported and not self . stream . closed and self . stream . isatty ()
112
+ convert = need_conversion and have_tty
98
113
self .convert = convert
99
114
100
115
# dict of ansi codes to win32 functions and parameters
@@ -256,3 +271,7 @@ def convert_osc(self, text):
256
271
if params [0 ] in '02' :
257
272
winterm .set_title (params [1 ])
258
273
return text
274
+
275
+
276
+ def flush (self ):
277
+ self .wrapped .flush ()
0 commit comments