home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / py2s152.zip / Modules / arraymodule.c < prev    next >
C/C++ Source or Header  |  1999-06-27  |  34KB  |  1,477 lines

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /* Array object implementation */
  33.  
  34. /* An array is a uniform list -- all items have the same type.
  35.    The item type is restricted to simple C types like int or float */
  36.  
  37. #include "Python.h"
  38.  
  39. #ifdef STDC_HEADERS
  40. #include <stddef.h>
  41. #else
  42. #include <sys/types.h>        /* For size_t */
  43. #endif
  44.  
  45. struct arrayobject; /* Forward */
  46.  
  47. struct arraydescr {
  48.     int typecode;
  49.     int itemsize;
  50.     PyObject * (*getitem) Py_FPROTO((struct arrayobject *, int));
  51.     int (*setitem) Py_FPROTO((struct arrayobject *, int, PyObject *));
  52. };
  53.  
  54. typedef struct arrayobject {
  55.     PyObject_VAR_HEAD
  56.     char *ob_item;
  57.     struct arraydescr *ob_descr;
  58. } arrayobject;
  59.  
  60. staticforward PyTypeObject Arraytype;
  61.  
  62. #define is_arrayobject(op) ((op)->ob_type == &Arraytype)
  63.  
  64. /* Forward */
  65. static PyObject *newarrayobject Py_PROTO((int, struct arraydescr *));
  66. #if 0
  67. static int getarraysize Py_PROTO((PyObject *));
  68. #endif
  69. static PyObject *getarrayitem Py_PROTO((PyObject *, int));
  70. static int setarrayitem Py_PROTO((PyObject *, int, PyObject *));
  71. #if 0
  72. static int insarrayitem Py_PROTO((PyObject *, int, PyObject *));
  73. static int addarrayitem Py_PROTO((PyObject *, PyObject *));
  74. #endif
  75.  
  76. static PyObject *
  77. c_getitem(ap, i)
  78.     arrayobject *ap;
  79.     int i;
  80. {
  81.     return PyString_FromStringAndSize(&((char *)ap->ob_item)[i], 1);
  82. }
  83.  
  84. static int
  85. c_setitem(ap, i, v)
  86.     arrayobject *ap;
  87.     int i;
  88.     PyObject *v;
  89. {
  90.     char x;
  91.     if (!PyArg_Parse(v, "c;array item must be char", &x))
  92.         return -1;
  93.     if (i >= 0)
  94.              ((char *)ap->ob_item)[i] = x;
  95.     return 0;
  96. }
  97.  
  98. static PyObject *
  99. b_getitem(ap, i)
  100.     arrayobject *ap;
  101.     int i;
  102. {
  103.     long x = ((char *)ap->ob_item)[i];
  104.     if (x >= 128)
  105.         x -= 256;
  106.     return PyInt_FromLong(x);
  107. }
  108.  
  109. static int
  110. b_setitem(ap, i, v)
  111.     arrayobject *ap;
  112.     int i;
  113.     PyObject *v;
  114. {
  115.     char x;
  116.     if (!PyArg_Parse(v, "b;array item must be integer", &x))
  117.         return -1;
  118.     if (i >= 0)
  119.              ((char *)ap->ob_item)[i] = x;
  120.     return 0;
  121. }
  122.  
  123. static PyObject *
  124. BB_getitem(ap, i)
  125.     arrayobject *ap;
  126.     int i;
  127. {
  128.     long x = ((unsigned char *)ap->ob_item)[i];
  129.     return PyInt_FromLong(x);
  130. }
  131.  
  132. #define BB_setitem b_setitem
  133.  
  134. static PyObject *
  135. h_getitem(ap, i)
  136.     arrayobject *ap;
  137.     int i;
  138. {
  139.     return PyInt_FromLong((long) ((short *)ap->ob_item)[i]);
  140. }
  141.  
  142. static int
  143. h_setitem(ap, i, v)
  144.     arrayobject *ap;
  145.     int i;
  146.     PyObject *v;
  147. {
  148.     short x;
  149.     if (!PyArg_Parse(v, "h;array item must be integer", &x))
  150.         return -1;
  151.     if (i >= 0)
  152.              ((short *)ap->ob_item)[i] = x;
  153.     return 0;
  154. }
  155.  
  156. static PyObject *
  157. HH_getitem(ap, i)
  158.     arrayobject *ap;
  159.     int i;
  160. {
  161.     return PyInt_FromLong((long) ((unsigned short *)ap->ob_item)[i]);
  162. }
  163.  
  164. #define HH_setitem h_setitem
  165.  
  166. static PyObject *
  167. i_getitem(ap, i)
  168.     arrayobject *ap;
  169.     int i;
  170. {
  171.     return PyInt_FromLong((long) ((int *)ap->ob_item)[i]);
  172. }
  173.  
  174. static int
  175. i_setitem(ap, i, v)
  176.     arrayobject *ap;
  177.     int i;
  178.     PyObject *v;
  179. {
  180.     int x;
  181.     if (!PyArg_Parse(v, "i;array item must be integer", &x))
  182.         return -1;
  183.     if (i >= 0)
  184.              ((int *)ap->ob_item)[i] = x;
  185.     return 0;
  186. }
  187.  
  188. static PyObject *
  189. II_getitem(ap, i)
  190.     arrayobject *ap;
  191.     int i;
  192. {
  193.     return PyLong_FromUnsignedLong(
  194.         (unsigned long) ((unsigned int *)ap->ob_item)[i]);
  195. }
  196.  
  197. static int
  198. II_setitem(ap, i, v)
  199.     arrayobject *ap;
  200.     int i;
  201.     PyObject *v;
  202. {
  203.     unsigned long x;
  204.     if (PyLong_Check(v)) {
  205.         x = PyLong_AsUnsignedLong(v);
  206.         if (x == (unsigned long) -1 && PyErr_Occurred())
  207.             return -1;
  208.     }
  209.     else {
  210.         if (!PyArg_Parse(v, "l;array item must be integer", &x))
  211.             return -1;
  212.     }
  213.     if (i >= 0)
  214.         ((unsigned int *)ap->ob_item)[i] = x;
  215.     return 0;
  216. }
  217.  
  218. static PyObject *
  219. l_getitem(ap, i)
  220.     arrayobject *ap;
  221.     int i;
  222. {
  223.     return PyInt_FromLong(((long *)ap->ob_item)[i]);
  224. }
  225.  
  226. static int
  227. l_setitem(ap, i, v)
  228.     arrayobject *ap;
  229.     int i;
  230.     PyObject *v;
  231. {
  232.     long x;
  233.     if (!PyArg_Parse(v, "l;array item must be integer", &x))
  234.         return -1;
  235.     if (i >= 0)
  236.              ((long *)ap->ob_item)[i] = x;
  237.     return 0;
  238. }
  239.  
  240. static PyObject *
  241. LL_getitem(ap, i)
  242.     arrayobject *ap;
  243.     int i;
  244. {
  245.     return PyLong_FromUnsignedLong(((unsigned long *)ap->ob_item)[i]);
  246. }
  247.  
  248. static int
  249. LL_setitem(ap, i, v)
  250.     arrayobject *ap;
  251.     int i;
  252.     PyObject *v;
  253. {
  254.     unsigned long x;
  255.     if (PyLong_Check(v)) {
  256.         x = PyLong_AsUnsignedLong(v);
  257.         if (x == (unsigned long) -1 && PyErr_Occurred())
  258.             return -1;
  259.     }
  260.     else {
  261.         if (!PyArg_Parse(v, "l;array item must be integer", &x))
  262.             return -1;
  263.     }
  264.     if (i >= 0)
  265.         ((unsigned long *)ap->ob_item)[i] = x;
  266.     return 0;
  267. }
  268.  
  269. static PyObject *
  270. f_getitem(ap, i)
  271.     arrayobject *ap;
  272.     int i;
  273. {
  274.     return PyFloat_FromDouble((double) ((float *)ap->ob_item)[i]);
  275. }
  276.  
  277. static int
  278. f_setitem(ap, i, v)
  279.     arrayobject *ap;
  280.     int i;
  281.     PyObject *v;
  282. {
  283.     float x;
  284.     if (!PyArg_Parse(v, "f;array item must be float", &x))
  285.         return -1;
  286.     if (i >= 0)
  287.              ((float *)ap->ob_item)[i] = x;
  288.     return 0;
  289. }
  290.  
  291. static PyObject *
  292. d_getitem(ap, i)
  293.     arrayobject *ap;
  294.     int i;
  295. {
  296.     return PyFloat_FromDouble(((double *)ap->ob_item)[i]);
  297. }
  298.  
  299. static int
  300. d_setitem(ap, i, v)
  301.     arrayobject *ap;
  302.     int i;
  303.     PyObject *v;
  304. {
  305.     double x;
  306.     if (!PyArg_Parse(v, "d;array item must be float", &x))
  307.         return -1;
  308.     if (i >= 0)
  309.              ((double *)ap->ob_item)[i] = x;
  310.     return 0;
  311. }
  312.  
  313. /* Description of types */
  314. static struct arraydescr descriptors[] = {
  315.     {'c', sizeof(char), c_getitem, c_setitem},
  316.     {'b', sizeof(char), b_getitem, b_setitem},
  317.     {'B', sizeof(char), BB_getitem, BB_setitem},
  318.     {'h', sizeof(short), h_getitem, h_setitem},
  319.     {'H', sizeof(short), HH_getitem, HH_setitem},
  320.     {'i', sizeof(int), i_getitem, i_setitem},
  321.     {'I', sizeof(int), II_getitem, II_setitem},
  322.     {'l', sizeof(long), l_getitem, l_setitem},
  323.     {'L', sizeof(long), LL_getitem, LL_setitem},
  324.     {'f', sizeof(float), f_getitem, f_setitem},
  325.     {'d', sizeof(double), d_getitem, d_setitem},
  326.     {'\0', 0, 0, 0} /* Sentinel */
  327. };
  328. /* If we ever allow items larger than double, we must change reverse()! */
  329.     
  330.  
  331. static PyObject *
  332. newarrayobject(size, descr)
  333.     int size;
  334.     struct arraydescr *descr;
  335. {
  336.     arrayobject *op;
  337.     size_t nbytes;
  338.     if (size < 0) {
  339.         PyErr_BadInternalCall();
  340.         return NULL;
  341.     }
  342.     nbytes = size * descr->itemsize;
  343.     /* Check for overflow */
  344.     if (nbytes / descr->itemsize != (size_t)size) {
  345.         return PyErr_NoMemory();
  346.     }
  347.     op = PyMem_NEW(arrayobject, 1);
  348.     if (op == NULL) {
  349.         return PyErr_NoMemory();
  350.     }
  351.     if (size <= 0) {
  352.         op->ob_item = NULL;
  353.     }
  354.     else {
  355.         op->ob_item = PyMem_NEW(char, nbytes);
  356.         if (op->ob_item == NULL) {
  357.             PyMem_DEL(op);
  358.             return PyErr_NoMemory();
  359.         }
  360.     }
  361.     op->ob_type = &Arraytype;
  362.     op->ob_size = size;
  363.     op->ob_descr = descr;
  364.     _Py_NewReference(op);
  365.     return (PyObject *) op;
  366. }
  367.  
  368. #if 0
  369. static int
  370. getarraysize(op)
  371.     PyObject *op;
  372. {
  373.     if (!is_arrayobject(op)) {
  374.         PyErr_BadInternalCall();
  375.         return -1;
  376.     }
  377.     return ((arrayobject *)op) -> ob_size;
  378. }
  379. #endif
  380.  
  381. static PyObject *
  382. getarrayitem(op, i)
  383.     PyObject *op;
  384.     int i;
  385. {
  386.     register arrayobject *ap;
  387.     if (!is_arrayobject(op)) {
  388.         PyErr_BadInternalCall();
  389.         return NULL;
  390.     }
  391.     ap = (arrayobject *)op;
  392.     if (i < 0 || i >= ap->ob_size) {
  393.         PyErr_SetString(PyExc_IndexError, "array index out of range");
  394.         return NULL;
  395.     }
  396.     return (*ap->ob_descr->getitem)(ap, i);
  397. }
  398.  
  399. static int
  400. ins1(self, where, v)
  401.     arrayobject *self;
  402.     int where;
  403.     PyObject *v;
  404. {
  405.     char *items;
  406.     if (v == NULL) {
  407.         PyErr_BadInternalCall();
  408.         return -1;
  409.     }
  410.     if ((*self->ob_descr->setitem)(self, -1, v) < 0)
  411.         return -1;
  412.     items = self->ob_item;
  413.     PyMem_RESIZE(items, char,
  414.              (self->ob_size+1) * self->ob_descr->itemsize);
  415.     if (items == NULL) {
  416.         PyErr_NoMemory();
  417.         return -1;
  418.     }
  419.     if (where < 0)
  420.         where = 0;
  421.     if (where > self->ob_size)
  422.         where = self->ob_size;
  423.     memmove(items + (where+1)*self->ob_descr->itemsize,
  424.         items + where*self->ob_descr->itemsize,
  425.         (self->ob_size-where)*self->ob_descr->itemsize);
  426.     self->ob_item = items;
  427.     self->ob_size++;
  428.     return (*self->ob_descr->setitem)(self, where, v);
  429. }
  430.  
  431. #if 0
  432. static int
  433. insarrayitem(op, where, newitem)
  434.     PyObject *op;
  435.     int where;
  436.     PyObject *newitem;
  437. {
  438.     if (!is_arrayobject(op)) {
  439.         PyErr_BadInternalCall();
  440.         return -1;
  441.     }
  442.     return ins1((arrayobject *)op, where, newitem);
  443. }
  444.  
  445. static int
  446. addarrayitem(op, newitem)
  447.     PyObject *op;
  448.     PyObject *newitem;
  449. {
  450.     if (!is_arrayobject(op)) {
  451.         PyErr_BadInternalCall();
  452.         return -1;
  453.     }
  454.     return ins1((arrayobject *)op,
  455.         (int) ((arrayobject *)op)->ob_size, newitem);
  456. }
  457. #endif
  458.  
  459. /* Methods */
  460.  
  461. static void
  462. array_dealloc(op)
  463.     arrayobject *op;
  464. {
  465.     if (op->ob_item != NULL)
  466.         PyMem_DEL(op->ob_item);
  467.     PyMem_DEL(op);
  468. }
  469.  
  470. static int
  471. array_compare(v, w)
  472.     arrayobject *v, *w;
  473. {
  474.     int len = (v->ob_size < w->ob_size) ? v->ob_size : w->ob_size;
  475.     int i;
  476.     for (i = 0; i < len; i++) {
  477.         PyObject *ai, *bi;
  478.         int cmp;
  479.         ai = getarrayitem((PyObject *)v, i);
  480.         bi = getarrayitem((PyObject *)w, i);
  481.         if (ai && bi)
  482.             cmp = PyObject_Compare(ai, bi);
  483.         else
  484.             cmp = -1;
  485.         Py_XDECREF(ai);
  486.         Py_XDECREF(bi);
  487.         if (cmp != 0)
  488.             return cmp;
  489.     }
  490.     return v->ob_size - w->ob_size;
  491. }
  492.  
  493. static int
  494. array_length(a)
  495.     arrayobject *a;
  496. {
  497.     return a->ob_size;
  498. }
  499.  
  500. static PyObject *
  501. array_item(a, i)
  502.     arrayobject *a;
  503.     int i;
  504. {
  505.     if (i < 0 || i >= a->ob_size) {
  506.         PyErr_SetString(PyExc_IndexError, "array index out of range");
  507.         return NULL;
  508.     }
  509.     return getarrayitem((PyObject *)a, i);
  510. }
  511.  
  512. static PyObject *
  513. array_slice(a, ilow, ihigh)
  514.     arrayobject *a;
  515.     int ilow, ihigh;
  516. {
  517.     arrayobject *np;
  518.     if (ilow < 0)
  519.         ilow = 0;
  520.     else if (ilow > a->ob_size)
  521.         ilow = a->ob_size;
  522.     if (ihigh < 0)
  523.         ihigh = 0;
  524.     if (ihigh < ilow)
  525.         ihigh = ilow;
  526.     else if (ihigh > a->ob_size)
  527.         ihigh = a->ob_size;
  528.     np = (arrayobject *) newarrayobject(ihigh - ilow, a->ob_descr);
  529.     if (np == NULL)
  530.         return NULL;
  531.     memcpy(np->ob_item, a->ob_item + ilow * a->ob_descr->itemsize,
  532.            (ihigh-ilow) * a->ob_descr->itemsize);
  533.     return (PyObject *)np;
  534. }
  535.  
  536. static PyObject *
  537. array_concat(a, bb)
  538.     arrayobject *a;
  539.     PyObject *bb;
  540. {
  541.     int size;
  542.     arrayobject *np;
  543.     if (!is_arrayobject(bb)) {
  544.         PyErr_BadArgument();
  545.         return NULL;
  546.     }
  547. #define b ((arrayobject *)bb)
  548.     if (a->ob_descr != b->ob_descr) {
  549.         PyErr_BadArgument();
  550.         return NULL;
  551.     }
  552.     size = a->ob_size + b->ob_size;
  553.     np = (arrayobject *) newarrayobject(size, a->ob_descr);
  554.     if (np == NULL) {
  555.         return NULL;
  556.     }
  557.     memcpy(np->ob_item, a->ob_item, a->ob_size*a->ob_descr->itemsize);
  558.     memcpy(np->ob_item + a->ob_size*a->ob_descr->itemsize,
  559.            b->ob_item, b->ob_size*b->ob_descr->itemsize);
  560.     return (PyObject *)np;
  561. #undef b
  562. }
  563.  
  564. static PyObject *
  565. array_repeat(a, n)
  566.     arrayobject *a;
  567.     int n;
  568. {
  569.     int i;
  570.     int size;
  571.     arrayobject *np;
  572.     char *p;
  573.     int nbytes;
  574.     if (n < 0)
  575.         n = 0;
  576.     size = a->ob_size * n;
  577.     np = (arrayobject *) newarrayobject(size, a->ob_descr);
  578.     if (np == NULL)
  579.         return NULL;
  580.     p = np->ob_item;
  581.     nbytes = a->ob_size * a->ob_descr->itemsize;
  582.     for (i = 0; i < n; i++) {
  583.         memcpy(p, a->ob_item, nbytes);
  584.         p += nbytes;
  585.     }
  586.     return (PyObject *) np;
  587. }
  588.  
  589. static int
  590. array_ass_slice(a, ilow, ihigh, v)
  591.     arrayobject *a;
  592.     int ilow, ihigh;
  593.     PyObject *v;
  594. {
  595.     char *item;
  596.     int n; /* Size of replacement array */
  597.     int d; /* Change in size */
  598. #define b ((arrayobject *)v)
  599.     if (v == NULL)
  600.         n = 0;
  601.     else if (is_arrayobject(v)) {
  602.         n = b->ob_size;
  603.         if (a == b) {
  604.             /* Special case "a[i:j] = a" -- copy b first */
  605.             int ret;
  606.             v = array_slice(b, 0, n);
  607.             ret = array_ass_slice(a, ilow, ihigh, v);
  608.             Py_DECREF(v);
  609.             return ret;
  610.         }
  611.         if (b->ob_descr != a->ob_descr) {
  612.             PyErr_BadArgument();
  613.             return -1;
  614.         }
  615.     }
  616.     else {
  617.         PyErr_BadArgument();
  618.         return -1;
  619.     }
  620.     if (ilow < 0)
  621.         ilow = 0;
  622.     else if (ilow > a->ob_size)
  623.         ilow = a->ob_size;
  624.     if (ihigh < 0)
  625.         ihigh = 0;
  626.     if (ihigh < ilow)
  627.         ihigh = ilow;
  628.     else if (ihigh > a->ob_size)
  629.         ihigh = a->ob_size;
  630.     item = a->ob_item;
  631.     d = n - (ihigh-ilow);
  632.     if (d < 0) { /* Delete -d items */
  633.         memmove(item + (ihigh+d)*a->ob_descr->itemsize,
  634.             item + ihigh*a->ob_descr->itemsize,
  635.             (a->ob_size-ihigh)*a->ob_descr->itemsize);
  636.         a->ob_size += d;
  637.         PyMem_RESIZE(item, char, a->ob_size*a->ob_descr->itemsize);
  638.                         /* Can't fail */
  639.         a->ob_item = item;
  640.     }
  641.     else if (d > 0) { /* Insert d items */
  642.         PyMem_RESIZE(item, char,
  643.                  (a->ob_size + d)*a->ob_descr->itemsize);
  644.         if (item == NULL) {
  645.             PyErr_NoMemory();
  646.             return -1;
  647.         }
  648.         memmove(item + (ihigh+d)*a->ob_descr->itemsize,
  649.             item + ihigh*a->ob_descr->itemsize,
  650.             (a->ob_size-ihigh)*a->ob_descr->itemsize);
  651.         a->ob_item = item;
  652.         a->ob_size += d;
  653.     }
  654.     if (n > 0)
  655.         memcpy(item + ilow*a->ob_descr->itemsize, b->ob_item,
  656.                n*b->ob_descr->itemsize);
  657.     return 0;
  658. #undef b
  659. }
  660.  
  661. static int
  662. array_ass_item(a, i, v)
  663.     arrayobject *a;
  664.     int i;
  665.     PyObject *v;
  666. {
  667.     if (i < 0 || i >= a->ob_size) {
  668.         PyErr_SetString(PyExc_IndexError,
  669.                      "array assignment index out of range");
  670.         return -1;
  671.     }
  672.     if (v == NULL)
  673.         return array_ass_slice(a, i, i+1, v);
  674.     return (*a->ob_descr->setitem)(a, i, v);
  675. }
  676.  
  677. static int
  678. setarrayitem(a, i, v)
  679.     PyObject *a;
  680.     int i;
  681.     PyObject *v;
  682. {
  683.     if (!is_arrayobject(a)) {
  684.         PyErr_BadInternalCall();
  685.         return -1;
  686.     }
  687.     return array_ass_item((arrayobject *)a, i, v);
  688. }
  689.  
  690. static PyObject *
  691. ins(self, where, v)
  692.     arrayobject *self;
  693.     int where;
  694.     PyObject *v;
  695. {
  696.     if (ins1(self, where, v) != 0)
  697.         return NULL;
  698.     Py_INCREF(Py_None);
  699.     return Py_None;
  700. }
  701.  
  702. static PyObject *
  703. array_insert(self, args)
  704.     arrayobject *self;
  705.     PyObject *args;
  706. {
  707.     int i;
  708.     PyObject *v;
  709.     if (!PyArg_Parse(args, "(iO)", &i, &v))
  710.         return NULL;
  711.     return ins(self, i, v);
  712. }
  713.  
  714. static char insert_doc [] =
  715. "insert (i,x)\n\
  716. \n\
  717. Insert a new item x into the array before position i.";
  718.  
  719.  
  720. static PyObject *
  721. array_buffer_info(self, args)
  722.     arrayobject *self;
  723.     PyObject *args;
  724. {
  725.     return Py_BuildValue("ll",
  726.                  (long)(self->ob_item), (long)(self->ob_size));
  727. }
  728.  
  729. static char buffer_info_doc [] =
  730. "buffer_info -> (address, length)\n\
  731. \n\
  732. Return a tuple (address, length) giving the current memory address and\n\
  733. the length in bytes of the buffer used to hold array's contents.";
  734.  
  735.  
  736. static PyObject *
  737. array_append(self, args)
  738.     arrayobject *self;
  739.     PyObject *args;
  740. {
  741.     PyObject *v;
  742.     if (!PyArg_Parse(args, "O", &v))
  743.         return NULL;
  744.     return ins(self, (int) self->ob_size, v);
  745. }
  746.  
  747. static char append_doc [] =
  748. "append(x)\n\
  749. \n\
  750. Append new value x to the end of the array.";
  751.  
  752.  
  753. static PyObject *
  754. array_byteswap(self, args)
  755.     arrayobject *self;
  756.     PyObject *args;
  757. {
  758.     char *p;
  759.     int i;
  760.     switch (self->ob_descr->itemsize) {
  761.     case 1:
  762.         break;
  763.     case 2:
  764.         for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 2) {
  765.             char p0 = p[0];
  766.             p[0] = p[1];
  767.             p[1] = p0;
  768.         }
  769.         break;
  770.     case 4:
  771.         for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 4) {
  772.             char p0 = p[0];
  773.             char p1 = p[1];
  774.             p[0] = p[3];
  775.             p[1] = p[2];
  776.             p[2] = p1;
  777.             p[3] = p0;
  778.         }
  779.         break;
  780.     case 8:
  781.         for (p = self->ob_item, i = self->ob_size; --i >= 0; p += 8) {
  782.             char p0 = p[0];
  783.             char p1 = p[1];
  784.             char p2 = p[2];
  785.             char p3 = p[3];
  786.             p[0] = p[7];
  787.             p[1] = p[6];
  788.             p[2] = p[5];
  789.             p[3] = p[4];
  790.             p[4] = p3;
  791.             p[5] = p2;
  792.             p[6] = p1;
  793.             p[7] = p0;
  794.         }
  795.         break;
  796.     default:
  797.         PyErr_SetString(PyExc_RuntimeError,
  798.                "don't know how to byteswap this array type");
  799.         return NULL;
  800.     }
  801.     Py_INCREF(Py_None);
  802.     return Py_None;
  803. }
  804.  
  805. static char byteswap_doc [] =
  806. "byteswap(x)\n\
  807. \n\
  808. Byteswap all items of the array.  This is only supported for integer\n\
  809. values of x, which determines the size of the blocks swapped.";
  810.  
  811. static PyObject *
  812. array_reverse(self, args)
  813.     arrayobject *self;
  814.     PyObject *args;
  815. {
  816.     register int itemsize = self->ob_descr->itemsize;
  817.     register char *p, *q;
  818.     char tmp[sizeof(double)]; /* Assume that's the max item size */
  819.  
  820.     if (args != NULL) {
  821.         PyErr_BadArgument();
  822.         return NULL;
  823.     }
  824.  
  825.     if (self->ob_size > 1) {
  826.         for (p = self->ob_item,
  827.              q = self->ob_item + (self->ob_size - 1)*itemsize;
  828.              p < q;
  829.              p += itemsize, q -= itemsize) {
  830.             memmove(tmp, p, itemsize);
  831.             memmove(p, q, itemsize);
  832.             memmove(q, tmp, itemsize);
  833.         }
  834.     }
  835.     
  836.     Py_INCREF(Py_None);
  837.     return Py_None;
  838. }
  839.  
  840. static char reverse_doc [] =
  841. "reverse()\n\
  842. \n\
  843. Reverse the order of the items in the array.";
  844.  
  845. /* The following routines were adapted from listobject.c but not converted.
  846.    To make them work you will have to work! */
  847.  
  848. #if 0
  849. static PyObject *
  850. array_index(self, args)
  851.     arrayobject *self;
  852.     PyObject *args;
  853. {
  854.     int i;
  855.     
  856.     if (args == NULL) {
  857.         PyErr_BadArgument();
  858.         return NULL;
  859.     }
  860.     for (i = 0; i < self->ob_size; i++) {
  861.         if (PyObject_Compare(self->ob_item[i], args) == 0)
  862.             return PyInt_FromLong((long)i);
  863.         /* XXX PyErr_Occurred */
  864.     }
  865.     PyErr_SetString(PyExc_ValueError, "array.index(x): x not in array");
  866.     return NULL;
  867. }
  868. #endif
  869.  
  870. #if 0
  871. static PyObject *
  872. array_count(self, args)
  873.     arrayobject *self;
  874.     PyObject *args;
  875. {
  876.     int count = 0;
  877.     int i;
  878.     
  879.     if (args == NULL) {
  880.         PyErr_BadArgument();
  881.         return NULL;
  882.     }
  883.     for (i = 0; i < self->ob_size; i++) {
  884.         if (PyObject_Compare(self->ob_item[i], args) == 0)
  885.             count++;
  886.         /* XXX PyErr_Occurred */
  887.     }
  888.     return PyInt_FromLong((long)count);
  889. }
  890. #endif
  891.  
  892. #if 0
  893. static PyObject *
  894. array_remove(self, args)
  895.     arrayobject *self;
  896.     PyObject *args;
  897. {
  898.     int i;
  899.     
  900.     if (args == NULL) {
  901.         PyErr_BadArgument();
  902.         return NULL;
  903.     }
  904.     for (i = 0; i < self->ob_size; i++) {
  905.         if (PyObject_Compare(self->ob_item[i], args) == 0) {
  906.             if (array_ass_slice(self, i, i+1,
  907.                         (PyObject *)NULL) != 0)
  908.                 return NULL;
  909.             Py_INCREF(Py_None);
  910.             return Py_None;
  911.         }
  912.         /* XXX PyErr_Occurred */
  913.     }
  914.     PyErr_SetString(PyExc_ValueError, "array.remove(x): x not in array");
  915.     return NULL;
  916. }
  917. #endif
  918.  
  919. static PyObject *
  920. array_fromfile(self, args)
  921.     arrayobject *self;
  922.     PyObject *args;
  923. {
  924.     PyObject *f;
  925.     int n;
  926.     FILE *fp;
  927.     if (!PyArg_Parse(args, "(Oi)", &f, &n))
  928.         return NULL;
  929.     fp = PyFile_AsFile(f);
  930.     if (fp == NULL) {
  931.         PyErr_SetString(PyExc_TypeError, "arg1 must be open file");
  932.         return NULL;
  933.     }
  934.     if (n > 0) {
  935.         char *item = self->ob_item;
  936.         int itemsize = self->ob_descr->itemsize;
  937.         int nread;
  938.         int newlength;
  939.         size_t newbytes;
  940.         /* Be careful here about overflow */
  941.         if ((newlength = self->ob_size + n) <= 0 ||
  942.             (newbytes = newlength * itemsize) / itemsize !=
  943.             (size_t)newlength)
  944.             goto nomem;
  945.         PyMem_RESIZE(item, char, newbytes);
  946.         if (item == NULL) {
  947.           nomem:
  948.             PyErr_NoMemory();
  949.             return NULL;
  950.         }
  951.         self->ob_item = item;
  952.         self->ob_size += n;
  953.         nread = fread(item + (self->ob_size - n) * itemsize,
  954.                   itemsize, n, fp);
  955.         if (nread < n) {
  956.             self->ob_size -= (n - nread);
  957.             PyMem_RESIZE(item, char, self->ob_size*itemsize);
  958.             self->ob_item = item;
  959.             PyErr_SetString(PyExc_EOFError,
  960.                          "not enough items in file");
  961.             return NULL;
  962.         }
  963.     }
  964.     Py_INCREF(Py_None);
  965.     return Py_None;
  966. }
  967.  
  968. static char fromfile_doc [] =
  969. "fromfile(f, n)\n\
  970. \n\
  971. Read n objects from the file object f and append them to the end of the\n\
  972. array.  Also called as read.";
  973.  
  974.  
  975. static PyObject *
  976. array_tofile(self, args)
  977.     arrayobject *self;
  978.     PyObject *args;
  979. {
  980.     PyObject *f;
  981.     FILE *fp;
  982.     if (!PyArg_Parse(args, "O", &f))
  983.         return NULL;
  984.     fp = PyFile_AsFile(f);
  985.     if (fp == NULL) {
  986.         PyErr_SetString(PyExc_TypeError, "arg must be open file");
  987.         return NULL;
  988.     }
  989.     if (self->ob_size > 0) {
  990.         if ((int)fwrite(self->ob_item, self->ob_descr->itemsize,
  991.                self->ob_size, fp) != self->ob_size) {
  992.             PyErr_SetFromErrno(PyExc_IOError);
  993.             clearerr(fp);
  994.             return NULL;
  995.         }
  996.     }
  997.     Py_INCREF(Py_None);
  998.     return Py_None;
  999. }
  1000.  
  1001. static char tofile_doc [] =
  1002. "tofile(f)\n\
  1003. \n\
  1004. Write all items (as machine values) to the file object f.  Also called as\n\
  1005. write.";
  1006.  
  1007.  
  1008. static PyObject *
  1009. array_fromlist(self, args)
  1010.     arrayobject *self;
  1011.     PyObject *args;
  1012. {
  1013.     int n;
  1014.     PyObject *list;
  1015.     int itemsize = self->ob_descr->itemsize;
  1016.     if (!PyArg_Parse(args, "O", &list))
  1017.         return NULL;
  1018.     if (!PyList_Check(list)) {
  1019.         PyErr_SetString(PyExc_TypeError, "arg must be list");
  1020.         return NULL;
  1021.     }
  1022.     n = PyList_Size(list);
  1023.     if (n > 0) {
  1024.         char *item = self->ob_item;
  1025.         int i;
  1026.         PyMem_RESIZE(item, char, (self->ob_size + n) * itemsize);
  1027.         if (item == NULL) {
  1028.             PyErr_NoMemory();
  1029.             return NULL;
  1030.         }
  1031.         self->ob_item = item;
  1032.         self->ob_size += n;
  1033.         for (i = 0; i < n; i++) {
  1034.             PyObject *v = PyList_GetItem(list, i);
  1035.             if ((*self->ob_descr->setitem)(self,
  1036.                     self->ob_size - n + i, v) != 0) {
  1037.                 self->ob_size -= n;
  1038.                 PyMem_RESIZE(item, char,
  1039.                               self->ob_size * itemsize);
  1040.                 self->ob_item = item;
  1041.                 return NULL;
  1042.             }
  1043.         }
  1044.     }
  1045.     Py_INCREF(Py_None);
  1046.     return Py_None;
  1047. }
  1048.  
  1049. static char fromlist_doc [] =
  1050. "fromlist(list)\n\
  1051. \n\
  1052. Append items to array from list.";
  1053.  
  1054.  
  1055. static PyObject *
  1056. array_tolist(self, args)
  1057.     arrayobject *self;
  1058.     PyObject *args;
  1059. {
  1060.     PyObject *list = PyList_New(self->ob_size);
  1061.     int i;
  1062.     if (list == NULL)
  1063.         return NULL;
  1064.     for (i = 0; i < self->ob_size; i++) {
  1065.         PyObject *v = getarrayitem((PyObject *)self, i);
  1066.         if (v == NULL) {
  1067.             Py_DECREF(list);
  1068.             return NULL;
  1069.         }
  1070.         PyList_SetItem(list, i, v);
  1071.     }
  1072.     return list;
  1073. }
  1074.  
  1075. static char tolist_doc [] =
  1076. "tolist() -> list\n\
  1077. \n\
  1078. Convert array to an ordinary list with the same items.";
  1079.  
  1080.  
  1081. static PyObject *
  1082. array_fromstring(self, args)
  1083.     arrayobject *self;
  1084.     PyObject *args;
  1085. {
  1086.     char *str;
  1087.     int n;
  1088.     int itemsize = self->ob_descr->itemsize;
  1089.     if (!PyArg_Parse(args, "s#", &str, &n))
  1090.         return NULL;
  1091.     if (n % itemsize != 0) {
  1092.         PyErr_SetString(PyExc_ValueError,
  1093.                "string length not a multiple of item size");
  1094.         return NULL;
  1095.     }
  1096.     n = n / itemsize;
  1097.     if (n > 0) {
  1098.         char *item = self->ob_item;
  1099.         PyMem_RESIZE(item, char, (self->ob_size + n) * itemsize);
  1100.         if (item == NULL) {
  1101.             PyErr_NoMemory();
  1102.             return NULL;
  1103.         }
  1104.         self->ob_item = item;
  1105.         self->ob_size += n;
  1106.         memcpy(item + (self->ob_size - n) * itemsize,
  1107.                str, itemsize*n);
  1108.     }
  1109.     Py_INCREF(Py_None);
  1110.     return Py_None;
  1111. }
  1112.  
  1113. static char fromstring_doc [] =
  1114. "fromstring(string)\n\
  1115. \n\
  1116. Appends items from the string, interpreting it as an array of machine\n\
  1117. values,as if it had been read from a file using the fromfile() method).";
  1118.  
  1119.  
  1120. static PyObject *
  1121. array_tostring(self, args)
  1122.     arrayobject *self;
  1123.     PyObject *args;
  1124. {
  1125.     if (!PyArg_Parse(args, ""))
  1126.         return NULL;
  1127.     return PyString_FromStringAndSize(self->ob_item,
  1128.                     self->ob_size * self->ob_descr->itemsize);
  1129. }
  1130.  
  1131. static char tostring_doc [] =
  1132. "tostring() -> string\n\
  1133. \n\
  1134. Convert the array to an array of machine values and return the string\n\
  1135. representation.";
  1136.  
  1137. PyMethodDef array_methods[] = {
  1138.     {"append",    (PyCFunction)array_append, 0, append_doc},
  1139.     {"buffer_info", (PyCFunction)array_buffer_info, 0, buffer_info_doc},
  1140.     {"byteswap",    (PyCFunction)array_byteswap, 0, byteswap_doc},
  1141. /*    {"count",    (method)array_count},*/
  1142.     {"fromfile",    (PyCFunction)array_fromfile, 0, fromfile_doc},
  1143.     {"fromlist",    (PyCFunction)array_fromlist, 0, fromlist_doc},
  1144.     {"fromstring",    (PyCFunction)array_fromstring, 0, fromstring_doc},
  1145. /*    {"index",    (method)array_index},*/
  1146.     {"insert",    (PyCFunction)array_insert, 0, insert_doc},
  1147.     {"read",    (PyCFunction)array_fromfile, 0, fromfile_doc},
  1148. /*    {"remove",    (method)array_remove},*/
  1149.     {"reverse",    (PyCFunction)array_reverse, 0, reverse_doc},
  1150. /*    {"sort",    (method)array_sort},*/
  1151.     {"tofile",    (PyCFunction)array_tofile, 0, tofile_doc},
  1152.     {"tolist",    (PyCFunction)array_tolist, 0, tolist_doc},
  1153.     {"tostring",    (PyCFunction)array_tostring, 0, tostring_doc},
  1154.     {"write",    (PyCFunction)array_tofile, 0, tofile_doc},
  1155.     {NULL,        NULL}        /* sentinel */
  1156. };
  1157.  
  1158. static PyObject *
  1159. array_getattr(a, name)
  1160.     arrayobject *a;
  1161.     char *name;
  1162. {
  1163.     if (strcmp(name, "typecode") == 0) {
  1164.         char tc = a->ob_descr->typecode;
  1165.         return PyString_FromStringAndSize(&tc, 1);
  1166.     }
  1167.     if (strcmp(name, "itemsize") == 0) {
  1168.         return PyInt_FromLong((long)a->ob_descr->itemsize);
  1169.     }
  1170.     if (strcmp(name, "__members__") == 0) {
  1171.         PyObject *list = PyList_New(2);
  1172.         if (list) {
  1173.             PyList_SetItem(list, 0,
  1174.                        PyString_FromString("typecode"));
  1175.             PyList_SetItem(list, 1,
  1176.                        PyString_FromString("itemsize"));
  1177.             if (PyErr_Occurred()) {
  1178.                 Py_DECREF(list);
  1179.                 list = NULL;
  1180.             }
  1181.         }
  1182.         return list;
  1183.     }
  1184.     return Py_FindMethod(array_methods, (PyObject *)a, name);
  1185. }
  1186.  
  1187. static int
  1188. array_print(a, fp, flags)
  1189.     arrayobject *a;
  1190.     FILE *fp;
  1191.     int flags;
  1192. {
  1193.     int ok = 0;
  1194.     int i, len;
  1195.     PyObject *v;
  1196.     len = a->ob_size;
  1197.     if (len == 0) {
  1198.         fprintf(fp, "array('%c')", a->ob_descr->typecode);
  1199.         return ok;
  1200.     }
  1201.     if (a->ob_descr->typecode == 'c') {
  1202.         fprintf(fp, "array('c', ");
  1203.         v = array_tostring(a, (PyObject *)NULL);
  1204.         ok = PyObject_Print(v, fp, 0);
  1205.         Py_XDECREF(v);
  1206.         fprintf(fp, ")");
  1207.         return ok;
  1208.     }
  1209.     fprintf(fp, "array('%c', [", a->ob_descr->typecode);
  1210.     for (i = 0; i < len && ok == 0; i++) {
  1211.         if (i > 0)
  1212.             fprintf(fp, ", ");
  1213.         v = (a->ob_descr->getitem)(a, i);
  1214.         ok = PyObject_Print(v, fp, 0);
  1215.         Py_XDECREF(v);
  1216.     }
  1217.     fprintf(fp, "])");
  1218.     return ok;
  1219. }
  1220.  
  1221. static PyObject *
  1222. array_repr(a)
  1223.     arrayobject *a;
  1224. {
  1225.     char buf[256];
  1226.     PyObject *s, *t, *comma, *v;
  1227.     int i, len;
  1228.     len = a->ob_size;
  1229.     if (len == 0) {
  1230.         sprintf(buf, "array('%c')", a->ob_descr->typecode);
  1231.         return PyString_FromString(buf);
  1232.     }
  1233.     if (a->ob_descr->typecode == 'c') {
  1234.         sprintf(buf, "array('c', ");
  1235.         s = PyString_FromString(buf);
  1236.         v = array_tostring(a, (PyObject *)NULL);
  1237.         t = PyObject_Repr(v);
  1238.         Py_XDECREF(v);
  1239.         PyString_ConcatAndDel(&s, t);
  1240.         PyString_ConcatAndDel(&s, PyString_FromString(")"));
  1241.         return s;
  1242.     }
  1243.     sprintf(buf, "array('%c', [", a->ob_descr->typecode);
  1244.     s = PyString_FromString(buf);
  1245.     comma = PyString_FromString(", ");
  1246.     for (i = 0; i < len && !PyErr_Occurred(); i++) {
  1247.         if (i > 0)
  1248.             PyString_Concat(&s, comma);
  1249.         v = (a->ob_descr->getitem)(a, i);
  1250.         t = PyObject_Repr(v);
  1251.         Py_XDECREF(v);
  1252.         PyString_ConcatAndDel(&s, t);
  1253.     }
  1254.     Py_XDECREF(comma);
  1255.     PyString_ConcatAndDel(&s, PyString_FromString("])"));
  1256.     return s;
  1257. }
  1258.  
  1259. static int
  1260. array_buffer_getreadbuf(self, index, ptr)
  1261.     arrayobject *self;
  1262.     int index;
  1263.     const void **ptr;
  1264. {
  1265.     if ( index != 0 ) {
  1266.         PyErr_SetString(PyExc_SystemError,
  1267.                 "Accessing non-existent array segment");
  1268.         return -1;
  1269.     }
  1270.     *ptr = (void *)self->ob_item;
  1271.     return self->ob_size*self->ob_descr->itemsize;
  1272. }
  1273.  
  1274. static int
  1275. array_buffer_getwritebuf(self, index, ptr)
  1276.     arrayobject *self;
  1277.     int index;
  1278.     const void **ptr;
  1279. {
  1280.     if ( index != 0 ) {
  1281.         PyErr_SetString(PyExc_SystemError,
  1282.                 "Accessing non-existent array segment");
  1283.         return -1;
  1284.     }
  1285.     *ptr = (void *)self->ob_item;
  1286.     return self->ob_size*self->ob_descr->itemsize;
  1287. }
  1288.  
  1289. static int
  1290. array_buffer_getsegcount(self, lenp)
  1291.     arrayobject *self;
  1292.     int *lenp;
  1293. {
  1294.     if ( lenp )
  1295.         *lenp = self->ob_size*self->ob_descr->itemsize;
  1296.     return 1;
  1297. }
  1298.  
  1299. static PySequenceMethods array_as_sequence = {
  1300.     (inquiry)array_length,                /*sq_length*/
  1301.     (binaryfunc)array_concat,               /*sq_concat*/
  1302.     (intargfunc)array_repeat,        /*sq_repeat*/
  1303.     (intargfunc)array_item,                /*sq_item*/
  1304.     (intintargfunc)array_slice,        /*sq_slice*/
  1305.     (intobjargproc)array_ass_item,        /*sq_ass_item*/
  1306.     (intintobjargproc)array_ass_slice,    /*sq_ass_slice*/
  1307. };
  1308.  
  1309. static PyBufferProcs array_as_buffer = {
  1310.     (getreadbufferproc)array_buffer_getreadbuf,
  1311.     (getwritebufferproc)array_buffer_getwritebuf,
  1312.     (getsegcountproc)array_buffer_getsegcount,
  1313. };
  1314.  
  1315.  
  1316.  
  1317.  
  1318. static PyObject *
  1319. a_array(self, args)
  1320.     PyObject *self;
  1321.     PyObject *args;
  1322. {
  1323.     char c;
  1324.     PyObject *initial = NULL;
  1325.     struct arraydescr *descr;
  1326.     if (!PyArg_Parse(args, "c", &c)) {
  1327.         PyErr_Clear();
  1328.         if (!PyArg_Parse(args, "(cO)", &c, &initial))
  1329.             return NULL;
  1330.         if (!PyList_Check(initial) && !PyString_Check(initial)) {
  1331.             PyErr_SetString(PyExc_TypeError,
  1332.                  "array initializer must be list or string");
  1333.             return NULL;
  1334.         }
  1335.     }
  1336.     for (descr = descriptors; descr->typecode != '\0'; descr++) {
  1337.         if (descr->typecode == c) {
  1338.             PyObject *a;
  1339.             int len;
  1340.             if (initial == NULL || !PyList_Check(initial))
  1341.                 len = 0;
  1342.             else
  1343.                 len = PyList_Size(initial);
  1344.             a = newarrayobject(len, descr);
  1345.             if (a == NULL)
  1346.                 return NULL;
  1347.             if (len > 0) {
  1348.                 int i;
  1349.                 for (i = 0; i < len; i++) {
  1350.                     PyObject *v =
  1351.                              PyList_GetItem(initial, i);
  1352.                     if (setarrayitem(a, i, v) != 0) {
  1353.                         Py_DECREF(a);
  1354.                         return NULL;
  1355.                     }
  1356.                 }
  1357.             }
  1358.             if (initial != NULL && PyString_Check(initial)) {
  1359.                 PyObject *v =
  1360.                   array_fromstring((arrayobject *)a, initial);
  1361.                 if (v == NULL) {
  1362.                     Py_DECREF(a);
  1363.                     return NULL;
  1364.                 }
  1365.                 Py_DECREF(v);
  1366.             }
  1367.             return a;
  1368.         }
  1369.     }
  1370.     PyErr_SetString(PyExc_ValueError,
  1371.         "bad typecode (must be c, b, B, h, H, i, I, l, L, f or d)");
  1372.     return NULL;
  1373. }
  1374.  
  1375. static char a_array_doc [] =
  1376. "array(typecode [, initializer]) -> array\n\
  1377. \n\
  1378. Return a new array whose items are restricted by typecode, and\n\
  1379. initialized from the optional initializer value, which must be a list\n\
  1380. or a string.";
  1381.  
  1382. static PyMethodDef a_methods[] = {
  1383.     {"array",    a_array, 0, a_array_doc},
  1384.     {NULL,        NULL}        /* sentinel */
  1385. };
  1386.  
  1387. static char module_doc [] =
  1388. "This module defines a new object type which can efficiently represent\n\
  1389. an array of basic values: characters, integers, floating point\n\
  1390. numbers.  Arrays are sequence types and behave very much like lists,\n\
  1391. except that the type of objects stored in them is constrained.  The\n\
  1392. type is specified at object creation time by using a type code, which\n\
  1393. is a single character.  The following type codes are defined:\n\
  1394. \n\
  1395.     Type code   C Type             Minimum size in bytes \n\
  1396.     'c'         character          1 \n\
  1397.     'b'         signed integer     1 \n\
  1398.     'B'         unsigned integer   1 \n\
  1399.     'h'         signed integer     2 \n\
  1400.     'H'         unsigned integer   2 \n\
  1401.     'i'         signed integer     2 \n\
  1402.     'I'         unsigned integer   2 \n\
  1403.     'l'         signed integer     4 \n\
  1404.     'L'         unsigned integer   4 \n\
  1405.     'f'         floating point     4 \n\
  1406.     'd'         floating point     8 \n\
  1407. \n\
  1408. Functions:\n\
  1409. \n\
  1410. array(typecode [, initializer]) -- create a new array\n\
  1411. \n\
  1412. Special Objects:\n\
  1413. \n\
  1414. ArrayType -- type object for array objects\n\
  1415. ";
  1416.  
  1417. static char arraytype_doc [] =
  1418. "An array represents basic values and behave very much like lists, except\n\
  1419. the type of objects stored in them is constrained.\n\
  1420. \n\
  1421. Methods:\n\
  1422. \n\
  1423. append() -- append a new item to the end of the array\n\
  1424. buffer_info() -- return information giving the current memory info\n\
  1425. byteswap() -- byteswap all the items of the array\n\
  1426. fromfile() -- read items from a file object\n\
  1427. fromlist() -- append items from the list\n\
  1428. fromstring() -- append items from the string\n\
  1429. insert() -- insert a new item into the array at a provided position\n\
  1430. read() -- DEPRECATED, use fromfile()\n\
  1431. reverse() -- reverse the order of the items in the array\n\
  1432. tofile() -- write all items to a file object\n\
  1433. tolist() -- return the array converted to an ordinary list\n\
  1434. tostring() -- return the array converted to a string\n\
  1435. write() -- DEPRECATED, use tofile()\n\
  1436. \n\
  1437. Variables:\n\
  1438. \n\
  1439. typecode -- the typecode character used to create the array\n\
  1440. itemsize -- the length in bytes of one array item\n\
  1441. ";
  1442.  
  1443. statichere PyTypeObject Arraytype = {
  1444.     PyObject_HEAD_INIT(&PyType_Type)
  1445.     0,
  1446.     "array",
  1447.     sizeof(arrayobject),
  1448.     0,
  1449.     (destructor)array_dealloc,    /*tp_dealloc*/
  1450.     (printfunc)array_print,        /*tp_print*/
  1451.     (getattrfunc)array_getattr,    /*tp_getattr*/
  1452.     0,                /*tp_setattr*/
  1453.     (cmpfunc)array_compare,        /*tp_compare*/
  1454.     (reprfunc)array_repr,        /*tp_repr*/
  1455.     0,                /*tp_as_number*/
  1456.     &array_as_sequence,        /*tp_as_sequence*/
  1457.     0,                /*tp_as_mapping*/
  1458.     0,                 /*tp_hash*/
  1459.     0,                /*tp_call*/
  1460.     0,                /*tp_str*/
  1461.     0,                /*tp_getattro*/
  1462.     0,                /*tp_setattro*/
  1463.     &array_as_buffer,        /*tp_as_buffer*/
  1464.     0,                /*tp_xxx4*/
  1465.     arraytype_doc,            /*tp_doc*/
  1466. };
  1467.  
  1468. DL_EXPORT(void)
  1469. initarray()
  1470. {
  1471.     PyObject *m, *d;
  1472.     m = Py_InitModule3("array", a_methods, module_doc);
  1473.     d = PyModule_GetDict(m);
  1474.     PyDict_SetItemString(d, "ArrayType", (PyObject *)&Arraytype);
  1475.     /* No need to check the error here, the caller will do that */
  1476. }
  1477.