[Python-Dev] Patch for Weak References for Functions

Phil Thompson phil@river-bank.demon.co.uk
Mon, 05 Mar 2001 16:15:13 +0000


This is a multi-part message in MIME format.
--------------8B3DF66E5341F2A79134074D
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Any chance of the attached small patch be applied to enable weak
references to functions?

It's particularly useful for lambda functions and closes the "very last
loophole where a programmer can cause a PyQt script to seg fault" :)

Phil
--------------8B3DF66E5341F2A79134074D
Content-Type: text/plain; charset=us-ascii;
 name="wrfunctions.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="wrfunctions.patch"

diff -ruN Python-2.1b1.orig/Include/funcobject.h Python-2.1b1/Include/funcobject.h
--- Python-2.1b1.orig/Include/funcobject.h	Thu Jan 25 20:06:58 2001
+++ Python-2.1b1/Include/funcobject.h	Mon Mar  5 13:00:58 2001
@@ -16,6 +16,7 @@
     PyObject *func_doc;
     PyObject *func_name;
     PyObject *func_dict;
+    PyObject *func_weakreflist;
 } PyFunctionObject;
 
 extern DL_IMPORT(PyTypeObject) PyFunction_Type;
diff -ruN Python-2.1b1.orig/Objects/funcobject.c Python-2.1b1/Objects/funcobject.c
--- Python-2.1b1.orig/Objects/funcobject.c	Thu Mar  1 06:06:37 2001
+++ Python-2.1b1/Objects/funcobject.c	Mon Mar  5 13:39:37 2001
@@ -245,6 +245,8 @@
 static void
 func_dealloc(PyFunctionObject *op)
 {
+	PyObject_ClearWeakRefs((PyObject *) op);
+
 	PyObject_GC_Fini(op);
 	Py_DECREF(op->func_code);
 	Py_DECREF(op->func_globals);
@@ -336,4 +338,7 @@
 	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/
 	0,		/* tp_doc */
 	(traverseproc)func_traverse,	/* tp_traverse */
+	0,		/* tp_clear */
+	0,		/* tp_richcompare */
+	offsetof(PyFunctionObject, func_weakreflist)	/* tp_weaklistoffset */
 };

--------------8B3DF66E5341F2A79134074D--