def test_lc_numeric_basic(self): # Test nl_langinfo against localeconv tested = False for loc in candidate_locales: try: setlocale(LC_NUMERIC, loc) setlocale(LC_CTYPE, loc) except Error: continue for li, lc in ((RADIXCHAR, "decimal_point"), (THOUSEP, "thousands_sep")): nl_radixchar = nl_langinfo(li) li_radixchar = localeconv()[lc] try: set_locale = setlocale(LC_NUMERIC) except Error: set_locale = "<not able to determine>" self.assertEqual(nl_radixchar, li_radixchar, "%s (nl_langinfo) != %s (localeconv) " "(set to %s, using %s)" % ( nl_radixchar, li_radixchar, loc, set_locale)) tested = True if not tested: self.skipTest('no suitable locales')
def test_lc_numeric_basic(self): # Test nl_langinfo against localeconv tested = False for loc in candidate_locales: try: setlocale(LC_NUMERIC, loc) setlocale(LC_CTYPE, loc) except Error: continue for li, lc in ((RADIXCHAR, "decimal_point"), (THOUSEP, "thousands_sep")): nl_radixchar = nl_langinfo(li) li_radixchar = localeconv()[lc] try: set_locale = setlocale(LC_NUMERIC) except Error: set_locale = "<not able to determine>" self.assertEqual( nl_radixchar, li_radixchar, "%s (nl_langinfo) != %s (localeconv) " "(set to %s, using %s)" % (nl_radixchar, li_radixchar, loc, set_locale)) tested = True if not tested: self.skipTest('no suitable locales')
def test_lc_numeric_nl_langinfo(self): # Test nl_langinfo against known values for loc in candidate_locales: try: setlocale(LC_NUMERIC, loc) except Error: continue for li, lc in ((RADIXCHAR, "decimal_point"), (THOUSEP, "thousands_sep")): self.numeric_tester("nl_langinfo", nl_langinfo(li), lc, loc)
def test_lc_numeric_nl_langinfo(self): # Test nl_langinfo against known values for loc in candidate_locales: try: setlocale(LC_NUMERIC, loc) except Error: continue for li, lc in ((RADIXCHAR, "decimal_point"), (THOUSEP, "thousands_sep")): self.numeric_tester('nl_langinfo', nl_langinfo(li), lc, loc)
def getpreferredencoding(do_setlocale=True): assert not do_setlocale result = _locale.nl_langinfo(_locale.CODESET) if not result and sys.platform == "darwin": # nl_langinfo can return an empty string # when the setting has an invalid value. # Default to UTF-8 in that case because # UTF-8 is the default charset on OSX and # returning nothing will crash the # interpreter. result = "UTF-8" return result
def getpreferredencoding(do_setlocale=True): assert not do_setlocale result = _locale.nl_langinfo(_locale.CODESET) if not result and sys.platform == 'darwin': # nl_langinfo can return an empty string # when the setting has an invalid value. # Default to UTF-8 in that case because # UTF-8 is the default charset on OSX and # returning nothing will crash the # interpreter. result = 'UTF-8' return result
def test_lc_numeric_nl_langinfo(self): # Test nl_langinfo against known values tested = False for loc in candidate_locales: try: setlocale(LC_NUMERIC, loc) except Error: continue for li, lc in ((RADIXCHAR, "decimal_point"), (THOUSEP, "thousands_sep")): if self.numeric_tester('nl_langinfo', nl_langinfo(li), lc, loc): tested = True if not tested: self.skipTest('no suitable locales')
def test_lc_numeric_nl_langinfo(self): # Test nl_langinfo against known values tested = False for loc in candidate_locales: try: setlocale(LC_NUMERIC, loc) setlocale(LC_CTYPE, loc) except Error: continue for li, lc in ((RADIXCHAR, "decimal_point"), (THOUSEP, "thousands_sep")): if self.numeric_tester('nl_langinfo', nl_langinfo(li), lc, loc): tested = True if not tested: self.skipTest('no suitable locales')
def test_lc_numeric(self): for loc in candidate_locales: try: setlocale(LC_NUMERIC, loc) except Error: continue for li, lc in ((RADIXCHAR, "decimal_point"), (THOUSEP, "thousands_sep")): nl_radixchar = nl_langinfo(li) li_radixchar = localeconv()[lc] # Both with seeing what the locale is set to in order to detect # when setlocale lies and says it accepted the locale setting # but in actuality didn't use it (as seen in OS X 10.3) try: set_locale = setlocale(LC_NUMERIC) except Error: set_locale = "<not able to determine>" self.assertEquals( nl_radixchar, li_radixchar, "%s != %s (%s); " "supposed to be %s, set to %s" % (nl_radixchar, li_radixchar, lc, loc, set_locale))
def test_lc_numeric_basic(self): import _locale if sys.platform == 'win32': skip("No nl_langinfo to test") from _locale import (setlocale, nl_langinfo, Error, LC_NUMERIC, LC_CTYPE, RADIXCHAR, THOUSEP, localeconv) # Test nl_langinfo against localeconv candidate_locales = [ 'es_UY', 'fr_FR', 'fi_FI', 'es_CO', 'pt_PT', 'it_IT', 'et_EE', 'es_PY', 'no_NO', 'nl_NL', 'lv_LV', 'el_GR', 'be_BY', 'fr_BE', 'ro_RO', 'ru_UA', 'ru_RU', 'es_VE', 'ca_ES', 'se_NO', 'es_EC', 'id_ID', 'ka_GE', 'es_CL', 'wa_BE', 'hu_HU', 'lt_LT', 'sl_SI', 'hr_HR', 'es_AR', 'es_ES', 'oc_FR', 'gl_ES', 'bg_BG', 'is_IS', 'mk_MK', 'de_AT', 'pt_BR', 'da_DK', 'nn_NO', 'cs_CZ', 'de_LU', 'es_BO', 'sq_AL', 'sk_SK', 'fr_CH', 'de_DE', 'sr_YU', 'br_FR', 'nl_BE', 'sv_FI', 'pl_PL', 'fr_CA', 'fo_FO', 'bs_BA', 'fr_LU', 'kl_GL', 'fa_IR', 'de_BE', 'sv_SE', 'it_CH', 'uk_UA', 'eu_ES', 'vi_VN', 'af_ZA', 'nb_NO', 'en_DK', 'tg_TJ', 'ps_AF', 'en_US', 'fr_FR.ISO8859-1', 'fr_FR.UTF-8', 'fr_FR.ISO8859-15@euro', 'ru_RU.KOI8-R', 'ko_KR.eucKR' ] tested = False for loc in candidate_locales: try: setlocale(LC_NUMERIC, loc) setlocale(LC_CTYPE, loc) except Error: continue for li, lc in ((RADIXCHAR, "decimal_point"), (THOUSEP, "thousands_sep")): nl_radixchar = nl_langinfo(li) li_radixchar = localeconv()[lc] try: set_locale = setlocale(LC_NUMERIC) except Error: set_locale = "<not able to determine>" assert nl_radixchar == li_radixchar, ( "nl_langinfo != localeconv " "(set to %s, using %s)" % (loc, set_locale))
def test_lc_numeric(self): for loc in candidate_locales: try: setlocale(LC_NUMERIC, loc) except Error: continue for li, lc in ((RADIXCHAR, "decimal_point"), (THOUSEP, "thousands_sep")): nl_radixchar = nl_langinfo(li) li_radixchar = localeconv()[lc] # Both with seeing what the locale is set to in order to detect # when setlocale lies and says it accepted the locale setting # but in actuality didn't use it (as seen in OS X 10.3) try: set_locale = setlocale(LC_NUMERIC) except Error: set_locale = "<not able to determine>" self.assertEquals(nl_radixchar, li_radixchar, "%s != %s (%s); " "supposed to be %s, set to %s" % (nl_radixchar, li_radixchar, lc, loc, set_locale))
def getpreferredencoding(do_setlocale=True): assert not do_setlocale result = _locale.nl_langinfo(_locale.CODESET) if not result and sys.platform == 'darwin': result = 'UTF-8' return result
def test_nl_langinfo(self): import sys if sys.platform == 'win32': skip("No langinfo on Windows") import _locale langinfo_consts = [ 'ABDAY_1', 'ABDAY_2', 'ABDAY_3', 'ABDAY_4', 'ABDAY_5', 'ABDAY_6', 'ABDAY_7', 'ABMON_1', 'ABMON_10', 'ABMON_11', 'ABMON_12', 'ABMON_2', 'ABMON_3', 'ABMON_4', 'ABMON_5', 'ABMON_6', 'ABMON_7', 'ABMON_8', 'ABMON_9', 'CODESET', 'CRNCYSTR', 'DAY_1', 'DAY_2', 'DAY_3', 'DAY_4', 'DAY_5', 'DAY_6', 'DAY_7', 'D_FMT', 'D_T_FMT', 'MON_1', 'MON_10', 'MON_11', 'MON_12', 'MON_2', 'MON_3', 'MON_4', 'MON_5', 'MON_6', 'MON_7', 'MON_8', 'MON_9', 'NOEXPR', 'RADIXCHAR', 'THOUSEP', 'T_FMT', 'YESEXPR', 'AM_STR', 'PM_STR', ] for constant in langinfo_consts: assert hasattr(_locale, constant) _locale.setlocale(_locale.LC_ALL, "C") assert _locale.nl_langinfo(_locale.ABDAY_1) == "Sun" assert _locale.nl_langinfo(_locale.ABMON_1) == "Jan" assert _locale.nl_langinfo(_locale.T_FMT) == "%H:%M:%S" assert _locale.nl_langinfo(_locale.YESEXPR) == '^[yY]' assert _locale.nl_langinfo(_locale.NOEXPR) == "^[nN]" assert _locale.nl_langinfo(_locale.THOUSEP) == '' raises(ValueError, _locale.nl_langinfo, 12345) raises(TypeError, _locale.nl_langinfo, None)