1617: #line 1904 "mxTools.pak"
1618:
1619: Py_C_Function( mxTools_reverse,
1620: "reverse(seq)\n\n"
1621: "Creates a new sequence with reversed order of the items in\n"
1622: "seq. If seq is a tuple, then a tuple is returned. Otherwise\n"
1623: "a list is returned.")
1624: {
1625: PyObject *seq,*w = 0;
1626:
1627: Py_GetArgObject(seq);
1628:
1629: if (PyTuple_Check(seq)) {
1630: register int i;
1631: register int n = PyTuple_GET_SIZE(seq);
1632:
1633: w = PyTuple_New(n);
1634: if (!w)
1635: goto onError;
1636:
1637: for(i = 0; i < n;) {
1638: register PyObject *o;
1639:
1640: o = PyTuple_GET_ITEM(seq,i);
1641: Py_INCREF(o);
1642: i++;
1643: PyTuple_SET_ITEM(w,n-i,o);
1644: }
1645: }
1646: else if (PyList_Check(seq)) {
1647: register int i;
1648: register int n = PyList_GET_SIZE(seq);
1649:
1650: w = PyList_New(n);
1651: if (!w)
1652: goto onError;
1653:
1654: for(i = 0; i < n;) {
1655: register PyObject *o;
1656:
1657: o = PyList_GET_ITEM(seq,i);
1658: Py_INCREF(o);
1659: i++;
1660: PyList_SET_ITEM(w,n-i,o);
1661: }
1662: }
1663: else {
1664: register int i;
1665: register int n = PySequence_Length(seq);
1666:
1667: if (n < 0)
1668: Py_Error(PyExc_TypeError,
1669: "argument must be a sequence");
1670:
1671: w = PyList_New(n);
1672: if (!w)
1673: goto onError;
1674:
1675: for(i = 0; i < n;) {
1676: register PyObject *o;
1677:
1678: o = PySequence_GetItem(seq,i);
1679: if (!o)
1680: PyErr_Format(PyExc_IndexError,
1681: "item %i of sequence",i);
1682: Py_INCREF(o);
1683: i++;
1684: PyList_SET_ITEM(w,n-i,o);
1685: }
1686: }
1687:
1688: return w;
1689: onError:
1690: Py_XDECREF(w);
1691: return NULL;
1692: }
1693:
1694: #ifdef INCLUDE_FUNSTUFF
1695: static
1696: void mxTools_free(void *p)
1697: {
1698: free(p);
1699: }
1700:
1701: Py_C_Function( mxTools_malloc,
1702: "malloc(x)\n\n")
1703: {
1704: int x;
1705: char *p;
1706: register int i;
1707: register char *c;
1708:
1709: Py_GetSingleArg("i",x);
1710:
1711: /* Allocate virtual memory */
1712: p = (char *)malloc(x);
1713: if (!p) {
1714: PyErr_NoMemory();
1715: goto onError;
1716: }
1717:
1718: /* Turn the virtual memory into "real" one */
1719: i = x;
1720: c = p;
1721: while (i--)
1722: *c++ = '\0';
1723:
1724: return PyCObject_FromVoidPtr(p,mxTools_free);
1725: onError:
1726: return NULL;
1727: }
1728:
1729: Py_C_Function( mxTools_caching_eval,
1730: "caching_eval(code_string)\n\n"
1731: )
1732: {
1733: PyObject *codestr;
1734:
1735: Py_GetArg("O",codestr);
1736:
1737: Py_Assert(PyString_Check(codestr),
1738: PyExc_TypeError,
1739: "arg must be a string");
1740:
1741: return mxTools_EvalCodeString(codestr);
1742:
1743: onError:
1744: return NULL;
1745: }
1746: #endif
1747: