Skip to content

Commit

Permalink
Fixed 'get_encodings_from_content' for Python3
Browse files Browse the repository at this point in the history
- Fixed 'get_encodings_from_content' incompatible with Python3.

- Rewrite using `br` prefix. Move compile calls up to the module level.

See pull request gh-176.

Closes gh-172
  • Loading branch information
adoal authored and sigmavirus24 committed Feb 13, 2017
1 parent 4b0406e commit 5e5591d
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions requests_toolbelt/utils/deprecated.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@

from requests import utils

find_charset = re.compile(
br'<meta.*?charset=["\']*(.+?)["\'>]', flags=re.I
).findall

find_pragma = re.compile(
br'<meta.*?content=["\']*;?charset=(.+?)["\'>]', flags=re.I
).findall

find_xml = re.compile(
br'^<\?xml.*?encoding=["\']*(.+?)["\'>]'
).findall


def get_encodings_from_content(content):
"""Return encodings from given content string.
Expand All @@ -19,17 +31,6 @@ def get_encodings_from_content(content):
:param content: bytestring to extract encodings from.
:type content: bytes
"""
find_charset = re.compile(
r'<meta.*?charset=["\']*(.+?)["\'>]', flags=re.I
).findall

find_pragma = re.compile(
r'<meta.*?content=["\']*;?charset=(.+?)["\'>]', flags=re.I
).findall

find_xml = re.compile(
r'^<\?xml.*?encoding=["\']*(.+?)["\'>]'
).findall

return find_charset(content) + find_pragma(content) + find_xml(content)

Expand Down

0 comments on commit 5e5591d

Please sign in to comment.