cffi_utils/py2to3.py
def get_rand_int(encoding='latin1', avoid=[]):
    '''
    encoding-->str: one of ENCODINGS
    avoid-->list of int: to void (unprintable chars etc)
    Returns-->int that can be converted to requested encoding
              which is NOT in avoid
    '''
    UNICODE_LIMIT = 0x10ffff
    # See: https://en.wikipedia.org/wiki/UTF-8#Invalid_code_points
    SURROGATE_RANGE = (0xD800, 0xDFFF)
    if encoding not in ENCODINGS:
        raise ValueError('Unsupported encoding: ' + str(encoding))

    if encoding == 'ascii':
        maxord = 2 ** 7
    elif encoding == 'latin1':
        maxord = 2 ** 8
    elif encoding == 'utf16':
        maxord = 2 ** 16
    elif encoding == 'utf8':
        maxord = 2 ** 32
    elif encoding == 'utf32':
        maxord = 2 ** 32

    rndint = random.randrange(0, min(maxord, UNICODE_LIMIT))
    while (
        (rndint in avoid) or
        (SURROGATE_RANGE[0] <= rndint <= SURROGATE_RANGE[1])
    ):
        rndint = random.randrange(0, min(maxord, UNICODE_LIMIT))
    return rndint
def get_rand_int(encoding='latin1', avoid=[]):
    '''
    encoding-->str: one of ENCODINGS
    avoid-->list of int: to void (unprintable chars etc)
    Returns-->int that can be converted to requested encoding
              which is NOT in avoid
    '''
    UNICODE_LIMIT = 0x10ffff
    # See: https://en.wikipedia.org/wiki/UTF-8#Invalid_code_points
    SURROGATE_RANGE = (0xD800, 0xDFFF)
    if encoding not in ENCODINGS:
        raise ValueError('Unsupported encoding: ' + str(encoding))

    if encoding == 'ascii':
        maxord = 2 ** 7
    elif encoding == 'latin1':
        maxord = 2 ** 8
    elif encoding == 'utf16':
        maxord = 2 ** 16
    elif encoding == 'utf8':
        maxord = 2 ** 32
    elif encoding == 'utf32':
        maxord = 2 ** 32

    rndint = random.randrange(0, min(maxord, UNICODE_LIMIT))
    while (
        (rndint in avoid) or
        (SURROGATE_RANGE[0] <= rndint <= SURROGATE_RANGE[1])
    ):
        rndint = random.randrange(0, min(maxord, UNICODE_LIMIT))
    return rndint
encoding-->str: one of ENCODINGS
    avoid-->list of int: to void (unprintable chars etc)
    Returns-->int that can be converted to requested encoding
              which is NOT in avoid