def calculate_repetition(char, pos, offset, maxoffset):
    """
    Recursively calculates offset/maxoffset repetition,
    when one or both offsets is greater than 65535 bytes (64KB)
    see: bugs.python.org/issue13169
    Otherwise it returns the {offset,maxoffset}
    """
    calcbuf = cStringIO.StringIO()
    
    calcremain = False
    offsetremain = 0
    maxoffsetremain = 0
    
    if offset != None and offset != '':
        if int(offset) > 65535:
            offsetremain = str(int(offset) - 65535)
            offset = '65535'
            calcremain = True
    if maxoffset != None and maxoffset != '':
        if int(maxoffset) > 65535:
            maxoffsetremain = str(int(maxoffset) - 65535)
            maxoffset = '65535'
            calcremain = True
    
    if pos == "BOF" or pos == "EOF":
        if offset != '0':
            calcbuf.write(char + '{' + str(offset))
            if maxoffset != None:
                calcbuf.write(',' + maxoffset)
            calcbuf.write('}')
        elif maxoffset != None:
            calcbuf.write(char + '{0,' + maxoffset + '}')

    if pos == "IFB":
        if offset != '0':
            calcbuf.write(char + '{' + str(offset))
            if maxoffset != None:
                calcbuf.write(',' + maxoffset)
            calcbuf.write('}')
            if maxoffset == None:
                calcbuf.write(',}')
        elif maxoffset != None:
            calcbuf.write(char + '{0,' + maxoffset + '}')

    if calcremain: # recursion happens here
        calcbuf.write(calculate_repetition(char, pos, offsetremain, maxoffsetremain))
    
    val = calcbuf.getvalue()
    calcbuf.close()
    return val
dataset/ETHPy150Open openpreserve/fido/fido/prepare.py/calculate_repetition/original