Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/ijson/compat.py: 69%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1'''
2Python2/Python3 compatibility utilities.
3'''
5import sys
6import warnings
9class utf8reader:
10 """Takes a utf8-encoded string reader and reads bytes out of it"""
12 def __init__(self, str_reader):
13 self.str_reader = str_reader
15 def read(self, n):
16 return self.str_reader.read(n).encode('utf-8')
18_str_vs_bytes_warning = '''
19ijson works by reading bytes, but a string reader has been given instead. This
20probably, but not necessarily, means a file-like object has been opened in text
21mode ('t') rather than binary mode ('b').
23An automatic conversion is being performed on the fly to continue, but on the
24other hand this creates unnecessary encoding/decoding operations that decrease
25the efficiency of the system. In the future this automatic conversion will be
26removed, and users will receive errors instead of this warning. To avoid this
27problem make sure file-like objects are opened in binary mode instead of text
28mode.
29'''
31def _warn_and_return(o):
32 warnings.warn(_str_vs_bytes_warning, DeprecationWarning)
33 return o
35def bytes_reader(f):
36 """Returns a file-like object that reads bytes"""
37 if type(f.read(0)) == bytes:
38 return f
39 return _warn_and_return(utf8reader(f))