Python use Py_ssize_t instead of int for better portability

This commit is contained in:
William S Fulton 2015-12-04 23:32:46 +00:00
commit c5322a9ecb
9 changed files with 50 additions and 63 deletions

View file

@ -119,10 +119,9 @@
static PyObject *asdict(const map_type& map) {
SWIG_PYTHON_THREAD_BEGIN_BLOCK;
size_type size = map.size();
int pysize = (size <= (size_type) INT_MAX) ? (int) size : -1;
Py_ssize_t pysize = (size <= (size_type) INT_MAX) ? (Py_ssize_t) size : -1;
if (pysize < 0) {
PyErr_SetString(PyExc_OverflowError,
"map size not valid in python");
PyErr_SetString(PyExc_OverflowError, "map size not valid in python");
SWIG_PYTHON_THREAD_END_BLOCK;
return NULL;
}
@ -211,17 +210,16 @@
PyObject* keys() {
Map::size_type size = self->size();
int pysize = (size <= (Map::size_type) INT_MAX) ? (int) size : -1;
Py_ssize_t pysize = (size <= (Map::size_type) INT_MAX) ? (Py_ssize_t) size : -1;
SWIG_PYTHON_THREAD_BEGIN_BLOCK;
if (pysize < 0) {
PyErr_SetString(PyExc_OverflowError,
"map size not valid in python");
PyErr_SetString(PyExc_OverflowError, "map size not valid in python");
SWIG_PYTHON_THREAD_END_BLOCK;
return NULL;
}
PyObject* keyList = PyList_New(pysize);
Map::const_iterator i = self->begin();
for (int j = 0; j < pysize; ++i, ++j) {
for (Py_ssize_t j = 0; j < pysize; ++i, ++j) {
PyList_SET_ITEM(keyList, j, swig::from(i->first));
}
SWIG_PYTHON_THREAD_END_BLOCK;
@ -230,17 +228,16 @@
PyObject* values() {
Map::size_type size = self->size();
int pysize = (size <= (Map::size_type) INT_MAX) ? (int) size : -1;
Py_ssize_t pysize = (size <= (Map::size_type) INT_MAX) ? (Py_ssize_t) size : -1;
SWIG_PYTHON_THREAD_BEGIN_BLOCK;
if (pysize < 0) {
PyErr_SetString(PyExc_OverflowError,
"map size not valid in python");
PyErr_SetString(PyExc_OverflowError, "map size not valid in python");
SWIG_PYTHON_THREAD_END_BLOCK;
return NULL;
}
PyObject* valList = PyList_New(pysize);
Map::const_iterator i = self->begin();
for (int j = 0; j < pysize; ++i, ++j) {
for (Py_ssize_t j = 0; j < pysize; ++i, ++j) {
PyList_SET_ITEM(valList, j, swig::from(i->second));
}
SWIG_PYTHON_THREAD_END_BLOCK;
@ -249,17 +246,16 @@
PyObject* items() {
Map::size_type size = self->size();
int pysize = (size <= (Map::size_type) INT_MAX) ? (int) size : -1;
Py_ssize_t pysize = (size <= (Map::size_type) INT_MAX) ? (Py_ssize_t) size : -1;
SWIG_PYTHON_THREAD_BEGIN_BLOCK;
if (pysize < 0) {
PyErr_SetString(PyExc_OverflowError,
"map size not valid in python");
PyErr_SetString(PyExc_OverflowError, "map size not valid in python");
SWIG_PYTHON_THREAD_END_BLOCK;
return NULL;
}
PyObject* itemList = PyList_New(pysize);
Map::const_iterator i = self->begin();
for (int j = 0; j < pysize; ++i, ++j) {
for (Py_ssize_t j = 0; j < pysize; ++i, ++j) {
PyList_SET_ITEM(itemList, j, swig::from(*i));
}
SWIG_PYTHON_THREAD_END_BLOCK;