1 /* pgcasts_basic.c - basic typecasting functions to python types
3 * Copyright (C) 2001-2003 Federico Di Gregorio <fog@debian.org>
5 * This file is part of the psycopg module.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2,
10 * or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 /** INTEGER - cast normal integers (4 bytes) to python int **/
25 typecast_INTEGER_cast(char *s, int len, PyObject *curs)
29 if (s == NULL) {Py_INCREF(Py_None); return Py_None;}
31 strncpy(buffer, s, len); buffer[len] = '\0';
34 return PyInt_FromString(s, NULL, 0);
37 /** LONGINTEGER - cast long integers (8 bytes) to python long **/
40 typecast_LONGINTEGER_cast(char *s, int len, PyObject *curs)
44 if (s == NULL) {Py_INCREF(Py_None); return Py_None;}
46 strncpy(buffer, s, len); buffer[len] = '\0';
49 return PyLong_FromString(s, NULL, 0);
52 /** FLOAT - cast floating point numbers to python float **/
55 typecast_FLOAT_cast(char *s, int len, PyObject *curs)
57 PyObject *str = NULL, *flo = NULL;
60 if (s == NULL) {Py_INCREF(Py_None); return Py_None;}
61 str = PyString_FromStringAndSize(s, len);
62 flo = PyFloat_FromString(str, &pend);
67 /** STRING - cast strings of any type to python string **/
70 typecast_STRING_cast(char *s, int len, PyObject *curs)
72 if (s == NULL) {Py_INCREF(Py_None); return Py_None;}
73 return PyString_FromStringAndSize(s, len);
76 /** UNICODE - cast strings of any type to a python unicode object **/
79 typecast_UNICODE_cast(char *s, int len, PyObject *curs)
83 if (s == NULL) {Py_INCREF(Py_None); return Py_None;}
85 enc = PyDict_GetItemString(psycoEncodings,
86 ((cursorObject*)curs)->conn->encoding);
88 return PyUnicode_Decode(s, len, PyString_AsString(enc), NULL);
91 PyErr_Format(InterfaceError,
92 "can't decode into unicode string from %s",
93 ((cursorObject*)curs)->conn->encoding);
98 /** BOOLEAN - cast boolean value into right python object **/
101 typecast_BOOLEAN_cast(char *s, int len, PyObject *curs)
105 if (s == NULL) {Py_INCREF(Py_None); return Py_None;}
116 /** DECIMAL - cast any kind of number into a Python Decimal object **/
120 typecast_DECIMAL_cast(char *s, int len, PyObject *curs)
122 PyObject *res = NULL;
125 if (s == NULL) {Py_INCREF(Py_None); return Py_None;}
127 if ((buffer = PyMem_Malloc(len+1)) == NULL)
129 strncpy(buffer, s, len); buffer[len] = '\0';
130 res = PyObject_CallFunction(decimalType, "s", buffer);
136 #define typecast_DECIMAL_cast typecast_FLOAT_cast
139 /* some needed aliases */
140 #define typecast_NUMBER_cast typecast_FLOAT_cast
141 #define typecast_ROWID_cast typecast_INTEGER_cast