Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add compat layer for text based stream writers #756

Merged
merged 1 commit into from
Apr 18, 2014

Conversation

jamesls
Copy link
Member

@jamesls jamesls commented Apr 17, 2014

In python2, if a stream has no encoding, unicode is encoded
using the default encoding. In python3, sys.stdout is already
text based (it expects str() types).

In python2, we introduce a compat layer that will give you a
stream writer that accepts unicode and encodes the contents into
the local.preferredencoding().

Simple repro: tag an ec2 instance with a unicode char and run:

aws ec2 describe-instances | grep TAGS

You'll get a ascii encode error. With this change you won't.

Fixes #742.

cc @danielgtaylor

In python2, if a stream has no encoding, unicode is encoded
using the default encoding.  In python3, sys.stdout is already
text based (it expects str() types).

In python2, we introduce a compat layer that will give you a
stream writer that accepts unicode and encodes the contents into
the local.preferredencoding().

Simple repro: tag an ec2 instance with a unicode char and run:

    aws ec2 describe-instances | grep TAGS

You'll get a ascii encode error.  With this change you won't.

Fixes aws#742.
import codecs
import locale
def get_stdout_text_writer():
return codecs.getwriter(locale.getpreferredencoding())(sys.stdout)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Presumably putting this into a shared compat file means others might want to use it. Can we add some documentation (even just one sentence as a description) about what this does? It's a public function and not immediately obvious unless I track down where it is used below.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep I'll add a docstring.

@danielgtaylor
Copy link
Contributor

Outside of the comment above this LGTM. 🚢-it when docs are updated.

@jamesls jamesls merged commit 8bb3f1a into aws:develop Apr 18, 2014
@jamesls jamesls deleted the missing-encoding branch June 23, 2014 18:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

encoding Ö (O-Umlaut) error in command 'aws ec2 describe-instances' when redirecting to a file
2 participants