Skip to content

Commit

Permalink
Properly handle Unicode characters that come out of ibtool. (#967)
Browse files Browse the repository at this point in the history
In some cases, `ibtool` emits messages with curly quotes:

```
warning: “SomeViewController“ is unreachable because it has no entry points, and no identifier for runtime access via -[UIStoryboard instantiateViewControllerWithIdentifier:].
```

If `sys.{stdout,stderr}` have a default encoding of `ascii`, the later operation to write the Unicode strings to these streams will fail unless we reopen the stream with `utf8` encoding.

PiperOrigin-RevId: 339937656
(cherry picked from commit 14bfbaa)

Co-authored-by: Tony Allevato <[email protected]>
  • Loading branch information
keith and allevato authored Nov 4, 2020
1 parent 6b59d58 commit 6ba2508
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions tools/wrapper_common/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"""Common functionality for tool wrappers to execute jobs.
"""

import io
import os
import re
import subprocess
Expand Down Expand Up @@ -105,10 +106,23 @@ def execute_and_filter_output(
print("ERROR:{stdout}\n\n{stderr}".format(stdout=stdout, stderr=stderr))
raise subprocess.CalledProcessError(proc.returncode, cmd_args)
elif print_output:
# The default encoding of stdout/stderr is 'ascii', so we need to reopen the
# streams in utf8 mode since some messages from Apple's tools use characters
# like curly quotes. (It would be nice to use the `reconfigure` method here,
# but that's only available in Python 3.7, which we can't guarantee.)
if _PY3:
try:
sys.stdout = open(
sys.stdout.fileno(), mode="w", encoding="utf8", buffering=1)
sys.stderr = open(sys.stderr.fileno(), mode="w", encoding="utf8")
except io.UnsupportedOperation:
# When running under test, `fileno` is not supported.
pass

if stdout:
sys.stdout.write("%s" % stdout)
sys.stdout.write(stdout)
if stderr:
sys.stderr.write("%s" % stderr)
sys.stderr.write(stderr)

return cmd_result, stdout, stderr

Expand Down

0 comments on commit 6ba2508

Please sign in to comment.