| 1 |
import pytest |
| 2 |
|
| 3 |
import sys |
| 4 |
|
| 5 |
class AppTestLocaleTrivia: |
| 6 |
spaceconfig = dict(usemodules=['_locale', 'unicodedata']) |
| 7 |
|
| 8 |
def setup_class(cls): |
| 9 |
if sys.platform != 'win32': |
| 10 |
cls.w_language_en = cls.space.wrap("C") |
| 11 |
cls.w_language_utf8 = cls.space.wrap("en_US.utf8") |
| 12 |
cls.w_language_pl = cls.space.wrap("pl_PL.utf8") |
| 13 |
cls.w_encoding_pl = cls.space.wrap("utf-8") |
| 14 |
else: |
| 15 |
cls.w_language_en = cls.space.wrap("English_US") |
| 16 |
cls.w_language_utf8 = cls.space.wrap("English_US.65001") |
| 17 |
cls.w_language_pl = cls.space.wrap("Polish_Poland.1257") |
| 18 |
cls.w_encoding_pl = cls.space.wrap("cp1257") |
| 19 |
import _locale |
| 20 |
|
| 21 |
|
| 22 |
current = _locale.setlocale(_locale.LC_ALL) |
| 23 |
cls.oldlocale = current |
| 24 |
try: |
| 25 |
try: |
| 26 |
|
| 27 |
try: |
| 28 |
_locale.setlocale(_locale.LC_ALL, |
| 29 |
cls.space.utf8_w(cls.w_language_en)) |
| 30 |
except _locale.Error: |
| 31 |
_locale.setlocale(_locale.LC_ALL, |
| 32 |
cls.space.utf8_w(cls.w_language_utf8)) |
| 33 |
cls.w_language_en = cls.w_language_utf8 |
| 34 |
|
| 35 |
_locale.setlocale(_locale.LC_ALL, |
| 36 |
cls.space.utf8_w(cls.w_language_pl)) |
| 37 |
except _locale.Error: |
| 38 |
pytest.skip("necessary locales not installed") |
| 39 |
|
| 40 |
|
| 41 |
try: |
| 42 |
_locale.setlocale(_locale.LC_ALL, |
| 43 |
cls.space.utf8_w(cls.w_language_utf8)) |
| 44 |
except _locale.Error: |
| 45 |
del cls.w_language_utf8 |
| 46 |
finally: |
| 47 |
_locale.setlocale(_locale.LC_ALL, current) |
| 48 |
|
| 49 |
def teardown_class(cls): |
| 50 |
import _locale |
| 51 |
_locale.setlocale(_locale.LC_ALL, cls.oldlocale) |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
def test_import(self): |
| 56 |
import _locale |
| 57 |
assert _locale |
| 58 |
|
| 59 |
import locale |
| 60 |
assert locale |
| 61 |
|
| 62 |
def test_constants(self): |
| 63 |
import sys |
| 64 |
|
| 65 |
_CONSTANTS = ( |
| 66 |
'LC_CTYPE', |
| 67 |
'LC_NUMERIC', |
| 68 |
'LC_TIME', |
| 69 |
'LC_COLLATE', |
| 70 |
'LC_MONETARY', |
| 71 |
'LC_ALL', |
| 72 |
'CHAR_MAX', |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
) |
| 83 |
|
| 84 |
import _locale |
| 85 |
|
| 86 |
for constant in _CONSTANTS: |
| 87 |
assert hasattr(_locale, constant) |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
if sys.platform != 'win32': |
| 92 |
_LANGINFO_NAMES = ('RADIXCHAR THOUSEP CRNCYSTR D_T_FMT D_FMT ' |
| 93 |
'T_FMT AM_STR PM_STR CODESET T_FMT_AMPM ERA ERA_D_FMT ' |
| 94 |
'ERA_D_T_FMT ERA_T_FMT ALT_DIGITS YESEXPR NOEXPR ' |
| 95 |
'_DATE_FMT').split() |
| 96 |
for i in range(1, 8): |
| 97 |
_LANGINFO_NAMES.append("DAY_%d" % i) |
| 98 |
_LANGINFO_NAMES.append("ABDAY_%d" % i) |
| 99 |
for i in range(1, 13): |
| 100 |
_LANGINFO_NAMES.append("MON_%d" % i) |
| 101 |
_LANGINFO_NAMES.append("ABMON_%d" % i) |
| 102 |
|
| 103 |
for constant in _LANGINFO_NAMES: |
| 104 |
assert hasattr(_locale, constant) |
| 105 |
|
| 106 |
def test_setlocale(self): |
| 107 |
import _locale |
| 108 |
|
| 109 |
raises(TypeError, _locale.setlocale, "", self.language_en) |
| 110 |
raises(TypeError, _locale.setlocale, _locale.LC_ALL, 6) |
| 111 |
raises(_locale.Error, _locale.setlocale, 123456, self.language_en) |
| 112 |
|
| 113 |
assert _locale.setlocale(_locale.LC_ALL, None) |
| 114 |
assert _locale.setlocale(_locale.LC_ALL) |
| 115 |
|
| 116 |
def test_localeconv(self): |
| 117 |
import _locale |
| 118 |
|
| 119 |
lconv_c = { |
| 120 |
"currency_symbol": "", |
| 121 |
"decimal_point": ".", |
| 122 |
"frac_digits": _locale.CHAR_MAX, |
| 123 |
"grouping": [], |
| 124 |
"int_curr_symbol": "", |
| 125 |
"int_frac_digits": _locale.CHAR_MAX, |
| 126 |
"mon_decimal_point": "", |
| 127 |
"mon_grouping": [], |
| 128 |
"mon_thousands_sep": "", |
| 129 |
"n_cs_precedes": _locale.CHAR_MAX, |
| 130 |
"n_sep_by_space": _locale.CHAR_MAX, |
| 131 |
"n_sign_posn": _locale.CHAR_MAX, |
| 132 |
"negative_sign": "", |
| 133 |
"p_cs_precedes": _locale.CHAR_MAX, |
| 134 |
"p_sep_by_space": _locale.CHAR_MAX, |
| 135 |
"p_sign_posn": _locale.CHAR_MAX, |
| 136 |
"positive_sign": "", |
| 137 |
"thousands_sep": "" } |
| 138 |
|
| 139 |
_locale.setlocale(_locale.LC_ALL, "C") |
| 140 |
|
| 141 |
lconv = _locale.localeconv() |
| 142 |
for k, v in lconv_c.items(): |
| 143 |
assert lconv[k] == v |
| 144 |
|
| 145 |
def test_strcoll(self): |
| 146 |
import _locale |
| 147 |
|
| 148 |
_locale.setlocale(_locale.LC_ALL, self.language_pl) |
| 149 |
assert _locale.strcoll("a", "b") < 0 |
| 150 |
assert _locale.strcoll( |
| 151 |
"\N{LATIN SMALL LETTER A WITH OGONEK}", |
| 152 |
"b") < 0 |
| 153 |
|
| 154 |
assert _locale.strcoll( |
| 155 |
"\N{LATIN SMALL LETTER C WITH ACUTE}", |
| 156 |
"b") > 0 |
| 157 |
assert _locale.strcoll("c", "b") > 0 |
| 158 |
|
| 159 |
assert _locale.strcoll("b", "b") == 0 |
| 160 |
|
| 161 |
raises(TypeError, _locale.strcoll, 1, "b") |
| 162 |
raises(TypeError, _locale.strcoll, "b", 1) |
| 163 |
|
| 164 |
def test_strxfrm(self): |
| 165 |
|
| 166 |
import _locale |
| 167 |
|
| 168 |
_locale.setlocale(_locale.LC_ALL, "C") |
| 169 |
a = "1234" |
| 170 |
b = _locale.strxfrm(a) |
| 171 |
assert a is not b |
| 172 |
assert a == b |
| 173 |
|
| 174 |
with raises(TypeError): |
| 175 |
_locale.strxfrm(1) |
| 176 |
with raises(ValueError): |
| 177 |
_locale.strxfrm("a\x00b") |
| 178 |
|
| 179 |
_locale.setlocale(_locale.LC_ALL, self.language_pl) |
| 180 |
a = "1234" |
| 181 |
b = _locale.strxfrm(a) |
| 182 |
assert a is not b |
| 183 |
|
| 184 |
def test_str_float(self): |
| 185 |
import _locale |
| 186 |
import locale |
| 187 |
|
| 188 |
_locale.setlocale(_locale.LC_ALL, self.language_en) |
| 189 |
assert locale.str(1.1) == '1.1' |
| 190 |
_locale.setlocale(_locale.LC_ALL, self.language_pl) |
| 191 |
assert locale.str(1.1) == '1,1' |
| 192 |
|
| 193 |
def test_text(self): |
| 194 |
import sys |
| 195 |
if sys.platform == 'win32': |
| 196 |
skip("No gettext on Windows") |
| 197 |
|
| 198 |
|
| 199 |
import _locale |
| 200 |
|
| 201 |
assert _locale.gettext("1234") == "1234" |
| 202 |
assert _locale.dgettext(None, "1234") == "1234" |
| 203 |
assert _locale.dcgettext(None, "1234", _locale.LC_MESSAGES) == "1234" |
| 204 |
assert _locale.textdomain("1234") == "1234" |
| 205 |
|
| 206 |
def test_nl_langinfo(self): |
| 207 |
import sys |
| 208 |
if sys.platform == 'win32': |
| 209 |
skip("No langinfo on Windows") |
| 210 |
|
| 211 |
import _locale |
| 212 |
|
| 213 |
langinfo_consts = [ |
| 214 |
'ABDAY_1', |
| 215 |
'ABDAY_2', |
| 216 |
'ABDAY_3', |
| 217 |
'ABDAY_4', |
| 218 |
'ABDAY_5', |
| 219 |
'ABDAY_6', |
| 220 |
'ABDAY_7', |
| 221 |
'ABMON_1', |
| 222 |
'ABMON_10', |
| 223 |
'ABMON_11', |
| 224 |
'ABMON_12', |
| 225 |
'ABMON_2', |
| 226 |
'ABMON_3', |
| 227 |
'ABMON_4', |
| 228 |
'ABMON_5', |
| 229 |
'ABMON_6', |
| 230 |
'ABMON_7', |
| 231 |
'ABMON_8', |
| 232 |
'ABMON_9', |
| 233 |
'CODESET', |
| 234 |
'CRNCYSTR', |
| 235 |
'DAY_1', |
| 236 |
'DAY_2', |
| 237 |
'DAY_3', |
| 238 |
'DAY_4', |
| 239 |
'DAY_5', |
| 240 |
'DAY_6', |
| 241 |
'DAY_7', |
| 242 |
'D_FMT', |
| 243 |
'D_T_FMT', |
| 244 |
'MON_1', |
| 245 |
'MON_10', |
| 246 |
'MON_11', |
| 247 |
'MON_12', |
| 248 |
'MON_2', |
| 249 |
'MON_3', |
| 250 |
'MON_4', |
| 251 |
'MON_5', |
| 252 |
'MON_6', |
| 253 |
'MON_7', |
| 254 |
'MON_8', |
| 255 |
'MON_9', |
| 256 |
'NOEXPR', |
| 257 |
'RADIXCHAR', |
| 258 |
'THOUSEP', |
| 259 |
'T_FMT', |
| 260 |
'YESEXPR', |
| 261 |
'AM_STR', |
| 262 |
'PM_STR', |
| 263 |
] |
| 264 |
for constant in langinfo_consts: |
| 265 |
assert hasattr(_locale, constant) |
| 266 |
|
| 267 |
_locale.setlocale(_locale.LC_ALL, "C") |
| 268 |
assert _locale.nl_langinfo(_locale.ABDAY_1) == "Sun" |
| 269 |
assert _locale.nl_langinfo(_locale.ABMON_1) == "Jan" |
| 270 |
assert _locale.nl_langinfo(_locale.T_FMT) == "%H:%M:%S" |
| 271 |
assert _locale.nl_langinfo(_locale.YESEXPR) == '^[yY]' |
| 272 |
assert _locale.nl_langinfo(_locale.NOEXPR) == "^[nN]" |
| 273 |
assert _locale.nl_langinfo(_locale.THOUSEP) == '' |
| 274 |
|
| 275 |
raises(ValueError, _locale.nl_langinfo, 12345) |
| 276 |
raises(TypeError, _locale.nl_langinfo, None) |
| 277 |
|
| 278 |
def test_getencoding(self): |
| 279 |
import _locale |
| 280 |
enc = _locale.getencoding() |
| 281 |
assert isinstance(enc, str) |
| 282 |
assert enc |
| 283 |
|
| 284 |
def test_bindtextdomain(self): |
| 285 |
import sys |
| 286 |
if sys.platform == 'win32': |
| 287 |
skip("No textdomain on Windows") |
| 288 |
|
| 289 |
|
| 290 |
import _locale |
| 291 |
|
| 292 |
raises(OSError, _locale.bindtextdomain, '', '') |
| 293 |
raises(OSError, _locale.bindtextdomain, '', '1') |
| 294 |
|
| 295 |
def test_bind_textdomain_codeset(self): |
| 296 |
import sys |
| 297 |
if sys.platform == 'win32': |
| 298 |
skip("No textdomain on Windows") |
| 299 |
|
| 300 |
import _locale |
| 301 |
|
| 302 |
assert _locale.bind_textdomain_codeset('/', None) is None |
| 303 |
assert _locale.bind_textdomain_codeset('/', 'UTF-8') == 'UTF-8' |
| 304 |
assert _locale.bind_textdomain_codeset('/', None) == 'UTF-8' |
| 305 |
|
| 306 |
assert _locale.bind_textdomain_codeset('', '') is None |
| 307 |
|
| 308 |
def test_getdefaultlocale(self): |
| 309 |
import sys |
| 310 |
if sys.platform != 'win32': |
| 311 |
skip("No _getdefaultlocale() to test") |
| 312 |
|
| 313 |
import _locale |
| 314 |
lang, encoding = _locale._getdefaultlocale() |
| 315 |
assert lang is None or isinstance(lang, str) |
| 316 |
assert encoding.startswith('cp') or encoding == "utf-8" |
| 317 |
|
| 318 |
def test_lc_numeric_basic(self): |
| 319 |
import _locale, sys |
| 320 |
if sys.platform == 'win32': |
| 321 |
skip("No nl_langinfo to test") |
| 322 |
from _locale import (setlocale, nl_langinfo, Error, LC_NUMERIC, |
| 323 |
LC_CTYPE, RADIXCHAR, THOUSEP, localeconv) |
| 324 |
|
| 325 |
candidate_locales = ['es_UY', 'fr_FR', 'fi_FI', 'es_CO', 'pt_PT', 'it_IT', |
| 326 |
'et_EE', 'es_PY', 'no_NO', 'nl_NL', 'lv_LV', 'el_GR', 'be_BY', 'fr_BE', |
| 327 |
'ro_RO', 'ru_UA', 'ru_RU', 'es_VE', 'ca_ES', 'se_NO', 'es_EC', 'id_ID', |
| 328 |
'ka_GE', 'es_CL', 'wa_BE', 'hu_HU', 'lt_LT', 'sl_SI', 'hr_HR', 'es_AR', |
| 329 |
'es_ES', 'oc_FR', 'gl_ES', 'bg_BG', 'is_IS', 'mk_MK', 'de_AT', 'pt_BR', |
| 330 |
'da_DK', 'nn_NO', 'cs_CZ', 'de_LU', 'es_BO', 'sq_AL', 'sk_SK', 'fr_CH', |
| 331 |
'de_DE', 'sr_YU', 'br_FR', 'nl_BE', 'sv_FI', 'pl_PL', 'fr_CA', 'fo_FO', |
| 332 |
'bs_BA', 'fr_LU', 'kl_GL', 'fa_IR', 'de_BE', 'sv_SE', 'it_CH', 'uk_UA', |
| 333 |
'eu_ES', 'vi_VN', 'af_ZA', 'nb_NO', 'en_DK', 'tg_TJ', 'ps_AF', 'en_US', |
| 334 |
'fr_FR.ISO8859-1', 'fr_FR.UTF-8', 'fr_FR.ISO8859-15@euro', |
| 335 |
'ru_RU.KOI8-R', 'ko_KR.eucKR'] |
| 336 |
|
| 337 |
tested = False |
| 338 |
for loc in candidate_locales: |
| 339 |
try: |
| 340 |
setlocale(LC_NUMERIC, loc) |
| 341 |
setlocale(LC_CTYPE, loc) |
| 342 |
except Error: |
| 343 |
continue |
| 344 |
for li, lc in ((RADIXCHAR, "decimal_point"), |
| 345 |
(THOUSEP, "thousands_sep")): |
| 346 |
nl_radixchar = nl_langinfo(li) |
| 347 |
li_radixchar = localeconv()[lc] |
| 348 |
try: |
| 349 |
set_locale = setlocale(LC_NUMERIC) |
| 350 |
except Error: |
| 351 |
set_locale = "<not able to determine>" |
| 352 |
assert nl_radixchar == li_radixchar, ("nl_langinfo != localeconv " |
| 353 |
"(set to %s, using %s)" % ( loc, set_locale)) |
| 354 |
|
| 355 |
class AppTestLocaleFormatting: |
| 356 |
spaceconfig = dict(usemodules=['_locale', 'unicodedata']) |
| 357 |
|
| 358 |
def test_ru_utf8(self): |
| 359 |
import sys |
| 360 |
import _locale |
| 361 |
try: |
| 362 |
_locale.setlocale(_locale.LC_ALL, 'ru_RU.utf-8') |
| 363 |
except _locale.Error: |
| 364 |
skip("locale ru_RU not installed") |
| 365 |
f1 = format(123456789, 'n') |
| 366 |
ts = _locale.localeconv()['thousands_sep'] |
| 367 |
expected1 = "123X456X789".replace("X", ts) |
| 368 |
assert f1 == expected1 |
| 369 |
f2 = format(1111111111111.1, '020n') |
| 370 |
assert len(f2) >= 20 |
| 371 |
expected2 = "00X000X001,11111e+12".replace("X", ts) |
| 372 |
assert f2 == expected2 |