|
19 | 19 | import zipfile |
20 | 20 | import signal |
21 | 21 | import contextlib |
| 22 | +import queue |
| 23 | +import io |
| 24 | +import collections.abc as collections_abc |
| 25 | +import locale |
| 26 | +import urllib.parse as urlparse |
| 27 | +from urllib.error import URLError |
| 28 | +from urllib.request import urlopen |
22 | 29 | from configparser import RawConfigParser |
23 | 30 | from functools import partial |
24 | 31 |
|
25 | | -from botocore.compat import six |
26 | | -#import botocore.compat |
| 32 | +from urllib.error import URLError |
27 | 33 |
|
| 34 | +from botocore.compat import six |
28 | 35 | from botocore.compat import OrderedDict |
29 | 36 |
|
30 | | -# If you ever want to import from the vendored six. Add it here and then |
31 | | -# import from awscli.compat. Also try to keep it in alphabetical order. |
32 | | -# This may get large. |
33 | | -advance_iterator = six.advance_iterator |
34 | | -PY3 = six.PY3 |
35 | | -queue = six.moves.queue |
36 | | -shlex_quote = six.moves.shlex_quote |
37 | | -StringIO = six.StringIO |
38 | | -BytesIO = six.BytesIO |
39 | | -urlopen = six.moves.urllib.request.urlopen |
40 | | -binary_type = six.binary_type |
| 37 | +# Backwards compatible definitions from six |
| 38 | +PY3 = sys.version_info[0] == 3 |
| 39 | +advance_iterator = next |
| 40 | +shlex_quote = shlex.quote |
| 41 | +StringIO = io.StringIO |
| 42 | +BytesIO = io.BytesIO |
| 43 | +binary_type = bytes |
| 44 | +raw_input = input |
41 | 45 |
|
42 | 46 |
|
43 | 47 | # Most, but not all, python installations will have zlib. This is required to |
@@ -104,89 +108,33 @@ def ensure_text_type(s): |
104 | 108 | raise ValueError("Expected str, unicode or bytes, received %s." % type(s)) |
105 | 109 |
|
106 | 110 |
|
107 | | -if six.PY3: |
108 | | - import collections.abc as collections_abc |
109 | | - import locale |
110 | | - import urllib.parse as urlparse |
111 | | - |
112 | | - from urllib.error import URLError |
113 | | - |
114 | | - raw_input = input |
115 | | - |
116 | | - def get_binary_stdin(): |
117 | | - if sys.stdin is None: |
118 | | - raise StdinMissingError() |
119 | | - return sys.stdin.buffer |
120 | | - |
121 | | - def get_binary_stdout(): |
122 | | - return sys.stdout.buffer |
123 | | - |
124 | | - def _get_text_writer(stream, errors): |
125 | | - return stream |
126 | | - |
127 | | - def bytes_print(statement, stdout=None): |
128 | | - """ |
129 | | - This function is used to write raw bytes to stdout. |
130 | | - """ |
131 | | - if stdout is None: |
132 | | - stdout = sys.stdout |
133 | | - |
134 | | - if getattr(stdout, 'buffer', None): |
135 | | - stdout.buffer.write(statement) |
136 | | - else: |
137 | | - # If it is not possible to write to the standard out buffer. |
138 | | - # The next best option is to decode and write to standard out. |
139 | | - stdout.write(statement.decode('utf-8')) |
140 | | - |
141 | | -else: |
142 | | - import codecs |
143 | | - import collections as collections_abc |
144 | | - import locale |
145 | | - import io |
146 | | - import urlparse |
147 | | - |
148 | | - from urllib2 import URLError |
| 111 | +def get_binary_stdin(): |
| 112 | + if sys.stdin is None: |
| 113 | + raise StdinMissingError() |
| 114 | + return sys.stdin.buffer |
149 | 115 |
|
150 | | - raw_input = raw_input |
151 | 116 |
|
152 | | - def get_binary_stdin(): |
153 | | - if sys.stdin is None: |
154 | | - raise StdinMissingError() |
155 | | - return sys.stdin |
| 117 | +def get_binary_stdout(): |
| 118 | + return sys.stdout.buffer |
156 | 119 |
|
157 | | - def get_binary_stdout(): |
158 | | - return sys.stdout |
159 | 120 |
|
160 | | - def _get_text_writer(stream, errors): |
161 | | - # In python3, all the sys.stdout/sys.stderr streams are in text |
162 | | - # mode. This means they expect unicode, and will encode the |
163 | | - # unicode automatically before actually writing to stdout/stderr. |
164 | | - # In python2, that's not the case. In order to provide a consistent |
165 | | - # interface, we can create a wrapper around sys.stdout that will take |
166 | | - # unicode, and automatically encode it to the preferred encoding. |
167 | | - # That way consumers can just call get_text_writer(stream) and write |
168 | | - # unicode to the returned stream. Note that get_text_writer |
169 | | - # just returns the stream in the PY3 section above because python3 |
170 | | - # handles this. |
171 | | - |
172 | | - # We're going to use the preferred encoding, but in cases that there is |
173 | | - # no preferred encoding we're going to fall back to assuming ASCII is |
174 | | - # what we should use. This will currently break the use of |
175 | | - # PYTHONIOENCODING, which would require checking stream.encoding first, |
176 | | - # however, the existing behavior is to only use |
177 | | - # locale.getpreferredencoding() and so in the hope of not breaking what |
178 | | - # is currently working, we will continue to only use that. |
179 | | - encoding = locale.getpreferredencoding() |
180 | | - if encoding is None: |
181 | | - encoding = "ascii" |
| 121 | +def _get_text_writer(stream, errors): |
| 122 | + return stream |
182 | 123 |
|
183 | | - return codecs.getwriter(encoding)(stream, errors) |
184 | 124 |
|
185 | | - def bytes_print(statement, stdout=None): |
186 | | - if stdout is None: |
187 | | - stdout = sys.stdout |
| 125 | +def bytes_print(statement, stdout=None): |
| 126 | + """ |
| 127 | + This function is used to write raw bytes to stdout. |
| 128 | + """ |
| 129 | + if stdout is None: |
| 130 | + stdout = sys.stdout |
188 | 131 |
|
189 | | - stdout.write(statement) |
| 132 | + if getattr(stdout, 'buffer', None): |
| 133 | + stdout.buffer.write(statement) |
| 134 | + else: |
| 135 | + # If it is not possible to write to the standard out buffer. |
| 136 | + # The next best option is to decode and write to standard out. |
| 137 | + stdout.write(statement.decode('utf-8')) |
190 | 138 |
|
191 | 139 |
|
192 | 140 | def compat_open(filename, mode='r', encoding=None, access_permissions=None): |
@@ -252,7 +200,7 @@ def compat_shell_quote(s, platform=None): |
252 | 200 | if platform == "win32": |
253 | 201 | return _windows_shell_quote(s) |
254 | 202 | else: |
255 | | - return shlex_quote(s) |
| 203 | + return shlex.quote(s) |
256 | 204 |
|
257 | 205 |
|
258 | 206 | def _windows_shell_quote(s): |
|
0 commit comments