##// END OF EJS Templates
dispatch: remove Python 2 function variants...
Gregory Szorc -
r49746:4eae5333 default
parent child Browse files
Show More
@@ -149,93 +149,76 b' def run():'
149 149 sys.exit(status & 255)
150 150
151 151
152 if pycompat.ispy3:
153
154 def initstdio():
155 # stdio streams on Python 3 are io.TextIOWrapper instances proxying another
156 # buffer. These streams will normalize \n to \r\n by default. Mercurial's
157 # preferred mechanism for writing output (ui.write()) uses io.BufferedWriter
158 # instances, which write to the underlying stdio file descriptor in binary
159 # mode. ui.write() uses \n for line endings and no line ending normalization
160 # is attempted through this interface. This "just works," even if the system
161 # preferred line ending is not \n.
162 #
163 # But some parts of Mercurial (e.g. hooks) can still send data to sys.stdout
164 # and sys.stderr. They will inherit the line ending normalization settings,
165 # potentially causing e.g. \r\n to be emitted. Since emitting \n should
166 # "just work," here we change the sys.* streams to disable line ending
167 # normalization, ensuring compatibility with our ui type.
168
169 if sys.stdout is not None:
170 # write_through is new in Python 3.7.
171 kwargs = {
172 "newline": "\n",
173 "line_buffering": sys.stdout.line_buffering,
174 }
175 if util.safehasattr(sys.stdout, "write_through"):
176 # pytype: disable=attribute-error
177 kwargs["write_through"] = sys.stdout.write_through
178 # pytype: enable=attribute-error
179 sys.stdout = io.TextIOWrapper(
180 sys.stdout.buffer,
181 sys.stdout.encoding,
182 sys.stdout.errors,
183 **kwargs
184 )
152 def initstdio():
153 # stdio streams on Python 3 are io.TextIOWrapper instances proxying another
154 # buffer. These streams will normalize \n to \r\n by default. Mercurial's
155 # preferred mechanism for writing output (ui.write()) uses io.BufferedWriter
156 # instances, which write to the underlying stdio file descriptor in binary
157 # mode. ui.write() uses \n for line endings and no line ending normalization
158 # is attempted through this interface. This "just works," even if the system
159 # preferred line ending is not \n.
160 #
161 # But some parts of Mercurial (e.g. hooks) can still send data to sys.stdout
162 # and sys.stderr. They will inherit the line ending normalization settings,
163 # potentially causing e.g. \r\n to be emitted. Since emitting \n should
164 # "just work," here we change the sys.* streams to disable line ending
165 # normalization, ensuring compatibility with our ui type.
185 166
186 if sys.stderr is not None:
187 kwargs = {
188 "newline": "\n",
189 "line_buffering": sys.stderr.line_buffering,
190 }
191 if util.safehasattr(sys.stderr, "write_through"):
192 # pytype: disable=attribute-error
193 kwargs["write_through"] = sys.stderr.write_through
194 # pytype: enable=attribute-error
195 sys.stderr = io.TextIOWrapper(
196 sys.stderr.buffer,
197 sys.stderr.encoding,
198 sys.stderr.errors,
199 **kwargs
200 )
167 if sys.stdout is not None:
168 # write_through is new in Python 3.7.
169 kwargs = {
170 "newline": "\n",
171 "line_buffering": sys.stdout.line_buffering,
172 }
173 if util.safehasattr(sys.stdout, "write_through"):
174 # pytype: disable=attribute-error
175 kwargs["write_through"] = sys.stdout.write_through
176 # pytype: enable=attribute-error
177 sys.stdout = io.TextIOWrapper(
178 sys.stdout.buffer, sys.stdout.encoding, sys.stdout.errors, **kwargs
179 )
201 180
202 if sys.stdin is not None:
203 # No write_through on read-only stream.
204 sys.stdin = io.TextIOWrapper(
205 sys.stdin.buffer,
206 sys.stdin.encoding,
207 sys.stdin.errors,
208 # None is universal newlines mode.
209 newline=None,
210 line_buffering=sys.stdin.line_buffering,
211 )
181 if sys.stderr is not None:
182 kwargs = {
183 "newline": "\n",
184 "line_buffering": sys.stderr.line_buffering,
185 }
186 if util.safehasattr(sys.stderr, "write_through"):
187 # pytype: disable=attribute-error
188 kwargs["write_through"] = sys.stderr.write_through
189 # pytype: enable=attribute-error
190 sys.stderr = io.TextIOWrapper(
191 sys.stderr.buffer, sys.stderr.encoding, sys.stderr.errors, **kwargs
192 )
212 193
213 def _silencestdio():
214 for fp in (sys.stdout, sys.stderr):
215 if fp is None:
216 continue
217 # Check if the file is okay
218 try:
219 fp.flush()
220 continue
221 except IOError:
222 pass
223 # Otherwise mark it as closed to silence "Exception ignored in"
224 # message emitted by the interpreter finalizer.
225 try:
226 fp.close()
227 except IOError:
228 pass
194 if sys.stdin is not None:
195 # No write_through on read-only stream.
196 sys.stdin = io.TextIOWrapper(
197 sys.stdin.buffer,
198 sys.stdin.encoding,
199 sys.stdin.errors,
200 # None is universal newlines mode.
201 newline=None,
202 line_buffering=sys.stdin.line_buffering,
203 )
229 204
230 205
231 else:
232
233 def initstdio():
234 for fp in (sys.stdin, sys.stdout, sys.stderr):
235 procutil.setbinary(fp)
236
237 def _silencestdio():
238 pass
206 def _silencestdio():
207 for fp in (sys.stdout, sys.stderr):
208 if fp is None:
209 continue
210 # Check if the file is okay
211 try:
212 fp.flush()
213 continue
214 except IOError:
215 pass
216 # Otherwise mark it as closed to silence "Exception ignored in"
217 # message emitted by the interpreter finalizer.
218 try:
219 fp.close()
220 except IOError:
221 pass
239 222
240 223
241 224 def _formatargs(args):
General Comments 0
You need to be logged in to leave comments. Login now