Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sqlmapproject
GitHub Repository: sqlmapproject/sqlmap
Path: blob/master/thirdparty/chardet/__init__.py
2992 views
1
######################## BEGIN LICENSE BLOCK ########################
2
# This library is free software; you can redistribute it and/or
3
# modify it under the terms of the GNU Lesser General Public
4
# License as published by the Free Software Foundation; either
5
# version 2.1 of the License, or (at your option) any later version.
6
#
7
# This library is distributed in the hope that it will be useful,
8
# but WITHOUT ANY WARRANTY; without even the implied warranty of
9
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10
# Lesser General Public License for more details.
11
#
12
# You should have received a copy of the GNU Lesser General Public
13
# License along with this library; if not, write to the Free Software
14
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
15
# 02110-1301 USA
16
######################### END LICENSE BLOCK #########################
17
18
19
from .compat import PY2, PY3
20
from .universaldetector import UniversalDetector
21
from .version import __version__, VERSION
22
23
24
def detect(byte_str):
25
"""
26
Detect the encoding of the given byte string.
27
28
:param byte_str: The byte sequence to examine.
29
:type byte_str: ``bytes`` or ``bytearray``
30
"""
31
if not isinstance(byte_str, bytearray):
32
if not isinstance(byte_str, bytes):
33
raise TypeError('Expected object of type bytes or bytearray, got: '
34
'{0}'.format(type(byte_str)))
35
else:
36
byte_str = bytearray(byte_str)
37
detector = UniversalDetector()
38
detector.feed(byte_str)
39
return detector.close()
40
41