From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) by finch.gentoo.org (Postfix) with ESMTP id DF6C51381F1 for ; Fri, 20 May 2016 10:59:18 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 4D893E0863; Fri, 20 May 2016 10:59:17 +0000 (UTC) Received: from virtual.dyc.edu (mail.virtual.dyc.edu [67.222.116.22]) by pigeon.gentoo.org (Postfix) with ESMTP id C9C23E085C for ; Fri, 20 May 2016 10:59:16 +0000 (UTC) Received: from greysprite.dite (cpe-74-77-145-97.buffalo.res.rr.com [74.77.145.97]) by virtual.dyc.edu (Postfix) with ESMTPSA id 1B1637E005A for ; Fri, 20 May 2016 06:59:16 -0400 (EDT) Subject: Re: [gentoo-portage-dev] [PATCH 2/2] pym/portage/util/locale.py: add a C module to check locale To: gentoo-portage-dev@lists.gentoo.org References: <1463661818-6940-1-git-send-email-basile@opensource.dyc.edu> <1463661818-6940-2-git-send-email-basile@opensource.dyc.edu> <332609F6-F5A8-457B-837D-37F734BBC6B2@gentoo.org> From: "Anthony G. Basile" Message-ID: <565aa350-786e-6daa-0766-3b50fc244afb@opensource.dyc.edu> Date: Fri, 20 May 2016 06:59:14 -0400 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:45.0) Gecko/20100101 Thunderbird/45.1.0 Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-portage-dev@lists.gentoo.org Reply-to: gentoo-portage-dev@lists.gentoo.org MIME-Version: 1.0 In-Reply-To: <332609F6-F5A8-457B-837D-37F734BBC6B2@gentoo.org> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Archives-Salt: 8606568b-70cc-46ae-aa8a-9abcaae3f8ed X-Archives-Hash: 1965cc77105b91ed2f47aa215fb8845d On 5/19/16 9:38 AM, Michał Górny wrote: > Dnia 19 maja 2016 14:43:38 CEST, "Anthony G. Basile" napisał(a): >> From: "Anthony G. Basile" >> >> The current method to check for the system locale is to use python's >> ctypes.util.find_library() to construct a full library path to the >> system libc.so which is then passed to ctypes.CDLL(). However, >> this gets bogged down in implementation dependant details and >> fails with musl. >> >> We work around this design flaw in ctypes with a small python module >> written in C called 'portage_c_check_locale', and only fall back on >> the current ctypes-based check when this module is not available. >> >> This has been tested on glibc, uClibc and musl systems. >> >> X-Gentoo-bug: 571444 >> X-Gentoo-bug-url: https://bugs.gentoo.org/show_bug.cgi?id=571444 >> Signed-off-by: Anthony G. Basile > > 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. > > 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. > > 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. > > ctypes is itself a problem and so hacky python built-ins were replaced by more hacky python. CDLL shouldn't be used at all. The other problem is that non utf-8 encodings cause problems much earlier in the portage codebase than the test for a sane environment, which is a bigger problem than this small issue. No one checked what happens with python2.7 + portage + exotic locale. Since I don't know where this might head in the future, I kinda like the standalone potential. The only repeated code here is the message. Nonetheless, I can reduce this to just two functions, and do something like the following. I assume that's what you're suggesting: try: from portage_c_convert_case import _c_toupper, _c_tolower libc_toupper = _c_toupper libc_lolower = _c_tolower except ImportError: libc_fn = find_library("c") if libc_fn is None: return None libc = LoadLibrary(libc_fn) if libc is None: return None libc_toupper = libc.toupper libc_tolower = libc.tolower Incidentally, another approach, one that I use in bash is as follows. I think it requires bash4, and I'm not sure how to pull this into python gracefully. l="abcdefghijklmnopqrstuvwxyz" u="ABCDEFGHIJKLMNOPQRSTUVWXYZ" ru=${l^^} rl=${u,,} [[ $l == $rl ]] && echo "lower case okay" || echo "lower case bad" [[ $u == $ru ]] && echo "upper case okay" || echo "upper case bad" -- Anthony G. Basile, Ph. D. Chair of Information Technology D'Youville College Buffalo, NY 14201 (716) 829-8197