]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
Introduce PyInstaller support. Fixes #500
authorWolfgang Doll <wolfgang.doll@web.de>
Fri, 23 Jun 2017 10:03:23 +0000 (12:03 +0200)
committerWolfgang Doll <wolfgang.doll@web.de>
Sat, 24 Jun 2017 08:25:19 +0000 (10:25 +0200)
Add a get_base_dir function that works in the context of
PyInstaller, where the __file__ system variable is
not available.

babel/core.py
babel/localedata.py
tests/test_localedata.py

index 5140f49d79b80dc6c16995fff5a56a726bf2cf31..df03b24adac75ff728e3f5b984b3fac8c5d3b043 100644 (file)
@@ -68,7 +68,7 @@ def get_global(key):
     """
     global _global_data
     if _global_data is None:
-        dirname = os.path.join(os.path.dirname(__file__))
+        dirname = localedata.get_base_dir()
         filename = os.path.join(dirname, 'global.dat')
         if not os.path.isfile(filename):
             _raise_no_data_error()
index 9e272a279dac20644b5ad5e84485b73adb2fc8d8..46582212dfe82068ca960d16a308f60b5b10c685 100644 (file)
@@ -16,13 +16,23 @@ import os
 import threading
 from collections import MutableMapping
 from itertools import chain
+import sys
 
 from babel._compat import pickle
 
 
+def get_base_dir():
+    if getattr(sys, 'frozen', False):
+        # we are running in a |PyInstaller| bundle
+        basedir = sys._MEIPASS
+    else:
+        # we are running in a normal Python environment
+        basedir = os.path.dirname(__file__)
+    return basedir
+
 _cache = {}
 _cache_lock = threading.RLock()
-_dirname = os.path.join(os.path.dirname(__file__), 'locale-data')
+_dirname = os.path.join(get_base_dir(), 'locale-data')
 
 
 def normalize_locale(name):
index 6a41ac2adefa6f8a83aa1ae70a65974cdc5c04f1..a23987f288f9917431f421cd64ca9ae6875d64de 100644 (file)
@@ -14,6 +14,7 @@
 import unittest
 import random
 from operator import methodcaller
+import sys
 
 from babel import localedata
 
@@ -94,3 +95,16 @@ def test_mixedcased_locale():
         locale_id = ''.join([
             methodcaller(random.choice(['lower', 'upper']))(c) for c in l])
         assert localedata.exists(locale_id)
+
+def test_pi_support_frozen():
+    sys._MEIPASS, sys.frozen = 'testdir', True
+    try:
+        assert localedata.get_base_dir() == 'testdir'
+    finally:
+        del sys._MEIPASS
+        del sys.frozen
+
+
+def test_pi_support_not_frozen():
+    assert not getattr(sys, 'frozen', False)
+    assert localedata.get_base_dir().endswith('babel')