Skip to content

Commit d300309

Browse files
committed
Cope with missing geodata at generation time
1 parent 83e34ff commit d300309

1 file changed

Lines changed: 19 additions & 3 deletions

File tree

python/phonenumbers/geocoder.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,28 @@
4444
# See the License for the specific language governing permissions and
4545
# limitations under the License.
4646

47-
from .util import u
47+
from .util import prnt, u
4848
from .phonenumberutil import format_number, PhoneNumberFormat, is_valid_number
4949
from .phonenumberutil import region_code_for_number
50-
from .geodata import GEOCODE_DATA, GEOCODE_LONGEST_PREFIX
51-
from .geodata.locale import LOCALE_DATA
5250

51+
# Import auto-generated data structures
52+
try:
53+
from .geodata import GEOCODE_DATA, GEOCODE_LONGEST_PREFIX
54+
from .geodata.locale import LOCALE_DATA
55+
except ImportError: # pragma no cover
56+
# Before the generated code exists, the geodata/ directory is empty.
57+
# The generation process imports this module, creating a circular
58+
# dependency. The hack below works around this.
59+
import os
60+
import sys
61+
if (os.path.basename(sys.argv[0]) == "buildgeocodingdata.py" or
62+
os.path.basename(sys.argv[0]) == "buildmetadatafromxml.py"):
63+
prnt("Failed to import generated data (but OK as during autogeneration)", file=sys.stderr)
64+
GEOCODE_DATA = {}
65+
GEOCODE_LONGEST_PREFIX = 3
66+
LOCALE_DATA = {}
67+
else:
68+
raise
5369

5470
def _may_fall_back_to_english(lang):
5571
# Don't fall back to English if the requested language is among the following:

0 commit comments

Comments
 (0)