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

Skip to content

Commit 9b75dca

Browse files
committed
Expose nl_langinfo through locale where available.
1 parent 09379da commit 9b75dca

6 files changed

Lines changed: 550 additions & 321 deletions

File tree

Doc/lib/liblocale.tex

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,15 @@ \section{\module{locale} ---
108108
\end{tableii}
109109
\end{funcdesc}
110110

111+
\begin{funcdesc}{nl_langinfo}{option}
112+
113+
Return some locale-specific information as a string. This function is
114+
not available on all systems, and the set of possible options might
115+
also vary across platforms. The possible argument values are numbers,
116+
for which symbolic constants are available in the locale module.
117+
118+
\end{funcdesc}
119+
111120
\begin{funcdesc}{getdefaultlocale}{\optional{envvars}}
112121
Tries to determine the default locale settings and returns
113122
them as a tuple of the form \code{(\var{language code},
@@ -259,6 +268,116 @@ \section{\module{locale} ---
259268
\function{localeconv()}.
260269
\end{datadesc}
261270

271+
The \function{nl_langinfo} function accepts one of the following keys.
272+
Most descriptions are taken from the corresponding description in the
273+
GNU C library.
274+
275+
\begin{datadesc}{CODESET}
276+
Return a string with the name of the character encoding used in the
277+
selected locale.
278+
\end{datadesc}
279+
280+
\begin{datadesc}{D_T_FMT}
281+
Return a string that can be used as a format string for strftime(3) to
282+
represent time and date in a locale-specific way.
283+
\end{datadesc}
284+
285+
\begin{datadesc}{D_FMT}
286+
Return a string that can be used as a format string for strftime(3) to
287+
represent a date in a locale-specific way.
288+
\end{datadesc}
289+
290+
\begin{datadesc}{T_FMT}
291+
Return a string that can be used as a format string for strftime(3) to
292+
represent a time in a locale-specific way.
293+
\end{datadesc}
294+
295+
\begin{datadesc}{T_FMT_AMPM}
296+
The return value can be used as a format string for `strftime' to
297+
represent time in the am/pm format.
298+
\end{datadesc}
299+
300+
\begin{datadesc}{DAY_1 ... DAY_7}
301+
Return name of the n-th day of the week. \[Warning: this follows the US
302+
convention DAY_1 = Sunday, not the international convention (ISO 8601)
303+
that Monday is the first day of the week.\]
304+
\end{datadesc}
305+
306+
\begin{datadesc}{ABDAY_1 ... ABDAY_7}
307+
Return abbreviated name of the n-th day of the week.
308+
\end{datadesc}
309+
310+
\begin{datadesc}{MON_1 ... MON_12}
311+
Return name of the n-th month.
312+
\end{datadesc}
313+
314+
\begin{datadesc}{ABMON_1 ... ABMON_12}
315+
Return abbreviated name of the n-th month.
316+
\end{datadesc}
317+
318+
\begin{datadesc}{RADIXCHAR}
319+
Return radix character (decimal dot, decimal comma, etc.)
320+
\end{datadesc}
321+
322+
\begin{datadesc}{THOUSEP}
323+
Return separator character for thousands (groups of three digits).
324+
\end{datadesc}
325+
326+
\begin{datadesc}{YESEXPR}
327+
Return a regular expression that can be used with the regex
328+
function to recognize a positive response to a yes/no question.
329+
\[Warning: the expression is in the syntax suitable for the
330+
regex C library function, which might differ from the syntax
331+
used in \module{re}\]
332+
\end{datadesc}
333+
334+
\begin{datadesc}{NOEXPR}
335+
Return a regular expression that can be used with the regex(3)
336+
function to recognize a negative response to a yes/no question.
337+
\end{datadesc}
338+
339+
\begin{datadesc}{CRNCYSTR}
340+
Return the currency symbol, preceded by "-" if the symbol should
341+
appear before the value, "+" if the symbol should appear after the
342+
value, or "." if the symbol should replace the radix character.
343+
\end{datadesc}
344+
345+
\begin{datadesc}{ERA}
346+
The return value represents the era used in the current locale.
347+
348+
Most locales do not define this value. An example of a locale which
349+
does define this value is the Japanese one. In Japan, the traditional
350+
representation of dates includes the name of the era corresponding to
351+
the then-emperor's reign.
352+
353+
Normally it should not be necessary to use this value directly.
354+
Specifying the \code{E} modifier in their format strings causes the
355+
\function{strftime} function to use this information. The format of the
356+
returned string is not specified, and therefore you should not assume
357+
knowledge of it on different systems.
358+
\end{datadesc}
359+
360+
\begin{datadesc}{ERA_YEAR}
361+
The return value gives the year in the relevant era of the locale.
362+
\end{datadesc}
363+
364+
\begin{datadesc}{ERA_D_T_FMT}
365+
This return value can be used as a format string for
366+
\function{strftime} to represent dates and times in a locale-specific
367+
era-based way.
368+
\end{datadesc}
369+
370+
\begin{datadesc}{ERA_D_FMT}
371+
This return value can be used as a format string for
372+
\function{strftime} to represent time in a locale-specific era-based
373+
way.
374+
\end{datadesc}
375+
376+
\begin{datadesc}{ALT_DIGITS}
377+
The return value is a representation of up to 100 values used to
378+
represent the values 0 to 99.
379+
\end{datadesc}
380+
262381
Example:
263382

264383
\begin{verbatim}

Lib/encodings/aliases.py

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

3232
# ASCII
3333
'us_ascii': 'ascii',
34+
'ansi_x3.4_1968': 'ascii', # used on Linux
35+
'646': 'ascii', # used on Solaris
3436

3537
# EBCDIC
3638
'ebcdic_cp_us': 'cp037',

Modules/_localemodule.c

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ This software comes with no warranty. Use at your own risk.
1717
#include <string.h>
1818
#include <ctype.h>
1919

20+
#ifdef HAVE_LANGINFO_H
21+
#include <langinfo.h>
22+
#endif
23+
2024
#if defined(MS_WIN32)
2125
#define WINDOWS_LEAN_AND_MEAN
2226
#include <windows.h>
@@ -391,6 +395,23 @@ PyLocale_getdefaultlocale(PyObject* self, PyObject* args)
391395
}
392396
#endif
393397

398+
#ifdef HAVE_LANGINFO_H
399+
static char nl_langinfo__doc__[] =
400+
"nl_langinfo(key) -> string\n"
401+
"Return the value for the locale information associated with key."
402+
;
403+
404+
static PyObject*
405+
PyLocale_nl_langinfo(PyObject* self, PyObject* args)
406+
{
407+
int item;
408+
if (!PyArg_ParseTuple(args, "i:nl_langinfo", &item))
409+
return NULL;
410+
return PyString_FromString(nl_langinfo(item));
411+
}
412+
#endif
413+
414+
394415
static struct PyMethodDef PyLocale_Methods[] = {
395416
{"setlocale", (PyCFunction) PyLocale_setlocale,
396417
METH_VARARGS, setlocale__doc__},
@@ -403,6 +424,11 @@ static struct PyMethodDef PyLocale_Methods[] = {
403424
#if defined(MS_WIN32) || defined(macintosh)
404425
{"_getdefaultlocale", (PyCFunction) PyLocale_getdefaultlocale, 0},
405426
#endif
427+
#ifdef HAVE_LANGINFO_H
428+
{"nl_langinfo", (PyCFunction) PyLocale_nl_langinfo,
429+
METH_VARARGS, nl_langinfo__doc__},
430+
#endif
431+
406432
{NULL, NULL}
407433
};
408434

@@ -455,4 +481,81 @@ init_locale(void)
455481
x = PyString_FromString(locale__doc__);
456482
PyDict_SetItemString(d, "__doc__", x);
457483
Py_XDECREF(x);
484+
485+
#define ADDINT(X) PyModule_AddIntConstant(m, #X, X)
486+
#ifdef HAVE_LANGINFO_H
487+
/* These constants should exist on any langinfo implementation */
488+
ADDINT(DAY_1);
489+
ADDINT(DAY_2);
490+
ADDINT(DAY_3);
491+
ADDINT(DAY_4);
492+
ADDINT(DAY_5);
493+
ADDINT(DAY_6);
494+
ADDINT(DAY_7);
495+
496+
ADDINT(ABDAY_1);
497+
ADDINT(ABDAY_2);
498+
ADDINT(ABDAY_3);
499+
ADDINT(ABDAY_4);
500+
ADDINT(ABDAY_5);
501+
ADDINT(ABDAY_6);
502+
ADDINT(ABDAY_7);
503+
504+
ADDINT(MON_1);
505+
ADDINT(MON_2);
506+
ADDINT(MON_3);
507+
ADDINT(MON_4);
508+
ADDINT(MON_5);
509+
ADDINT(MON_6);
510+
ADDINT(MON_7);
511+
ADDINT(MON_8);
512+
ADDINT(MON_9);
513+
ADDINT(MON_10);
514+
ADDINT(MON_11);
515+
ADDINT(MON_12);
516+
517+
ADDINT(ABMON_1);
518+
ADDINT(ABMON_2);
519+
ADDINT(ABMON_3);
520+
ADDINT(ABMON_4);
521+
ADDINT(ABMON_5);
522+
ADDINT(ABMON_6);
523+
ADDINT(ABMON_7);
524+
ADDINT(ABMON_8);
525+
ADDINT(ABMON_9);
526+
ADDINT(ABMON_10);
527+
ADDINT(ABMON_11);
528+
ADDINT(ABMON_12);
529+
530+
ADDINT(RADIXCHAR);
531+
ADDINT(THOUSEP);
532+
/* YESSTR and NOSTR are deprecated in glibc, since they are
533+
a special case of message translation, which should be rather
534+
done using gettext. So we don't expose it to Python in the
535+
first place.
536+
ADDINT(YESSTR);
537+
ADDINT(NOSTR);
538+
*/
539+
ADDINT(CRNCYSTR);
540+
541+
ADDINT(D_T_FMT);
542+
ADDINT(D_FMT);
543+
ADDINT(T_FMT);
544+
ADDINT(AM_STR);
545+
ADDINT(PM_STR);
546+
547+
#ifdef CODESET
548+
/* The following constants are available only with XPG4. */
549+
ADDINT(CODESET);
550+
ADDINT(T_FMT_AMPM);
551+
ADDINT(ERA);
552+
ADDINT(ERA_D_FMT);
553+
ADDINT(ERA_D_T_FMT);
554+
ADDINT(ERA_T_FMT);
555+
ADDINT(ALT_DIGITS);
556+
ADDINT(YESEXPR);
557+
ADDINT(NOEXPR);
558+
ADDINT(_DATE_FMT);
559+
#endif
560+
#endif /* HAVE_LANGINFO_H */
458561
}

0 commit comments

Comments
 (0)