openhab-scripters / openhab-helper-libraries

Scripts and modules for use with openHAB
Eclipse Public License 1.0
88 stars 69 forks source link

Having accented character in rule name leads to a UnicodeEncodeError #302

Closed jenche closed 3 years ago

jenche commented 4 years ago

I tried to define a rule with the following decorator: @rule(u'Contrôle de l\'alarme', description=u'Active ou désactive l\'alarme')

When the script containing this rule loads, I get the following error: Error during evaluation of script 'file:/etc/openhab2/automation/jsr223/python/personal/alarme.py': UnicodeEncodeError: 'ascii' codec can't encode character u'\xf4' in position 5: ordinal not in range(128) in <script> at line number 10

Replacing the "ô" character in the rule name by "o" fixes the problem. Surprisingly, having this character in the rule description doesn't cause any problem.

Environment (please complete the following information):

5iver commented 4 years ago

Sometime soon I will need to do some unicode testing and cleanup. I'll take a look at this one tonight. My WAG is that there is a log entry in core.rules causing the issue.

5iver commented 4 years ago

@jenche, it was a log entry as I suspected. I will submit a PR for this, but you can correct this by changing line 129 of core.rules to...

log.debug(u"Added rule [{}]".format(rule.name))

In case you missed it, there's a 'u' added to make a unicode string :slightly_smiling_face:. You'll need to reload the module in your script (or restart OH) for the change to be picked up. Thank you for reporting this!

jenche commented 4 years ago

Great, thanks for fixing it that fast !

jenche commented 4 years ago

Mmmm, I applied the fix in core.rules as you told me, restarted openhab, but still have the same error. Is it possible there's another unicode encoding problem elsewhere in the code ?

5iver commented 4 years ago

Yes, it is very possible! But in testing, I was able to use unicode in a rule name without error using this fix. It's been a while though, so I'll test again. I was hoping to merge this today. What was your error?

jenche commented 4 years ago

It's exactly the same than in my first post :UnicodeEncodeError: 'ascii' codec can't encode character u'\xf4' in position 5: ordinal not in range(128) in <script> at line number 10

jenche commented 4 years ago

Ah, found it. Need to change line 98 in core/rules.py to callback.log = logging.getLogger(u"{}.{}".format(LOG_PREFIX, name))

5iver commented 4 years ago

My bad... I already have this, so I neglected to point out this change.... sorry about that!

5iver commented 3 years ago

I finally got this merged in #372.