Gentoo Archives: gentoo-portage-dev

From: "Michał Górny" <mgorny@g.o>
To: gentoo-portage-dev@l.g.o
Cc: "Michał Górny" <mgorny@g.o>
Subject: [gentoo-portage-dev] [PATCH 1/2] Warn if LC_CTYPE does not transform ASCII chars like POSIX
Date: Sat, 14 Nov 2015 22:56:58
Message-Id: 1447541806-16108-1-git-send-email-mgorny@gentoo.org
1 Output a warning if LC_CTYPE is set to a value that causes libc
2 toupper() and/or tolower() conversions not apply correctly to printable
3 ASCII characters.
4 ---
5 pym/_emerge/actions.py | 3 +++
6 pym/portage/util/locale.py | 63 ++++++++++++++++++++++++++++++++++++++++++++++
7 2 files changed, 66 insertions(+)
8 create mode 100644 pym/portage/util/locale.py
9
10 diff --git a/pym/_emerge/actions.py b/pym/_emerge/actions.py
11 index 7f1cb59..e03e8d4 100644
12 --- a/pym/_emerge/actions.py
13 +++ b/pym/_emerge/actions.py
14 @@ -27,6 +27,7 @@ portage.proxy.lazyimport.lazyimport(globals(),
15 'portage.debug',
16 'portage.news:count_unread_news,display_news_notifications',
17 'portage.util._get_vm_info:get_vm_info',
18 + 'portage.util.locale:check_locale',
19 'portage.emaint.modules.sync.sync:SyncRepos',
20 '_emerge.chk_updated_cfg_files:chk_updated_cfg_files',
21 '_emerge.help:help@emerge_help',
22 @@ -2467,6 +2468,8 @@ def validate_ebuild_environment(trees):
23 for line in textwrap.wrap(msg, 65):
24 out.ewarn(line)
25
26 + check_locale()
27 +
28 def check_procfs():
29 procfs_path = '/proc'
30 if platform.system() not in ("Linux",) or \
31 diff --git a/pym/portage/util/locale.py b/pym/portage/util/locale.py
32 new file mode 100644
33 index 0000000..6919ceb
34 --- /dev/null
35 +++ b/pym/portage/util/locale.py
36 @@ -0,0 +1,63 @@
37 +#-*- coding:utf-8 -*-
38 +# Copyright 2015 Gentoo Foundation
39 +# Distributed under the terms of the GNU General Public License v2
40 +"""
41 +Function to check whether the current used LC_CTYPE handles case
42 +transformations of ASCII characters in a way compatible with the POSIX
43 +locale.
44 +get_ro_checker().
45 +"""
46 +from __future__ import unicode_literals
47 +
48 +import logging
49 +import textwrap
50 +
51 +from portage.util import writemsg_level
52 +from portage.util._ctypes import find_library, LoadLibrary
53 +
54 +
55 +def check_locale():
56 + """
57 + Check whether the locale is sane. Returns True if it is, prints
58 + warning and returns False if it is not. Returns None if the check
59 + can not be executed due to platform limitations.
60 + """
61 +
62 + libc_fn = find_library("c")
63 + if libc_fn is None:
64 + return None
65 + libc = LoadLibrary(libc_fn)
66 + if libc is None:
67 + return None
68 +
69 + lc = list(range(ord('a'), ord('z')+1))
70 + uc = list(range(ord('A'), ord('Z')+1))
71 + rlc = [libc.tolower(c) for c in uc]
72 + ruc = [libc.toupper(c) for c in lc]
73 +
74 + if lc != rlc or uc != ruc:
75 + msg = ("WARNING: The LC_CTYPE variable is set to a locale " +
76 + "that specifies transformation between lowercase " +
77 + "and uppercase ASCII characters that is different than " +
78 + "the one specified by POSIX locale. This can break " +
79 + "ebuilds and cause issues in programs that rely on " +
80 + "the common character conversion scheme. " +
81 + "Please consider enabling another locale (such as " +
82 + "en_US.UTF-8) in /etc/locale.gen and setting it " +
83 + "as LC_CTYPE in make.conf.")
84 + msg = [l for l in textwrap.wrap(msg, 70)]
85 + msg.append("")
86 + chars = lambda l: ''.join(chr(x) for x in l)
87 + if uc != ruc:
88 + msg.extend([
89 + " %s -> %s" % (chars(lc), chars(ruc)),
90 + " %28s: %s" % ('expected', chars(uc))])
91 + if lc != rlc:
92 + msg.extend([
93 + " %s -> %s" % (chars(uc), chars(rlc)),
94 + " %28s: %s" % ('expected', chars(lc))])
95 + writemsg_level("".join(["!!! %s\n" % l for l in msg]),
96 + level=logging.ERROR, noiselevel=-1)
97 + return False
98 +
99 + return True
100 --
101 2.6.3

Replies