1 |
On Fri, 20 May 2016 06:59:14 -0400 |
2 |
"Anthony G. Basile" <basile@××××××××××××××.edu> wrote: |
3 |
|
4 |
> On 5/19/16 9:38 AM, Michał Górny wrote: |
5 |
> > Dnia 19 maja 2016 14:43:38 CEST, "Anthony G. Basile" <basile@××××××××××××××.edu> napisał(a): |
6 |
> >> From: "Anthony G. Basile" <blueness@g.o> |
7 |
> >> |
8 |
> >> The current method to check for the system locale is to use python's |
9 |
> >> ctypes.util.find_library() to construct a full library path to the |
10 |
> >> system libc.so which is then passed to ctypes.CDLL(). However, |
11 |
> >> this gets bogged down in implementation dependant details and |
12 |
> >> fails with musl. |
13 |
> >> |
14 |
> >> We work around this design flaw in ctypes with a small python module |
15 |
> >> written in C called 'portage_c_check_locale', and only fall back on |
16 |
> >> the current ctypes-based check when this module is not available. |
17 |
> >> |
18 |
> >> This has been tested on glibc, uClibc and musl systems. |
19 |
> >> |
20 |
> >> X-Gentoo-bug: 571444 |
21 |
> >> X-Gentoo-bug-url: https://bugs.gentoo.org/show_bug.cgi?id=571444 |
22 |
> >> Signed-off-by: Anthony G. Basile <blueness@g.o> |
23 |
> > |
24 |
> > To be honest, I don't like this. Do we really want to duplicate all this, including duplicating the complete messages? This really looks messy and unmaintainable. |
25 |
> > |
26 |
> > The only reason libc functions were being used was to workaround the hacky built-in case conversion functions. And this is the only part where CDLL was really used. |
27 |
> > |
28 |
> > So please change this to provide trivial wrappers around the C library functions, and use them alternatively to ones obtained using CDLL. With a single common code doing all the check logic and messaging. |
29 |
> > |
30 |
> > |
31 |
> |
32 |
> ctypes is itself a problem and so hacky python built-ins were replaced |
33 |
> by more hacky python. CDLL shouldn't be used at all. The other problem |
34 |
> is that non utf-8 encodings cause problems much earlier in the portage |
35 |
> codebase than the test for a sane environment, which is a bigger problem |
36 |
> than this small issue. No one checked what happens with python2.7 + |
37 |
> portage + exotic locale. Since I don't know where this might head in |
38 |
> the future, I kinda like the standalone potential. The only repeated |
39 |
> code here is the message. Nonetheless, I can reduce this to just two |
40 |
> functions, and do something like the following. I assume that's what |
41 |
> you're suggesting: |
42 |
> |
43 |
> try: |
44 |
> from portage_c_convert_case import _c_toupper, _c_tolower |
45 |
> libc_toupper = _c_toupper |
46 |
> libc_lolower = _c_tolower |
47 |
> except ImportError: |
48 |
> libc_fn = find_library("c") |
49 |
> if libc_fn is None: |
50 |
> return None |
51 |
> libc = LoadLibrary(libc_fn) |
52 |
> if libc is None: |
53 |
> return None |
54 |
> libc_toupper = libc.toupper |
55 |
> libc_tolower = libc.tolower |
56 |
> |
57 |
> |
58 |
> Incidentally, another approach, one that I use in bash is as follows. I |
59 |
> think it requires bash4, and I'm not sure how to pull this into python |
60 |
> gracefully. |
61 |
> |
62 |
> l="abcdefghijklmnopqrstuvwxyz" |
63 |
> u="ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
64 |
> ru=${l^^} |
65 |
> rl=${u,,} |
66 |
> [[ $l == $rl ]] && echo "lower case okay" || echo "lower case bad" |
67 |
> [[ $u == $ru ]] && echo "upper case okay" || echo "upper case bad" |
68 |
|
69 |
Exactly as pointed out above. You have to import tolower() |
70 |
and toupper() from libc as Python case conversion functions don't give |
71 |
the same results as 'pure' libc used by bash. |
72 |
|
73 |
-- |
74 |
Best regards, |
75 |
Michał Górny |
76 |
<http://dev.gentoo.org/~mgorny/> |