MCPcopy Index your code
hub / github.com/aws/aws-cli / uni_print

Function uni_print

awscli/customizations/utils.py:164–205  ·  view source on GitHub ↗

This function is used to properly write unicode to a file, usually stdout or stdderr. It ensures that the proper encoding is used if the statement is not a string type.

(statement, out_file=None)

Source from the content-addressed store, hash-verified

162
163
164def uni_print(statement, out_file=None):
165 """
166 This function is used to properly write unicode to a file, usually
167 stdout or stdderr. It ensures that the proper encoding is used if the
168 statement is not a string type.
169 """
170 if out_file is None:
171 out_file = sys.stdout
172 try:
173 # Otherwise we assume that out_file is a
174 # text writer type that accepts str/unicode instead
175 # of bytes.
176 out_file.write(statement)
177 except UnicodeEncodeError:
178 # Some file like objects like cStringIO will
179 # try to decode as ascii on python2.
180 #
181 # This can also fail if our encoding associated
182 # with the text writer cannot encode the unicode
183 # ``statement`` we've been given. This commonly
184 # happens on windows where we have some S3 key
185 # previously encoded with utf-8 that can't be
186 # encoded using whatever codepage the user has
187 # configured in their console.
188 #
189 # At this point we've already failed to do what's
190 # been requested. We now try to make a best effort
191 # attempt at printing the statement to the outfile.
192 # We're using 'ascii' as the default because if the
193 # stream doesn't give us any encoding information
194 # we want to pick an encoding that has the highest
195 # chance of printing successfully.
196 new_encoding = getattr(out_file, 'encoding', 'ascii')
197 # When the output of the aws command is being piped,
198 # ``sys.stdout.encoding`` is ``None``.
199 if new_encoding is None:
200 new_encoding = 'ascii'
201 new_statement = statement.encode(new_encoding, 'replace').decode(
202 new_encoding
203 )
204 out_file.write(new_statement)
205 out_file.flush()
206
207
208def get_policy_arn_suffix(region):

Callers 15

yes_no_choiceFunction · 0.90
_run_mainMethod · 0.90
_run_mainMethod · 0.90
_run_mainMethod · 0.90
_run_mainMethod · 0.90
_run_mainMethod · 0.90
_run_mainMethod · 0.90
_update_netrc_entryMethod · 0.90
loginMethod · 0.90

Calls 4

decodeMethod · 0.80
writeMethod · 0.45
encodeMethod · 0.45
flushMethod · 0.45

Tested by

no test coverage detected