/Modules/_weakref.c

http://unladen-swallow.googlecode.com/ · C · 112 lines · 90 code · 22 blank · 0 comment · 9 complexity · 25b41d9f0e362db775a7c91c7348a3b5 MD5 · raw file

  1. #include "Python.h"
  2. #define GET_WEAKREFS_LISTPTR(o) \
  3. ((PyWeakReference **) PyObject_GET_WEAKREFS_LISTPTR(o))
  4. PyDoc_STRVAR(weakref_getweakrefcount__doc__,
  5. "getweakrefcount(object) -- return the number of weak references\n"
  6. "to 'object'.");
  7. static PyObject *
  8. weakref_getweakrefcount(PyObject *self, PyObject *object)
  9. {
  10. PyObject *result = NULL;
  11. if (PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))) {
  12. PyWeakReference **list = GET_WEAKREFS_LISTPTR(object);
  13. result = PyInt_FromSsize_t(_PyWeakref_GetWeakrefCount(*list));
  14. }
  15. else
  16. result = PyInt_FromLong(0);
  17. return result;
  18. }
  19. PyDoc_STRVAR(weakref_getweakrefs__doc__,
  20. "getweakrefs(object) -- return a list of all weak reference objects\n"
  21. "that point to 'object'.");
  22. static PyObject *
  23. weakref_getweakrefs(PyObject *self, PyObject *object)
  24. {
  25. PyObject *result = NULL;
  26. if (PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))) {
  27. PyWeakReference **list = GET_WEAKREFS_LISTPTR(object);
  28. Py_ssize_t count = _PyWeakref_GetWeakrefCount(*list);
  29. result = PyList_New(count);
  30. if (result != NULL) {
  31. PyWeakReference *current = *list;
  32. Py_ssize_t i;
  33. for (i = 0; i < count; ++i) {
  34. PyList_SET_ITEM(result, i, (PyObject *) current);
  35. Py_INCREF(current);
  36. current = current->wr_next;
  37. }
  38. }
  39. }
  40. else {
  41. result = PyList_New(0);
  42. }
  43. return result;
  44. }
  45. PyDoc_STRVAR(weakref_proxy__doc__,
  46. "proxy(object[, callback]) -- create a proxy object that weakly\n"
  47. "references 'object'. 'callback', if given, is called with a\n"
  48. "reference to the proxy when 'object' is about to be finalized.");
  49. static PyObject *
  50. weakref_proxy(PyObject *self, PyObject *args)
  51. {
  52. PyObject *object;
  53. PyObject *callback = NULL;
  54. PyObject *result = NULL;
  55. if (PyArg_UnpackTuple(args, "proxy", 1, 2, &object, &callback)) {
  56. result = PyWeakref_NewProxy(object, callback);
  57. }
  58. return result;
  59. }
  60. static PyMethodDef
  61. weakref_functions[] = {
  62. {"getweakrefcount", weakref_getweakrefcount, METH_O,
  63. weakref_getweakrefcount__doc__},
  64. {"getweakrefs", weakref_getweakrefs, METH_O,
  65. weakref_getweakrefs__doc__},
  66. {"proxy", weakref_proxy, METH_VARARGS,
  67. weakref_proxy__doc__},
  68. {NULL, NULL, 0, NULL}
  69. };
  70. PyMODINIT_FUNC
  71. init_weakref(void)
  72. {
  73. PyObject *m;
  74. m = Py_InitModule3("_weakref", weakref_functions,
  75. "Weak-reference support module.");
  76. if (m != NULL) {
  77. Py_INCREF(&_PyWeakref_RefType);
  78. PyModule_AddObject(m, "ref",
  79. (PyObject *) &_PyWeakref_RefType);
  80. Py_INCREF(&_PyWeakref_RefType);
  81. PyModule_AddObject(m, "ReferenceType",
  82. (PyObject *) &_PyWeakref_RefType);
  83. Py_INCREF(&_PyWeakref_ProxyType);
  84. PyModule_AddObject(m, "ProxyType",
  85. (PyObject *) &_PyWeakref_ProxyType);
  86. Py_INCREF(&_PyWeakref_CallableProxyType);
  87. PyModule_AddObject(m, "CallableProxyType",
  88. (PyObject *) &_PyWeakref_CallableProxyType);
  89. }
  90. }