1919 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
2020 */
2121
22+ #define PY_SSIZE_T_CLEAN
2223#include <Python.h>
2324#include <structmember.h>
2425#include <stringobject.h>
3839
3940#ifndef PSYCOPG_OWN_QUOTING
4041static unsigned char *
41- binary_escape (unsigned char * from , unsigned int from_length ,
42- unsigned int * to_length , PGconn * conn )
42+ binary_escape (unsigned char * from , size_t from_length ,
43+ size_t * to_length , PGconn * conn )
4344{
4445#if PG_MAJOR_VERSION > 8 || \
4546 (PG_MAJOR_VERSION == 8 && PG_MINOR_VERSION > 1 ) || \
@@ -52,11 +53,11 @@ binary_escape(unsigned char *from, unsigned int from_length,
5253}
5354#else
5455static unsigned char *
55- binary_escape (unsigned char * from , unsigned int from_length ,
56- unsigned int * to_length , PGconn * conn )
56+ binary_escape (unsigned char * from , size_t from_length ,
57+ size_t * to_length , PGconn * conn )
5758{
58- unsigneed char * quoted , * chptr , * newptr ;
59- int i , space , new_space ;
59+ unsigned char * quoted , * chptr , * newptr ;
60+ size_t i , space , new_space ;
6061
6162 space = from_length + 2 ;
6263
@@ -67,7 +68,7 @@ binary_escape(unsigned char *from, unsigned int from_length,
6768
6869 chptr = quoted ;
6970
70- for (i = 0 ; i < len ; i ++ ) {
71+ for (i = 0 ; i < from_length ; i ++ ) {
7172 if (chptr - quoted > space - 6 ) {
7273 new_space = space * ((space ) / (i + 1 )) + 2 + 6 ;
7374 if (new_space - space < 1024 ) space += 1024 ;
@@ -102,7 +103,7 @@ binary_escape(unsigned char *from, unsigned int from_length,
102103 }
103104 else {
104105 unsigned char c ;
105-
106+
106107 /* escape to octal notation \nnn */
107108 * chptr ++ = '\\' ;
108109 * chptr ++ = '\\' ;
@@ -122,7 +123,7 @@ binary_escape(unsigned char *from, unsigned int from_length,
122123
123124 Py_END_ALLOW_THREADS ;
124125
125- * to_size = chptr - quoted + 1 ;
126+ * to_length = chptr - quoted + 1 ;
126127 return quoted ;
127128}
128129#endif
@@ -142,26 +143,26 @@ binary_quote(binaryObject *self)
142143 /* escape and build quoted buffer */
143144 PyObject_AsCharBuffer (self -> wrapped , & buffer , & buffer_len );
144145
145- to = (char * )binary_escape ((unsigned char * )buffer , buffer_len , & len ,
146- self -> conn ? ((connectionObject * )self -> conn )-> pgconn : NULL );
146+ to = (char * )binary_escape ((unsigned char * )buffer , ( size_t ) buffer_len ,
147+ & len , self -> conn ? ((connectionObject * )self -> conn )-> pgconn : NULL );
147148 if (to == NULL ) {
148149 PyErr_NoMemory ();
149150 return NULL ;
150151 }
151152
152- if (len > 0 )
153- self -> buffer = PyString_FromFormat ("'%s'" , to );
154- else
155- self -> buffer = PyString_FromString ("''" );
153+ if (len > 0 )
154+ self -> buffer = PyString_FromFormat ("'%s'" , to );
155+ else
156+ self -> buffer = PyString_FromString ("''" );
156157 PQfreemem (to );
157158 }
158-
159- /* if the wrapped object is not a string or a buffer, this is an error */
159+
160+ /* if the wrapped object is not a string or a buffer, this is an error */
160161 else {
161162 PyErr_SetString (PyExc_TypeError , "can't escape non-string object" );
162163 return NULL ;
163164 }
164-
165+
165166 return self -> buffer ;
166167}
167168
@@ -206,14 +207,14 @@ PyObject *
206207binary_conform (binaryObject * self , PyObject * args )
207208{
208209 PyObject * res , * proto ;
209-
210+
210211 if (!PyArg_ParseTuple (args , "O" , & proto )) return NULL ;
211212
212213 if (proto == (PyObject * )& isqlquoteType )
213214 res = (PyObject * )self ;
214215 else
215216 res = Py_None ;
216-
217+
217218 Py_INCREF (res );
218219 return res ;
219220}
@@ -244,16 +245,19 @@ static PyMethodDef binaryObject_methods[] = {
244245static int
245246binary_setup (binaryObject * self , PyObject * str )
246247{
247- Dprintf ("binary_setup: init binary object at %p, refcnt = %d" ,
248- self , ((PyObject * )self )-> ob_refcnt );
248+ Dprintf ("binary_setup: init binary object at %p, refcnt = "
249+ FORMAT_CODE_PY_SSIZE_T ,
250+ self , ((PyObject * )self )-> ob_refcnt
251+ );
249252
250253 self -> buffer = NULL ;
251254 self -> conn = NULL ;
252255 self -> wrapped = str ;
253256 Py_INCREF (self -> wrapped );
254-
255- Dprintf ("binary_setup: good binary object at %p, refcnt = %d" ,
256- self , ((PyObject * )self )-> ob_refcnt );
257+
258+ Dprintf ("binary_setup: good binary object at %p, refcnt = "
259+ FORMAT_CODE_PY_SSIZE_T ,
260+ self , ((PyObject * )self )-> ob_refcnt );
257261 return 0 ;
258262}
259263
@@ -265,18 +269,20 @@ binary_dealloc(PyObject* obj)
265269 Py_XDECREF (self -> wrapped );
266270 Py_XDECREF (self -> buffer );
267271 Py_XDECREF (self -> conn );
268-
269- Dprintf ("binary_dealloc: deleted binary object at %p, refcnt = %d" ,
270- obj , obj -> ob_refcnt );
271-
272+
273+ Dprintf ("binary_dealloc: deleted binary object at %p, refcnt = "
274+ FORMAT_CODE_PY_SSIZE_T ,
275+ obj , obj -> ob_refcnt
276+ );
277+
272278 obj -> ob_type -> tp_free (obj );
273279}
274280
275281static int
276282binary_init (PyObject * obj , PyObject * args , PyObject * kwds )
277283{
278284 PyObject * str ;
279-
285+
280286 if (!PyArg_ParseTuple (args , "O" , & str ))
281287 return -1 ;
282288
@@ -285,7 +291,7 @@ binary_init(PyObject *obj, PyObject *args, PyObject *kwds)
285291
286292static PyObject *
287293binary_new (PyTypeObject * type , PyObject * args , PyObject * kwds )
288- {
294+ {
289295 return type -> tp_alloc (type , 0 );
290296}
291297
@@ -315,7 +321,7 @@ PyTypeObject binaryType = {
315321 binary_dealloc , /*tp_dealloc*/
316322 0 , /*tp_print*/
317323 0 , /*tp_getattr*/
318- 0 , /*tp_setattr*/
324+ 0 , /*tp_setattr*/
319325
320326 0 , /*tp_compare*/
321327 (reprfunc )binary_repr , /*tp_repr*/
@@ -333,7 +339,7 @@ PyTypeObject binaryType = {
333339 Py_TPFLAGS_DEFAULT |Py_TPFLAGS_BASETYPE , /*tp_flags*/
334340
335341 binaryType_doc , /*tp_doc*/
336-
342+
337343 0 , /*tp_traverse*/
338344 0 , /*tp_clear*/
339345
@@ -350,11 +356,11 @@ PyTypeObject binaryType = {
350356 0 , /*tp_getset*/
351357 0 , /*tp_base*/
352358 0 , /*tp_dict*/
353-
359+
354360 0 , /*tp_descr_get*/
355361 0 , /*tp_descr_set*/
356362 0 , /*tp_dictoffset*/
357-
363+
358364 binary_init , /*tp_init*/
359365 0 , /*tp_alloc will be set to PyType_GenericAlloc in module init*/
360366 binary_new , /*tp_new*/
@@ -374,9 +380,9 @@ PyObject *
374380psyco_Binary (PyObject * module , PyObject * args )
375381{
376382 PyObject * str ;
377-
383+
378384 if (!PyArg_ParseTuple (args , "O" , & str ))
379385 return NULL ;
380-
386+
381387 return PyObject_CallFunction ((PyObject * )& binaryType , "O" , str );
382388}
0 commit comments