def _convertTextToType(valueToConvert, defaultValue, desiredType=None):
    """
    Ensure the value has the correct expected type by converting it.
    Since the values are extracted from text, it is normal that they
    don't have the expected type right away.

    If the value cannot be converted, use the default value. This could
    happen if the optionVar got corrupted, for example from corrupted user
    prefs.
    """
    if not desiredType:
        desiredType = type(defaultValue)
    if isinstance(valueToConvert, desiredType):
        return valueToConvert
    # Try to convert the value to the desired type.
    # We only support a subset of types to avoid problems,
    # for example trying to convert text to a list would
    # create a list of single letters.
    try:
        textConvertibleTypes = [str, int, float]
        if desiredType in textConvertibleTypes:
            return desiredType(valueToConvert)
        elif desiredType == bool:
            if valueToConvert.lower() == 'true':
                return True
            if valueToConvert.lower() == 'false':
                return False
            return bool(int(valueToConvert))
    except:
        pass

    # Verify to convert list values by converting each element.
    # Unfortunately, for historical reasons, text lists have used
    # comma-separated values, while floating-point used space-separated values.
    try:
        if isinstance(defaultValue, list):
            if ',' in valueToConvert:
                values = valueToConvert.split(',')
                desiredType = str
            else:
                values = valueToConvert.split()
                desiredType = float
            convertedValues = []
            for value in values:
                value = value.strip()
                if not value:
                    continue
                convertedValue = _convertTextToType(value.strip(), None, desiredType)
                if convertedValue is None:
                    continue
                convertedValues.append(convertedValue)
            return convertedValues
    except:
        pass

    # Use the default value if the data is somehow corrupted,
    # for example from corrupted user prefs.
    return defaultValue