Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if self.pbtype == 'MESSAGE':
encsize = None
if str(self.submsgname) in dependencies:
submsg = dependencies[str(self.submsgname)]
encsize = submsg.encoded_size(dependencies)
if encsize is not None:
# Include submessage length prefix
encsize += varint_max_size(encsize.upperlimit())
if encsize is None:
# Submessage or its size cannot be found.
# This can occur if submessage is defined in different
# file, and it or its .options could not be found.
# Instead of direct numeric value, reference the size that
# has been #defined in the other file.
encsize = EncodedSize(self.submsgname + 'size')
# We will have to make a conservative assumption on the length
# prefix size, though.
encsize += 5
elif self.pbtype in ['ENUM', 'UENUM']:
if str(self.ctype) in dependencies:
enumtype = dependencies[str(self.ctype)]
encsize = enumtype.encoded_size()
else:
# Conservative assumption
encsize = 10
elif self.enc_size is None:
raise RuntimeError("Could not determine encoded size for %s.%s"
% (self.struct_name, self.name))
def encoded_size(self, dependencies):
'''Returns the size of the largest oneof field.'''
largest = EncodedSize(0)
for f in self.fields:
size = EncodedSize(f.encoded_size(dependencies))
if size.value is None:
return None
elif size.symbols:
return None # Cannot resolve maximum of symbols
elif size.value > largest.value:
largest = size
return largest
def encoded_size(self, dependencies):
'''Returns the size of the largest oneof field.'''
largest = EncodedSize(0)
for f in self.fields:
size = EncodedSize(f.encoded_size(dependencies))
if size.value is None:
return None
elif size.symbols:
return None # Cannot resolve maximum of symbols
elif size.value > largest.value:
largest = size
return largest
def __add__(self, other):
if isinstance(other, int):
return EncodedSize(self.value + other, self.symbols)
elif isinstance(other, strtypes + (Names,)):
return EncodedSize(self.value, self.symbols + [str(other)])
elif isinstance(other, EncodedSize):
return EncodedSize(self.value + other.value, self.symbols + other.symbols)
else:
raise ValueError("Cannot add size: " + repr(other))
def __add__(self, other):
if isinstance(other, int):
return EncodedSize(self.value + other, self.symbols)
elif isinstance(other, strtypes + (Names,)):
return EncodedSize(self.value, self.symbols + [str(other)])
elif isinstance(other, EncodedSize):
return EncodedSize(self.value + other.value, self.symbols + other.symbols)
else:
raise ValueError("Cannot add size: " + repr(other))
def __add__(self, other):
if isinstance(other, int):
return EncodedSize(self.value + other, self.symbols)
elif isinstance(other, strtypes + (Names,)):
return EncodedSize(self.value, self.symbols + [str(other)])
elif isinstance(other, EncodedSize):
return EncodedSize(self.value + other.value, self.symbols + other.symbols)
else:
raise ValueError("Cannot add size: " + repr(other))
def encoded_size(self, dependencies):
# We exclude extensions from the count, because they cannot be known
# until runtime. Other option would be to return None here, but this
# way the value remains useful if extensions are not used.
return EncodedSize(0)
def __mul__(self, other):
if isinstance(other, int):
return EncodedSize(self.value * other, [str(other) + '*' + s for s in self.symbols])
else:
raise ValueError("Cannot multiply size: " + repr(other))
# prefix size, though.
encsize += 5
elif self.pbtype in ['ENUM', 'UENUM']:
if str(self.ctype) in dependencies:
enumtype = dependencies[str(self.ctype)]
encsize = enumtype.encoded_size()
else:
# Conservative assumption
encsize = 10
elif self.enc_size is None:
raise RuntimeError("Could not determine encoded size for %s.%s"
% (self.struct_name, self.name))
else:
encsize = EncodedSize(self.enc_size)
encsize += varint_max_size(self.tag << 3) # Tag + wire type
if self.rules == 'REPEATED':
# Decoders must be always able to handle unpacked arrays.
# Therefore we have to reserve space for it, even though
# we emit packed arrays ourselves. For length of 1, packed
# arrays are larger however so we need to add allowance
# for the length byte.
encsize *= self.max_count
if self.max_count == 1:
encsize += 1
return encsize
def __add__(self, other):
if isinstance(other, int):
return EncodedSize(self.value + other, self.symbols)
elif isinstance(other, strtypes + (Names,)):
return EncodedSize(self.value, self.symbols + [str(other)])
elif isinstance(other, EncodedSize):
return EncodedSize(self.value + other.value, self.symbols + other.symbols)
else:
raise ValueError("Cannot add size: " + repr(other))