@@ -87,13 +87,20 @@ class Builder(object):
8787 __slots__ = ("Bytes" , "current_vtable" , "head" , "minalign" , "objectEnd" ,
8888 "vtables" )
8989
90+ """
91+ Maximum buffer size constant, in bytes.
92+ Builder will never allow it's buffer grow over this size.
93+ Currently equals 2Gb.
94+ """
95+ MAX_BUFFER_SIZE = 2 ** 31
96+
9097 def __init__ (self , initialSize ):
9198 """
9299 Initializes a Builder of size `initial_size`.
93100 The internal buffer is grown as needed.
94101 """
95102
96- if not (0 <= initialSize <= self . MaxBufferSize () ):
103+ if not (0 <= initialSize <= Builder . MAX_BUFFER_SIZE ):
97104 msg = "flatbuffers: Cannot create Builder larger than 2 gigabytes."
98105 raise BuilderSizeError (msg )
99106
@@ -104,12 +111,6 @@ def __init__(self, initialSize):
104111 self .objectEnd = None
105112 self .vtables = []
106113
107- def MaxBufferSize (self ):
108- """
109- Maximum buffer size is 2Gb.
110- """
111- return 2 ** 31
112-
113114 def Output (self ):
114115 """
115116 Output returns the portion of the buffer that has been used for
@@ -244,11 +245,11 @@ def EndObject(self):
244245 def growByteBuffer (self ):
245246 """Doubles the size of the byteslice, and copies the old data towards
246247 the end of the new buffer (since we build the buffer backwards)."""
247- if not len (self .Bytes ) <= self . MaxBufferSize () :
248+ if not len (self .Bytes ) <= Builder . MAX_BUFFER_SIZE :
248249 msg = "flatbuffers: cannot grow buffer beyond 2 gigabytes"
249250 raise BuilderSizeError (msg )
250251
251- newSize = len (self .Bytes ) * 2
252+ newSize = min ( len (self .Bytes ) * 2 , Builder . MAX_BUFFER_SIZE )
252253 if newSize == 0 :
253254 newSize = 1
254255 bytes2 = bytearray (newSize )
0 commit comments