Debian Codegraph @

tests/__init__.py

raw
1 """Base classes and helpers for testing paramiko."""
2
3 import functools
4 import locale
5 import os
6
7 from pytest import skip
8
9
10 # List of locales which have non-ascii characters in all categories.
11 # Omits most European languages which for instance may have only some months
12 # with names that include accented characters.
13 _non_ascii_locales = [
14 # East Asian locales
15 "ja_JP",
16 "ko_KR",
17 "zh_CN",
18 "zh_TW",
19 # European locales with non-latin alphabets
20 "el_GR",
21 "ru_RU",
22 "uk_UA",
23 ]
24 # Also include UTF-8 versions of these locales
25 _non_ascii_locales.extend([name + ".utf8" for name in _non_ascii_locales])
26
27
28 def requireNonAsciiLocale(category_name="LC_ALL"):
29 """Run decorated test under a non-ascii locale or skip if not possible."""
30 if os.name != "posix":
31 return skip("Non-posix OSes don't really use C locales")
32 cat = getattr(locale, category_name)
33 return functools.partial(_decorate_with_locale, cat, _non_ascii_locales)
34
35
36 def _decorate_with_locale(category, try_locales, test_method):
37 """Decorate test_method to run after switching to a different locale."""
38
39 def _test_under_locale(testself, *args, **kwargs):
40 original = locale.setlocale(category)
41 while try_locales:
42 try:
43 locale.setlocale(category, try_locales[0])
44 except locale.Error:
45 # Mutating original list is ok, setlocale would keep failing
46 try_locales.pop(0)
47 else:
48 try:
49 return test_method(testself, *args, **kwargs)
50 finally:
51 locale.setlocale(category, original)
52 # No locales could be used? Just skip the decorated test :(
53 skip("No usable locales installed")
54
55 functools.update_wrapper(_test_under_locale, test_method)
56 return _test_under_locale