Skip to content

Commit 634405d

Browse files
committed
First pass at merging upstream r314
UTs not working yet
1 parent 4614099 commit 634405d

9 files changed

Lines changed: 536 additions & 117 deletions

python/HISTORY

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
What's new in 3.8b1
44
=====================
55

6-
Merge up to upstream Subversion revision 312.
6+
Merge up to upstream Subversion revision 314.
77
Includes initial simplistic implementation of geocoding functionality.
88

99

python/phonenumbers/asyoutypeformatter.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
import unicode_util
3030
from re_util import fullmatch
3131
from phonemetadata import PhoneMetadata
32-
from phonenumberutil import _VALID_START_CHAR_PATTERN, _VALID_PUNCTUATION
33-
from phonenumberutil import _PLUS_SIGN
32+
from phonenumberutil import _VALID_PUNCTUATION
33+
from phonenumberutil import _PLUS_SIGN, _PLUS_CHARS_PATTERN
3434
from phonenumberutil import _extract_country_code, region_code_for_country_code
3535
from phonenumberutil import country_code_for_region
3636

@@ -201,10 +201,10 @@ def _clear(self):
201201
self._prefix_before_national_number = ""
202202
self._national_number = ""
203203
self._able_to_format = True
204-
# The position of a digit upon which input_digit_and_remember_position is
204+
# The position of a digit upon which input_digit(remember_position=True) is
205205
# most recently invoked, as found in accrued_input_without_formatting.
206206
self._position_to_remember = 0
207-
# The position of a digit upon which input_digit_and_remember_position is
207+
# The position of a digit upon which input_digit(remember_position=True) is
208208
# most recently invoked, as found in the original sequence of
209209
# characters the user entered.
210210
self._original_position = 0
@@ -243,8 +243,9 @@ def input_digit(self, next_char, remember_position=False):
243243
if remember_position:
244244
self._original_position = len(self._accrued_input)
245245
# We do formatting on-the-fly only when each character entered is
246-
# either a plus sign or a digit.
247-
if not fullmatch(_VALID_START_CHAR_PATTERN, next_char):
246+
# either a digit, or a plus sign (accepted at the start of the number
247+
# only).
248+
if not self._is_digit_or_leading_plus_sign(next_char):
248249
self._able_to_format = False
249250
if not self._able_to_format:
250251
self._current_output = self._accrued_input
@@ -306,6 +307,11 @@ def input_digit(self, next_char, remember_position=False):
306307
self._current_output = self._attempt_to_choose_formatting_pattern()
307308
return self._current_output
308309

310+
def _is_digit_or_leading_plus_sign(self, next_char):
311+
return (next_char.isdigit() or
312+
(len(self._accrued_input) == 1 and
313+
fullmatch(_PLUS_CHARS_PATTERN, next_char)))
314+
309315
def _attempt_to_format_accrued_digits(self):
310316
for num_format in self._possible_formats:
311317
num_re = re.compile(num_format.pattern)

python/phonenumbers/geocoder.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,17 @@ def country_name_for_number(numobj, lang, script=None, region=None):
122122
return u""
123123

124124

125-
def description_for_number(numobj, lang, script=None, region=None):
126-
"""Return a text description of the given PhoneNumber object for the given language.
125+
def description_for_valid_number(numobj, lang, script=None, region=None):
126+
"""Return a text description of a PhoneNumber object for the given language.
127127
128128
The description might consist of the name of the country where the phone
129129
number is from and/or the name of the geographical area the phone number
130-
is from.
130+
is from. This function assumes the validity of the number passed in has
131+
already been checked.
131132
132133
Arguments:
133-
numobj -- The PhoneNumber object for which we want to get a text description.
134+
numobj -- A valid PhoneNumber object for which we want to get a text
135+
description.
134136
lang -- A 2-letter lowercase ISO 639-1 language code for the language in
135137
which the description should be returned (e.g. "en")
136138
script -- A 4-letter titlecase (first letter uppercase, rest lowercase)
@@ -140,15 +142,36 @@ def description_for_number(numobj, lang, script=None, region=None):
140142
141143
Returns a text description in the given language code, for the given phone
142144
number, or an empty string if no description is available."""
143-
if not is_valid_number(numobj):
144-
return ""
145145
area_description = area_description_for_number(numobj, lang, script, region)
146146
if area_description != "":
147147
return area_description
148148
else:
149149
# Fall back to the description of the number's region
150150
return country_name_for_number(numobj, lang, script, region)
151151

152+
153+
def description_for_number(numobj, lang, script=None, region=None):
154+
"""Return a text description of a PhoneNumber object for the given language.
155+
156+
The description might consist of the name of the country where the phone
157+
number is from and/or the name of the geographical area the phone number
158+
is from. This function explicitly checks the validity of the number passed in
159+
160+
Arguments:
161+
numobj -- The PhoneNumber object for which we want to get a text description.
162+
lang -- A 2-letter lowercase ISO 639-1 language code for the language in
163+
which the description should be returned (e.g. "en")
164+
script -- A 4-letter titlecase (first letter uppercase, rest lowercase)
165+
ISO script code as defined in ISO 15924, separated by an
166+
underscore (e.g. "Hant")
167+
region -- A 2-letter uppercase ISO 3166-1 country code (e.g. "GB")
168+
169+
Returns a text description in the given language code, for the given phone
170+
number, or an empty string if no description is available."""
171+
if not is_valid_number(numobj):
172+
return ""
173+
return description_for_valid_number(numobj, lang, script, region)
174+
152175
if __name__ == '__main__': # pragma no cover
153176
import doctest
154177
doctest.testmod()

0 commit comments

Comments
 (0)