Index: Modules/arraymodule.c =================================================================== --- Modules/arraymodule.c (revision 80673) +++ Modules/arraymodule.c (working copy) @@ -1919,7 +1919,9 @@ if (!(initial == NULL || PyList_Check(initial) || PyString_Check(initial) || PyTuple_Check(initial) - || (c == 'u' && PyUnicode_Check(initial)))) { + || (c == 'u' && PyUnicode_Check(initial)) + || (array_Check(initial) + && c == ((arrayobject*)initial)->ob_descr->typecode))) { it = PyObject_GetIter(initial); if (it == NULL) return NULL; @@ -1935,17 +1937,20 @@ PyObject *a; Py_ssize_t len; - if (initial == NULL || !(PyList_Check(initial) - || PyTuple_Check(initial))) + if (initial == NULL) len = 0; + else if (PyList_Check(initial)) + len = PyList_GET_SIZE(initial); + else if (PyTuple_Check(initial) || array_Check(initial)) + len = Py_SIZE(initial); else - len = PySequence_Size(initial); + len = 0; a = newarrayobject(type, len, descr); if (a == NULL) return NULL; - if (len > 0) { + if (len > 0 && !array_Check(initial)) { Py_ssize_t i; for (i = 0; i < len; i++) { PyObject *v = @@ -1994,6 +1999,10 @@ self->allocated = Py_SIZE(self); } #endif + } else if (initial != NULL && array_Check(initial)) { + arrayobject *self = (arrayobject *)a; + arrayobject *other = (arrayobject *)initial; + memcpy(self->ob_item, other->ob_item, len * other->ob_descr->itemsize); } if (it != NULL) { if (array_iter_extend((arrayobject *)a, it) == -1) { Index: Lib/test/test_array.py =================================================================== --- Lib/test/test_array.py (revision 80673) +++ Lib/test/test_array.py (working copy) @@ -228,6 +228,11 @@ if a.itemsize>1: self.assertRaises(ValueError, b.fromstring, "x") + def test_fromarray(self): + a = array.array(self.typecode, self.example) + b = array.array(self.typecode, a) + self.assertEqual(a, b) + def test_repr(self): a = array.array(self.typecode, 2*self.example) self.assertEqual(a, eval(repr(a), {"array": array.array})) @@ -947,6 +952,11 @@ self.assertRaises(AttributeError, setattr, a, "color", "blue") + def test_frombytearray(self): + a = array.array('b', range(10)) + b = array.array(self.typecode, a) + self.assertEqual(a, b) + class SignedNumberTest(NumberTest): example = [-1, 0, 1, 42, 0x7f] smallerexample = [-1, 0, 1, 42, 0x7e]