~ubuntu-branches/ubuntu/natty/python3.1/natty-security

« back to all changes in this revision

Viewing changes to Objects/memoryobject.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2009-07-23 18:52:17 UTC
  • mfrom: (1.1.2 upstream) (2.1.1 experimental)
  • Revision ID: james.westby@ubuntu.com-20090723185217-orj9vm2mappvz4ze
Tags: 3.1-0ubuntu1
* Python 3.1 final release.
* Update to the 3.1 release branch, 20090723.
* Add explicit build dependency on tk8.5-dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
505
505
    return get_shape0(&self->view);
506
506
}
507
507
 
 
508
/* Alternate version of memory_subcript that only accepts indices.
 
509
   Used by PySeqIter_New().
 
510
*/
 
511
static PyObject *
 
512
memory_item(PyMemoryViewObject *self, Py_ssize_t result)
 
513
{
 
514
    Py_buffer *view = &(self->view);
 
515
 
 
516
    if (view->ndim == 0) {
 
517
        PyErr_SetString(PyExc_IndexError,
 
518
                        "invalid indexing of 0-dim memory");
 
519
        return NULL;
 
520
    }
 
521
    if (view->ndim == 1) {
 
522
        /* Return a bytes object */
 
523
        char *ptr;
 
524
        ptr = (char *)view->buf;
 
525
        if (result < 0) {
 
526
            result += get_shape0(view);
 
527
        }
 
528
        if ((result < 0) || (result >= get_shape0(view))) {
 
529
            PyErr_SetString(PyExc_IndexError,
 
530
                                "index out of bounds");
 
531
            return NULL;
 
532
        }
 
533
        if (view->strides == NULL)
 
534
            ptr += view->itemsize * result;
 
535
        else
 
536
            ptr += view->strides[0] * result;
 
537
        if (view->suboffsets != NULL &&
 
538
            view->suboffsets[0] >= 0) {
 
539
            ptr = *((char **)ptr) + view->suboffsets[0];
 
540
        }
 
541
        return PyBytes_FromStringAndSize(ptr, view->itemsize);
 
542
    } else {
 
543
        /* Return a new memory-view object */
 
544
        Py_buffer newview;
 
545
        memset(&newview, 0, sizeof(newview));
 
546
        /* XXX:  This needs to be fixed so it actually returns a sub-view */
 
547
        return PyMemoryView_FromBuffer(&newview);
 
548
    }
 
549
}
 
550
 
508
551
/*
509
552
  mem[obj] returns a bytes object holding the data for one element if
510
553
           obj fully indexes the memory view or another memory-view object
536
579
        result = PyNumber_AsSsize_t(key, NULL);
537
580
        if (result == -1 && PyErr_Occurred())
538
581
                return NULL;
539
 
        if (view->ndim == 1) {
540
 
            /* Return a bytes object */
541
 
            char *ptr;
542
 
            ptr = (char *)view->buf;
543
 
            if (result < 0) {
544
 
                result += get_shape0(view);
545
 
            }
546
 
            if ((result < 0) || (result >= get_shape0(view))) {
547
 
                PyErr_SetString(PyExc_IndexError,
548
 
                                "index out of bounds");
549
 
                return NULL;
550
 
            }
551
 
            if (view->strides == NULL)
552
 
                ptr += view->itemsize * result;
553
 
            else
554
 
                ptr += view->strides[0] * result;
555
 
            if (view->suboffsets != NULL &&
556
 
                view->suboffsets[0] >= 0) {
557
 
                ptr = *((char **)ptr) + view->suboffsets[0];
558
 
            }
559
 
            return PyBytes_FromStringAndSize(ptr, view->itemsize);
560
 
        }
561
 
        else {
562
 
            /* Return a new memory-view object */
563
 
            Py_buffer newview;
564
 
            memset(&newview, 0, sizeof(newview));
565
 
            /* XXX:  This needs to be fixed so it
566
 
                         actually returns a sub-view
567
 
            */
568
 
            return PyMemoryView_FromBuffer(&newview);
569
 
        }
 
582
        return memory_item(self, result);
570
583
    }
571
584
    else if (PySlice_Check(key)) {
572
585
        Py_ssize_t start, stop, step, slicelength;
771
784
    (objobjargproc)memory_ass_sub,        /* mp_ass_subscript */
772
785
};
773
786
 
 
787
static PySequenceMethods memory_as_sequence = {
 
788
        0,                                  /* sq_length */
 
789
        0,                                  /* sq_concat */
 
790
        0,                                  /* sq_repeat */
 
791
        (ssizeargfunc)memory_item,          /* sq_item */
 
792
};
774
793
 
775
794
/* Buffer methods */
776
795
 
792
811
    0,                                        /* tp_reserved */
793
812
    (reprfunc)memory_repr,                    /* tp_repr */
794
813
    0,                                        /* tp_as_number */
795
 
    0,                                        /* tp_as_sequence */
 
814
    &memory_as_sequence,                      /* tp_as_sequence */
796
815
    &memory_as_mapping,                       /* tp_as_mapping */
797
816
    0,                                        /* tp_hash */
798
817
    0,                                        /* tp_call */