📄 _localemodule.c
字号:
/***********************************************************
Copyright (C) 1997, 2002 Martin von Loewis
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies.
This software comes with no warranty. Use at your own risk.
******************************************************************/
#include "Python.h"
#include <stdio.h>
#include <errno.h>
#include <locale.h>
#include <string.h>
#include <ctype.h>
#ifdef HAVE_LANGINFO_H
#include <langinfo.h>
#endif
#if defined(MS_WIN32)
#define WINDOWS_LEAN_AND_MEAN
#include <windows.h>
#endif
#ifdef macintosh
#include "macglue.h"
#endif
#ifdef RISCOS
char *strdup(const char *);
#endif
static char locale__doc__[] = "Support for POSIX locales.";
static PyObject *Error;
/* support functions for formatting floating point numbers */
static char setlocale__doc__[] =
"(integer,string=None) -> string. Activates/queries locale processing."
;
/* to record the LC_NUMERIC settings */
static PyObject* grouping = NULL;
static PyObject* thousands_sep = NULL;
static PyObject* decimal_point = NULL;
/* if non-null, indicates that LC_NUMERIC is different from "C" */
static char* saved_numeric = NULL;
/* the grouping is terminated by either 0 or CHAR_MAX */
static PyObject*
copy_grouping(char* s)
{
int i;
PyObject *result, *val = NULL;
if (s[0] == '\0')
/* empty string: no grouping at all */
return PyList_New(0);
for (i = 0; s[i] != '\0' && s[i] != CHAR_MAX; i++)
; /* nothing */
result = PyList_New(i+1);
if (!result)
return NULL;
i = -1;
do {
i++;
val = PyInt_FromLong(s[i]);
if (!val)
break;
if (PyList_SetItem(result, i, val)) {
Py_DECREF(val);
val = NULL;
break;
}
} while (s[i] != '\0' && s[i] != CHAR_MAX);
if (!val) {
Py_DECREF(result);
return NULL;
}
return result;
}
static void
fixup_ulcase(void)
{
PyObject *mods, *strop, *string, *ulo;
unsigned char ul[256];
int n, c;
/* find the string and strop modules */
mods = PyImport_GetModuleDict();
if (!mods)
return;
string = PyDict_GetItemString(mods, "string");
if (string)
string = PyModule_GetDict(string);
strop=PyDict_GetItemString(mods, "strop");
if (strop)
strop = PyModule_GetDict(strop);
if (!string && !strop)
return;
/* create uppercase map string */
n = 0;
for (c = 0; c < 256; c++) {
if (isupper(c))
ul[n++] = c;
}
ulo = PyString_FromStringAndSize((const char *)ul, n);
if (!ulo)
return;
if (string)
PyDict_SetItemString(string, "uppercase", ulo);
if (strop)
PyDict_SetItemString(strop, "uppercase", ulo);
Py_DECREF(ulo);
/* create lowercase string */
n = 0;
for (c = 0; c < 256; c++) {
if (islower(c))
ul[n++] = c;
}
ulo = PyString_FromStringAndSize((const char *)ul, n);
if (!ulo)
return;
if (string)
PyDict_SetItemString(string, "lowercase", ulo);
if (strop)
PyDict_SetItemString(strop, "lowercase", ulo);
Py_DECREF(ulo);
/* create letters string */
n = 0;
for (c = 0; c < 256; c++) {
if (isalpha(c))
ul[n++] = c;
}
ulo = PyString_FromStringAndSize((const char *)ul, n);
if (!ulo)
return;
if (string)
PyDict_SetItemString(string, "letters", ulo);
Py_DECREF(ulo);
}
#if defined(HAVE_LANGINFO_H) && defined(CODESET)
static int fileencoding_uses_locale = 0;
#endif
static PyObject*
PyLocale_setlocale(PyObject* self, PyObject* args)
{
int category;
char *locale = NULL, *result;
PyObject *result_object;
struct lconv *lc;
if (!PyArg_ParseTuple(args, "i|z:setlocale", &category, &locale))
return NULL;
if (locale) {
/* set locale */
result = setlocale(category, locale);
if (!result) {
/* operation failed, no setting was changed */
PyErr_SetString(Error, "locale setting not supported");
return NULL;
}
result_object = PyString_FromString(result);
if (!result)
return NULL;
/* record changes to LC_NUMERIC */
if (category == LC_NUMERIC || category == LC_ALL) {
if (strcmp(locale, "C") == 0 || strcmp(locale, "POSIX") == 0) {
/* user just asked for default numeric locale */
if (saved_numeric)
free(saved_numeric);
saved_numeric = NULL;
} else {
/* remember values */
lc = localeconv();
Py_XDECREF(grouping);
grouping = copy_grouping(lc->grouping);
Py_XDECREF(thousands_sep);
thousands_sep = PyString_FromString(lc->thousands_sep);
Py_XDECREF(decimal_point);
decimal_point = PyString_FromString(lc->decimal_point);
saved_numeric = strdup(locale);
/* restore to "C" */
setlocale(LC_NUMERIC, "C");
}
}
/* record changes to LC_CTYPE */
if (category == LC_CTYPE || category == LC_ALL)
fixup_ulcase();
/* things that got wrong up to here are ignored */
PyErr_Clear();
#if defined(HAVE_LANGINFO_H) && defined(CODESET)
if (Py_FileSystemDefaultEncoding == NULL)
fileencoding_uses_locale = 1;
if (fileencoding_uses_locale) {
char *codeset = nl_langinfo(CODESET);
PyObject *enc = NULL;
if (*codeset && (enc = PyCodec_Encoder(codeset))) {
/* Release previous file encoding */
if (Py_FileSystemDefaultEncoding)
free((char *)Py_FileSystemDefaultEncoding);
Py_FileSystemDefaultEncoding = strdup(codeset);
Py_DECREF(enc);
} else
PyErr_Clear();
}
#endif
} else {
/* get locale */
/* restore LC_NUMERIC first, if appropriate */
if (saved_numeric)
setlocale(LC_NUMERIC, saved_numeric);
result = setlocale(category, NULL);
if (!result) {
PyErr_SetString(Error, "locale query failed");
return NULL;
}
result_object = PyString_FromString(result);
/* restore back to "C" */
if (saved_numeric)
setlocale(LC_NUMERIC, "C");
}
return result_object;
}
static char localeconv__doc__[] =
"() -> dict. Returns numeric and monetary locale-specific parameters."
;
static PyObject*
PyLocale_localeconv(PyObject* self, PyObject* args)
{
PyObject* result;
struct lconv *l;
PyObject *x;
if (!PyArg_NoArgs(args))
return NULL;
result = PyDict_New();
if (!result)
return NULL;
/* if LC_NUMERIC is different in the C library, use saved value */
l = localeconv();
/* hopefully, the localeconv result survives the C library calls
involved herein */
#define RESULT_STRING(s)\
x = PyString_FromString(l->s);\
if (!x) goto failed;\
PyDict_SetItemString(result, #s, x);\
Py_XDECREF(x)
#define RESULT_INT(i)\
x = PyInt_FromLong(l->i);\
if (!x) goto failed;\
PyDict_SetItemString(result, #i, x);\
Py_XDECREF(x)
/* Numeric information */
if (saved_numeric){
/* cannot use localeconv results */
PyDict_SetItemString(result, "decimal_point", decimal_point);
PyDict_SetItemString(result, "grouping", grouping);
PyDict_SetItemString(result, "thousands_sep", thousands_sep);
} else {
RESULT_STRING(decimal_point);
RESULT_STRING(thousands_sep);
x = copy_grouping(l->grouping);
if (!x)
goto failed;
PyDict_SetItemString(result, "grouping", x);
Py_XDECREF(x);
}
/* Monetary information */
RESULT_STRING(int_curr_symbol);
RESULT_STRING(currency_symbol);
RESULT_STRING(mon_decimal_point);
RESULT_STRING(mon_thousands_sep);
x = copy_grouping(l->mon_grouping);
if (!x)
goto failed;
PyDict_SetItemString(result, "mon_grouping", x);
Py_XDECREF(x);
RESULT_STRING(positive_sign);
RESULT_STRING(negative_sign);
RESULT_INT(int_frac_digits);
RESULT_INT(frac_digits);
RESULT_INT(p_cs_precedes);
RESULT_INT(p_sep_by_space);
RESULT_INT(n_cs_precedes);
RESULT_INT(n_sep_by_space);
RESULT_INT(p_sign_posn);
RESULT_INT(n_sign_posn);
return result;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -