|
22 | 22 |
|
23 | 23 | logger = logging.getLogger(__name__) |
24 | 24 |
|
| 25 | +console_encoding = sys.stdout.encoding |
| 26 | + |
| 27 | + |
| 28 | +def console_to_str(s: bytes) -> str: |
| 29 | + """From pypa/pip project, pip.backwardwardcompat. License MIT.""" |
| 30 | + try: |
| 31 | + return s.decode(console_encoding) |
| 32 | + except UnicodeDecodeError: |
| 33 | + return s.decode("utf_8") |
| 34 | + except AttributeError: # for tests, #13 |
| 35 | + return str(s) |
| 36 | + |
25 | 37 |
|
26 | 38 | if t.TYPE_CHECKING: |
27 | 39 | _LoggerAdapter = logging.LoggerAdapter[logging.Logger] |
@@ -182,7 +194,7 @@ def progress_cb(output, timestamp): |
182 | 194 | restore_signals=restore_signals, |
183 | 195 | start_new_session=start_new_session, |
184 | 196 | pass_fds=pass_fds, |
185 | | - text=True, |
| 197 | + text=False, # Keep in bytes mode to preserve \r properly |
186 | 198 | encoding=encoding, |
187 | 199 | errors=errors, |
188 | 200 | user=user, |
@@ -213,25 +225,17 @@ def progress_cb(output: t.AnyStr, timestamp: datetime.datetime) -> None: |
213 | 225 | code = proc.poll() |
214 | 226 |
|
215 | 227 | if callback and callable(callback) and proc.stderr is not None: |
216 | | - line = str(proc.stderr.read(128)) |
| 228 | + line = console_to_str(proc.stderr.read(128)) |
217 | 229 | if line: |
218 | 230 | callback(output=line, timestamp=datetime.datetime.now()) |
219 | 231 | if callback and callable(callback): |
220 | 232 | callback(output="\r", timestamp=datetime.datetime.now()) |
221 | 233 |
|
222 | | - lines = ( |
223 | | - filter(None, (line.strip() for line in proc.stdout.readlines())) |
224 | | - if proc.stdout is not None |
225 | | - else [] |
226 | | - ) |
227 | | - all_output = "\n".join(lines) |
| 234 | + lines = filter(None, (line.strip() for line in proc.stdout.readlines())) |
| 235 | + all_output = console_to_str(b"\n".join(lines)) |
228 | 236 | if code: |
229 | | - stderr_lines = ( |
230 | | - filter(None, (line.strip() for line in proc.stderr.readlines())) |
231 | | - if proc.stderr is not None |
232 | | - else [] |
233 | | - ) |
234 | | - all_output = "".join(stderr_lines) |
| 237 | + stderr_lines = filter(None, (line.strip() for line in proc.stderr.readlines())) |
| 238 | + all_output = console_to_str(b"".join(stderr_lines)) |
235 | 239 | output = "".join(all_output) |
236 | 240 | if code != 0 and check_returncode: |
237 | 241 | raise exc.CommandError( |
|
0 commit comments