22
33import contextlib
44import os
5+ import selectors
56import shutil
67import subprocess
78import sys
89import tempfile
10+ from asyncio import DefaultEventLoopPolicy , get_event_loop_policy , set_event_loop_policy
11+ from io import IOBase
912
1013import questionary
1114
2730from commitizen .git import smart_open
2831
2932
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 )
3454 self .tty = tty
3555
3656 def __getattr__ (self , key ):
37- if key == "encoding" :
57+ if key == "encoding" and ( sys . platform != "linux" or self . _fileno == 0 ) :
3858 return "UTF-8"
3959 return getattr (self .tty , key )
4060
@@ -126,9 +146,11 @@ def __call__(self):
126146 old_stdin = sys .stdin
127147 old_stdout = sys .stdout
128148 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 )
132154
133155 if git .is_staging_clean () and not (dry_run or allow_empty ):
134156 raise NothingToCommitError ("No files added to staging!" )
@@ -151,9 +173,17 @@ def __call__(self):
151173 else :
152174 m = self .prompt_commit_questions ()
153175
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+
154185 if manual_edit :
155186 m = self .manual_edit (m )
156-
157187 out .info (f"\n { m } \n " )
158188
159189 if write_message_to_file :
@@ -164,12 +194,6 @@ def __call__(self):
164194 raise DryRunExit ()
165195
166196 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
173197 defaultmesaage = ""
174198 with open (commit_msg_file ) as f :
175199 defaultmesaage = f .read ()
0 commit comments