1481: #line 1732 "mxTools.pak"
1482:
1483: Py_C_Function( mxTools_dict,
1484: "dict(seq)\n\n"
1485: "Creates a dictionary from the given items sequence.\n"
1486: "The sequence must contain sub-sequences of at least length 2,\n"
1487: "the first entry being interpreted as the key and the second as\n"
1488: "the value.")
1489: {
1490: int n;
1491: PyObject *seq,*d = 0;
1492: register int i;
1493: register PyObject *k = 0;
1494: register PyObject *v = 0;
1495:
1496: Py_GetArgObject(seq);
1497: n = PySequence_Length(seq);
1498: if (n < 0)
1499: Py_Error(PyExc_TypeError,
1500: "argument must be a sequence");
1501:
1502: d = PyDict_New();
1503: if (!d)
1504: goto onError;
1505:
1506: for (i = 0; i < n; i++) {
1507: register PyObject *o;
1508:
1509: o = PySequence_GetItem(seq,i);
1510: if (!o)
1511: goto onError;
1512: k = PySequence_GetItem(o,0);
1513: v = PySequence_GetItem(o,1);
1514: Py_DECREF(o);
1515:
1516: if (!k || !v) {
1517: PyErr_Format(PyExc_TypeError,
1518: "item %i in sequence doesn't have two entries",i);
1519: goto onError;
1520: }
1521: if (PyDict_SetItem(d,k,v))
1522: goto onError;
1523: Py_DECREF(k);
1524: Py_DECREF(v);
1525: }
1526:
1527: return d;
1528: onError:
1529: Py_XDECREF(d);
1530: Py_XDECREF(k);
1531: Py_XDECREF(v);
1532: return NULL;
1533: }
1534: