Skip to content

Commit 5d4efb8

Browse files
committed
Apply updates from upstream r247, bringing code into line with r266
1 parent f45b08b commit 5d4efb8

6 files changed

Lines changed: 48 additions & 47 deletions

File tree

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.6b1
44
=====================
55

6-
Merge up to upstream Subversion revision 231 (but
6+
Merge up to upstream Subversion revision 266 (but
77
geocoding functionality not yet ported).
88
Require Python 2.5, to allow import unicodedata.
99

python/phonenumbers/asyoutypeformatter.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ def __init__(self, region_code):
7171
7272
Arguments:
7373
74-
region_code -- The ISO 3166-1 two-letter region code that denotes the region where
75-
the phone number is being entered
74+
region_code -- The region where the phone number is being entered
7675
7776
Return an AsYouTypeFormatter} object, which could be used to format
7877
phone numbers in the specific region "as you type"

python/phonenumbers/phonenumbermatcher.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,12 @@ def __init__(self, text, region,
150150
151151
Arguments:
152152
text -- The character sequence that we will search, None for no text.
153-
country -- The ISO 3166-1 two-letter country code indicating the
154-
country to assume for phone numbers not written in international
155-
format (with a leading plus, or with the international dialing
156-
prefix of the specified region). May be None or "ZZ" if only
157-
numbers with a leading plus should be considered.
153+
country -- The region that we are expecting the number to be from.
154+
This is only used if the number being parsed is not written in
155+
international format (with a leading plus, or with the
156+
international dialing prefix of the specified region). May be
157+
None or "ZZ" if only numbers with a leading plus should be
158+
considered.
158159
leniency -- The leniency to use when evaluating candidate phone
159160
numbers.
160161
max_tries -- The maximum number of invalid numbers to try before

python/phonenumbers/phonenumberutil.py

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
1-
"""Python phone number parsing and formatting library"""
1+
"""Python phone number parsing and formatting library
22
3+
If you use this library, and want to be notified about important changes,
4+
please sign up to the libphonenumber mailing list at
5+
http://groups.google.com/group/libphonenumber-discuss/about.
6+
7+
NOTE: A lot of methods in this module require Region Code strings. These must
8+
be provided using ISO 3166-1 two-letter country-code format. These should be
9+
in upper-case. The list of the codes can be found here:
10+
http://www.iso.org/iso/english_country_names_and_code_elements
11+
12+
author: Shaopeng Jia (original Java version)
13+
author: Lara Rennie (original Java Version)
14+
author: David Drysdale (Python version)
15+
"""
316
# Based on original Java code:
417
# java/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java
518
# Copyright (C) 2009-2011 Google Inc.
@@ -608,7 +621,7 @@ def _is_valid_region_code(region_code):
608621
"""Helper function to check region code is not unknown or None"""
609622
if region_code is None:
610623
return False
611-
return (region_code.upper() in SUPPORTED_REGIONS)
624+
return (region_code in SUPPORTED_REGIONS)
612625

613626

614627
def format_number(numobj, num_format):
@@ -630,18 +643,18 @@ def format_number(numobj, num_format):
630643
631644
Returns the formatted phone number.
632645
"""
633-
country_code = numobj.country_code
646+
country_calling_code = numobj.country_code
634647
nsn = national_significant_number(numobj)
635648
if num_format == PhoneNumberFormat.E164:
636649
# Early exit for E164 case since no formatting of the national number needs to be applied.
637650
# Extensions are not formatted.
638-
return _format_number_by_format(country_code, num_format, nsn)
651+
return _format_number_by_format(country_calling_code, num_format, nsn)
639652

640653
# Note region_code_for_country_code() is used because formatting
641654
# information for regions which share a country calling code is contained
642655
# by only one region for performance reasons. For example, for NANPA
643656
# regions it will be contained in the metadata for US.
644-
region_code = region_code_for_country_code(country_code)
657+
region_code = region_code_for_country_code(country_calling_code)
645658
if not _is_valid_region_code(region_code):
646659
return nsn
647660

@@ -650,7 +663,7 @@ def format_number(numobj, num_format):
650663
region_code,
651664
num_format,
652665
formatted_number)
653-
return _format_number_by_format(country_code,
666+
return _format_number_by_format(country_calling_code,
654667
num_format,
655668
formatted_number)
656669

@@ -815,8 +828,7 @@ def format_out_of_country_calling_number(numobj, region_calling_from):
815828
816829
Arguments:
817830
numobj -- The phone number to be formatted
818-
region_calling_from -- The ISO 3166-1 two-letter region code that denotes
819-
the region where the call is being placed
831+
region_calling_from -- The region where the call is being placed
820832
821833
Returns the formatted phone number
822834
"""
@@ -1143,8 +1155,7 @@ def example_number(region_code):
11431155
"""Gets a valid number for the specified region.
11441156
11451157
Arguments:
1146-
region_code -- The ISO 3166-1 two-letter region code that denotes
1147-
the region for which an example number is needed.
1158+
region_code -- The region for which an example number is needed.
11481159
11491160
Returns a valid fixed-line number for the specified region. Returns None
11501161
when the metadata does not contain such information.
@@ -1156,12 +1167,12 @@ def example_number_for_type(region_code, num_type):
11561167
"""Gets a valid number for the specified region and number type.
11571168
11581169
Arguments:
1159-
region_code -- The ISO 3166-1 two-letter region code that denotes
1160-
the region for which an example number is needed.
1170+
region_code -- The region for which an example number is needed.
11611171
num_type -- The type of number that is needed.
11621172
11631173
Returns a valid number for the specified region and type. Returns None
1164-
when the metadata does not contain such information.
1174+
when the metadata does not contain such information or if an invalid
1175+
region was specified.
11651176
"""
11661177
# Check the region code is valid.
11671178
if not _is_valid_region_code(region_code):
@@ -1316,8 +1327,7 @@ def is_valid_number_for_region(numobj, region_code):
13161327
13171328
Arguments:
13181329
numobj -- The phone number object that we want to validate.
1319-
region_code -- The ISO 3166-1 two-letter region code that denotes the
1320-
region that we want to validate the phone number for.
1330+
region_code -- The region that we want to validate the phone number for.
13211331
13221332
Returns a boolean that indicates whether the number is of a valid pattern.
13231333
"""
@@ -1399,8 +1409,7 @@ def country_code_for_region(region_code):
13991409
Zealand.
14001410
14011411
Arguments:
1402-
region_code -- The ISO 3166-1 two-letter region code that denotes
1403-
the region that we want to get the country calling code for.
1412+
region_code -- The region that we want to get the country calling code for.
14041413
14051414
Returns the country calling code for the region denoted by region_code.
14061415
"""
@@ -1424,8 +1433,7 @@ def ndd_prefix_for_region(region_code, strip_non_digits):
14241433
prefix when required.
14251434
14261435
Arguments:
1427-
region_code -- The ISO 3166-1 two-letter region code that denotes
1428-
the region that we want to get the dialling prefix for.
1436+
region_code -- The region that we want to get the dialling prefix for.
14291437
strip_non_digits -- whether to strip non-digits from the national
14301438
dialling prefix.
14311439
@@ -1451,7 +1459,7 @@ def is_nanpa_country(region_code):
14511459
Numbering Plan Administration (NANPA).
14521460
"""
14531461
return (region_code is not None and
1454-
region_code.upper() in _NANPA_REGIONS)
1462+
region_code in _NANPA_REGIONS)
14551463

