Use Py_ssize_t as much as possible.

This fixes issues with x86_64 builds, which would cause very strange
errors to occur.
This commit is contained in:
lericson 2009-09-07 23:55:37 +02:00
parent 384ef86b9a
commit f567323f04
2 changed files with 18 additions and 14 deletions

View File

@ -187,7 +187,7 @@ static PyObject *_PylibMC_RunSetCommand(PylibMC_Client *self,
_PylibMC_SetCommand f, char *fname, PyObject *args, _PylibMC_SetCommand f, char *fname, PyObject *args,
PyObject *kwds) { PyObject *kwds) {
char *key; char *key;
size_t key_sz; Py_ssize_t key_sz;
memcached_return rc; memcached_return rc;
PyObject *val, *tmp; PyObject *val, *tmp;
PyObject *retval = NULL; PyObject *retval = NULL;
@ -294,15 +294,15 @@ static PyObject *PylibMC_Client_append(PylibMC_Client *self, PyObject *args,
static PyObject *PylibMC_Client_delete(PylibMC_Client *self, PyObject *args) { static PyObject *PylibMC_Client_delete(PylibMC_Client *self, PyObject *args) {
PyObject *retval; PyObject *retval;
char *key; char *key;
int key_len; Py_ssize_t key_sz;
unsigned int time; unsigned int time;
memcached_return rc; memcached_return rc;
retval = NULL; retval = NULL;
time = 0; time = 0;
if (PyArg_ParseTuple(args, "s#|I", &key, &key_len, &time) if (PyArg_ParseTuple(args, "s#|I", &key, &key_sz, &time)
&& _PylibMC_CheckKeyStringAndSize(key, key_len)) { && _PylibMC_CheckKeyStringAndSize(key, key_sz)) {
switch (rc = memcached_delete(self->mc, key, key_len, time)) { switch (rc = memcached_delete(self->mc, key, key_sz, time)) {
case MEMCACHED_SUCCESS: case MEMCACHED_SUCCESS:
Py_RETURN_TRUE; Py_RETURN_TRUE;
break; break;
@ -325,19 +325,19 @@ static PyObject *_PylibMC_IncDec(PylibMC_Client *self, uint8_t dir,
PyObject *args) { PyObject *args) {
PyObject *retval; PyObject *retval;
char *key; char *key;
int key_len; Py_ssize_t key_sz;
unsigned int delta; unsigned int delta;
uint64_t result; uint64_t result;
retval = NULL; retval = NULL;
delta = 1; delta = 1;
if (PyArg_ParseTuple(args, "s#|I", &key, &key_len, &delta) if (PyArg_ParseTuple(args, "s#|I", &key, &key_sz, &delta)
&& _PylibMC_CheckKeyStringAndSize(key, key_len)) { && _PylibMC_CheckKeyStringAndSize(key, key_sz)) {
memcached_return (*incdec)(memcached_st *, const char *, size_t, memcached_return (*incdec)(memcached_st *, const char *, size_t,
unsigned int, uint64_t *); unsigned int, uint64_t *);
incdec = (dir == PYLIBMC_INC) ? memcached_increment incdec = (dir == PYLIBMC_INC) ? memcached_increment
: memcached_decrement; : memcached_decrement;
incdec(self->mc, key, key_len, delta, &result); incdec(self->mc, key, key_sz, delta, &result);
retval = PyLong_FromUnsignedLong((unsigned long)result); retval = PyLong_FromUnsignedLong((unsigned long)result);
} }
@ -357,7 +357,7 @@ static PyObject *PylibMC_Client_get_multi(PylibMC_Client *self, PyObject *args,
PyObject *kwds) { PyObject *kwds) {
PyObject *key_seq, **key_objs, *retval = NULL; PyObject *key_seq, **key_objs, *retval = NULL;
char **keys, *prefix = NULL; char **keys, *prefix = NULL;
unsigned int prefix_len = 0; Py_ssize_t prefix_len = 0;
Py_ssize_t i; Py_ssize_t i;
PyObject *key_it, *ckey; PyObject *key_it, *ckey;
size_t *key_lens; size_t *key_lens;
@ -376,7 +376,7 @@ static PyObject *PylibMC_Client_get_multi(PylibMC_Client *self, PyObject *args,
return NULL; return NULL;
} }
if ((nkeys = PySequence_Length(key_seq)) == -1) { if ((nkeys = (size_t)PySequence_Length(key_seq)) == -1) {
return NULL; return NULL;
} }
@ -406,7 +406,7 @@ static PyObject *PylibMC_Client_get_multi(PylibMC_Client *self, PyObject *args,
if (!_PylibMC_CheckKey(ckey)) { if (!_PylibMC_CheckKey(ckey)) {
break; break;
} else { } else {
key_lens[i] = PyString_GET_SIZE(ckey) + prefix_len; key_lens[i] = (size_t)(PyString_GET_SIZE(ckey) + prefix_len);
if (prefix != NULL) { if (prefix != NULL) {
rkey = PyString_FromFormat("%s%s", rkey = PyString_FromFormat("%s%s",
prefix, PyString_AS_STRING(ckey)); prefix, PyString_AS_STRING(ckey));
@ -815,7 +815,7 @@ static int _PylibMC_CheckKey(PyObject *key) {
PyString_AS_STRING(key), PyString_GET_SIZE(key)); PyString_AS_STRING(key), PyString_GET_SIZE(key));
} }
static int _PylibMC_CheckKeyStringAndSize(char *key, int size) { static int _PylibMC_CheckKeyStringAndSize(char *key, Py_ssize_t size) {
if (size > MEMCACHED_MAX_KEY) { if (size > MEMCACHED_MAX_KEY) {
PyErr_Format(PyExc_ValueError, "key too long, max is %d", PyErr_Format(PyExc_ValueError, "key too long, max is %d",
MEMCACHED_MAX_KEY); MEMCACHED_MAX_KEY);

View File

@ -34,6 +34,10 @@
#ifndef __PYLIBMC_H__ #ifndef __PYLIBMC_H__
#define __PYLIBMC_H__ #define __PYLIBMC_H__
/* This makes the "s#" format for PyArg_ParseTuple and such take a Py_ssize_t
* instead of an int or whatever. */
#define PY_SSIZE_T_CLEAN
#include <Python.h> #include <Python.h>
#include <libmemcached/memcached.h> #include <libmemcached/memcached.h>
@ -143,7 +147,7 @@ static PyObject *PylibMC_ErrFromMemcached(PylibMC_Client *, const char *,
static PyObject *_PylibMC_Unpickle(const char *, size_t); static PyObject *_PylibMC_Unpickle(const char *, size_t);
static PyObject *_PylibMC_Pickle(PyObject *); static PyObject *_PylibMC_Pickle(PyObject *);
static int _PylibMC_CheckKey(PyObject *); static int _PylibMC_CheckKey(PyObject *);
static int _PylibMC_CheckKeyStringAndSize(char *, int); static int _PylibMC_CheckKeyStringAndSize(char *, Py_ssize_t);
/* }}} */ /* }}} */
/* {{{ Type's method table */ /* {{{ Type's method table */