Skip to content

Commit

Permalink
Merge pull request #57 from kxepal/fix-detect-error-report
Browse files Browse the repository at this point in the history
Fix chardet.detect exception
  • Loading branch information
dan-blanchard committed Apr 19, 2015
2 parents b8da859 + db77e13 commit ff40135
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
9 changes: 4 additions & 5 deletions chardet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@
######################### END LICENSE BLOCK #########################


from .compat import PY2, PY3
from .compat import PY2, PY3, bin_type as _bin_type
from .universaldetector import UniversalDetector
from .version import __version__, VERSION


def detect(byte_str):
if (PY2 and isinstance(byte_str, unicode)) or (PY3 and
not isinstance(byte_str,
bytes)):
raise ValueError('Expected a bytes object, not a unicode object')
if not isinstance(byte_str, _bin_type):
raise TypeError('Expected object of {0} type, got: {1}'
''.format(_bin_type, type(byte_str)))

u = UniversalDetector()
u.feed(byte_str)
Expand Down
2 changes: 2 additions & 0 deletions chardet/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@
PY3 = False
base_str = (str, unicode)
text_type = unicode
bin_type = str
else:
PY2 = False
PY3 = True
base_str = (bytes, str)
text_type = str
bin_type = (bytes, bytearray)


def wrap_ord(a):
Expand Down

0 comments on commit ff40135

Please sign in to comment.