11from bson import InvalidBSON , BSON , codec_options
2- import six
32import sys
43import struct
54
@@ -15,9 +14,14 @@ class BSONInput(object):
1514 https://github.com/klbostee/typedbytes
1615 """
1716
18- def __init__ (self , fh = sys .stdin , unicode_errors = 'strict' , fast_string_prematch = "" , decode = True ):
17+ def __init__ (self , fh = sys .stdin ,
18+ unicode_errors : str = 'strict' ,
19+ fast_string_prematch : bytes = b"" ,
20+ decode : bool = True ):
1921 self .fh = fh
2022 self .unicode_errors = unicode_errors
23+ if not isinstance (fast_string_prematch , bytes ):
24+ raise ValueError ("fast_string_prematch must be a bytes object" )
2125 self .fast_string_prematch = fast_string_prematch
2226 self .eof = False
2327 self .decode = decode
@@ -26,26 +30,22 @@ def __init__(self, fh=sys.stdin, unicode_errors='strict', fast_string_prematch="
2630 def _read (self ):
2731 try :
2832 size_bits = self .fh .read (4 )
29- size = struct .unpack ("<i" , size_bits )[0 ] - 4 # BSON size byte includes itself
33+ size = struct .unpack ("<i" , size_bits )[0 ] - 4 # BSON size byte includes itself
3034 data = size_bits + self .fh .read (size )
3135 if len (data ) != size + 4 :
3236 raise struct .error ("Unable to read expected BSON Chunk; " +
3337 "EOF, underful buffer or invalid object size." )
34- if six .PY3 :
35- eoo = 0x00
36- else : # six.PY2
37- eoo = "\x00 "
38+ eoo = 0x00
3839 if data [size + 4 - 1 ] != eoo :
3940 raise InvalidBSON ("Bad EOO in BSON Data" )
40- if self .fast_string_prematch . encode ( "utf-8" ) in data :
41+ if self .fast_string_prematch in data :
4142 if self .decode :
4243 try :
4344 return BSON (data ).decode (self .codec )
4445 except TypeError :
4546 return BSON (data ).decode ()
4647 else :
4748 return data
48- raise ValueError ("Unknown Error" )
4949 except struct .error as e :
5050 self .eof = True
5151 raise StopIteration (e )
@@ -60,13 +60,19 @@ def read(self):
6060 def _reads (self ):
6161 r = self ._read
6262 while 1 :
63- yield r ()
63+ try :
64+ data = r ()
65+ if data :
66+ yield data
67+ except StopIteration :
68+ break
6469
6570 def close (self ):
6671 self .fh .close ()
6772
6873 __iter__ = reads = _reads
6974
75+
7076class KeyValueBSONInput (BSONInput ):
7177 def read (self ):
7278 try :
@@ -78,12 +84,12 @@ def read(self):
7884
7985 def reads (self ):
8086 it = self ._reads ()
81- if six .PY3 :
82- n = it .__next__
83- else : # six.PY2
84- n = it .next
87+ n = it .__next__
8588 while 1 :
86- doc = n ()
87- yield doc
89+ try :
90+ doc = n ()
91+ yield doc
92+ except StopIteration :
93+ break
8894
8995 __iter__ = reads
0 commit comments