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

Skip to content

Commit 7b9fb92

Browse files
committed
Fix the size() method to return the size of the file on Unix, not the
size of the mapped area. This seems to be what the Windows version does. This change requires keeping around the fd of the mapped file.
1 parent b805069 commit 7b9fb92

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

Modules/mmapmodule.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#ifdef UNIX
3030
#include <unistd.h>
3131
#include <sys/mman.h>
32+
#include <sys/stat.h>
3233
#endif
3334

3435
#include <string.h>
@@ -49,7 +50,7 @@ typedef struct {
4950
#endif
5051

5152
#ifdef UNIX
52-
/* No Unix-specific information at this point in time */
53+
int fd;
5354
#endif
5455
} mmap_object;
5556

@@ -210,7 +211,7 @@ mmap_find_method (mmap_object *self,
210211

211212
static PyObject *
212213
mmap_write_method (mmap_object * self,
213-
PyObject * args)
214+
PyObject * args)
214215
{
215216
long length;
216217
char * data;
@@ -264,7 +265,14 @@ mmap_size_method (mmap_object * self,
264265
#endif /* MS_WIN32 */
265266

266267
#ifdef UNIX
267-
return (Py_BuildValue ("l", self->size) );
268+
{
269+
struct stat buf;
270+
if (-1 == fstat(self->fd, &buf)) {
271+
PyErr_SetFromErrno(mmap_module_error);
272+
return NULL;
273+
}
274+
return (Py_BuildValue ("l", buf.st_size) );
275+
}
268276
#endif /* UNIX */
269277
}
270278

@@ -717,6 +725,7 @@ new_mmap_object (PyObject * self, PyObject * args, PyObject *kwdict)
717725
if (m_obj == NULL) {return NULL;}
718726
m_obj->size = (size_t) map_size;
719727
m_obj->pos = (size_t) 0;
728+
m_obj->fd = fd;
720729
m_obj->data = mmap(NULL, map_size,
721730
prot, flags,
722731
fd, 0);

0 commit comments

Comments
 (0)