Skip to content

Commit c53204b

Browse files
committed
Issue python#4885: Add weakref support to mmap objects. Patch by Valerie Lambert.
1 parent 914061a commit c53204b

4 files changed

Lines changed: 23 additions & 3 deletions

File tree

Lib/test/test_mmap.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
from test.support import (TESTFN, run_unittest, import_module, unlink,
2-
requires, _2G, _4G)
2+
requires, _2G, _4G, gc_collect)
33
import unittest
44
import os
55
import re
66
import itertools
77
import socket
88
import sys
9+
import weakref
910

1011
# Skip test if we can't import mmap.
1112
mmap = import_module('mmap')
@@ -692,6 +693,15 @@ def test_context_manager_exception(self):
692693
"wrong exception raised in context manager")
693694
self.assertTrue(m.closed, "context manager failed")
694695

696+
def test_weakref(self):
697+
# Check mmap objects are weakrefable
698+
mm = mmap.mmap(-1, 16)
699+
wr = weakref.ref(mm)
700+
self.assertIs(wr(), mm)
701+
del mm
702+
gc_collect()
703+
self.assertIs(wr(), None)
704+
695705
class LargeMmapTests(unittest.TestCase):
696706

697707
def setUp(self):

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,7 @@ Ross Lagerwall
701701
Cameron Laird
702702
David Lam
703703
Thomas Lamb
704+
Valerie Lambert
704705
Jean-Baptiste "Jiba" Lamy
705706
Ronan Lamy
706707
Torsten Landschoff

Misc/NEWS

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ Core and Builtins
1313
Library
1414
-------
1515

16-
- Issue 8860: Fixed rounding in timedelta constructor.
16+
- Issue #4885: Add weakref support to mmap objects. Patch by Valerie Lambert.
17+
18+
- Issue #8860: Fixed rounding in timedelta constructor.
19+
1720

1821
What's New in Python 3.4.0 Alpha 1?
1922
===================================

Modules/mmapmodule.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#define PY_SSIZE_T_CLEAN
2222
#include <Python.h>
23+
#include "structmember.h"
2324

2425
#ifndef MS_WINDOWS
2526
#define UNIX
@@ -108,6 +109,7 @@ typedef struct {
108109
int fd;
109110
#endif
110111

112+
PyObject *weakreflist;
111113
access_mode access;
112114
} mmap_object;
113115

@@ -134,6 +136,8 @@ mmap_object_dealloc(mmap_object *m_obj)
134136
}
135137
#endif /* UNIX */
136138

139+
if (m_obj->weakreflist != NULL)
140+
PyObject_ClearWeakRefs((PyObject *) m_obj);
137141
Py_TYPE(m_obj)->tp_free((PyObject*)m_obj);
138142
}
139143

@@ -1032,7 +1036,7 @@ static PyTypeObject mmap_object_type = {
10321036
0, /* tp_traverse */
10331037
0, /* tp_clear */
10341038
0, /* tp_richcompare */
1035-
0, /* tp_weaklistoffset */
1039+
offsetof(mmap_object, weakreflist), /* tp_weaklistoffset */
10361040
0, /* tp_iter */
10371041
0, /* tp_iternext */
10381042
mmap_object_methods, /* tp_methods */
@@ -1190,6 +1194,7 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
11901194
m_obj->data = NULL;
11911195
m_obj->size = (size_t) map_size;
11921196
m_obj->pos = (size_t) 0;
1197+
m_obj->weakreflist = NULL;
11931198
m_obj->exports = 0;
11941199
m_obj->offset = offset;
11951200
if (fd == -1) {
@@ -1394,6 +1399,7 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
13941399
/* set the initial position */
13951400
m_obj->pos = (size_t) 0;
13961401

1402+
m_obj->weakreflist = NULL;
13971403
m_obj->exports = 0;
13981404
/* set the tag name */
13991405
if (tagname != NULL && *tagname != '\0') {

0 commit comments

Comments
 (0)