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

Skip to content

Commit bec4d57

Browse files
committed
Merged revisions 74524 via svnmerge from
svn+ssh://[email protected]/python/trunk ........ r74524 | gregory.p.smith | 2009-08-20 04:39:38 -0500 (Thu, 20 Aug 2009) | 2 lines Add weakref support to the thread.lock type. ........
1 parent bf2e0aa commit bec4d57

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

Doc/library/weakref.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ instances, functions written in Python (but not in C), instance methods, sets,
6161
frozensets, file objects, :term:`generator`\s, type objects, sockets, arrays,
6262
deques, and regular expression pattern objects.
6363

64+
.. versionchanged:: 3.2
65+
Added support for thread.lock and threading.Lock.
66+
6467
Several built-in types such as :class:`list` and :class:`dict` do not directly
6568
support weak references but can add support through subclassing::
6669

Modules/_threadmodule.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
/* Interface to Sjoerd's portable C thread library */
44

55
#include "Python.h"
6+
#include "structmember.h" /* offsetof */
67

78
#ifndef WITH_THREAD
89
#error "Error! The rest of Python is not compiled with thread support."
@@ -20,12 +21,15 @@ static PyObject *ThreadError;
2021
typedef struct {
2122
PyObject_HEAD
2223
PyThread_type_lock lock_lock;
24+
PyObject *in_weakreflist;
2325
} lockobject;
2426

2527
static void
2628
lock_dealloc(lockobject *self)
2729
{
2830
assert(self->lock_lock);
31+
if (self->in_weakreflist != NULL)
32+
PyObject_ClearWeakRefs((PyObject *) self);
2933
/* Unlock the lock so it's safe to free it */
3034
PyThread_acquire_lock(self->lock_lock, 0);
3135
PyThread_release_lock(self->lock_lock);
@@ -145,7 +149,7 @@ static PyTypeObject Locktype = {
145149
0, /*tp_traverse*/
146150
0, /*tp_clear*/
147151
0, /*tp_richcompare*/
148-
0, /*tp_weaklistoffset*/
152+
offsetof(lockobject, in_weakreflist), /*tp_weaklistoffset*/
149153
0, /*tp_iter*/
150154
0, /*tp_iternext*/
151155
lock_methods, /*tp_methods*/
@@ -159,6 +163,7 @@ newlockobject(void)
159163
if (self == NULL)
160164
return NULL;
161165
self->lock_lock = PyThread_allocate_lock();
166+
self->in_weakreflist = NULL;
162167
if (self->lock_lock == NULL) {
163168
PyObject_Del(self);
164169
self = NULL;

0 commit comments

Comments
 (0)