Skip to content

When piping imapcli output, an exception is raised if emails contain …#26

Open
codedump wants to merge 1 commit intoGentux:masterfrom
codedump:fix/8-bit-stdout
Open

When piping imapcli output, an exception is raised if emails contain …#26
codedump wants to merge 1 commit intoGentux:masterfrom
codedump:fix/8-bit-stdout

Conversation

@codedump
Copy link

This will typically fail with imapcli:

$ imapcli search --date 2019-04-01 | less

It will throw an exception, something like this:

Traceback (most recent call last):
  File "/usr/local/bin/imap-cli-search", line 11, in <module>
    sys.exit(main())
  File "/usr/local/lib/python2.7/dist-packages/imap_cli/search.py", line 484, in main
    sys.stdout.write(text)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 5: ordinal not in range(128)

When piping imapcli output, an exception is raised if emails contain non-8-bit
characters. This usually does NOT happen during regular usage with a fairly
smart terminal, only during pipe usage. The reason is that stdout, the
device usually used for output, by default just expects non-encoded 8-bit
character data.

This patch fixes this by explicitly encoding everyting into utf-8 before it
is being written to stdout.

(See also this thread on SO: https://stackoverflow.com/questions/15740236/stdout-encoding-in-python )

Cheers

…non-8-bit

characters (e.g. utf-8 encoding). This does NOT happen during regular usage,
only during pipe usage. The reason is that /dev/stdout, the device usually
used for output, is essentially just an 8-bit character output device.

This patch fixes this by explicitlyu converting everyting to utf-8 before it
is being written to /dev/stdout.
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.

2 participants