Python源码示例:locale.resetlocale()
示例1
def test_main_resets_locale(self):
"""
Reporter entry point should reset encoding to utf-8, as libapt-pkg
encodes description with system encoding and python-apt decodes
them as utf-8 (LP: #1827857).
"""
self._add_package_to_deb_dir(
self.repository_dir, "gosa", description=u"GOsa\u00B2")
self.facade.reload_channels()
# Set the only non-utf8 locale which we're sure exists.
# It behaves slightly differently than the bug, but fails on the
# same condition.
locale.setlocale(locale.LC_CTYPE, (None, None))
self.addCleanup(locale.resetlocale)
with mock.patch("landscape.client.package.reporter.run_task_handler"):
main([])
# With the actual package, the failure will occur looking up the
# description translation.
pkg = self.facade.get_packages_by_name("gosa")[0]
skel = self.facade.get_package_skeleton(pkg, with_info=True)
self.assertEqual(u"GOsa\u00B2", skel.description)
示例2
def test_build_skeleton_with_unicode_and_non_ascii(self):
"""
If with_unicode and with_info are passed to build_skeleton_apt,
the description is decoded.
"""
# Py2 used to convert to lossy ascii (thus LC_ in Makefile)
# Py3 doesn't, and python3-apt assumes UTF8 (LP: #1827857).
# If you revisit this test, also check reporter.main(), which
# should set this globally to the reporter process.
locale.setlocale(locale.LC_CTYPE, "C.UTF-8")
self.addCleanup(locale.resetlocale)
self._add_package_to_deb_dir(
self.skeleton_repository_dir, "pkg", description=u"T\xe9st")
self.facade._cache.update(None)
self.facade._cache.open(None)
pkg = self.get_package("pkg")
skeleton = build_skeleton_apt(pkg, with_unicode=True, with_info=True)
self.assertEqual(u"T\u00E9st", skeleton.description)
示例3
def test_float_rejects_point_for_comma_locale():
locale.setlocale(locale.LC_ALL, "de_DE")
IMPORTSTRUCT = {"RFCFLOAT": "1.2"}
try:
output = client.call("STFC_STRUCTURE", IMPORTSTRUCT=IMPORTSTRUCT)
except Exception as ex:
assert type(ex) is ExternalRuntimeError
assert ex.code == 22
assert ex.key == "RFC_CONVERSION_FAILURE"
assert (
ex.message
== "Cannot convert string value 1.2 at position 1 for the field RFCFLOAT to type RFCTYPE_FLOAT"
)
locale.resetlocale(locale.LC_ALL)
示例4
def test_float_accepts_comma_for_comma_locale():
locale.setlocale(locale.LC_ALL, "de_DE")
IMPORTSTRUCT = {"RFCFLOAT": "1,2"}
output = client.call("STFC_STRUCTURE", IMPORTSTRUCT=IMPORTSTRUCT)["ECHOSTRUCT"]
assert output["RFCFLOAT"] == 1.2
locale.resetlocale(locale.LC_ALL)
示例5
def reader_status(status_fd, update):
# try to get nicer number formating
try:
locale.resetlocale(locale.LC_NUMERIC)
except Exception:
pass
count = 0
while True:
update('{0:n} lines read'.format(count))
data = os.read(status_fd, 8)
if not data:
break
count = struct.unpack("=Q", data)[0]
示例6
def script():
import locale
locale.resetlocale()
parser = argparse.ArgumentParser()
parser.add_argument('-d', '--database', metavar='DB', help='database connection string')
parser.add_argument('-v', '--verbose', action='store_true', help='be more verbose')
parser.add_argument('-o', '--outfile', help='write output to given file instead of stdout')
subparsers = parser.add_subparsers(title="Commands")
parserBrowse = subparsers.add_parser('browse', help='browse and plot results')
browse.initParser(parserBrowse)
parserCodes = subparsers.add_parser('code', help='code toolkit')
code.initParser(parserCodes)
args = parser.parse_args()
args.func(args)
示例7
def resetlocale():
# locale.resetlocale is bugged with some locales.
for ln in get_locales():
try:
ln = ln[0:ln.index('.')]
return locale.setlocale(locale.LC_ALL, ln)
except locale.Error:
continue