Kaydet (Commit) cfe7e091 authored tarafından Martin v. Löwis's avatar Martin v. Löwis

Remove size restrictions.

üst f2882957
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
/ ftp://squirl.nightmare.com/pub/python/python-ext. / ftp://squirl.nightmare.com/pub/python/python-ext.
*/ */
#define PY_SSIZE_T_CLEAN
#include <Python.h> #include <Python.h>
#ifndef MS_WINDOWS #ifndef MS_WINDOWS
...@@ -242,7 +243,7 @@ mmap_find_method(mmap_object *self, ...@@ -242,7 +243,7 @@ mmap_find_method(mmap_object *self,
{ {
Py_ssize_t start = self->pos; Py_ssize_t start = self->pos;
char *needle; char *needle;
int len; Py_ssize_t len;
CHECK_VALID(NULL); CHECK_VALID(NULL);
if (!PyArg_ParseTuple(args, "s#|n:find", &needle, &len, &start)) { if (!PyArg_ParseTuple(args, "s#|n:find", &needle, &len, &start)) {
...@@ -259,16 +260,14 @@ mmap_find_method(mmap_object *self, ...@@ -259,16 +260,14 @@ mmap_find_method(mmap_object *self,
start = self->size; start = self->size;
for (p = self->data + start; p + len <= e; ++p) { for (p = self->data + start; p + len <= e; ++p) {
int i; Py_ssize_t i;
for (i = 0; i < len && needle[i] == p[i]; ++i) for (i = 0; i < len && needle[i] == p[i]; ++i)
/* nothing */; /* nothing */;
if (i == len) { if (i == len) {
return Py_BuildValue( return PyInt_FromSsize_t(p - self->data);
"l",
(long) (p - self->data));
} }
} }
return Py_BuildValue("l", (long) -1); return PyInt_FromLong(-1);
} }
} }
...@@ -296,7 +295,7 @@ static PyObject * ...@@ -296,7 +295,7 @@ static PyObject *
mmap_write_method(mmap_object *self, mmap_write_method(mmap_object *self,
PyObject *args) PyObject *args)
{ {
int length; Py_ssize_t length;
char *data; char *data;
CHECK_VALID(NULL); CHECK_VALID(NULL);
...@@ -377,9 +376,9 @@ static PyObject * ...@@ -377,9 +376,9 @@ static PyObject *
mmap_resize_method(mmap_object *self, mmap_resize_method(mmap_object *self,
PyObject *args) PyObject *args)
{ {
unsigned long new_size; Py_ssize_t new_size;
CHECK_VALID(NULL); CHECK_VALID(NULL);
if (!PyArg_ParseTuple(args, "k:resize", &new_size) || if (!PyArg_ParseTuple(args, "n:resize", &new_size) ||
!is_resizeable(self)) { !is_resizeable(self)) {
return NULL; return NULL;
#ifdef MS_WINDOWS #ifdef MS_WINDOWS
...@@ -498,10 +497,10 @@ mmap_flush_method(mmap_object *self, PyObject *args) ...@@ -498,10 +497,10 @@ mmap_flush_method(mmap_object *self, PyObject *args)
static PyObject * static PyObject *
mmap_seek_method(mmap_object *self, PyObject *args) mmap_seek_method(mmap_object *self, PyObject *args)
{ {
int dist; Py_ssize_t dist;
int how=0; int how=0;
CHECK_VALID(NULL); CHECK_VALID(NULL);
if (!PyArg_ParseTuple(args, "i|i:seek", &dist, &how)) if (!PyArg_ParseTuple(args, "n|i:seek", &dist, &how))
return NULL; return NULL;
else { else {
size_t where; size_t where;
...@@ -512,12 +511,12 @@ mmap_seek_method(mmap_object *self, PyObject *args) ...@@ -512,12 +511,12 @@ mmap_seek_method(mmap_object *self, PyObject *args)
where = dist; where = dist;
break; break;
case 1: /* relative to current position */ case 1: /* relative to current position */
if ((int)self->pos + dist < 0) if ((Py_ssize_t)self->pos + dist < 0)
goto onoutofrange; goto onoutofrange;
where = self->pos + dist; where = self->pos + dist;
break; break;
case 2: /* relative to end */ case 2: /* relative to end */
if ((int)self->size + dist < 0) if ((Py_ssize_t)self->size + dist < 0)
goto onoutofrange; goto onoutofrange;
where = self->size + dist; where = self->size + dist;
break; break;
...@@ -802,14 +801,9 @@ static PyTypeObject mmap_object_type = { ...@@ -802,14 +801,9 @@ static PyTypeObject mmap_object_type = {
/* extract the map size from the given PyObject /* extract the map size from the given PyObject
The map size is restricted to [0, INT_MAX] because this is the current
Python limitation on object sizes. Although the mmap object *could* handle
a larger map size, there is no point because all the useful operations
(len(), slicing(), sequence indexing) are limited by a C int.
Returns -1 on error, with an appropriate Python exception raised. On Returns -1 on error, with an appropriate Python exception raised. On
success, the map size is returned. */ success, the map size is returned. */
static int static Py_ssize_t
_GetMapSize(PyObject *o) _GetMapSize(PyObject *o)
{ {
if (PyInt_Check(o)) { if (PyInt_Check(o)) {
...@@ -818,12 +812,10 @@ _GetMapSize(PyObject *o) ...@@ -818,12 +812,10 @@ _GetMapSize(PyObject *o)
return -1; return -1;
if (i < 0) if (i < 0)
goto onnegoverflow; goto onnegoverflow;
if (i > INT_MAX) return i;
goto onposoverflow;
return (int)i;
} }
else if (PyLong_Check(o)) { else if (PyLong_Check(o)) {
long i = PyLong_AsLong(o); Py_ssize_t i = PyInt_AsSsize_t(o);
if (PyErr_Occurred()) { if (PyErr_Occurred()) {
/* yes negative overflow is mistaken for positive overflow /* yes negative overflow is mistaken for positive overflow
but not worth the trouble to check sign of 'i' */ but not worth the trouble to check sign of 'i' */
...@@ -834,9 +826,7 @@ _GetMapSize(PyObject *o) ...@@ -834,9 +826,7 @@ _GetMapSize(PyObject *o)
} }
if (i < 0) if (i < 0)
goto onnegoverflow; goto onnegoverflow;
if (i > INT_MAX) return i;
goto onposoverflow;
return (int)i;
} }
else { else {
PyErr_SetString(PyExc_TypeError, PyErr_SetString(PyExc_TypeError,
...@@ -864,7 +854,7 @@ new_mmap_object(PyObject *self, PyObject *args, PyObject *kwdict) ...@@ -864,7 +854,7 @@ new_mmap_object(PyObject *self, PyObject *args, PyObject *kwdict)
#endif #endif
mmap_object *m_obj; mmap_object *m_obj;
PyObject *map_size_obj = NULL; PyObject *map_size_obj = NULL;
int map_size; Py_ssize_t map_size;
int fd, flags = MAP_SHARED, prot = PROT_WRITE | PROT_READ; int fd, flags = MAP_SHARED, prot = PROT_WRITE | PROT_READ;
int devzero = -1; int devzero = -1;
int access = (int)ACCESS_DEFAULT; int access = (int)ACCESS_DEFAULT;
...@@ -912,7 +902,7 @@ new_mmap_object(PyObject *self, PyObject *args, PyObject *kwdict) ...@@ -912,7 +902,7 @@ new_mmap_object(PyObject *self, PyObject *args, PyObject *kwdict)
# endif # endif
if (fstat(fd, &st) == 0 && S_ISREG(st.st_mode)) { if (fstat(fd, &st) == 0 && S_ISREG(st.st_mode)) {
if (map_size == 0) { if (map_size == 0) {
map_size = (int)st.st_size; map_size = st.st_size;
} else if ((size_t)map_size > st.st_size) { } else if ((size_t)map_size > st.st_size) {
PyErr_SetString(PyExc_ValueError, PyErr_SetString(PyExc_ValueError,
"mmap length is greater than file size"); "mmap length is greater than file size");
...@@ -977,7 +967,7 @@ new_mmap_object(PyObject *self, PyObject *args, PyObject *kwdict) ...@@ -977,7 +967,7 @@ new_mmap_object(PyObject *self, PyObject *args, PyObject *kwdict)
{ {
mmap_object *m_obj; mmap_object *m_obj;
PyObject *map_size_obj = NULL; PyObject *map_size_obj = NULL;
int map_size; Py_ssize_t map_size;
DWORD size_hi; /* upper 32 bits of m_obj->size */ DWORD size_hi; /* upper 32 bits of m_obj->size */
DWORD size_lo; /* lower 32 bits of m_obj->size */ DWORD size_lo; /* lower 32 bits of m_obj->size */
char *tagname = ""; char *tagname = "";
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment