Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cfe7e09

Browse files
committed
Remove size restrictions.
1 parent f288295 commit cfe7e09

1 file changed

Lines changed: 19 additions & 29 deletions

File tree

Modules/mmapmodule.c

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
/ ftp://squirl.nightmare.com/pub/python/python-ext.
1919
*/
2020

21+
#define PY_SSIZE_T_CLEAN
2122
#include <Python.h>
2223

2324
#ifndef MS_WINDOWS
@@ -242,7 +243,7 @@ mmap_find_method(mmap_object *self,
242243
{
243244
Py_ssize_t start = self->pos;
244245
char *needle;
245-
int len;
246+
Py_ssize_t len;
246247

247248
CHECK_VALID(NULL);
248249
if (!PyArg_ParseTuple(args, "s#|n:find", &needle, &len, &start)) {
@@ -259,16 +260,14 @@ mmap_find_method(mmap_object *self,
259260
start = self->size;
260261

261262
for (p = self->data + start; p + len <= e; ++p) {
262-
int i;
263+
Py_ssize_t i;
263264
for (i = 0; i < len && needle[i] == p[i]; ++i)
264265
/* nothing */;
265266
if (i == len) {
266-
return Py_BuildValue(
267-
"l",
268-
(long) (p - self->data));
267+
return PyInt_FromSsize_t(p - self->data);
269268
}
270269
}
271-
return Py_BuildValue("l", (long) -1);
270+
return PyInt_FromLong(-1);
272271
}
273272
}
274273

@@ -296,7 +295,7 @@ static PyObject *
296295
mmap_write_method(mmap_object *self,
297296
PyObject *args)
298297
{
299-
int length;
298+
Py_ssize_t length;
300299
char *data;
301300

302301
CHECK_VALID(NULL);
@@ -377,9 +376,9 @@ static PyObject *
377376
mmap_resize_method(mmap_object *self,
378377
PyObject *args)
379378
{
380-
unsigned long new_size;
379+
Py_ssize_t new_size;
381380
CHECK_VALID(NULL);
382-
if (!PyArg_ParseTuple(args, "k:resize", &new_size) ||
381+
if (!PyArg_ParseTuple(args, "n:resize", &new_size) ||
383382
!is_resizeable(self)) {
384383
return NULL;
385384
#ifdef MS_WINDOWS
@@ -498,10 +497,10 @@ mmap_flush_method(mmap_object *self, PyObject *args)
498497
static PyObject *
499498
mmap_seek_method(mmap_object *self, PyObject *args)
500499
{
501-
int dist;
500+
Py_ssize_t dist;
502501
int how=0;
503502
CHECK_VALID(NULL);
504-
if (!PyArg_ParseTuple(args, "i|i:seek", &dist, &how))
503+
if (!PyArg_ParseTuple(args, "n|i:seek", &dist, &how))
505504
return NULL;
506505
else {
507506
size_t where;
@@ -512,12 +511,12 @@ mmap_seek_method(mmap_object *self, PyObject *args)
512511
where = dist;
513512
break;
514513
case 1: /* relative to current position */
515-
if ((int)self->pos + dist < 0)
514+
if ((Py_ssize_t)self->pos + dist < 0)
516515
goto onoutofrange;
517516
where = self->pos + dist;
518517
break;
519518
case 2: /* relative to end */
520-
if ((int)self->size + dist < 0)
519+
if ((Py_ssize_t)self->size + dist < 0)
521520
goto onoutofrange;
522521
where = self->size + dist;
523522
break;
@@ -802,14 +801,9 @@ static PyTypeObject mmap_object_type = {
802801

803802
/* extract the map size from the given PyObject
804803
805-
The map size is restricted to [0, INT_MAX] because this is the current
806-
Python limitation on object sizes. Although the mmap object *could* handle
807-
a larger map size, there is no point because all the useful operations
808-
(len(), slicing(), sequence indexing) are limited by a C int.
809-
810804
Returns -1 on error, with an appropriate Python exception raised. On
811805
success, the map size is returned. */
812-
static int
806+
static Py_ssize_t
813807
_GetMapSize(PyObject *o)
814808
{
815809
if (PyInt_Check(o)) {
@@ -818,12 +812,10 @@ _GetMapSize(PyObject *o)
818812
return -1;
819813
if (i < 0)
820814
goto onnegoverflow;
821-
if (i > INT_MAX)
822-
goto onposoverflow;
823-
return (int)i;
815+
return i;
824816
}
825817
else if (PyLong_Check(o)) {
826-
long i = PyLong_AsLong(o);
818+
Py_ssize_t i = PyInt_AsSsize_t(o);
827819
if (PyErr_Occurred()) {
828820
/* yes negative overflow is mistaken for positive overflow
829821
but not worth the trouble to check sign of 'i' */
@@ -834,9 +826,7 @@ _GetMapSize(PyObject *o)
834826
}
835827
if (i < 0)
836828
goto onnegoverflow;
837-
if (i > INT_MAX)
838-
goto onposoverflow;
839-
return (int)i;
829+
return i;
840830
}
841831
else {
842832
PyErr_SetString(PyExc_TypeError,
@@ -864,7 +854,7 @@ new_mmap_object(PyObject *self, PyObject *args, PyObject *kwdict)
864854
#endif
865855
mmap_object *m_obj;
866856
PyObject *map_size_obj = NULL;
867-
int map_size;
857+
Py_ssize_t map_size;
868858
int fd, flags = MAP_SHARED, prot = PROT_WRITE | PROT_READ;
869859
int devzero = -1;
870860
int access = (int)ACCESS_DEFAULT;
@@ -912,7 +902,7 @@ new_mmap_object(PyObject *self, PyObject *args, PyObject *kwdict)
912902
# endif
913903
if (fstat(fd, &st) == 0 && S_ISREG(st.st_mode)) {
914904
if (map_size == 0) {
915-
map_size = (int)st.st_size;
905+
map_size = st.st_size;
916906
} else if ((size_t)map_size > st.st_size) {
917907
PyErr_SetString(PyExc_ValueError,
918908
"mmap length is greater than file size");
@@ -977,7 +967,7 @@ new_mmap_object(PyObject *self, PyObject *args, PyObject *kwdict)
977967
{
978968
mmap_object *m_obj;
979969
PyObject *map_size_obj = NULL;
980-
int map_size;
970+
Py_ssize_t map_size;
981971
DWORD size_hi; /* upper 32 bits of m_obj->size */
982972
DWORD size_lo; /* lower 32 bits of m_obj->size */
983973
char *tagname = "";

0 commit comments

Comments
 (0)