- Py_INCREF(q);
- if (!PySequence_Check(q)) TYERR("want a sequence of residues");
- i = PySequence_Size(q); if (i < 0) goto end;
- if (i != n) VALERR("residue count mismatch");
- v = xmalloc(n * sizeof(*v));
- for (i = 0; i < n; i++) {
- if ((x = PySequence_GetItem(q, i)) == 0) goto end;
+ v = xmalloc(n*sizeof(*v));
+ it = PyObject_GetIter(q); if (!it) goto end;
+ while (i < n) {
+ x = PyIter_Next(it);
+ if (!x) {
+ if (PyErr_Occurred()) goto end;
+ VALERR("residue count mismatch");
+ }