Since Connection.execute_command() returns bytes, deal with the repurcussions
Toshio Kuratomi authored on 2015/10/03 04:43:26... | ... |
@@ -28,14 +28,14 @@ import stat |
28 | 28 |
import tempfile |
29 | 29 |
import time |
30 | 30 |
|
31 |
-from six import string_types, iteritems |
|
31 |
+from six import binary_type, text_type, iteritems |
|
32 | 32 |
from six.moves import StringIO |
33 | 33 |
|
34 | 34 |
from ansible import constants as C |
35 | 35 |
from ansible.errors import AnsibleError, AnsibleConnectionFailure |
36 | 36 |
from ansible.executor.module_common import modify_module |
37 | 37 |
from ansible.parsing.utils.jsonify import jsonify |
38 |
-from ansible.utils.unicode import to_bytes |
|
38 |
+from ansible.utils.unicode import to_bytes, to_unicode |
|
39 | 39 |
|
40 | 40 |
try: |
41 | 41 |
from __main__ import display |
... | ... |
@@ -479,13 +479,23 @@ class ActionBase: |
479 | 479 |
rc, stdout, stderr = self._connection.exec_command(cmd, in_data=in_data, sudoable=sudoable) |
480 | 480 |
self._display.debug("command execution done") |
481 | 481 |
|
482 |
- if not isinstance(stdout, string_types): |
|
483 |
- out = ''.join(stdout.readlines()) |
|
482 |
+ # stdout and stderr may be either a file-like or a bytes object. |
|
483 |
+ # Convert either one to a text type |
|
484 |
+ # Note: when we address non-utf-8 data we'll have to figure out |
|
485 |
+ # a better strategy than errors='strict'. Perhaps pass the |
|
486 |
+ # errors argument into this method so that the caller can decide or |
|
487 |
+ # even make the caller convert to text type so they can choose. |
|
488 |
+ if isinstance(stdout, binary_type): |
|
489 |
+ out = to_unicode(stdout, errors='strict') |
|
490 |
+ elif not isinstance(stdout, text_type): |
|
491 |
+ out = to_unicode(b''.join(stdout.readlines()), errors='strict') |
|
484 | 492 |
else: |
485 | 493 |
out = stdout |
486 | 494 |
|
487 |
- if not isinstance(stderr, string_types): |
|
488 |
- err = ''.join(stderr.readlines()) |
|
495 |
+ if isinstance(stderr, binary_type): |
|
496 |
+ err = to_unicode(stderr, errors='strict') |
|
497 |
+ elif not isinstance(stderr, text_type): |
|
498 |
+ err = to_unicode(b''.join(stderr.readlines()), errors='strict') |
|
489 | 499 |
else: |
490 | 500 |
err = stderr |
491 | 501 |
|