summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2013-08-05 21:17:30 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2013-08-05 21:17:30 (GMT)
commitc53204b9477a2e28b6b366fd271f61c73b805cee (patch)
treea58457d2a8f68a43ae5ce33adc3da41944b7a547
parent914061ab151a7dd25e9b05091eabbfd2690ee438 (diff)
downloadcpython-c53204b9477a2e28b6b366fd271f61c73b805cee.zip
cpython-c53204b9477a2e28b6b366fd271f61c73b805cee.tar.gz
cpython-c53204b9477a2e28b6b366fd271f61c73b805cee.tar.bz2
Issue #4885: Add weakref support to mmap objects. Patch by Valerie Lambert.
-rw-r--r--Lib/test/test_mmap.py12
-rw-r--r--Misc/ACKS1
-rw-r--r--Misc/NEWS5
-rw-r--r--Modules/mmapmodule.c8
4 files changed, 23 insertions, 3 deletions
diff --git a/Lib/test/test_mmap.py b/Lib/test/test_mmap.py
index 505ffba..b1cc973 100644
--- a/Lib/test/test_mmap.py
+++ b/Lib/test/test_mmap.py
@@ -1,11 +1,12 @@
from test.support import (TESTFN, run_unittest, import_module, unlink,
- requires, _2G, _4G)
+ requires, _2G, _4G, gc_collect)
import unittest
import os
import re
import itertools
import socket
import sys
+import weakref
# Skip test if we can't import mmap.
mmap = import_module('mmap')
@@ -692,6 +693,15 @@ class MmapTests(unittest.TestCase):
"wrong exception raised in context manager")
self.assertTrue(m.closed, "context manager failed")
+ def test_weakref(self):
+ # Check mmap objects are weakrefable
+ mm = mmap.mmap(-1, 16)
+ wr = weakref.ref(mm)
+ self.assertIs(wr(), mm)
+ del mm
+ gc_collect()
+ self.assertIs(wr(), None)
+
class LargeMmapTests(unittest.TestCase):
def setUp(self):
diff --git a/Misc/ACKS b/Misc/ACKS
index 69b1074..6d7fcfc 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -701,6 +701,7 @@ Ross Lagerwall
Cameron Laird
David Lam
Thomas Lamb
+Valerie Lambert
Jean-Baptiste "Jiba" Lamy
Ronan Lamy
Torsten Landschoff
diff --git a/Misc/NEWS b/Misc/NEWS
index 04bd92f..9f2c2dd 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -13,7 +13,10 @@ Core and Builtins
Library
-------
-- Issue 8860: Fixed rounding in timedelta constructor.
+- Issue #4885: Add weakref support to mmap objects. Patch by Valerie Lambert.
+
+- Issue #8860: Fixed rounding in timedelta constructor.
+
What's New in Python 3.4.0 Alpha 1?
===================================
diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c
index 1107eb8..4b4be61 100644
--- a/Modules/mmapmodule.c
+++ b/Modules/mmapmodule.c
@@ -20,6 +20,7 @@
#define PY_SSIZE_T_CLEAN
#include <Python.h>
+#include "structmember.h"
#ifndef MS_WINDOWS
#define UNIX
@@ -108,6 +109,7 @@ typedef struct {
int fd;
#endif
+ PyObject *weakreflist;
access_mode access;
} mmap_object;
@@ -134,6 +136,8 @@ mmap_object_dealloc(mmap_object *m_obj)
}
#endif /* UNIX */
+ if (m_obj->weakreflist != NULL)
+ PyObject_ClearWeakRefs((PyObject *) m_obj);
Py_TYPE(m_obj)->tp_free((PyObject*)m_obj);
}
@@ -1032,7 +1036,7 @@ static PyTypeObject mmap_object_type = {
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
+ offsetof(mmap_object, weakreflist), /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
mmap_object_methods, /* tp_methods */
@@ -1190,6 +1194,7 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
m_obj->data = NULL;
m_obj->size = (size_t) map_size;
m_obj->pos = (size_t) 0;
+ m_obj->weakreflist = NULL;
m_obj->exports = 0;
m_obj->offset = offset;
if (fd == -1) {
@@ -1394,6 +1399,7 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
/* set the initial position */
m_obj->pos = (size_t) 0;
+ m_obj->weakreflist = NULL;
m_obj->exports = 0;
/* set the tag name */
if (tagname != NULL && *tagname != '\0') {