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.
24
/** INTEGER - cast normal integers (4 bytes) to python int **/
27
typecast_INTEGER_cast(PyObject *s, PyObject *curs)
29
if (s == Py_None) {Py_INCREF(s); return s;}
30
return PyNumber_Int(s);
33
/** LONGINTEGER - cast long integers (8 bytes) to python long **/
36
typecast_LONGINTEGER_cast(PyObject *s, PyObject *curs)
38
if (s == Py_None) {Py_INCREF(s); return s;}
39
return PyNumber_Long(s);
42
/** FLOAT - cast floating point numbers to python float **/
45
typecast_FLOAT_cast(PyObject *s, PyObject *curs)
47
if (s == Py_None) {Py_INCREF(s); return s;}
48
return PyNumber_Float(s);
51
/** STRING - cast strings of any type to python string **/
54
typecast_STRING_cast(PyObject *s, PyObject *curs)
60
/** BINARY - cast a binary string into a python string **/
62
/* the function typecast_BINARY_cast_unescape is used when libpq does not
63
provide PQunescapeBytea: it convert all the \xxx octal sequences to the
66
#ifdef PSYCOPG_OWN_QUOTING
67
static unsigned char *
68
typecast_BINARY_cast_unescape(unsigned char *str, size_t *to_length)
70
char *dstptr, *dststr;
74
dststr = (char*)calloc(len, sizeof(char));
77
if (dststr == NULL) return NULL;
79
Py_BEGIN_ALLOW_THREADS;
81
for (i = 0; i < len; i++) {
89
*dstptr |= (str[i++] & 7) << 6;
90
*dstptr |= (str[i++] & 7) << 3;
91
*dstptr |= (str[i] & 7);
101
Py_END_ALLOW_THREADS;
103
*to_length = (size_t)(dstptr-dststr);
108
#define PQunescapeBytea typecast_BINARY_cast_unescape
112
typecast_BINARY_cast(PyObject *s, PyObject *curs)
118
if (s == Py_None) {Py_INCREF(s); return s;}
120
str = PQunescapeBytea(PyString_AS_STRING(s), &len);
121
res = PyBuffer_FromMemory((void*)str, len);
127
/** BOOLEAN - cast boolean value into right python object **/
130
typecast_BOOLEAN_cast(PyObject *s, PyObject *curs)
134
if (s == Py_None) {Py_INCREF(s); return s;}
136
if (PyString_AS_STRING(s)[0] == 't')
145
/* some needed aliases */
146
#define typecast_NUMBER_cast typecast_FLOAT_cast
147
#define typecast_ROWID_cast typecast_INTEGER_cast