From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <gentoo-portage-dev+bounces-5786-garchives=archives.gentoo.org@lists.gentoo.org>
Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80])
	by finch.gentoo.org (Postfix) with ESMTP id 7458013828B
	for <garchives@archives.gentoo.org>; Fri, 27 May 2016 14:27:10 +0000 (UTC)
Received: from pigeon.gentoo.org (localhost [127.0.0.1])
	by pigeon.gentoo.org (Postfix) with SMTP id EE0D71423F;
	Fri, 27 May 2016 14:27:01 +0000 (UTC)
Received: from virtual.dyc.edu (mail.virtual.dyc.edu [67.222.116.22])
	by pigeon.gentoo.org (Postfix) with ESMTP id 3992D1422E
	for <gentoo-portage-dev@lists.gentoo.org>; Fri, 27 May 2016 14:27:01 +0000 (UTC)
Received: from opensource.dyc.edu (unknown [67.222.116.23])
	by virtual.dyc.edu (Postfix) with ESMTP id BD07C7E0002;
	Fri, 27 May 2016 10:27:00 -0400 (EDT)
Received: by opensource.dyc.edu (Postfix, from userid 1001)
	id 4E48F2B00111; Fri, 27 May 2016 10:26:51 -0400 (EDT)
From: "Anthony G. Basile" <basile@opensource.dyc.edu>
To: gentoo-portage-dev@lists.gentoo.org
Cc: "Anthony G. Basile" <blueness@gentoo.org>
Subject: [gentoo-portage-dev] [PATCH 3/3] pym/portage/util/locale.py: add a C module to help check locale
Date: Fri, 27 May 2016 10:26:44 -0400
Message-Id: <1464359204-21987-3-git-send-email-basile@opensource.dyc.edu>
X-Mailer: git-send-email 1.7.6.1
In-Reply-To: <1464359204-21987-1-git-send-email-basile@opensource.dyc.edu>
References: <1464359204-21987-1-git-send-email-basile@opensource.dyc.edu>
Precedence: bulk
List-Post: <mailto:gentoo-portage-dev@lists.gentoo.org>
List-Help: <mailto:gentoo-portage-dev+help@lists.gentoo.org>
List-Unsubscribe: <mailto:gentoo-portage-dev+unsubscribe@lists.gentoo.org>
List-Subscribe: <mailto:gentoo-portage-dev+subscribe@lists.gentoo.org>
List-Id: Gentoo Linux mail <gentoo-portage-dev.gentoo.org>
X-BeenThere: gentoo-portage-dev@lists.gentoo.org
Reply-to: gentoo-portage-dev@lists.gentoo.org
X-Archives-Salt: d0d299ad-1f0f-40f4-b093-cfb39d5bce66
X-Archives-Hash: eca50c242a0afcffa52dc8292d4fce32

From: "Anthony G. Basile" <blueness@gentoo.org>

The current method to check for a sane system locale is to use python's
ctypes.util.find_library() to construct a full library path to the
system libc.so and pass that path to ctypes.CDLL() so we can call
toupper() and tolower() directly.  However, this gets bogged down in
implementation details and fails with musl.

We work around this design flaw in ctypes with a small python module
written in C which provides thin wrappers to toupper() and tolower(),
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 <blueness@gentoo.org>
---
 pym/portage/util/locale.py | 16 ++++++-----
 setup.py                   |  6 +++-
 src/portage_util_libc.c    | 70 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 84 insertions(+), 8 deletions(-)
 create mode 100644 src/portage_util_libc.c

diff --git a/pym/portage/util/locale.py b/pym/portage/util/locale.py
index 093eb86..5b09945 100644
--- a/pym/portage/util/locale.py
+++ b/pym/portage/util/locale.py
@@ -34,13 +34,15 @@ def _check_locale(silent):
 	"""
 	The inner locale check function.
 	"""
-
-	libc_fn = find_library("c")
-	if libc_fn is None:
-		return None
-	libc = LoadLibrary(libc_fn)
-	if libc is None:
-		return None
+	try:
+		from portage.util import libc
+	except ImportError:
+		libc_fn = find_library("c")
+		if libc_fn is None:
+			return None
+		libc = LoadLibrary(libc_fn)
+		if libc is None:
+			return None
 
 	lc = list(range(ord('a'), ord('z')+1))
 	uc = list(range(ord('A'), ord('Z')+1))
diff --git a/setup.py b/setup.py
index 25429bc..5ca8156 100755
--- a/setup.py
+++ b/setup.py
@@ -47,7 +47,11 @@ x_scripts = {
 # Dictionary custom modules written in C/C++ here.  The structure is
 #   key   = module name
 #   value = list of C/C++ source code, path relative to top source directory
-x_c_helpers = {}
+x_c_helpers = {
+	'portage.util.libc' : [
+		'src/portage_util_libc.c',
+	],
+}
 
 class x_build(build):
 	""" Build command with extra build_man call. """
diff --git a/src/portage_util_libc.c b/src/portage_util_libc.c
new file mode 100644
index 0000000..00b09c2
--- /dev/null
+++ b/src/portage_util_libc.c
@@ -0,0 +1,70 @@
+/* Copyright 2005-2016 Gentoo Foundation
+ * Distributed under the terms of the GNU General Public License v2
+ */
+
+#include <Python.h>
+#include <stdlib.h>
+#include <ctype.h>
+
+static PyObject * _libc_tolower(PyObject *, PyObject *);
+static PyObject * _libc_toupper(PyObject *, PyObject *);
+
+static PyMethodDef LibcMethods[] = {
+	{"tolower", _libc_tolower, METH_VARARGS, "Convert to lower case using system locale."},
+	{"toupper", _libc_toupper, METH_VARARGS, "Convert to upper case using system locale."},
+	{NULL, NULL, 0, NULL}
+};
+
+#if PY_MAJOR_VERSION >= 3
+static struct PyModuleDef moduledef = {
+	PyModuleDef_HEAD_INIT,
+	"libc",								/* m_name */
+	"Module for converting case using the system locale",		/* m_doc */
+	-1,								/* m_size */
+	LibcMethods,							/* m_methods */
+	NULL,								/* m_reload */
+	NULL,								/* m_traverse */
+	NULL,								/* m_clear */
+	NULL,								/* m_free */
+};
+#endif
+
+PyMODINIT_FUNC
+
+#if PY_MAJOR_VERSION >= 3
+PyInit_libc(void)
+{
+	PyObject *m;
+	m = PyModule_Create(&moduledef);
+	return m;
+}
+#else
+initlibc(void)
+{
+	Py_InitModule("libc", LibcMethods);
+}
+#endif
+
+
+static PyObject *
+_libc_tolower(PyObject *self, PyObject *args)
+{
+	int c;
+
+	if (!PyArg_ParseTuple(args, "i", &c))
+		return NULL;
+
+	return Py_BuildValue("i", tolower(c));
+}
+
+
+static PyObject *
+_libc_toupper(PyObject *self, PyObject *args)
+{
+	int c;
+
+	if (!PyArg_ParseTuple(args, "i", &c))
+		return NULL;
+
+	return Py_BuildValue("i", toupper(c));
+}
-- 
2.7.3