14561464

14571465
def _is_leading_zero_possible(country_code):
@@ -1585,17 +1593,16 @@ def is_possible_number_string(number, region_dialing_from):
15851593
15861594
Arguments:
15871595
number -- The number that needs to be checked, in the form of a string.
1588-
region_dialling_from -- The ISO 3166-1 two-letter region code that denotes
1589-
the region that we are expecting the number to be dialed from.
1590-
Note this is different from the region where the number belongs.
1591-
For example, the number +1 650 253 0000 is a number that belongs
1592-
to US. When written in this form, it can be dialed from any
1593-
region. When it is written as 00 1 650 253 0000, it can be
1594-
dialed from any region which uses an international dialling
1595-
prefix of 00. When it is written as 650 253 0000, it can only be
1596-
dialed from within the US, and when written as 253 0000, it can
1597-
only be dialed from within a smaller area in the US (Mountain
1598-
View, CA, to be more specific).
1596+
region_dialling_from -- The region that we are expecting the number to be
1597+
dialed from. Note this is different from the region where the
1598+
number belongs. For example, the number +1 650 253 0000 is a
1599+
number that belongs to US. When written in this form, it can be
1600+
dialed from any region. When it is written as 00 1 650 253 0000,
1601+
it can be dialed from any region which uses an international
1602+
dialling prefix of 00. When it is written as 650 253 0000, it
1603+
can only be dialed from within the US, and when written as 253
1604+
0000, it can only be dialed from within a smaller area in the US
1605+
(Mountain View, CA, to be more specific).
15991606
16001607
Returns True if the number is possible
16011608
"""
@@ -1942,8 +1949,7 @@ def parse(number, region, keep_raw_input=False,
19421949
number -- The number that we are attempting to parse. This can
19431950
contain formatting such as +, ( and -, as well as a phone
19441951
number extension.
1945-
region -- The ISO 3166-1 two-letter region code that denotes the
1946-
region that we are expecting the number to be from. This
1952+
region -- The region that we are expecting the number to be from. This
19471953
is only used if the number being parsed is not written in
19481954
international format. The country_code for the number in
19491955
this case would be stored as that of the default region

python/tests/asyoutypetest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ def testAYTFGBFixedLine(self):
379379
self.assertEquals("020 7031 3000", formatter.input_digit('0'))
380380

381381
def testAYTFGBTollFree(self):
382-
formatter = AsYouTypeFormatter("gb")
382+
formatter = AsYouTypeFormatter("GB")
383383
self.assertEquals("0", formatter.input_digit('0'))
384384
self.assertEquals("08", formatter.input_digit('8'))
385385
self.assertEquals("080", formatter.input_digit('0'))

python/tests/phonenumberutiltest.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,6 @@ def testIsValidForRegion(self):
773773
# This number is valid for the Bahamas, but is not a valid US number.
774774
self.assertTrue(phonenumbers.is_valid_number(BS_NUMBER))
775775
self.assertTrue(phonenumbers.is_valid_number_for_region(BS_NUMBER, "BS"))
776-
self.assertTrue(phonenumbers.is_valid_number_for_region(BS_NUMBER, "bs"))
777776
self.assertFalse(phonenumbers.is_valid_number_for_region(BS_NUMBER, "US"))
778777
bsInvalidNumber = PhoneNumber(country_code=1, national_number=2421232345L)
779778
# This number is no longer valid.
@@ -836,7 +835,6 @@ def testGetRegionCodeForNumber(self):
836835
def testGetCountryCodeForRegion(self):
837836
self.assertEquals(1, phonenumbers.country_code_for_region("US"))
838837
self.assertEquals(64, phonenumbers.country_code_for_region("NZ"))
839-
self.assertEquals(64, phonenumbers.country_code_for_region("nz"))
840838
self.assertEquals(0, phonenumbers.country_code_for_region(None))
841839
self.assertEquals(0, phonenumbers.country_code_for_region("ZZ"))
842840
# CS is already deprecated so the library doesn't support it.
@@ -863,7 +861,6 @@ def testGetNationalDiallingPrefixForRegion(self):
863861
def testIsNANPACountry(self):
864862
self.assertTrue(phonenumbers.is_nanpa_country("US"))
865863
self.assertTrue(phonenumbers.is_nanpa_country("BS"))
866-
self.assertTrue(phonenumbers.is_nanpa_country("bs"))
867864

868865
def testIsPossibleNumber(self):
869866
self.assertTrue(phonenumbers.is_possible_number(US_NUMBER))
@@ -879,7 +876,6 @@ def testIsPossibleNumber(self):
879876
self.assertTrue(phonenumbers.is_possible_number_string("(020) 7031 3000", "GB"))
880877
self.assertTrue(phonenumbers.is_possible_number_string("7031 3000", "GB"))
881878
self.assertTrue(phonenumbers.is_possible_number_string("3331 6005", "NZ"))
882-
self.assertTrue(phonenumbers.is_possible_number_string("3331 6005", "nz"))
883879

884880
def testIsPossibleNumberWithReason(self):
885881
# National numbers for country calling code +1 that are within 7 to 10 digits are possible.
@@ -1234,7 +1230,6 @@ def testMaybeExtractCountryCode(self):
12341230
def testParseNationalNumber(self):
12351231
# National prefix attached.
12361232
self.assertEquals(NZ_NUMBER, phonenumbers.parse("033316005", "NZ"))
1237-
self.assertEquals(NZ_NUMBER, phonenumbers.parse("033316005", "nz"))
12381233
self.assertEquals(NZ_NUMBER, phonenumbers.parse("33316005", "NZ"))
12391234
# National prefix attached and some formatting present.
12401235
self.assertEquals(NZ_NUMBER, phonenumbers.parse("03-331 6005", "NZ"))

0 commit comments

Comments
 (0)