2
2
3
3
import contextlib
4
4
import os
5
+ import selectors
5
6
import shutil
6
7
import subprocess
7
8
import sys
8
9
import tempfile
10
+ from asyncio import DefaultEventLoopPolicy , get_event_loop_policy , set_event_loop_policy
11
+ from io import IOBase
9
12
10
13
import questionary
11
14
27
30
from commitizen .git import smart_open
28
31
29
32
30
- class WrapStdin :
31
- def __init__ (self ):
32
- fd = os .open ("/dev/tty" , os .O_RDWR | os .O_NOCTTY )
33
- tty = open (fd , "wb+" , buffering = 0 )
33
+ class CZEventLoopPolicy (DefaultEventLoopPolicy ):
34
+ def get_event_loop (self ):
35
+ self .set_event_loop (self ._loop_factory (selectors .SelectSelector ()))
36
+ return self ._local ._loop
37
+
38
+
39
+ class WrapStdx :
40
+ def __init__ (self , stdx : IOBase ):
41
+ self ._fileno = stdx .fileno ()
42
+ if sys .platform == "linux" :
43
+ if self ._fileno == 0 :
44
+ fd = os .open ("/dev/tty" , os .O_RDWR | os .O_NOCTTY )
45
+ tty = open (fd , "wb+" , buffering = 0 )
46
+ else :
47
+ tty = open ("/dev/tty" , "w" )
48
+ else :
49
+ fd = os .open ("/dev/tty" , os .O_RDWR | os .O_NOCTTY )
50
+ if self ._fileno == 0 :
51
+ tty = open (fd , "wb+" , buffering = 0 )
52
+ else :
53
+ tty = open (fd , "rb+" , buffering = 0 )
34
54
self .tty = tty
35
55
36
56
def __getattr__ (self , key ):
37
- if key == "encoding" :
57
+ if key == "encoding" and ( sys . platform != "linux" or self . _fileno == 0 ) :
38
58
return "UTF-8"
39
59
return getattr (self .tty , key )
40
60
@@ -126,9 +146,11 @@ def __call__(self):
126
146
old_stdin = sys .stdin
127
147
old_stdout = sys .stdout
128
148
old_stderr = sys .stderr
129
- sys .stdin = WrapStdin ()
130
- sys .stdout = open ("/dev/tty" , "w" )
131
- sys .stderr = open ("/dev/tty" , "w" )
149
+ old_event_loop_policy = get_event_loop_policy ()
150
+ set_event_loop_policy (CZEventLoopPolicy ())
151
+ sys .stdin = WrapStdx (sys .stdin )
152
+ sys .stdout = WrapStdx (sys .stdout )
153
+ sys .stderr = WrapStdx (sys .stderr )
132
154
133
155
if git .is_staging_clean () and not (dry_run or allow_empty ):
134
156
raise NothingToCommitError ("No files added to staging!" )
@@ -151,9 +173,17 @@ def __call__(self):
151
173
else :
152
174
m = self .prompt_commit_questions ()
153
175
176
+ if commit_msg_file :
177
+ sys .stdin .close ()
178
+ sys .stdout .close ()
179
+ sys .stderr .close ()
180
+ set_event_loop_policy (old_event_loop_policy )
181
+ sys .stdin = old_stdin
182
+ sys .stdout = old_stdout
183
+ sys .stderr = old_stderr
184
+
154
185
if manual_edit :
155
186
m = self .manual_edit (m )
156
-
157
187
out .info (f"\n { m } \n " )
158
188
159
189
if write_message_to_file :
@@ -164,12 +194,6 @@ def __call__(self):
164
194
raise DryRunExit ()
165
195
166
196
if commit_msg_file :
167
- sys .stdin .close ()
168
- sys .stdout .close ()
169
- sys .stderr .close ()
170
- sys .stdin = old_stdin
171
- sys .stdout = old_stdout
172
- sys .stderr = old_stderr
173
197
defaultmesaage = ""
174
198
with open (commit_msg_file ) as f :
175
199
defaultmesaage = f .read ()
0 commit comments