| 
									
										
										
										
											2001-02-01 05:27:45 +00:00
										 |  |  | #include "Python.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define GET_WEAKREFS_LISTPTR(o) \
 | 
					
						
							|  |  |  |         ((PyWeakReference **) PyObject_GET_WEAKREFS_LISTPTR(o)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | PyDoc_STRVAR(weakref_getweakrefcount__doc__, | 
					
						
							| 
									
										
										
										
											2001-02-18 05:20:18 +00:00
										 |  |  | "getweakrefcount(object) -- return the number of weak references\n" | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | "to 'object'."); | 
					
						
							| 
									
										
										
										
											2001-02-18 05:20:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-02-01 05:27:45 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2001-08-16 14:11:30 +00:00
										 |  |  | weakref_getweakrefcount(PyObject *self, PyObject *object) | 
					
						
							| 
									
										
										
										
											2001-02-01 05:27:45 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     PyObject *result = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-08-16 14:11:30 +00:00
										 |  |  |     if (PyType_SUPPORTS_WEAKREFS(object->ob_type)) { | 
					
						
							|  |  |  |         PyWeakReference **list = GET_WEAKREFS_LISTPTR(object); | 
					
						
							| 
									
										
										
										
											2001-02-01 05:27:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-05 22:00:24 +00:00
										 |  |  |         result = PyInt_FromLong(_PyWeakref_GetWeakrefCount(*list)); | 
					
						
							| 
									
										
										
										
											2001-02-01 05:27:45 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2001-08-16 14:11:30 +00:00
										 |  |  |     else | 
					
						
							|  |  |  |         result = PyInt_FromLong(0); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-02-01 05:27:45 +00:00
										 |  |  |     return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | PyDoc_STRVAR(weakref_getweakrefs__doc__, | 
					
						
							| 
									
										
										
										
											2001-02-18 05:20:18 +00:00
										 |  |  | "getweakrefs(object) -- return a list of all weak reference objects\n" | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | "that point to 'object'."); | 
					
						
							| 
									
										
										
										
											2001-02-18 05:20:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-02-01 05:27:45 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2001-08-16 14:11:30 +00:00
										 |  |  | weakref_getweakrefs(PyObject *self, PyObject *object) | 
					
						
							| 
									
										
										
										
											2001-02-01 05:27:45 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     PyObject *result = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-08-16 14:11:30 +00:00
										 |  |  |     if (PyType_SUPPORTS_WEAKREFS(object->ob_type)) { | 
					
						
							|  |  |  |         PyWeakReference **list = GET_WEAKREFS_LISTPTR(object); | 
					
						
							| 
									
										
										
										
											2001-10-05 22:00:24 +00:00
										 |  |  |         long count = _PyWeakref_GetWeakrefCount(*list); | 
					
						
							| 
									
										
										
										
											2001-02-01 05:27:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-08-16 14:11:30 +00:00
										 |  |  |         result = PyList_New(count); | 
					
						
							|  |  |  |         if (result != NULL) { | 
					
						
							|  |  |  |             PyWeakReference *current = *list; | 
					
						
							|  |  |  |             long i; | 
					
						
							|  |  |  |             for (i = 0; i < count; ++i) { | 
					
						
							|  |  |  |                 PyList_SET_ITEM(result, i, (PyObject *) current); | 
					
						
							|  |  |  |                 Py_INCREF(current); | 
					
						
							|  |  |  |                 current = current->wr_next; | 
					
						
							| 
									
										
										
										
											2001-02-01 05:27:45 +00:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2001-08-16 14:11:30 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         result = PyList_New(0); | 
					
						
							| 
									
										
										
										
											2001-02-01 05:27:45 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |     return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | PyDoc_STRVAR(weakref_ref__doc__, | 
					
						
							| 
									
										
										
										
											2002-08-02 20:23:40 +00:00
										 |  |  | "ref(object[, callback]) -- create a weak reference to 'object';\n" | 
					
						
							| 
									
										
										
										
											2001-02-18 05:20:18 +00:00
										 |  |  | "when 'object' is finalized, 'callback' will be called and passed\n" | 
					
						
							| 
									
										
										
										
											2002-08-02 20:23:40 +00:00
										 |  |  | "a reference to the weak reference object when 'object' is about\n" | 
					
						
							|  |  |  | "to be finalized."); | 
					
						
							| 
									
										
										
										
											2001-02-18 05:20:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-02-01 05:27:45 +00:00
										 |  |  | static PyObject * | 
					
						
							|  |  |  | weakref_ref(PyObject *self, PyObject *args) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     PyObject *object; | 
					
						
							|  |  |  |     PyObject *callback = NULL; | 
					
						
							| 
									
										
										
										
											2001-10-05 22:00:24 +00:00
										 |  |  |     PyObject *result = NULL; | 
					
						
							| 
									
										
										
										
											2001-02-01 05:27:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-23 21:12:47 +00:00
										 |  |  |     if (PyArg_UnpackTuple(args, "ref", 1, 2, &object, &callback)) { | 
					
						
							| 
									
										
										
										
											2001-10-05 22:00:24 +00:00
										 |  |  |         result = PyWeakref_NewRef(object, callback); | 
					
						
							| 
									
										
										
										
											2001-02-01 05:27:45 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2001-10-05 22:00:24 +00:00
										 |  |  |     return result; | 
					
						
							| 
									
										
										
										
											2001-02-01 05:27:45 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | PyDoc_STRVAR(weakref_proxy__doc__, | 
					
						
							| 
									
										
										
										
											2001-02-18 05:20:18 +00:00
										 |  |  | "proxy(object[, callback]) -- create a proxy object that weakly\n" | 
					
						
							|  |  |  | "references 'object'.  'callback', if given, is called with a\n" | 
					
						
							| 
									
										
										
										
											2002-08-02 20:23:40 +00:00
										 |  |  | "reference to the proxy when 'object' is about to be finalized."); | 
					
						
							| 
									
										
										
										
											2001-02-18 05:20:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-02-01 05:27:45 +00:00
										 |  |  | static PyObject * | 
					
						
							|  |  |  | weakref_proxy(PyObject *self, PyObject *args) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     PyObject *object; | 
					
						
							|  |  |  |     PyObject *callback = NULL; | 
					
						
							| 
									
										
										
										
											2001-10-05 22:00:24 +00:00
										 |  |  |     PyObject *result = NULL; | 
					
						
							| 
									
										
										
										
											2001-02-01 05:27:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-23 21:12:47 +00:00
										 |  |  |     if (PyArg_UnpackTuple(args, "proxy", 1, 2, &object, &callback)) { | 
					
						
							| 
									
										
										
										
											2001-10-05 22:00:24 +00:00
										 |  |  |         result = PyWeakref_NewProxy(object, callback); | 
					
						
							| 
									
										
										
										
											2001-02-01 05:27:45 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2001-10-05 22:00:24 +00:00
										 |  |  |     return result; | 
					
						
							| 
									
										
										
										
											2001-02-01 05:27:45 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyMethodDef | 
					
						
							|  |  |  | weakref_functions[] =  { | 
					
						
							| 
									
										
										
										
											2001-08-16 14:11:30 +00:00
										 |  |  |     {"getweakrefcount", weakref_getweakrefcount,        METH_O, | 
					
						
							| 
									
										
										
										
											2001-02-18 05:20:18 +00:00
										 |  |  |      weakref_getweakrefcount__doc__}, | 
					
						
							| 
									
										
										
										
											2001-08-16 14:11:30 +00:00
										 |  |  |     {"getweakrefs",     weakref_getweakrefs,            METH_O, | 
					
						
							| 
									
										
										
										
											2001-02-18 05:20:18 +00:00
										 |  |  |      weakref_getweakrefs__doc__}, | 
					
						
							|  |  |  |     {"proxy",           weakref_proxy,                  METH_VARARGS, | 
					
						
							|  |  |  |      weakref_proxy__doc__}, | 
					
						
							| 
									
										
										
										
											2001-02-01 05:27:45 +00:00
										 |  |  |     {"ref",             weakref_ref,                    METH_VARARGS, | 
					
						
							| 
									
										
										
										
											2001-02-18 05:20:18 +00:00
										 |  |  |      weakref_ref__doc__}, | 
					
						
							| 
									
										
										
										
											2001-02-01 05:27:45 +00:00
										 |  |  |     {NULL, NULL, 0, NULL} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-08-02 02:27:13 +00:00
										 |  |  | PyMODINIT_FUNC | 
					
						
							| 
									
										
										
										
											2001-02-01 05:27:45 +00:00
										 |  |  | init_weakref(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     PyObject *m; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     m = Py_InitModule3("_weakref", weakref_functions, | 
					
						
							|  |  |  |                        "Weak-reference support module."); | 
					
						
							|  |  |  |     if (m != NULL) { | 
					
						
							| 
									
										
										
										
											2001-10-05 22:00:24 +00:00
										 |  |  |         Py_INCREF(&_PyWeakref_RefType); | 
					
						
							| 
									
										
										
										
											2001-02-01 05:27:45 +00:00
										 |  |  |         PyModule_AddObject(m, "ReferenceType", | 
					
						
							| 
									
										
										
										
											2001-10-05 22:00:24 +00:00
										 |  |  |                            (PyObject *) &_PyWeakref_RefType); | 
					
						
							|  |  |  |         Py_INCREF(&_PyWeakref_ProxyType); | 
					
						
							| 
									
										
										
										
											2001-02-01 05:27:45 +00:00
										 |  |  |         PyModule_AddObject(m, "ProxyType", | 
					
						
							| 
									
										
										
										
											2001-10-05 22:00:24 +00:00
										 |  |  |                            (PyObject *) &_PyWeakref_ProxyType); | 
					
						
							|  |  |  |         Py_INCREF(&_PyWeakref_CallableProxyType); | 
					
						
							| 
									
										
										
										
											2001-02-01 05:27:45 +00:00
										 |  |  |         PyModule_AddObject(m, "CallableProxyType", | 
					
						
							| 
									
										
										
										
											2001-10-05 22:00:24 +00:00
										 |  |  |                            (PyObject *) &_PyWeakref_CallableProxyType); | 
					
						
							| 
									
										
										
										
											2001-02-01 05:27:45 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | } |