Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2e64c34

Browse files
committed
Expose C library's gettext. Fixes #516412.
1 parent 4208d4f commit 2e64c34

6 files changed

Lines changed: 448 additions & 342 deletions

File tree

Doc/lib/liblocale.tex

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,3 +467,21 @@ \subsection{For extension writers and programs that embed Python
467467
\module{_locale} extension module (which does all the work) from the
468468
table of built-in modules in the \file{config.c} file, and make sure
469469
that the \module{_locale} module is not accessible as a shared library.
470+
471+
\subsection{Access to message catalogs}
472+
473+
The locale module exposes the C library's gettext interface on systems
474+
that provide this interface. It consists of the functions
475+
\function{gettext}, \function{dgettext}, \function{dcgettext},
476+
\function{textdomain}, and \function{bindtextdomain}. These are
477+
similar to the same functions in the \module{gettext} module, but use
478+
the C library's binary format for message catalogs, and the C
479+
library's search algorithms for locating message catalogs.
480+
481+
Python applications should normally find no need to invoke these
482+
functions, and should use \module{gettext} instead. A known exception
483+
to this rule are applications that link use additional C libraries
484+
which internally invoke \function{gettext} or \function{dgettext}. For
485+
these applications, it may be necessary to bind the text domain, so
486+
that the libraries can properly locate their message catalogs.
487+

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ Core and builtins
3131

3232
Extension modules
3333

34+
- The locale module now exposes the C library's gettext interface.
35+
3436
- A security hole ("double free") was found in zlib-1.1.3, a popular
3537
third party compression library used by some Python modules. The
3638
hole was quickly plugged in zlib-1.1.4, and the Windows build of

Modules/_localemodule.c

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ This software comes with no warranty. Use at your own risk.
2121
#include <langinfo.h>
2222
#endif
2323

24+
#ifdef HAVE_LIBINTL_H
25+
#include <libintl.h>
26+
#endif
27+
2428
#if defined(MS_WIN32)
2529
#define WINDOWS_LEAN_AND_MEAN
2630
#include <windows.h>
@@ -521,7 +525,86 @@ PyLocale_nl_langinfo(PyObject* self, PyObject* args)
521525
return NULL;
522526
}
523527
#endif /* HAVE_LANGINFO_H */
524-
528+
529+
#ifdef HAVE_LIBINTL_H
530+
531+
static char gettext__doc__[]=
532+
"gettext(msg) -> string\n"
533+
"Return translation of msg.";
534+
535+
static PyObject*
536+
PyIntl_gettext(PyObject* self, PyObject *args)
537+
{
538+
char *in;
539+
if (!PyArg_ParseTuple(args, "z", &in))
540+
return 0;
541+
return PyString_FromString(gettext(in));
542+
}
543+
544+
static char dgettext__doc__[]=
545+
"dgettext(domain, msg) -> string\n"
546+
"Return translation of msg in domain.";
547+
548+
static PyObject*
549+
PyIntl_dgettext(PyObject* self, PyObject *args)
550+
{
551+
char *domain, *in;
552+
if (!PyArg_ParseTuple(args, "zz", &domain, &in))
553+
return 0;
554+
return PyString_FromString(dgettext(domain, in));
555+
}
556+
557+
static char dcgettext__doc__[]=
558+
"dcgettext(domain, msg, category) -> string\n"
559+
"Return translation of msg in domain and category.";
560+
561+
static PyObject*
562+
PyIntl_dcgettext(PyObject *self, PyObject *args)
563+
{
564+
char *domain, *msgid;
565+
int category;
566+
if (!PyArg_ParseTuple(args, "zzi", &domain, &msgid, &category))
567+
return 0;
568+
return PyString_FromString(dcgettext(domain,msgid,category));
569+
}
570+
571+
static char textdomain__doc__[]=
572+
"textdomain(domain) -> string\n"
573+
"Set the C library's textdmain to domain, returning the new domain.";
574+
575+
static PyObject*
576+
PyIntl_textdomain(PyObject* self, PyObject* args)
577+
{
578+
char *domain;
579+
if (!PyArg_ParseTuple(args, "z", &domain))
580+
return 0;
581+
domain = textdomain(domain);
582+
if (!domain) {
583+
PyErr_SetFromErrno(PyExc_OSError);
584+
return NULL;
585+
}
586+
return PyString_FromString(domain);
587+
}
588+
589+
static char bindtextdomain__doc__[]=
590+
"bindtextdomain(domain, dir) -> string\n"
591+
"Bind the C library's domain to dir.";
592+
593+
static PyObject*
594+
PyIntl_bindtextdomain(PyObject* self,PyObject*args)
595+
{
596+
char *domain,*dirname;
597+
if (!PyArg_ParseTuple(args, "zz", &domain, &dirname))
598+
return 0;
599+
dirname = bindtextdomain(domain, dirname);
600+
if (!dirname) {
601+
PyErr_SetFromErrno(PyExc_OSError);
602+
return NULL;
603+
}
604+
return PyString_FromString(dirname);
605+
}
606+
607+
#endif
525608

526609
static struct PyMethodDef PyLocale_Methods[] = {
527610
{"setlocale", (PyCFunction) PyLocale_setlocale,
@@ -539,7 +622,18 @@ static struct PyMethodDef PyLocale_Methods[] = {
539622
{"nl_langinfo", (PyCFunction) PyLocale_nl_langinfo,
540623
METH_VARARGS, nl_langinfo__doc__},
541624
#endif
542-
625+
#ifdef HAVE_LANGINFO_H
626+
{"gettext",(PyCFunction)PyIntl_gettext,METH_VARARGS,
627+
gettext__doc__},
628+
{"dgettext",(PyCFunction)PyIntl_dgettext,METH_VARARGS,
629+
dgettext__doc__},
630+
{"dcgettext",(PyCFunction)PyIntl_dcgettext,METH_VARARGS,
631+
dcgettext__doc__},
632+
{"textdomain",(PyCFunction)PyIntl_textdomain,METH_VARARGS,
633+
textdomain__doc__},
634+
{"bindtextdomain",(PyCFunction)PyIntl_bindtextdomain,METH_VARARGS,
635+
bindtextdomain__doc__},
636+
#endif
543637
{NULL, NULL}
544638
};
545639

0 commit comments

Comments
 (0)