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

Smart quotes cause --list to fail #86

Open
chrismetcalf opened this issue Jul 5, 2016 · 5 comments
Open

Smart quotes cause --list to fail #86

chrismetcalf opened this issue Jul 5, 2016 · 5 comments
Labels

Comments

@chrismetcalf
Copy link

Calling icloud --list causes a stack trace with my iCloud account because one of my devices has a smart quote (single backtick) in its name:

Traceback (most recent call last):
  File "/usr/local/bin/icloud", line 9, in <module>
    load_entry_point('pyicloud==0.9.1', 'console_scripts', 'icloud')()
  File "/usr/local/lib/python2.7/site-packages/pyicloud/cmdline.py", line 256, in main
    print("Name - %s" % contents["name"])
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 12: ordinal not in range(128)
@MikeTheCanuck
Copy link

This sounds like it might have an ugly workaround - while it recommends not using the print statement, I have to believe that print is necessary under the current design for PyiCloud:
http://stackoverflow.com/questions/20662075/unicodeencodeerror-when-printing-from-django#20662196

Ugh, further discussion emphasizes that a global solution is bad (some modules and libraries require ASCII encoding), so the only way I can think to attack it is (a) choose the encoding line by line, which means every time someone reports utf-8 characters in some string, that print() statement has to be patched, or (b) patch print() throughout PyiCloud and pray nothing breaks.

Man, you can't (get an easy) win in the real world.

@the01
Copy link

the01 commented Dec 27, 2016

Or how about using the logging module instead of simple prints?

@coddingtonbear
Copy link
Collaborator

Oh, this shouldn't be that big of a deal; we should really just be <str>.encode(sys.stdout.encoding)for every print, or am I misunderstanding?

@the01
Copy link

the01 commented Dec 28, 2016

From a (very) brief glance at the code it appears to me that pyicloud already uses logging (at least in base.py). So why not use a logger in the cmdline as well?
You clould either use formatting that only writes the message for all loggers or use a custom formatter just for the logger that replaces the print-statements..
I think setting up the loggers etc. once instead of having awkward print statements is preferable

# using
print("It is now {}".format(datetime.utcnow()).encode(sys.stdout.encoding))
# instead of
logger.info("It is now {}".format(datetime.utcnow()))
# makes for a lot of redundant code

You could write your own print method my_print(output) that takes care of the encoding part, but then you are just starting to implement your own output methods.
On a side-note: A quickfix solution whould be something like

old_print = print

def my_print(output):
  old_print(output.encode("UTF-8"))

print = my_print

Ok, I got a little side-tracked there, but in the end you should keep in mind, some terminals have ascii default encoding.

@Quentame
Copy link
Collaborator

Does the new version fix the issue ?

@Quentame Quentame added the bug label Mar 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants