diff options
Diffstat (limited to 'Modules/mmapmodule.c')
-rw-r--r-- | Modules/mmapmodule.c | 62 |
1 files changed, 32 insertions, 30 deletions
diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c index 1371424..c5ba4b7 100644 --- a/Modules/mmapmodule.c +++ b/Modules/mmapmodule.c @@ -301,16 +301,17 @@ mmap_gfind(mmap_object *self, { Py_ssize_t start = self->pos; Py_ssize_t end = self->size; - const char *needle; - Py_ssize_t len; + Py_buffer view; CHECK_VALID(NULL); - if (!PyArg_ParseTuple(args, reverse ? "y#|nn:rfind" : "y#|nn:find", - &needle, &len, &start, &end)) { + if (!PyArg_ParseTuple(args, reverse ? "y*|nn:rfind" : "y*|nn:find", + &view, &start, &end)) { return NULL; } else { const char *p, *start_p, *end_p; int sign = reverse ? -1 : 1; + const char *needle = view.buf; + Py_ssize_t len = view.len; if (start < 0) start += self->size; @@ -335,9 +336,11 @@ mmap_gfind(mmap_object *self, for (i = 0; i < len && needle[i] == p[i]; ++i) /* nothing */; if (i == len) { + PyBuffer_Release(&view); return PyLong_FromSsize_t(p - self->data); } } + PyBuffer_Release(&view); return PyLong_FromLong(-1); } } @@ -385,22 +388,25 @@ static PyObject * mmap_write_method(mmap_object *self, PyObject *args) { - Py_ssize_t length; - char *data; + Py_buffer data; CHECK_VALID(NULL); - if (!PyArg_ParseTuple(args, "y#:write", &data, &length)) + if (!PyArg_ParseTuple(args, "y*:write", &data)) return(NULL); - if (!is_writable(self)) + if (!is_writable(self)) { + PyBuffer_Release(&data); return NULL; + } - if ((self->pos + length) > self->size) { + if ((self->pos + data.len) > self->size) { PyErr_SetString(PyExc_ValueError, "data out of range"); + PyBuffer_Release(&data); return NULL; } - memcpy(self->data+self->pos, data, length); - self->pos = self->pos+length; + memcpy(self->data + self->pos, data.buf, data.len); + self->pos = self->pos + data.len; + PyBuffer_Release(&data); Py_INCREF(Py_None); return Py_None; } @@ -459,15 +465,13 @@ mmap_size_method(mmap_object *self, #ifdef UNIX { - struct stat buf; - if (-1 == fstat(self->fd, &buf)) { - PyErr_SetFromErrno(PyExc_OSError); + struct _Py_stat_struct status; + if (_Py_fstat(self->fd, &status) == -1) return NULL; - } #ifdef HAVE_LARGEFILE_SUPPORT - return PyLong_FromLongLong(buf.st_size); + return PyLong_FromLongLong(status.st_size); #else - return PyLong_FromLong(buf.st_size); + return PyLong_FromLong(status.st_size); #endif } #endif /* UNIX */ @@ -1106,9 +1110,7 @@ _GetMapSize(PyObject *o, const char* param) static PyObject * new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict) { -#ifdef HAVE_FSTAT - struct stat st; -#endif + struct _Py_stat_struct status; mmap_object *m_obj; PyObject *map_size_obj = NULL; Py_ssize_t map_size; @@ -1173,32 +1175,31 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict) if (fd != -1) (void)fcntl(fd, F_FULLFSYNC); #endif -#ifdef HAVE_FSTAT - if (fd != -1 && fstat(fd, &st) == 0 && S_ISREG(st.st_mode)) { + if (fd != -1 && _Py_fstat_noraise(fd, &status) == 0 + && S_ISREG(status.st_mode)) { if (map_size == 0) { - if (st.st_size == 0) { + if (status.st_size == 0) { PyErr_SetString(PyExc_ValueError, "cannot mmap an empty file"); return NULL; } - if (offset >= st.st_size) { + if (offset >= status.st_size) { PyErr_SetString(PyExc_ValueError, "mmap offset is greater than file size"); return NULL; } - if (st.st_size - offset > PY_SSIZE_T_MAX) { + if (status.st_size - offset > PY_SSIZE_T_MAX) { PyErr_SetString(PyExc_ValueError, "mmap length is too large"); return NULL; } - map_size = (Py_ssize_t) (st.st_size - offset); - } else if (offset + (size_t)map_size > st.st_size) { + map_size = (Py_ssize_t) (status.st_size - offset); + } else if (offset + map_size > status.st_size) { PyErr_SetString(PyExc_ValueError, "mmap length is greater than file size"); return NULL; } } -#endif m_obj = (mmap_object *)type->tp_alloc(type, 0); if (m_obj == NULL) {return NULL;} m_obj->data = NULL; @@ -1221,7 +1222,6 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict) fd = devzero = _Py_open("/dev/zero", O_RDWR); if (devzero == -1) { Py_DECREF(m_obj); - PyErr_SetFromErrno(PyExc_OSError); return NULL; } #endif @@ -1325,11 +1325,13 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict) */ if (fileno != -1 && fileno != 0) { /* Ensure that fileno is within the CRT's valid range */ - if (_PyVerify_fd(fileno) == 0) { + if (!_PyVerify_fd(fileno)) { PyErr_SetFromErrno(PyExc_OSError); return NULL; } + _Py_BEGIN_SUPPRESS_IPH fh = (HANDLE)_get_osfhandle(fileno); + _Py_END_SUPPRESS_IPH if (fh==(HANDLE)-1) { PyErr_SetFromErrno(PyExc_OSError); return NULL; |