-
-
Notifications
You must be signed in to change notification settings - Fork 263
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
Fix utf8 encoding on py2 #1575
Fix utf8 encoding on py2 #1575
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Possible alternative.
diff --git a/py3status/py3.py b/py3status/py3.py
index fca9448..5e8c4fc 100644
--- a/py3status/py3.py
+++ b/py3status/py3.py
@@ -480,6 +480,10 @@ class Py3:
message = "\n" + message
except: # noqa e722
pass
+ try:
+ message = message.decode()
+ except (AttributeError, UnicodeDecodeError):
+ pass
message = "Module `{}`: {}".format(self._module.module_full_name, message)
self._py3_wrapper.log(message, level)
EDIT: Maybe I'm confused. Error seems to be caused by adding decode()
in the module?
Hi @lasers thanks for having a look! I did some tests and it looks to me that the Using a py2 utf-8 strings have always giving me headaches ... |
The module shouldn't need to have to care. We should do the gatekeeping at some place. @lasers we probably want to hit any I'll try to look at the issue. @apiraino thanks for highlighting the problem. However I'm not sure this PR actually changes anything. from my point of view the error is With the typo in |
Could be (not 100% sure me either). Anyway I was showing a testcase. I can reproduce the same issue logging a variable that contains a utf-8 string. If I omit the utf8 decoding in the plugin module, then the error shows up again, so it might be the case to leave it anyways? If you want to manage this completely in the "core", it could be moved inside the logger facility ( --- a/py3status/py3.py
+++ b/py3status/py3.py
@@ -482,6 +482,7 @@ class Py3:
message = "\n" + message
except: # noqa e722
pass
+ message = message.decode('utf-8')
message = "Module `{}`: {}".format(self._module.module_full_name, message)
self._py3_wrapper.log(message, level) makes sense?
agreed, I was just trying to figure out why the CI fails on me, while locally tests are fine 🤔 I already opened the pr so this is exposed to the upstream (in general I rebase the pr). |
Some code that causes the issue is always nice to see :)
Travis seems to like breaking things just to keep people busy |
👍 And a unittest even better (see my first comment). This code, for example, reproduces the issue. |
@@ -482,6 +482,7 @@ def log(self, message, level=LOG_INFO): | |||
message = "\n" + message | |||
except: # noqa e722 | |||
pass | |||
message = message.decode('utf-8') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should do. Feel free to refactor something else in another PR and close this one if not deemed suitable in some aspects 👍
py3status/modules/mpris.py
Outdated
@@ -562,7 +562,7 @@ def mpris(self): | |||
'composite': composite | |||
} | |||
|
|||
# we are outputting so reset tries | |||
# we are outputing so reset tries |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reverted the unrelated fix
Thanks for your proposal @apiraino ! Travis is not happy tho as you can see, could you have a look please? |
yes I did notice that a couple of days ago but I still haven't sorted that out. I can't reproduce that on my workstation :-/ (Running py2 tests with version EDIT: ok need to run |
Try this: diff --git a/py3status/modules/mpris.py b/py3status/modules/mpris.py
index e8429a4..609a576 100644
--- a/py3status/modules/mpris.py
+++ b/py3status/modules/mpris.py
@@ -596,7 +596,7 @@ class Py3status:
_title = self._data.get('title', None)
if _title is not None:
if self.debug:
- self.py3.log(u'[{}] title: "{}"'.format(self.audio_perc, _title))
+ self.py3.log('[{}] title: "{}"'.format(self.audio_perc, _title))
self._maybe_save_audio()
_curr_vol, _ = self._get_volume()
if _title == '' or any(k in _title.lower() for k in self.state_quiet_keywords):
|
… into fix-utf8-encoding-py2
sorry @lasers my comment was not clear, I was referring to the bit in my comment that Travis was not happy about my patch :^) Well, now Travis, too, seems to be happy. Does this fix makes sense? In case you're happy with this, I will rebase this pr. |
That'll be up to @tobes. Just a heads up. #1584 "[RFC] Get rid of Python 2.7".
|
@ultrabug that's perfectly fine 👍 Please feel free to manage the issue the best way you envision, also close it if you prefer to tackle the issue from another point of view (you have the big picture here). This was just a teeny tiny issue I can live with of patch locally. I don't really care ;-) |
@ultrabug My proposal does not work too well. The better solution would be to cancel this PR and suggest the user to log |
Ok pursuing on this and going @lasers way, closing here and if we want to "fix" this just do as proposed |
While working on my plugin, I noticed that logging utf-8 strings raises a
UnicodeEncodeError
at line 485.I think the issue can be fixed by using in the module:
and applying this patch to
py3.py
, which removes the need to cast string to utf-8 withu'hey hey'
.Unfortunately I couldn't easily find a way to write a unit-test for this patch (any help on that would be great!).
Opinions?
Thanks for reviewing!