Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/ijson-3.2.0.post0-py3.8-linux-x86_64.egg/ijson/compat.py: 63%
27 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:10 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:10 +0000
1'''
2Python2/Python3 compatibility utilities.
3'''
5import sys
6import warnings
9IS_PY2 = sys.version_info[0] < 3
10IS_PY35 = sys.version_info[0:2] >= (3, 5)
13if IS_PY2:
14 b2s = lambda s: s
15 bytetype = str
16 texttype = unicode
17 from StringIO import StringIO
18 BytesIO = StringIO
19else:
20 b2s = lambda b: b.decode('utf-8')
21 bytetype = bytes
22 texttype = str
23 from io import BytesIO, StringIO
25class utf8reader(object):
26 """Takes a utf8-encoded string reader and reads bytes out of it"""
28 def __init__(self, str_reader):
29 self.str_reader = str_reader
31 def read(self, n):
32 return self.str_reader.read(n).encode('utf-8')
34_str_vs_bytes_warning = '''
35ijson works by reading bytes, but a string reader has been given instead. This
36probably, but not necessarily, means a file-like object has been opened in text
37mode ('t') rather than binary mode ('b').
39An automatic conversion is being performed on the fly to continue, but on the
40other hand this creates unnecessary encoding/decoding operations that decrease
41the efficiency of the system. In the future this automatic conversion will be
42removed, and users will receive errors instead of this warning. To avoid this
43problem make sure file-like objects are opened in binary mode instead of text
44mode.
45'''
47def _warn_and_return(o):
48 warnings.warn(_str_vs_bytes_warning, DeprecationWarning)
49 return o
51def bytes_reader(f):
52 """Returns a file-like object that reads bytes"""
53 if type(f.read(0)) == bytetype:
54 return f
55 return _warn_and_return(utf8reader(f))