Back to index

python3.2  3.2.2
Defines | Functions | Variables
_localemodule.c File Reference
#include "Python.h"
#include <stdio.h>
#include <locale.h>
#include <string.h>
#include <ctype.h>

Go to the source code of this file.

Defines

#define PY_SSIZE_T_CLEAN
#define RESULT_STRING(s)
#define RESULT_INT(i)

Functions

 PyDoc_STRVAR (locale__doc__,"Support for POSIX locales.")
static PyObjectstr2uni (const char *s)
 PyDoc_STRVAR (setlocale__doc__,"(integer,string=None) -> string. Activates/queries locale processing.")
static PyObjectcopy_grouping (char *s)
static PyObjectPyLocale_setlocale (PyObject *self, PyObject *args)
 PyDoc_STRVAR (localeconv__doc__,"() -> dict. Returns numeric and monetary locale-specific parameters.")
static PyObjectPyLocale_localeconv (PyObject *self)
PyMODINIT_FUNC PyInit__locale (void)

Variables

static PyObjectError
static struct PyMethodDef []
static struct PyModuleDef

Define Documentation

Definition at line 12 of file _localemodule.c.

#define RESULT_INT (   i)
Value:
x = PyLong_FromLong(l->i);\
    if (!x) goto failed;\
    PyDict_SetItemString(result, #i, x);\
    Py_XDECREF(x)
#define RESULT_STRING (   s)
Value:
x = str2uni(l->s);   \
    if (!x) goto failed;\
    PyDict_SetItemString(result, #s, x);\
    Py_XDECREF(x)

Function Documentation

static PyObject* copy_grouping ( char *  s) [static]

Definition at line 89 of file _localemodule.c.

{
    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 = PyLong_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;
}

Here is the call graph for this function:

Here is the caller graph for this function:

PyDoc_STRVAR ( locale__doc__  ,
"Support for POSIX locales."   
)
PyDoc_STRVAR ( setlocale__doc__  ,
"(integer,string=None) -> string. Activates/queries locale processing."   
)
PyDoc_STRVAR ( localeconv__doc__  ,
"() -> dict. Returns numeric and monetary locale-specific parameters."   
)

Definition at line 662 of file _localemodule.c.

{
    PyObject *m, *d, *x;
#ifdef HAVE_LANGINFO_H
    int i;
#endif

    m = PyModule_Create(&_localemodule);
    if (m == NULL)
    return NULL;

    d = PyModule_GetDict(m);

    x = PyLong_FromLong(LC_CTYPE);
    PyDict_SetItemString(d, "LC_CTYPE", x);
    Py_XDECREF(x);

    x = PyLong_FromLong(LC_TIME);
    PyDict_SetItemString(d, "LC_TIME", x);
    Py_XDECREF(x);

    x = PyLong_FromLong(LC_COLLATE);
    PyDict_SetItemString(d, "LC_COLLATE", x);
    Py_XDECREF(x);

    x = PyLong_FromLong(LC_MONETARY);
    PyDict_SetItemString(d, "LC_MONETARY", x);
    Py_XDECREF(x);

#ifdef LC_MESSAGES
    x = PyLong_FromLong(LC_MESSAGES);
    PyDict_SetItemString(d, "LC_MESSAGES", x);
    Py_XDECREF(x);
#endif /* LC_MESSAGES */

    x = PyLong_FromLong(LC_NUMERIC);
    PyDict_SetItemString(d, "LC_NUMERIC", x);
    Py_XDECREF(x);

    x = PyLong_FromLong(LC_ALL);
    PyDict_SetItemString(d, "LC_ALL", x);
    Py_XDECREF(x);

    x = PyLong_FromLong(CHAR_MAX);
    PyDict_SetItemString(d, "CHAR_MAX", x);
    Py_XDECREF(x);

    Error = PyErr_NewException("locale.Error", NULL, NULL);
    PyDict_SetItemString(d, "Error", Error);

#ifdef HAVE_LANGINFO_H
    for (i = 0; langinfo_constants[i].name; i++) {
        PyModule_AddIntConstant(m, langinfo_constants[i].name,
                                langinfo_constants[i].value);
    }
#endif
    return m;
}

Here is the call graph for this function:

static PyObject* PyLocale_localeconv ( PyObject self) [static]

Definition at line 171 of file _localemodule.c.

{
    PyObject* result;
    struct lconv *l;
    PyObject *x;

    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 = str2uni(l->s);   \
    if (!x) goto failed;\
    PyDict_SetItemString(result, #s, x);\
    Py_XDECREF(x)

#define RESULT_INT(i)\
    x = PyLong_FromLong(l->i);\
    if (!x) goto failed;\
    PyDict_SetItemString(result, #i, x);\
    Py_XDECREF(x)

    /* Numeric information */
    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;

  failed:
    Py_XDECREF(result);
    Py_XDECREF(x);
    return NULL;
}

Here is the call graph for this function:

static PyObject* PyLocale_setlocale ( PyObject self,
PyObject args 
) [static]

Definition at line 127 of file _localemodule.c.

{
    int category;
    char *locale = NULL, *result;
    PyObject *result_object;

    if (!PyArg_ParseTuple(args, "i|z:setlocale", &category, &locale))
        return NULL;

#if defined(MS_WINDOWS)
    if (category < LC_MIN || category > LC_MAX)
    {
        PyErr_SetString(Error, "invalid locale category");
        return NULL;
    }
#endif

    if (locale) {
        /* set locale */
        result = setlocale(category, locale);
        if (!result) {
            /* operation failed, no setting was changed */
            PyErr_SetString(Error, "unsupported locale setting");
            return NULL;
        }
        result_object = str2uni(result);
        if (!result_object)
            return NULL;
    } else {
        /* get locale */
        result = setlocale(category, NULL);
        if (!result) {
            PyErr_SetString(Error, "locale query failed");
            return NULL;
        }
        result_object = str2uni(result);
    }
    return result_object;
}

Here is the call graph for this function:

static PyObject* str2uni ( const char *  s) [static]

Definition at line 47 of file _localemodule.c.

{
#ifdef HAVE_BROKEN_MBSTOWCS
    size_t needed = strlen(s);
#else
    size_t needed = mbstowcs(NULL, s, 0);
#endif
    size_t res1;
    wchar_t smallbuf[30];
    wchar_t *dest;
    PyObject *res2;
    if (needed == (size_t)-1) {
        PyErr_SetString(PyExc_ValueError, "Cannot convert byte to string");
        return NULL;
    }
    if (needed*sizeof(wchar_t) < sizeof(smallbuf))
        dest = smallbuf;
    else {
        dest = PyMem_Malloc((needed+1)*sizeof(wchar_t));
        if (!dest)
            return PyErr_NoMemory();
    }
    /* This shouldn't fail now */
    res1 = mbstowcs(dest, s, needed+1);
#ifdef HAVE_BROKEN_MBSTOWCS
    assert(res1 != (size_t)-1);
#else
    assert(res1 == needed);
#endif
    res2 = PyUnicode_FromWideChar(dest, res1);
    if (dest != smallbuf)
        PyMem_Free(dest);
    return res2;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

PyObject* Error [static]

Definition at line 43 of file _localemodule.c.

struct PyMethodDef[] [static]

Definition at line 609 of file _localemodule.c.

struct PyModuleDef [static]
Initial value:
 {
    PyModuleDef_HEAD_INIT,
    "_locale",
    locale__doc__,
    -1,
    PyLocale_Methods,
    NULL,
    NULL,
    NULL,
    NULL
}

Definition at line 649 of file _localemodule.c.