[Python-checkins] CVS: python/dist/src/Modules _weakref.c,1.6,1.7

Martin v. L?wis loewis@users.sourceforge.net
Tue, 27 Feb 2001 10:36:58 -0800


Update of /cvsroot/python/python/dist/src/Modules
In directory usw-pr-cvs1:/tmp/cvs-serv22846/Modules

Modified Files:
	_weakref.c 
Log Message:
Patch #403985: Add support for weak-keyed dictionaries


Index: _weakref.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/_weakref.c,v
retrieving revision 1.6
retrieving revision 1.7
diff -C2 -r1.6 -r1.7
*** _weakref.c	2001/02/26 20:10:51	1.6
--- _weakref.c	2001/02/27 18:36:56	1.7
***************
*** 9,12 ****
--- 9,13 ----
      PyObject *wr_object;
      PyObject *wr_callback;
+     long hash;
      PyWeakReference *wr_prev;
      PyWeakReference *wr_next;
***************
*** 40,43 ****
--- 41,46 ----
          result = PyObject_NEW(PyWeakReference, &PyWeakReference_Type);
      }
+     if (result)
+         result->hash = -1;
      return result;
  }
***************
*** 113,116 ****
--- 116,133 ----
  
  
+ static long
+ weakref_hash(PyWeakReference *self)
+ {
+     if (self->hash != -1)
+         return self->hash;
+     if (self->wr_object == Py_None) {
+         PyErr_SetString(PyExc_TypeError, "weak object has gone away");
+         return -1;
+     }
+     self->hash = PyObject_Hash(self->wr_object);
+     return self->hash;
+ }
+     
+ 
  static PyObject *
  weakref_repr(PyWeakReference *self)
***************
*** 129,132 ****
--- 146,168 ----
  }
  
+ /* Weak references only support equality, not ordering. Two weak references
+    are equal if the underlying objects are equal. If the underlying object has
+    gone away, they are equal if they are identical. */
+ 
+ static PyObject *
+ weakref_richcompare(PyWeakReference* self, PyWeakReference* other, int op)
+ {
+     if (op != Py_EQ || self->ob_type != other->ob_type) {
+         Py_INCREF(Py_NotImplemented);
+         return Py_NotImplemented;
+     }
+     if (self->wr_object == Py_None || other->wr_object == Py_None) {
+         PyObject *res = self==other ? Py_True : Py_False;
+         Py_INCREF(res);
+         return res;
+     }
+     return PyObject_RichCompare(self->wr_object, other->wr_object, op);
+ }
+ 
  
  statichere PyTypeObject
***************
*** 146,150 ****
      0,                          /*tp_as_sequence*/
      0,                          /*tp_as_mapping*/
!     0,                          /*tp_hash*/
      (ternaryfunc)weakref_call,  /*tp_call*/
      0,                          /*tp_str*/
--- 182,186 ----
      0,                          /*tp_as_sequence*/
      0,                          /*tp_as_mapping*/
!     (hashfunc)weakref_hash,      /*tp_hash*/
      (ternaryfunc)weakref_call,  /*tp_call*/
      0,                          /*tp_str*/
***************
*** 152,159 ****
      0,                          /*tp_setattro*/
      0,                          /*tp_as_buffer*/
!     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC,
      0,                          /*tp_doc*/
      (traverseproc)gc_traverse,  /*tp_traverse*/
      (inquiry)gc_clear,          /*tp_clear*/
  };
  
--- 188,197 ----
      0,                          /*tp_setattro*/
      0,                          /*tp_as_buffer*/
!     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC | Py_TPFLAGS_HAVE_RICHCOMPARE,
      0,                          /*tp_doc*/
      (traverseproc)gc_traverse,  /*tp_traverse*/
      (inquiry)gc_clear,          /*tp_clear*/
+     (richcmpfunc)weakref_richcompare,	/*tp_richcompare*/
+     0,				/*tp_weaklistoffset*/
  };