Merged revisions 60245-60277 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r60246 | guido.van.rossum | 2008-01-24 18:58:05 +0100 (Thu, 24 Jan 2008) | 2 lines

  Fix test67.py from issue #1303614.
........
  r60248 | raymond.hettinger | 2008-01-24 19:05:54 +0100 (Thu, 24 Jan 2008) | 1 line

  Clean-up and speed-up code by accessing numerator/denominator directly.  There's no reason to enforce readonliness
........
  r60249 | raymond.hettinger | 2008-01-24 19:12:23 +0100 (Thu, 24 Jan 2008) | 1 line

  Revert 60189 and restore performance.
........
  r60250 | guido.van.rossum | 2008-01-24 19:21:02 +0100 (Thu, 24 Jan 2008) | 5 lines

  News about recently fixed crashers:
  - A few crashers fixed: weakref_in_del.py (issue #1377858);
    loosing_dict_ref.py (issue #1303614, test67.py);
    borrowed_ref_[34].py (not in tracker).
........
  r60252 | thomas.heller | 2008-01-24 19:36:27 +0100 (Thu, 24 Jan 2008) | 7 lines

  Use a PyDictObject again for the array type cache; retrieving items
  from the WeakValueDictionary was slower by nearly a factor of 3.

  To avoid leaks, weakref proxies for the array types are put into the
  cache dict, with weakref callbacks that removes the entries when the
  type goes away.
........
  r60253 | thomas.heller | 2008-01-24 19:54:12 +0100 (Thu, 24 Jan 2008) | 2 lines

  Replace Py_BuildValue with PyTuple_Pack because it is faster.
  Also add a missing DECREF.
........
  r60254 | raymond.hettinger | 2008-01-24 20:05:29 +0100 (Thu, 24 Jan 2008) | 1 line

  Add support for trunc().
........
  r60255 | thomas.heller | 2008-01-24 20:15:02 +0100 (Thu, 24 Jan 2008) | 5 lines

  Invert the checks in get_[u]long and get_[u]longlong.  The intent was
  to not accept float types; the result was that integer-like objects
  were not accepted.

  Ported from release25-maint.
........
  r60256 | raymond.hettinger | 2008-01-24 20:30:19 +0100 (Thu, 24 Jan 2008) | 1 line

  Add support for int(r) just like the other numeric classes.
........
  r60263 | raymond.hettinger | 2008-01-24 22:23:58 +0100 (Thu, 24 Jan 2008) | 1 line

  Expand tests to include nested graph structures.
........
  r60264 | raymond.hettinger | 2008-01-24 22:47:56 +0100 (Thu, 24 Jan 2008) | 1 line

  Shorter pprint's for empty sets and frozensets.  Fix indentation of frozensets.  Add tests including two complex data structures.
........
  r60265 | amaury.forgeotdarc | 2008-01-24 23:51:18 +0100 (Thu, 24 Jan 2008) | 14 lines

  #1920: when considering a block starting by "while 0", the compiler optimized the
  whole construct away, even when an 'else' clause is present::

      while 0:
          print("no")
      else:
          print("yes")

  did not generate any code at all.

  Now the compiler emits the 'else' block, like it already does for 'if' statements.

  Will backport.
........
  r60266 | amaury.forgeotdarc | 2008-01-24 23:59:25 +0100 (Thu, 24 Jan 2008) | 2 lines

  News entry for r60265 (Issue 1920).
........
  r60269 | raymond.hettinger | 2008-01-25 00:50:26 +0100 (Fri, 25 Jan 2008) | 1 line

  More code cleanup.  Remove unnecessary indirection to useless class methods.
........
  r60270 | raymond.hettinger | 2008-01-25 01:21:54 +0100 (Fri, 25 Jan 2008) | 1 line

  Add support for copy, deepcopy, and pickle.
........
  r60271 | raymond.hettinger | 2008-01-25 01:33:45 +0100 (Fri, 25 Jan 2008) | 1 line

  Mark todos and review comments.
........
  r60272 | raymond.hettinger | 2008-01-25 02:13:12 +0100 (Fri, 25 Jan 2008) | 1 line

  Add one other review comment.
........
  r60273 | raymond.hettinger | 2008-01-25 02:23:38 +0100 (Fri, 25 Jan 2008) | 1 line

  Fix-up signature for approximation.
........
  r60274 | raymond.hettinger | 2008-01-25 02:46:33 +0100 (Fri, 25 Jan 2008) | 1 line

  More design notes
........
  r60276 | neal.norwitz | 2008-01-25 07:37:23 +0100 (Fri, 25 Jan 2008) | 6 lines

  Make the test more robust by trying to reconnect up to 3 times
  in case there were transient failures.  This will hopefully silence
  the buildbots for this test.  As we find other tests that have a problem,
  we can fix with a similar strategy assuming it is successful.  It worked
  on my box in a loop for 10+ runs where it would have an exception otherwise.
........
  r60277 | neal.norwitz | 2008-01-25 09:04:16 +0100 (Fri, 25 Jan 2008) | 4 lines

  Add prototypes to get the mathmodule.c to compile on OSF1 5.1 (Tru64)
  and eliminate a compiler warning in floatobject.c.  There might be
  a better way to go about this, but it should be good enough for now.
........
diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c
index c6098b9..49f55c8 100644
--- a/Modules/_ctypes/_ctypes.c
+++ b/Modules/_ctypes/_ctypes.c
@@ -122,12 +122,134 @@
 
 PyObject *PyExc_ArgError;
 static PyTypeObject Simple_Type;
-PyObject *array_types_cache;
 
 char *conversion_mode_encoding = NULL;
 char *conversion_mode_errors = NULL;
 
 
+/****************************************************************/
+
+typedef struct {
+	PyObject_HEAD
+	PyObject *key;
+	PyObject *dict;
+} DictRemoverObject;
+
+static void
+_DictRemover_dealloc(PyObject *_self)
+{
+	DictRemoverObject *self = (DictRemoverObject *)_self;
+	Py_XDECREF(self->key);
+	Py_XDECREF(self->dict);
+	Py_TYPE(self)->tp_free(_self);
+}
+
+static PyObject *
+_DictRemover_call(PyObject *_self, PyObject *args, PyObject *kw)
+{
+	DictRemoverObject *self = (DictRemoverObject *)_self;
+	if (self->key && self->dict) {
+		if (-1 == PyDict_DelItem(self->dict, self->key))
+			/* XXX Error context */
+			PyErr_WriteUnraisable(Py_None);
+		Py_DECREF(self->key);
+		self->key = NULL;
+		Py_DECREF(self->dict);
+		self->dict = NULL;
+	}
+	Py_INCREF(Py_None);
+	return Py_None;
+}
+
+static PyTypeObject DictRemover_Type = {
+	PyVarObject_HEAD_INIT(NULL, 0)
+	"_ctypes.DictRemover",			/* tp_name */
+	sizeof(DictRemoverObject),		/* tp_basicsize */
+	0,					/* tp_itemsize */
+	_DictRemover_dealloc,			/* tp_dealloc */
+	0,					/* tp_print */
+	0,					/* tp_getattr */
+	0,					/* tp_setattr */
+	0,					/* tp_compare */
+	0,			       		/* tp_repr */
+	0,					/* tp_as_number */
+	0,					/* tp_as_sequence */
+	0,					/* tp_as_mapping */
+	0,					/* tp_hash */
+	_DictRemover_call,			/* tp_call */
+	0,					/* tp_str */
+	0,					/* tp_getattro */
+	0,					/* tp_setattro */
+	0,					/* tp_as_buffer */
+/* XXX should participate in GC? */
+	Py_TPFLAGS_DEFAULT,			/* tp_flags */
+	"deletes a key from a dictionary",	/* tp_doc */
+	0,					/* tp_traverse */
+	0,					/* tp_clear */
+	0,					/* tp_richcompare */
+	0,					/* tp_weaklistoffset */
+	0,					/* tp_iter */
+	0,					/* tp_iternext */
+	0,					/* tp_methods */
+	0,					/* tp_members */
+	0,					/* tp_getset */
+	0,					/* tp_base */
+	0,					/* tp_dict */
+	0,					/* tp_descr_get */
+	0,					/* tp_descr_set */
+	0,					/* tp_dictoffset */
+	0,					/* tp_init */
+	0,					/* tp_alloc */
+	PyType_GenericNew,			/* tp_new */
+	0,					/* tp_free */
+};
+
+int
+PyDict_SetItemProxy(PyObject *dict, PyObject *key, PyObject *item)
+{
+	PyObject *obj;
+	DictRemoverObject *remover;
+	PyObject *proxy;
+	int result;
+
+	obj = PyObject_CallObject((PyObject *)&DictRemover_Type, NULL);
+	if (obj == NULL)
+		return -1;
+
+	remover = (DictRemoverObject *)obj;
+	assert(remover->key == NULL);
+	assert(remover->dict == NULL);
+	Py_INCREF(key);
+	remover->key = key;
+	Py_INCREF(dict);
+	remover->dict = dict;
+
+	proxy = PyWeakref_NewProxy(item, obj);
+	Py_DECREF(obj);
+	if (proxy == NULL)
+		return -1;
+
+	result = PyDict_SetItem(dict, key, proxy);
+	Py_DECREF(proxy);
+	return result;
+}
+
+PyObject *
+PyDict_GetItemProxy(PyObject *dict, PyObject *key)
+{
+	PyObject *result;
+	PyObject *item = PyDict_GetItem(dict, key);
+
+	if (item == NULL)
+		return NULL;
+	if (!PyWeakref_CheckProxy(item))
+		return item;
+	result = PyWeakref_GET_OBJECT(item);
+	if (result == Py_None)
+		return NULL;
+	return result;
+}
+
 /******************************************************************/
 /*
   StructType_Type - a meta type/class.  Creating a new class using this one as
@@ -2381,7 +2503,7 @@
 		  only it's object list.  So we create a tuple, containing
 		  b_objects list PLUS the array itself, and return that!
 		*/
-		return Py_BuildValue("(OO)", keep, value);
+		return PyTuple_Pack(2, keep, value);
 	}
 	PyErr_Format(PyExc_TypeError,
 		     "incompatible types, %s instance instead of %s instance",
@@ -3993,19 +4115,30 @@
 PyObject *
 CreateArrayType(PyObject *itemtype, Py_ssize_t length)
 {
+	static PyObject *cache;
 	PyObject *key;
 	PyObject *result;
 	char name[256];
+	PyObject *len;
 
-	key = Py_BuildValue("(On)", itemtype, length);
+	if (cache == NULL) {
+		cache = PyDict_New();
+		if (cache == NULL)
+			return NULL;
+	}
+	len = PyLong_FromSsize_t(length);
+	if (len == NULL)
+		return NULL;
+	key = PyTuple_Pack(2, itemtype, len);
+	Py_DECREF(len);
 	if (!key)
 		return NULL;
-	result = PyObject_GetItem(array_types_cache, key);
+	result = PyDict_GetItemProxy(cache, key);
 	if (result) {
+		Py_INCREF(result);
 		Py_DECREF(key);
 		return result;
-	} else
-		PyErr_Clear();
+	}
 
 	if (!PyType_Check(itemtype)) {
 		PyErr_SetString(PyExc_TypeError,
@@ -4029,9 +4162,11 @@
 				       "_type_",
 				       itemtype
 		);
-	if (!result)
+	if (result == NULL) {
+		Py_DECREF(key);
 		return NULL;
-	if (-1 == PyObject_SetItem(array_types_cache, key, result)) {
+	}
+	if (-1 == PyDict_SetItemProxy(cache, key, result)) {
 		Py_DECREF(key);
 		Py_DECREF(result);
 		return NULL;
@@ -4792,7 +4927,6 @@
 init_ctypes(void)
 {
 	PyObject *m;
-	PyObject *weakref;
 
 /* Note:
    ob_type is the metatype (the 'type'), defaults to PyType_Type,
@@ -4805,16 +4939,6 @@
 	if (!m)
 		return;
 
-	weakref = PyImport_ImportModule("weakref");
-	if (weakref == NULL)
-		return;
-	array_types_cache = PyObject_CallMethod(weakref,
-						"WeakValueDictionary",
-						NULL);
-	if (array_types_cache == NULL)
-		return;
-	Py_DECREF(weakref);
-
 	if (PyType_Ready(&PyCArg_Type) < 0)
 		return;
 
@@ -4910,6 +5034,10 @@
 	 * Other stuff
 	 */
 
+	DictRemover_Type.tp_new = PyType_GenericNew;
+	if (PyType_Ready(&DictRemover_Type) < 0)
+		return;
+
 #ifdef MS_WIN32
 	if (create_comerror() < 0)
 		return;