def to_python(value):
    if value in validators.EMPTY_VALUES:  # None or ''
        phone_number = value
    elif value and isinstance(value, string_types):
        try:
            phone_number = PhoneNumber.from_string(phone_number=value)
        except NumberParseException:
            # the string provided is not a valid PhoneNumber.
            phone_number = PhoneNumber(raw_input=value)
    elif (isinstance(value, phonenumbers.PhoneNumber) and
          not isinstance(value, PhoneNumber)):
        phone_number = PhoneNumber()
        phone_number.merge_from(value)
    elif isinstance(value, PhoneNumber):
        phone_number = value
    else:
        # TODO: this should somehow show that it has invalid data, but not
        #       completely die for bad data in the database.
        #       (Same for the NumberParseException above)
        phone_number = None
    return phone_number
dataset/ETHPy150Open stefanfoulis/django-phonenumber-field/phonenumber_field/phonenumber.py/to_python/original