Skip to content

Fix UnicodeDecodeError in Python 2 with Object Macros - #28

Closed
kirsle wants to merge 6 commits into
masterfrom
bug/python-macro-text-type
Closed

Fix UnicodeDecodeError in Python 2 with Object Macros#28
kirsle wants to merge 6 commits into
masterfrom
bug/python-macro-text-type

Conversation

@kirsle

@kirsle kirsle commented Jul 8, 2016

Copy link
Copy Markdown
Member

This fixes a bug for Python 2 where a Python object macro was unable to return a Unicode string, due to the Python macro handler casting all return values as str() type, which in Python 2, assumes ASCII, and it would raise a UnicodeDecodeError.

Instead the return value is casted to six.text_type which will be unicode in Python 2 and str in Python 3.

The following RiveScript example code demonstrates the original problem and that this change fixes it:

> object wiki python
    import re
    import requests

    res = requests.get("http://bulbapedia.bulbagarden.net/wiki/Umbreon_(Pok%C3%A9mon)")
    m = re.search(r': <b>(.+?)</b> <i>Blacky</i>', res.text)

    if m:
        name = m.group(1)
        return name
    return "No match"
< object

+ test
- Result: <call>wiki</call>

Expected result:

You> test
Bot> Result: ブラッキー

@kirsle
kirsle force-pushed the bug/python-macro-text-type branch from 3deb2a1 to 8a2f239 Compare July 8, 2016 20:52
@kirsle
kirsle force-pushed the bug/python-macro-text-type branch from 37d2186 to 5ad6d6c Compare July 8, 2016 20:58
@kirsle kirsle closed this Jul 8, 2016
@kirsle

kirsle commented Jul 8, 2016

Copy link
Copy Markdown
Member Author

Python nightly (3.6) keeps failing due to what I can only hope is a temporary problem in Python nightly (it thinks having a \d in a quoted string is an error, but it's a very valid thing to have for regular expressions). I'll merge the individual commit that fixed this bug directly into the master branch, and turn off the Python-nightly build in the mean time.

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.

1 participant