-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathall_locales.py
More file actions
94 lines (81 loc) · 2.39 KB
/
all_locales.py
File metadata and controls
94 lines (81 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import locale
import _locale
import os
import subprocess
import sys
import time
PY3 = (sys.version_info >= (3,))
if PY3:
def test_ascii(value):
value.encode('ascii')
else:
def test_ascii(value):
value.decode('ascii')
ascii = repr
if len(sys.argv) >= 2 and sys.argv[1] == 'current':
locales = ['']
else:
proc = subprocess.Popen(['locale', '-a'], stdout=subprocess.PIPE, universal_newlines=True)
locales = proc.communicate()[0].splitlines()
def test_locale(loc):
global nonascii
if loc:
prefix = loc
else:
prefix = _locale.setlocale(locale.LC_CTYPE, None)
if hasattr(locale, 'nl_langinfo'):
codeset = locale.nl_langinfo(locale.CODESET)
prefix = '%s/%s' % (prefix, codeset)
lc = locale.localeconv()
for field, value in sorted(lc.items()):
if not isinstance(value, str):
continue
try:
test_ascii(value)
except UnicodeError:
print("%s: localeconv()['%s'] = %s" % (prefix, field, ascii(value)))
nonascii += 1
nerr = 0
for err in range(1, 150):
msg = os.strerror(err)
try:
test_ascii(msg)
except UnicodeError:
print("%s: strerror(%s) = %s" % (prefix, err, ascii(msg)))
nonascii += 1
nerr += 1
if nerr > 3:
print("%s: (skip next strerror)" % prefix)
break
nerr = 0
fmt = "%A %B %Z"
for month in range(1, 13):
t = time.localtime(time.mktime((2018, month, 1, 12, 0, 0, 0, 0, 0)))
msg = time.strftime(fmt, t)
try:
test_ascii(msg)
except UnicodeError:
print("%s: strftime(%r) = %s" % (prefix, fmt, ascii(msg)))
nonascii += 1
nerr += 1
if nerr > 3:
print("%s: (skip next strftime)" % prefix)
break
invalid_locales = 0
nonascii = 0
for loc in locales:
try:
locale.setlocale(locale.LC_ALL, loc)
except locale.Error:
invalid_locales += 1
continue
try:
test_locale(loc)
except Exception as exc:
raise Exception("Error while testing %s: %s" % (loc, exc))
print("")
if hasattr(sys.flags, 'utf8_mode'):
print("UTF-8 mode: %s" % sys.flags.utf8_mode)
print("locales: %s" % len(locales))
print("invalid locales: %s" % invalid_locales)
print("non-ASCII: %s" % nonascii)