Beispiel #1
0
int
typecast_init(PyObject *dict)
{
    int i;

    /* create type dictionary and put it in module namespace */
    psyco_types = PyDict_New();
    psyco_binary_types = PyDict_New();

    if (!psyco_types || !psyco_binary_types) {
        Py_XDECREF(psyco_types);
        Py_XDECREF(psyco_binary_types);
        return -1;
    }

    PyDict_SetItemString(dict, "string_types", psyco_types);
    PyDict_SetItemString(dict, "binary_types", psyco_binary_types);

    /* insert the cast types into the 'types' dictionary and register them in
       the module dictionary */
    for (i = 0; typecast_builtins[i].name != NULL; i++) {
        typecastObject *t;

        Dprintf("typecast_init: initializing %s", typecast_builtins[i].name);

        t = (typecastObject *)typecast_from_c(&(typecast_builtins[i]), dict);
        if (t == NULL) return -1;
        if (typecast_add((PyObject *)t, NULL, 0) != 0) return -1;

        PyDict_SetItem(dict, t->name, (PyObject *)t);

        /* export binary object */
        if (typecast_builtins[i].values == typecast_BINARY_types) {
            psyco_default_binary_cast = (PyObject *)t;
        }
    }

    /* create and save a default cast object (but does not register it) */
    psyco_default_cast = typecast_from_c(&typecast_default, dict);

    /* register the date/time typecasters with their original names */
#ifdef HAVE_MXDATETIME
    for (i = 0; typecast_mxdatetime[i].name != NULL; i++) {
        typecastObject *t;
        Dprintf("typecast_init: initializing %s", typecast_mxdatetime[i].name);
        t = (typecastObject *)typecast_from_c(&(typecast_mxdatetime[i]), dict);
        if (t == NULL) return -1;
        PyDict_SetItem(dict, t->name, (PyObject *)t);
    }
#endif
    for (i = 0; typecast_pydatetime[i].name != NULL; i++) {
        typecastObject *t;
        Dprintf("typecast_init: initializing %s", typecast_pydatetime[i].name);
        t = (typecastObject *)typecast_from_c(&(typecast_pydatetime[i]), dict);
        if (t == NULL) return -1;
        PyDict_SetItem(dict, t->name, (PyObject *)t);
    }

    return 0;
}
Beispiel #2
0
RAISES_NEG int
typecast_init(PyObject *dict)
{
    int i;
    int rv = -1;
    typecastObject *t = NULL;

    /* create type dictionary and put it in module namespace */
    if (!(psyco_types = PyDict_New())) { goto exit; }
    PyDict_SetItemString(dict, "string_types", psyco_types);

    if (!(psyco_binary_types = PyDict_New())) { goto exit; }
    PyDict_SetItemString(dict, "binary_types", psyco_binary_types);

    /* insert the cast types into the 'types' dictionary and register them in
       the module dictionary */
    for (i = 0; typecast_builtins[i].name != NULL; i++) {
        Dprintf("typecast_init: initializing %s", typecast_builtins[i].name);

        t = (typecastObject *)typecast_from_c(&(typecast_builtins[i]), dict);
        if (t == NULL) { goto exit; }
        if (typecast_add((PyObject *)t, NULL, 0) < 0) { goto exit; }

        PyDict_SetItem(dict, t->name, (PyObject *)t);

        /* export binary object */
        if (typecast_builtins[i].values == typecast_BINARY_types) {
            psyco_default_binary_cast = (PyObject *)t;
        }
        Py_DECREF((PyObject *)t);
        t = NULL;
    }

    /* create and save a default cast object (but does not register it) */
    psyco_default_cast = typecast_from_c(&typecast_default, dict);

    /* register the date/time typecasters with their original names */
#ifdef HAVE_MXDATETIME
    if (0 == psyco_typecast_mxdatetime_init()) {
        for (i = 0; typecast_mxdatetime[i].name != NULL; i++) {
            Dprintf("typecast_init: initializing %s", typecast_mxdatetime[i].name);
            t = (typecastObject *)typecast_from_c(&(typecast_mxdatetime[i]), dict);
            if (t == NULL) { goto exit; }
            PyDict_SetItem(dict, t->name, (PyObject *)t);
            Py_DECREF((PyObject *)t);
            t = NULL;
        }
    }
#endif

    if (0 > psyco_typecast_datetime_init()) { goto exit; }
    for (i = 0; typecast_pydatetime[i].name != NULL; i++) {
        Dprintf("typecast_init: initializing %s", typecast_pydatetime[i].name);
        t = (typecastObject *)typecast_from_c(&(typecast_pydatetime[i]), dict);
        if (t == NULL) { goto exit; }
        PyDict_SetItem(dict, t->name, (PyObject *)t);
        Py_DECREF((PyObject *)t);
        t = NULL;
    }

    rv = 0;

exit:
    Py_XDECREF((PyObject *)t);
    return rv;
}