2020 */
2121
2222#include <libpq-fe.h>
23+ #include <stdlib.h>
2324
2425/** INTEGER - cast normal integers (4 bytes) to python int **/
2526
2627static PyObject *
27- typecast_INTEGER_cast (PyObject * s , PyObject * curs )
28+ typecast_INTEGER_cast (unsigned char * s , int len , PyObject * curs )
2829{
29- if (s == Py_None ) {Py_INCREF (s ); return s ;}
30- return PyNumber_Int ( s );
30+ if (s == NULL ) {Py_INCREF (Py_None ); return Py_None ;}
31+ return PyInt_FromString ( s , NULL , 0 );
3132}
3233
3334/** LONGINTEGER - cast long integers (8 bytes) to python long **/
3435
3536static PyObject *
36- typecast_LONGINTEGER_cast (PyObject * s , PyObject * curs )
37+ typecast_LONGINTEGER_cast (unsigned char * s , int len , PyObject * curs )
3738{
38- if (s == Py_None ) {Py_INCREF (s ); return s ;}
39- return PyNumber_Long ( s );
39+ if (s == NULL ) {Py_INCREF (Py_None ); return Py_None ;}
40+ return PyLong_FromString ( s , NULL , 0 );
4041}
4142
4243/** FLOAT - cast floating point numbers to python float **/
4344
4445static PyObject *
45- typecast_FLOAT_cast (PyObject * s , PyObject * curs )
46+ typecast_FLOAT_cast (unsigned char * s , int len , PyObject * curs )
4647{
47- if (s == Py_None ) {Py_INCREF (s ); return s ;}
48- return PyNumber_Float ( s );
48+ if (s == NULL ) {Py_INCREF (Py_None ); return Py_None ;}
49+ return PyFloat_FromDouble ( atof ( s ) );
4950}
5051
5152/** STRING - cast strings of any type to python string **/
5253
5354static PyObject *
54- typecast_STRING_cast (PyObject * s , PyObject * curs )
55+ typecast_STRING_cast (unsigned char * s , int len , PyObject * curs )
5556{
56- Py_INCREF ( s );
57- return s ;
57+ if ( s == NULL ) { Py_INCREF ( Py_None ); return Py_None ;}
58+ return PyString_FromStringAndSize ( s , len ) ;
5859}
5960
6061/** UNICODE - cast strings of any type to a python unicode object **/
6162
6263static PyObject *
63- typecast_UNICODE_cast (PyObject * s , PyObject * curs )
64+ typecast_UNICODE_cast (unsigned char * s , int len , PyObject * curs )
6465{
6566 PyObject * enc ;
66-
67- if (s == Py_None ) {Py_INCREF (s ); return s ;}
67+
68+ if (s == NULL ) {Py_INCREF (Py_None ); return Py_None ;}
6869
6970 enc = PyDict_GetItemString (psycoEncodings ,
7071 ((cursorObject * )curs )-> conn -> encoding );
7172 if (enc ) {
72- return PyUnicode_Decode (PyString_AsString (s ),
73- PyString_Size (s ),
74- PyString_AsString (enc ),
75- NULL );
73+ return PyUnicode_Decode (s , strlen (s ), PyString_AsString (enc ), NULL );
7674 }
7775 else {
7876 PyErr_Format (InterfaceError ,
@@ -134,15 +132,15 @@ typecast_BINARY_cast_unescape(unsigned char *str, size_t *to_length)
134132#endif
135133
136134static PyObject *
137- typecast_BINARY_cast (PyObject * s , PyObject * curs )
135+ typecast_BINARY_cast (unsigned char * s , int l , PyObject * curs )
138136{
139137 PyObject * res ;
140138 unsigned char * str ;
141139 size_t len ;
142140
143- if (s == Py_None ) {Py_INCREF (s ); return s ;}
141+ if (s == NULL ) {Py_INCREF (Py_None ); return Py_None ;}
144142
145- str = PQunescapeBytea (PyString_AS_STRING ( s ) , & len );
143+ str = PQunescapeBytea (s , & len );
146144 Dprintf ("typecast_BINARY_cast: unescaped %d bytes" , len );
147145
148146 /* TODO: using a PyBuffer would make this a zero-copy operation but we'll
@@ -159,13 +157,13 @@ typecast_BINARY_cast(PyObject *s, PyObject *curs)
159157/** BOOLEAN - cast boolean value into right python object **/
160158
161159static PyObject *
162- typecast_BOOLEAN_cast (PyObject * s , PyObject * curs )
160+ typecast_BOOLEAN_cast (unsigned char * s , int len , PyObject * curs )
163161{
164162 PyObject * res ;
165-
166- if (s == Py_None ) {Py_INCREF (s ); return s ;}
167163
168- if (PyString_AS_STRING (s )[0 ] == 't' )
164+ if (s == NULL ) {Py_INCREF (Py_None ); return Py_None ;}
165+
166+ if (s [0 ] == 't' )
169167 res = Py_True ;
170168 else
171169 res = Py_False ;
@@ -178,10 +176,10 @@ typecast_BOOLEAN_cast(PyObject *s, PyObject *curs)
178176
179177#ifdef HAVE_DECIMAL
180178static PyObject *
181- typecast_DECIMAL_cast (PyObject * s , PyObject * curs )
179+ typecast_DECIMAL_cast (unsigned char * s , int len , PyObject * curs )
182180{
183- if (s == Py_None ) {Py_INCREF (s ); return s ;}
184- return PyObject_CallFunction (decimalType , "O " , s );
181+ if (s == NULL ) {Py_INCREF (Py_None ); return Py_None ;}
182+ return PyObject_CallFunction (decimalType , "s " , s );
185183}
186184#else
187185#define typecast_DECIMAL_cast typecast_FLOAT_cast
0 commit comments