static PyObject * psyco_register_type(PyObject *self, PyObject *args) { PyObject *type, *obj = NULL; if (!PyArg_ParseTuple(args, "O!|O", &typecastType, &type, &obj)) { return NULL; } if (obj != NULL && obj != Py_None) { if (PyObject_TypeCheck(obj, &cursorType)) { PyObject **dict = &(((cursorObject*)obj)->string_types); if (*dict == NULL) { if (!(*dict = PyDict_New())) { return NULL; } } if (0 > typecast_add(type, *dict, 0)) { return NULL; } } else if (PyObject_TypeCheck(obj, &connectionType)) { if (0 > typecast_add(type, ((connectionObject*)obj)->string_types, 0)) { return NULL; } } else { PyErr_SetString(PyExc_TypeError, "argument 2 must be a connection, cursor or None"); return NULL; } } else { if (0 > typecast_add(type, NULL, 0)) { return NULL; } } Py_RETURN_NONE; }
static PyObject * psyco_register_type(PyObject *self, PyObject *args) { PyObject *type, *obj = NULL; if (!PyArg_ParseTuple(args, "O!|O", &typecastType, &type, &obj)) { return NULL; } if (obj != NULL && obj != Py_None) { if (PyObject_TypeCheck(obj, &cursorType)) { _psyco_register_type_set(&(((cursorObject*)obj)->string_types), type); } else if (PyObject_TypeCheck(obj, &connectionType)) { typecast_add(type, ((connectionObject*)obj)->string_types, 0); } else { PyErr_SetString(PyExc_TypeError, "argument 2 must be a connection, cursor or None"); return NULL; } } else { typecast_add(type, NULL, 0); } Py_INCREF(Py_None); return Py_None; }
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; }
static void _psyco_register_type_set(PyObject **dict, PyObject *type) { if (*dict == NULL) *dict = PyDict_New(); typecast_add(type, *dict, 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; }