Skip to content

Commit

Permalink
Fix UnicodeEncodeError issue using Python 2
Browse files Browse the repository at this point in the history
  • Loading branch information
sergioburdisso committed Feb 19, 2020
1 parent 8feeef5 commit 867026e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
4 changes: 4 additions & 0 deletions pyss3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1772,6 +1772,8 @@ def learn(self, doc, cat, n_grams=1, prep=True, update=True):
"""
try:
doc = doc.decode(ENCODING)
except UnicodeEncodeError: # for python 2 compatibility
doc = doc.encode(ENCODING).decode(ENCODING)
except AttributeError:
pass
try:
Expand Down Expand Up @@ -1886,6 +1888,8 @@ def classify(self, doc, prep=True, sort=True, json=False):

try:
doc = doc.decode(ENCODING)
except UnicodeEncodeError: # for python 2 compatibility
doc = doc.encode(ENCODING).decode(ENCODING)
except BaseException:
pass

Expand Down
16 changes: 11 additions & 5 deletions tests/test_cmd_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from pyss3 import SS3
from os import path

import pyss3.cmd_line
import pyss3.util
import pytest
import sys

Expand All @@ -22,11 +24,15 @@ def test_ss3prompt(mocker, monkeypatch):
if PYTHON3:
monkeypatch.setattr('builtins.input', lambda: 'Y')
mocker.patch.object(SS3Prompt, "cmdloop")
mocker.patch("pyss3.cmd_line.STOPWORDS_FILE", "tests/ss3_models/ss3_stopwords[%s].txt")
mocker.patch(
"pyss3.util.EVAL_HTML_OUT_FILE",
"tests/ss3_models/ss3_model_evaluation[%s].html"
)
# not working in Python 2
# mocker.patch("pyss3.cmd_line.STOPWORDS_FILE", "tests/ss3_models/ss3_stopwords[%s].txt")
# mocker.patch(
# "pyss3.util.EVAL_HTML_OUT_FILE",
# "tests/ss3_models/ss3_model_evaluation[%s].html"
# )
# replaced by:
pyss3.cmd_line.STOPWORDS_FILE = "tests/ss3_models/ss3_stopwords[%s].txt"
pyss3.util.EVAL_HTML_OUT_FILE = "tests/ss3_models/ss3_model_evaluation[%s].html"

main()

Expand Down

0 comments on commit 867026e

Please sign in to comment.