Mercurial > cpython-lru_cache-weakref
annotate Objects/memoryobject.c @ 50401:7239fbcdcd4f 3.0
Merged revisions 68103 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
........
r68103 | benjamin.peterson | 2008-12-31 08:51:07 -0600 (Wed, 31 Dec 2008) | 1 line
#4406 Directory has to inherit from commondialog.Dialog
........
| author | Benjamin Peterson <benjamin@python.org> |
|---|---|
| date | Wed, 31 Dec 2008 14:54:48 +0000 |
| parents | 725192c64c28 |
| children | 26045ed0c26e |
| rev | line source |
|---|---|
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
1 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
2 /* Memoryview object implementation */ |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
3 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
4 #include "Python.h" |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
5 |
|
48928
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
6 static void |
|
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
7 dup_buffer(Py_buffer *dest, Py_buffer *src) |
|
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
8 { |
|
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
9 *dest = *src; |
|
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
10 if (src->shape == &(src->len)) |
|
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
11 dest->shape = &(dest->len); |
|
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
12 if (src->strides == &(src->itemsize)) |
|
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
13 dest->strides = &(dest->itemsize); |
|
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
14 } |
|
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
15 |
|
50083
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
16 /* XXX The buffer API should mandate that the shape array be non-NULL, but |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
17 it would complicate some code since the (de)allocation semantics of shape |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
18 are not specified. */ |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
19 static Py_ssize_t |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
20 get_shape0(Py_buffer *buf) |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
21 { |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
22 if (buf->shape != NULL) |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
23 return buf->shape[0]; |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
24 assert(buf->ndim == 1 && buf->itemsize > 0); |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
25 return buf->len / buf->itemsize; |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
26 } |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
27 |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
28 static int |
|
43423
0476a2cc5658
Change PyBuffer to Py_buffer to be consistent with other non-object structures like Py_complex. Add some more functionality to the memoryview object.
Travis E. Oliphant <oliphant@enthought.com>
parents:
43384
diff
changeset
|
29 memory_getbuf(PyMemoryViewObject *self, Py_buffer *view, int flags) |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
30 { |
|
48928
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
31 int res = 0; |
|
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
32 /* XXX for whatever reason fixing the flags seems necessary */ |
|
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
33 if (self->view.readonly) |
|
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
34 flags &= ~PyBUF_WRITABLE; |
|
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
35 if (self->view.obj != NULL) |
|
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
36 res = PyObject_GetBuffer(self->view.obj, view, flags); |
|
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
37 if (view) |
|
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
38 dup_buffer(view, &self->view); |
|
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
39 return res; |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
40 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
41 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
42 static void |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
43 memory_releasebuf(PyMemoryViewObject *self, Py_buffer *view) |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
44 { |
|
48928
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
45 PyBuffer_Release(view); |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
46 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
47 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
48 PyDoc_STRVAR(memory_doc, |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
49 "memoryview(object)\n\ |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
50 \n\ |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
51 Create a new memoryview object which references the given object."); |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
52 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
53 PyObject * |
|
48770
7511f3e672a2
#3560: cleanup C memoryview API
Antoine Pitrou <solipsis@pitrou.net>
parents:
48683
diff
changeset
|
54 PyMemoryView_FromBuffer(Py_buffer *info) |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
55 { |
|
43423
0476a2cc5658
Change PyBuffer to Py_buffer to be consistent with other non-object structures like Py_complex. Add some more functionality to the memoryview object.
Travis E. Oliphant <oliphant@enthought.com>
parents:
43384
diff
changeset
|
56 PyMemoryViewObject *mview; |
|
0476a2cc5658
Change PyBuffer to Py_buffer to be consistent with other non-object structures like Py_complex. Add some more functionality to the memoryview object.
Travis E. Oliphant <oliphant@enthought.com>
parents:
43384
diff
changeset
|
57 |
|
48928
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
58 mview = (PyMemoryViewObject *) |
|
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
59 PyObject_GC_New(PyMemoryViewObject, &PyMemoryView_Type); |
|
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
60 if (mview == NULL) |
|
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
61 return NULL; |
|
43423
0476a2cc5658
Change PyBuffer to Py_buffer to be consistent with other non-object structures like Py_complex. Add some more functionality to the memoryview object.
Travis E. Oliphant <oliphant@enthought.com>
parents:
43384
diff
changeset
|
62 mview->base = NULL; |
|
48928
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
63 dup_buffer(&mview->view, info); |
|
48782
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
64 /* NOTE: mview->view.obj should already have been incref'ed as |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
65 part of PyBuffer_FillInfo(). */ |
|
48928
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
66 _PyObject_GC_TRACK(mview); |
|
43423
0476a2cc5658
Change PyBuffer to Py_buffer to be consistent with other non-object structures like Py_complex. Add some more functionality to the memoryview object.
Travis E. Oliphant <oliphant@enthought.com>
parents:
43384
diff
changeset
|
67 return (PyObject *)mview; |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
68 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
69 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
70 PyObject * |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
71 PyMemoryView_FromObject(PyObject *base) |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
72 { |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
73 PyMemoryViewObject *mview; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
74 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
75 if (!PyObject_CheckBuffer(base)) { |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
76 PyErr_SetString(PyExc_TypeError, |
|
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
77 "cannot make memory view because object does " |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
78 "not have the buffer interface"); |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
79 return NULL; |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
80 } |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
81 |
|
48928
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
82 mview = (PyMemoryViewObject *) |
|
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
83 PyObject_GC_New(PyMemoryViewObject, &PyMemoryView_Type); |
|
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
84 if (mview == NULL) |
|
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
85 return NULL; |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
86 |
|
42671
fca354160458
SF #1777057, fix memoryview('test') so it works in debug mode too.
Neal Norwitz <nnorwitz@gmail.com>
parents:
42669
diff
changeset
|
87 mview->base = NULL; |
|
48623
bf50085fbcd3
#2538: bytes objects can only provide read-only buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
48330
diff
changeset
|
88 if (PyObject_GetBuffer(base, &(mview->view), PyBUF_FULL_RO) < 0) { |
|
42671
fca354160458
SF #1777057, fix memoryview('test') so it works in debug mode too.
Neal Norwitz <nnorwitz@gmail.com>
parents:
42669
diff
changeset
|
89 Py_DECREF(mview); |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
90 return NULL; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
91 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
92 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
93 mview->base = base; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
94 Py_INCREF(base); |
|
48928
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
95 _PyObject_GC_TRACK(mview); |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
96 return (PyObject *)mview; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
97 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
98 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
99 static PyObject * |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
100 memory_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds) |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
101 { |
|
43793
2d8d6855a602
Fixed memoryview constructor. It allowed arbitrary keyword arguments. The bug was found by mykhal from #python. I've also added a small test case in the new test_memoryview.py
Christian Heimes <christian@cheimes.de>
parents:
43687
diff
changeset
|
102 PyObject *obj; |
|
2d8d6855a602
Fixed memoryview constructor. It allowed arbitrary keyword arguments. The bug was found by mykhal from #python. I've also added a small test case in the new test_memoryview.py
Christian Heimes <christian@cheimes.de>
parents:
43687
diff
changeset
|
103 static char *kwlist[] = {"object", 0}; |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
104 |
|
43793
2d8d6855a602
Fixed memoryview constructor. It allowed arbitrary keyword arguments. The bug was found by mykhal from #python. I've also added a small test case in the new test_memoryview.py
Christian Heimes <christian@cheimes.de>
parents:
43687
diff
changeset
|
105 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:memoryview", kwlist, |
|
2d8d6855a602
Fixed memoryview constructor. It allowed arbitrary keyword arguments. The bug was found by mykhal from #python. I've also added a small test case in the new test_memoryview.py
Christian Heimes <christian@cheimes.de>
parents:
43687
diff
changeset
|
106 &obj)) { |
|
2d8d6855a602
Fixed memoryview constructor. It allowed arbitrary keyword arguments. The bug was found by mykhal from #python. I've also added a small test case in the new test_memoryview.py
Christian Heimes <christian@cheimes.de>
parents:
43687
diff
changeset
|
107 return NULL; |
|
2d8d6855a602
Fixed memoryview constructor. It allowed arbitrary keyword arguments. The bug was found by mykhal from #python. I've also added a small test case in the new test_memoryview.py
Christian Heimes <christian@cheimes.de>
parents:
43687
diff
changeset
|
108 } |
|
2d8d6855a602
Fixed memoryview constructor. It allowed arbitrary keyword arguments. The bug was found by mykhal from #python. I've also added a small test case in the new test_memoryview.py
Christian Heimes <christian@cheimes.de>
parents:
43687
diff
changeset
|
109 |
|
2d8d6855a602
Fixed memoryview constructor. It allowed arbitrary keyword arguments. The bug was found by mykhal from #python. I've also added a small test case in the new test_memoryview.py
Christian Heimes <christian@cheimes.de>
parents:
43687
diff
changeset
|
110 return PyMemoryView_FromObject(obj); |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
111 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
112 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
113 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
114 static void |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
115 _strided_copy_nd(char *dest, char *src, int nd, Py_ssize_t *shape, |
|
43687
6ab64f6b4c03
Fix some Py_ssize_t warnings on Win64 that were probably bugs
Neal Norwitz <nnorwitz@gmail.com>
parents:
43550
diff
changeset
|
116 Py_ssize_t *strides, Py_ssize_t itemsize, char fort) |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
117 { |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
118 int k; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
119 Py_ssize_t outstride; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
120 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
121 if (nd==0) { |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
122 memcpy(dest, src, itemsize); |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
123 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
124 else if (nd == 1) { |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
125 for (k = 0; k<shape[0]; k++) { |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
126 memcpy(dest, src, itemsize); |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
127 dest += itemsize; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
128 src += strides[0]; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
129 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
130 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
131 else { |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
132 if (fort == 'F') { |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
133 /* Copy first dimension first, |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
134 second dimension second, etc... |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
135 Set up the recursive loop backwards so that final |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
136 dimension is actually copied last. |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
137 */ |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
138 outstride = itemsize; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
139 for (k=1; k<nd-1;k++) { |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
140 outstride *= shape[k]; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
141 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
142 for (k=0; k<shape[nd-1]; k++) { |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
143 _strided_copy_nd(dest, src, nd-1, shape, |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
144 strides, itemsize, fort); |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
145 dest += outstride; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
146 src += strides[nd-1]; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
147 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
148 } |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
149 |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
150 else { |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
151 /* Copy last dimension first, |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
152 second-to-last dimension second, etc. |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
153 Set up the recursion so that the |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
154 first dimension is copied last |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
155 */ |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
156 outstride = itemsize; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
157 for (k=1; k < nd; k++) { |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
158 outstride *= shape[k]; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
159 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
160 for (k=0; k<shape[0]; k++) { |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
161 _strided_copy_nd(dest, src, nd-1, shape+1, |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
162 strides+1, itemsize, |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
163 fort); |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
164 dest += outstride; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
165 src += strides[0]; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
166 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
167 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
168 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
169 return; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
170 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
171 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
172 void _add_one_to_index_F(int nd, Py_ssize_t *index, Py_ssize_t *shape); |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
173 void _add_one_to_index_C(int nd, Py_ssize_t *index, Py_ssize_t *shape); |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
174 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
175 static int |
|
43423
0476a2cc5658
Change PyBuffer to Py_buffer to be consistent with other non-object structures like Py_complex. Add some more functionality to the memoryview object.
Travis E. Oliphant <oliphant@enthought.com>
parents:
43384
diff
changeset
|
176 _indirect_copy_nd(char *dest, Py_buffer *view, char fort) |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
177 { |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
178 Py_ssize_t *indices; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
179 int k; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
180 Py_ssize_t elements; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
181 char *ptr; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
182 void (*func)(int, Py_ssize_t *, Py_ssize_t *); |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
183 |
|
48330
ac854cde3135
Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47500
diff
changeset
|
184 if (view->ndim > PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) { |
|
ac854cde3135
Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47500
diff
changeset
|
185 PyErr_NoMemory(); |
|
ac854cde3135
Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47500
diff
changeset
|
186 return -1; |
|
ac854cde3135
Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47500
diff
changeset
|
187 } |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
188 |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
189 indices = (Py_ssize_t *)PyMem_Malloc(sizeof(Py_ssize_t)*view->ndim); |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
190 if (indices == NULL) { |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
191 PyErr_NoMemory(); |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
192 return -1; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
193 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
194 for (k=0; k<view->ndim;k++) { |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
195 indices[k] = 0; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
196 } |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
197 |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
198 elements = 1; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
199 for (k=0; k<view->ndim; k++) { |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
200 elements *= view->shape[k]; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
201 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
202 if (fort == 'F') { |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
203 func = _add_one_to_index_F; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
204 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
205 else { |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
206 func = _add_one_to_index_C; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
207 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
208 while (elements--) { |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
209 func(view->ndim, indices, view->shape); |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
210 ptr = PyBuffer_GetPointer(view, indices); |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
211 memcpy(dest, ptr, view->itemsize); |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
212 dest += view->itemsize; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
213 } |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
214 |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
215 PyMem_Free(indices); |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
216 return 0; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
217 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
218 |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
219 /* |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
220 Get a the data from an object as a contiguous chunk of memory (in |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
221 either 'C' or 'F'ortran order) even if it means copying it into a |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
222 separate memory area. |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
223 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
224 Returns a new reference to a Memory view object. If no copy is needed, |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
225 the memory view object points to the original memory and holds a |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
226 lock on the original. If a copy is needed, then the memory view object |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
227 points to a brand-new Bytes object (and holds a memory lock on it). |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
228 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
229 buffertype |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
230 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
231 PyBUF_READ buffer only needs to be read-only |
|
43384
4e57c36f6d2e
Fixing the spelling of "writeable" to "writable", particularly PyBUF_WRITEABLE.
Sean Reifscheider <jafo@tummy.com>
parents:
42671
diff
changeset
|
232 PyBUF_WRITE buffer needs to be writable (give error if not contiguous) |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
233 PyBUF_SHADOW buffer needs to be writable so shadow it with |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
234 a contiguous buffer if it is not. The view will point to |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
235 the shadow buffer which can be written to and then |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
236 will be copied back into the other buffer when the memory |
|
48623
bf50085fbcd3
#2538: bytes objects can only provide read-only buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
48330
diff
changeset
|
237 view is de-allocated. While the shadow buffer is |
|
bf50085fbcd3
#2538: bytes objects can only provide read-only buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
48330
diff
changeset
|
238 being used, it will have an exclusive write lock on |
|
bf50085fbcd3
#2538: bytes objects can only provide read-only buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
48330
diff
changeset
|
239 the original buffer. |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
240 */ |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
241 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
242 PyObject * |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
243 PyMemoryView_GetContiguous(PyObject *obj, int buffertype, char fort) |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
244 { |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
245 PyMemoryViewObject *mem; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
246 PyObject *bytes; |
|
43423
0476a2cc5658
Change PyBuffer to Py_buffer to be consistent with other non-object structures like Py_complex. Add some more functionality to the memoryview object.
Travis E. Oliphant <oliphant@enthought.com>
parents:
43384
diff
changeset
|
247 Py_buffer *view; |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
248 int flags; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
249 char *dest; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
250 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
251 if (!PyObject_CheckBuffer(obj)) { |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
252 PyErr_SetString(PyExc_TypeError, |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
253 "object does not have the buffer interface"); |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
254 return NULL; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
255 } |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
256 |
|
48928
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
257 mem = PyObject_GC_New(PyMemoryViewObject, &PyMemoryView_Type); |
|
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
258 if (mem == NULL) |
|
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
259 return NULL; |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
260 |
|
48770
7511f3e672a2
#3560: cleanup C memoryview API
Antoine Pitrou <solipsis@pitrou.net>
parents:
48683
diff
changeset
|
261 view = &mem->view; |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
262 flags = PyBUF_FULL_RO; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
263 switch(buffertype) { |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
264 case PyBUF_WRITE: |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
265 flags = PyBUF_FULL; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
266 break; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
267 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
268 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
269 if (PyObject_GetBuffer(obj, view, flags) != 0) { |
|
48928
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
270 Py_DECREF(mem); |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
271 return NULL; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
272 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
273 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
274 if (PyBuffer_IsContiguous(view, fort)) { |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
275 /* no copy needed */ |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
276 Py_INCREF(obj); |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
277 mem->base = obj; |
|
48928
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
278 _PyObject_GC_TRACK(mem); |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
279 return (PyObject *)mem; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
280 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
281 /* otherwise a copy is needed */ |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
282 if (buffertype == PyBUF_WRITE) { |
|
48928
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
283 Py_DECREF(mem); |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
284 PyErr_SetString(PyExc_BufferError, |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
285 "writable contiguous buffer requested " |
|
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
286 "for a non-contiguousobject."); |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
287 return NULL; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
288 } |
|
48782
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
289 bytes = PyBytes_FromStringAndSize(NULL, view->len); |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
290 if (bytes == NULL) { |
|
48928
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
291 Py_DECREF(mem); |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
292 return NULL; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
293 } |
|
48782
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
294 dest = PyBytes_AS_STRING(bytes); |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
295 /* different copying strategy depending on whether |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
296 or not any pointer de-referencing is needed |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
297 */ |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
298 /* strided or in-direct copy */ |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
299 if (view->suboffsets==NULL) { |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
300 _strided_copy_nd(dest, view->buf, view->ndim, view->shape, |
|
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
301 view->strides, view->itemsize, fort); |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
302 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
303 else { |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
304 if (_indirect_copy_nd(dest, view, fort) < 0) { |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
305 Py_DECREF(bytes); |
|
48928
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
306 Py_DECREF(mem); |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
307 return NULL; |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
308 } |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
309 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
310 if (buffertype == PyBUF_SHADOW) { |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
311 /* return a shadowed memory-view object */ |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
312 view->buf = dest; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
313 mem->base = PyTuple_Pack(2, obj, bytes); |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
314 Py_DECREF(bytes); |
|
43423
0476a2cc5658
Change PyBuffer to Py_buffer to be consistent with other non-object structures like Py_complex. Add some more functionality to the memoryview object.
Travis E. Oliphant <oliphant@enthought.com>
parents:
43384
diff
changeset
|
315 if (mem->base == NULL) { |
|
48928
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
316 Py_DECREF(mem); |
|
43423
0476a2cc5658
Change PyBuffer to Py_buffer to be consistent with other non-object structures like Py_complex. Add some more functionality to the memoryview object.
Travis E. Oliphant <oliphant@enthought.com>
parents:
43384
diff
changeset
|
317 return NULL; |
|
0476a2cc5658
Change PyBuffer to Py_buffer to be consistent with other non-object structures like Py_complex. Add some more functionality to the memoryview object.
Travis E. Oliphant <oliphant@enthought.com>
parents:
43384
diff
changeset
|
318 } |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
319 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
320 else { |
|
48928
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
321 PyBuffer_Release(view); /* XXX ? */ |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
322 /* steal the reference */ |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
323 mem->base = bytes; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
324 } |
|
48928
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
325 _PyObject_GC_TRACK(mem); |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
326 return (PyObject *)mem; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
327 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
328 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
329 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
330 static PyObject * |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
331 memory_format_get(PyMemoryViewObject *self) |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
332 { |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
333 return PyUnicode_FromString(self->view.format); |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
334 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
335 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
336 static PyObject * |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
337 memory_itemsize_get(PyMemoryViewObject *self) |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
338 { |
|
44099
f324631462a2
Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents:
43793
diff
changeset
|
339 return PyLong_FromSsize_t(self->view.itemsize); |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
340 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
341 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
342 static PyObject * |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
343 _IntTupleFromSsizet(int len, Py_ssize_t *vals) |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
344 { |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
345 int i; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
346 PyObject *o; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
347 PyObject *intTuple; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
348 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
349 if (vals == NULL) { |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
350 Py_INCREF(Py_None); |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
351 return Py_None; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
352 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
353 intTuple = PyTuple_New(len); |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
354 if (!intTuple) return NULL; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
355 for(i=0; i<len; i++) { |
|
44099
f324631462a2
Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents:
43793
diff
changeset
|
356 o = PyLong_FromSsize_t(vals[i]); |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
357 if (!o) { |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
358 Py_DECREF(intTuple); |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
359 return NULL; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
360 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
361 PyTuple_SET_ITEM(intTuple, i, o); |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
362 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
363 return intTuple; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
364 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
365 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
366 static PyObject * |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
367 memory_shape_get(PyMemoryViewObject *self) |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
368 { |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
369 return _IntTupleFromSsizet(self->view.ndim, self->view.shape); |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
370 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
371 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
372 static PyObject * |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
373 memory_strides_get(PyMemoryViewObject *self) |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
374 { |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
375 return _IntTupleFromSsizet(self->view.ndim, self->view.strides); |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
376 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
377 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
378 static PyObject * |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
379 memory_suboffsets_get(PyMemoryViewObject *self) |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
380 { |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
381 return _IntTupleFromSsizet(self->view.ndim, self->view.suboffsets); |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
382 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
383 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
384 static PyObject * |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
385 memory_readonly_get(PyMemoryViewObject *self) |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
386 { |
|
42671
fca354160458
SF #1777057, fix memoryview('test') so it works in debug mode too.
Neal Norwitz <nnorwitz@gmail.com>
parents:
42669
diff
changeset
|
387 return PyBool_FromLong(self->view.readonly); |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
388 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
389 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
390 static PyObject * |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
391 memory_ndim_get(PyMemoryViewObject *self) |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
392 { |
|
44099
f324631462a2
Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents:
43793
diff
changeset
|
393 return PyLong_FromLong(self->view.ndim); |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
394 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
395 |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
396 static PyGetSetDef memory_getsetlist[] ={ |
|
42671
fca354160458
SF #1777057, fix memoryview('test') so it works in debug mode too.
Neal Norwitz <nnorwitz@gmail.com>
parents:
42669
diff
changeset
|
397 {"format", (getter)memory_format_get, NULL, NULL}, |
|
fca354160458
SF #1777057, fix memoryview('test') so it works in debug mode too.
Neal Norwitz <nnorwitz@gmail.com>
parents:
42669
diff
changeset
|
398 {"itemsize", (getter)memory_itemsize_get, NULL, NULL}, |
|
fca354160458
SF #1777057, fix memoryview('test') so it works in debug mode too.
Neal Norwitz <nnorwitz@gmail.com>
parents:
42669
diff
changeset
|
399 {"shape", (getter)memory_shape_get, NULL, NULL}, |
|
fca354160458
SF #1777057, fix memoryview('test') so it works in debug mode too.
Neal Norwitz <nnorwitz@gmail.com>
parents:
42669
diff
changeset
|
400 {"strides", (getter)memory_strides_get, NULL, NULL}, |
|
fca354160458
SF #1777057, fix memoryview('test') so it works in debug mode too.
Neal Norwitz <nnorwitz@gmail.com>
parents:
42669
diff
changeset
|
401 {"suboffsets", (getter)memory_suboffsets_get, NULL, NULL}, |
|
fca354160458
SF #1777057, fix memoryview('test') so it works in debug mode too.
Neal Norwitz <nnorwitz@gmail.com>
parents:
42669
diff
changeset
|
402 {"readonly", (getter)memory_readonly_get, NULL, NULL}, |
|
fca354160458
SF #1777057, fix memoryview('test') so it works in debug mode too.
Neal Norwitz <nnorwitz@gmail.com>
parents:
42669
diff
changeset
|
403 {"ndim", (getter)memory_ndim_get, NULL, NULL}, |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
404 {NULL, NULL, NULL, NULL}, |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
405 }; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
406 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
407 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
408 static PyObject * |
|
42669
2c51be9904de
Code review of the new buffer protocol. Mostly add questions that should
Neal Norwitz <nnorwitz@gmail.com>
parents:
42665
diff
changeset
|
409 memory_tobytes(PyMemoryViewObject *mem, PyObject *noargs) |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
410 { |
|
48782
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
411 return PyObject_CallFunctionObjArgs( |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
412 (PyObject *) &PyBytes_Type, mem, NULL); |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
413 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
414 |
|
48782
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
415 /* TODO: rewrite this function using the struct module to unpack |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
416 each buffer item */ |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
417 |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
418 static PyObject * |
|
42669
2c51be9904de
Code review of the new buffer protocol. Mostly add questions that should
Neal Norwitz <nnorwitz@gmail.com>
parents:
42665
diff
changeset
|
419 memory_tolist(PyMemoryViewObject *mem, PyObject *noargs) |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
420 { |
|
48782
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
421 Py_buffer *view = &(mem->view); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
422 Py_ssize_t i; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
423 PyObject *res, *item; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
424 char *buf; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
425 |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
426 if (strcmp(view->format, "B") || view->itemsize != 1) { |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
427 PyErr_SetString(PyExc_NotImplementedError, |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
428 "tolist() only supports byte views"); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
429 return NULL; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
430 } |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
431 if (view->ndim != 1) { |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
432 PyErr_SetString(PyExc_NotImplementedError, |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
433 "tolist() only supports one-dimensional objects"); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
434 return NULL; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
435 } |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
436 res = PyList_New(view->len); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
437 if (res == NULL) |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
438 return NULL; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
439 buf = view->buf; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
440 for (i = 0; i < view->len; i++) { |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
441 item = PyLong_FromUnsignedLong((unsigned char) *buf); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
442 if (item == NULL) { |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
443 Py_DECREF(res); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
444 return NULL; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
445 } |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
446 PyList_SET_ITEM(res, i, item); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
447 buf++; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
448 } |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
449 return res; |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
450 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
451 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
452 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
453 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
454 static PyMethodDef memory_methods[] = { |
|
42669
2c51be9904de
Code review of the new buffer protocol. Mostly add questions that should
Neal Norwitz <nnorwitz@gmail.com>
parents:
42665
diff
changeset
|
455 {"tobytes", (PyCFunction)memory_tobytes, METH_NOARGS, NULL}, |
|
2c51be9904de
Code review of the new buffer protocol. Mostly add questions that should
Neal Norwitz <nnorwitz@gmail.com>
parents:
42665
diff
changeset
|
456 {"tolist", (PyCFunction)memory_tolist, METH_NOARGS, NULL}, |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
457 {NULL, NULL} /* sentinel */ |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
458 }; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
459 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
460 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
461 static void |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
462 memory_dealloc(PyMemoryViewObject *self) |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
463 { |
|
48928
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
464 _PyObject_GC_UNTRACK(self); |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48623
diff
changeset
|
465 if (self->view.obj != NULL) { |
|
48782
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
466 if (self->base && PyTuple_Check(self->base)) { |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
467 /* Special case when first element is generic object |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
468 with buffer interface and the second element is a |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
469 contiguous "shadow" that must be copied back into |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
470 the data areay of the first tuple element before |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
471 releasing the buffer on the first element. |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
472 */ |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
473 |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
474 PyObject_CopyData(PyTuple_GET_ITEM(self->base,0), |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
475 PyTuple_GET_ITEM(self->base,1)); |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
476 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
477 /* The view member should have readonly == -1 in |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
478 this instance indicating that the memory can |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
479 be "locked" and was locked and will be unlocked |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
480 again after this call. |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
481 */ |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48623
diff
changeset
|
482 PyBuffer_Release(&(self->view)); |
|
42671
fca354160458
SF #1777057, fix memoryview('test') so it works in debug mode too.
Neal Norwitz <nnorwitz@gmail.com>
parents:
42669
diff
changeset
|
483 } |
|
fca354160458
SF #1777057, fix memoryview('test') so it works in debug mode too.
Neal Norwitz <nnorwitz@gmail.com>
parents:
42669
diff
changeset
|
484 else { |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48623
diff
changeset
|
485 PyBuffer_Release(&(self->view)); |
|
42671
fca354160458
SF #1777057, fix memoryview('test') so it works in debug mode too.
Neal Norwitz <nnorwitz@gmail.com>
parents:
42669
diff
changeset
|
486 } |
|
fca354160458
SF #1777057, fix memoryview('test') so it works in debug mode too.
Neal Norwitz <nnorwitz@gmail.com>
parents:
42669
diff
changeset
|
487 Py_CLEAR(self->base); |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
488 } |
|
48928
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
489 PyObject_GC_Del(self); |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
490 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
491 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
492 static PyObject * |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
493 memory_repr(PyMemoryViewObject *self) |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
494 { |
|
43550
4ea945cbbb6d
Fix problems with memoryview object. There is still more to do to finish PEP 3118. The memory-view object needs to be fleshed out and the struct module needs to be modified.
Travis E. Oliphant <oliphant@enthought.com>
parents:
43488
diff
changeset
|
495 return PyUnicode_FromFormat("<memory at %p>", self); |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
496 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
497 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
498 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
499 static PyObject * |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
500 memory_str(PyMemoryViewObject *self) |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
501 { |
|
43423
0476a2cc5658
Change PyBuffer to Py_buffer to be consistent with other non-object structures like Py_complex. Add some more functionality to the memoryview object.
Travis E. Oliphant <oliphant@enthought.com>
parents:
43384
diff
changeset
|
502 Py_buffer view; |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
503 PyObject *res; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
504 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
505 if (PyObject_GetBuffer((PyObject *)self, &view, PyBUF_FULL) < 0) |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
506 return NULL; |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
507 |
|
48782
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
508 res = PyBytes_FromStringAndSize(NULL, view.len); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
509 PyBuffer_ToContiguous(PyBytes_AS_STRING(res), &view, view.len, 'C'); |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48623
diff
changeset
|
510 PyBuffer_Release(&view); |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
511 return res; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
512 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
513 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
514 /* Sequence methods */ |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
515 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
516 static Py_ssize_t |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
517 memory_length(PyMemoryViewObject *self) |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
518 { |
|
43423
0476a2cc5658
Change PyBuffer to Py_buffer to be consistent with other non-object structures like Py_complex. Add some more functionality to the memoryview object.
Travis E. Oliphant <oliphant@enthought.com>
parents:
43384
diff
changeset
|
519 Py_buffer view; |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
520 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
521 if (PyObject_GetBuffer((PyObject *)self, &view, PyBUF_FULL) < 0) |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
522 return -1; |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48623
diff
changeset
|
523 PyBuffer_Release(&view); |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
524 return view.len; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
525 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
526 |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
527 /* |
|
43423
0476a2cc5658
Change PyBuffer to Py_buffer to be consistent with other non-object structures like Py_complex. Add some more functionality to the memoryview object.
Travis E. Oliphant <oliphant@enthought.com>
parents:
43384
diff
changeset
|
528 mem[obj] returns a bytes object holding the data for one element if |
|
0476a2cc5658
Change PyBuffer to Py_buffer to be consistent with other non-object structures like Py_complex. Add some more functionality to the memoryview object.
Travis E. Oliphant <oliphant@enthought.com>
parents:
43384
diff
changeset
|
529 obj fully indexes the memory view or another memory-view object |
|
0476a2cc5658
Change PyBuffer to Py_buffer to be consistent with other non-object structures like Py_complex. Add some more functionality to the memoryview object.
Travis E. Oliphant <oliphant@enthought.com>
parents:
43384
diff
changeset
|
530 if it does not. |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
531 |
|
43423
0476a2cc5658
Change PyBuffer to Py_buffer to be consistent with other non-object structures like Py_complex. Add some more functionality to the memoryview object.
Travis E. Oliphant <oliphant@enthought.com>
parents:
43384
diff
changeset
|
532 0-d memory-view objects can be referenced using ... or () but |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
533 not with anything else. |
|
43423
0476a2cc5658
Change PyBuffer to Py_buffer to be consistent with other non-object structures like Py_complex. Add some more functionality to the memoryview object.
Travis E. Oliphant <oliphant@enthought.com>
parents:
43384
diff
changeset
|
534 */ |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
535 static PyObject * |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
536 memory_subscript(PyMemoryViewObject *self, PyObject *key) |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
537 { |
|
50083
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
538 Py_buffer *view; |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
539 view = &(self->view); |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
540 |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
541 if (view->ndim == 0) { |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
542 if (key == Py_Ellipsis || |
|
43423
0476a2cc5658
Change PyBuffer to Py_buffer to be consistent with other non-object structures like Py_complex. Add some more functionality to the memoryview object.
Travis E. Oliphant <oliphant@enthought.com>
parents:
43384
diff
changeset
|
543 (PyTuple_Check(key) && PyTuple_GET_SIZE(key)==0)) { |
|
50083
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
544 Py_INCREF(self); |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
545 return (PyObject *)self; |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
546 } |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
547 else { |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
548 PyErr_SetString(PyExc_IndexError, |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
549 "invalid indexing of 0-dim memory"); |
|
50083
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
550 return NULL; |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
551 } |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
552 } |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
553 if (PyIndex_Check(key)) { |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
554 Py_ssize_t result; |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
555 result = PyNumber_AsSsize_t(key, NULL); |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
556 if (result == -1 && PyErr_Occurred()) |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
557 return NULL; |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
558 if (view->ndim == 1) { |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
559 /* Return a bytes object */ |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
560 char *ptr; |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
561 ptr = (char *)view->buf; |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
562 if (result < 0) { |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
563 result += get_shape0(view); |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
564 } |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
565 if ((result < 0) || (result >= get_shape0(view))) { |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
566 PyErr_SetString(PyExc_IndexError, |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
567 "index out of bounds"); |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
568 return NULL; |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
569 } |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
570 if (view->strides == NULL) |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
571 ptr += view->itemsize * result; |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
572 else |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
573 ptr += view->strides[0] * result; |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
574 if (view->suboffsets != NULL && |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
575 view->suboffsets[0] >= 0) |
|
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
576 { |
|
50083
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
577 ptr = *((char **)ptr) + view->suboffsets[0]; |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
578 } |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
579 return PyBytes_FromStringAndSize(ptr, view->itemsize); |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
580 } |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
581 else { |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
582 /* Return a new memory-view object */ |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
583 Py_buffer newview; |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
584 memset(&newview, 0, sizeof(newview)); |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
585 /* XXX: This needs to be fixed so it |
|
43550
4ea945cbbb6d
Fix problems with memoryview object. There is still more to do to finish PEP 3118. The memory-view object needs to be fleshed out and the struct module needs to be modified.
Travis E. Oliphant <oliphant@enthought.com>
parents:
43488
diff
changeset
|
586 actually returns a sub-view |
|
50083
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
587 */ |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
588 return PyMemoryView_FromBuffer(&newview); |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
589 } |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
590 } |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
591 else if (PySlice_Check(key)) { |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
592 Py_ssize_t start, stop, step, slicelength; |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
593 |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
594 if (PySlice_GetIndicesEx((PySliceObject*)key, get_shape0(view), |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
595 &start, &stop, &step, &slicelength) < 0) { |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
596 return NULL; |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
597 } |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
598 |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
599 if (step == 1 && view->ndim == 1) { |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
600 Py_buffer newview; |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
601 void *newbuf = (char *) view->buf |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
602 + start * view->itemsize; |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
603 int newflags = view->readonly |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
604 ? PyBUF_CONTIG_RO : PyBUF_CONTIG; |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
605 |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
606 /* XXX There should be an API to create a subbuffer */ |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
607 if (view->obj != NULL) { |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
608 if (PyObject_GetBuffer(view->obj, |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
609 &newview, newflags) == -1) |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
610 return NULL; |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
611 } |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
612 else { |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
613 newview = *view; |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
614 } |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
615 newview.buf = newbuf; |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
616 newview.len = slicelength; |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
617 newview.format = view->format; |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
618 if (view->shape == &(view->len)) |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
619 newview.shape = &(newview.len); |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
620 if (view->strides == &(view->itemsize)) |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
621 newview.strides = &(newview.itemsize); |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
622 return PyMemoryView_FromBuffer(&newview); |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
623 } |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
624 PyErr_SetNone(PyExc_NotImplementedError); |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
625 return NULL; |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
626 } |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
627 PyErr_Format(PyExc_TypeError, |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
628 "cannot index memory using \"%.200s\"", |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
629 key->ob_type->tp_name); |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
630 return NULL; |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
631 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
632 |
|
43550
4ea945cbbb6d
Fix problems with memoryview object. There is still more to do to finish PEP 3118. The memory-view object needs to be fleshed out and the struct module needs to be modified.
Travis E. Oliphant <oliphant@enthought.com>
parents:
43488
diff
changeset
|
633 |
|
4ea945cbbb6d
Fix problems with memoryview object. There is still more to do to finish PEP 3118. The memory-view object needs to be fleshed out and the struct module needs to be modified.
Travis E. Oliphant <oliphant@enthought.com>
parents:
43488
diff
changeset
|
634 /* Need to support assigning memory if we can */ |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
635 static int |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
636 memory_ass_sub(PyMemoryViewObject *self, PyObject *key, PyObject *value) |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
637 { |
|
48782
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
638 Py_ssize_t start, len, bytelen, i; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
639 Py_buffer srcview; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
640 Py_buffer *view = &(self->view); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
641 char *srcbuf, *destbuf; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
642 |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
643 if (view->readonly) { |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
644 PyErr_SetString(PyExc_TypeError, |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
645 "cannot modify read-only memory"); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
646 return -1; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
647 } |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
648 if (view->ndim != 1) { |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
649 PyErr_SetNone(PyExc_NotImplementedError); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
650 return -1; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
651 } |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
652 if (PyIndex_Check(key)) { |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
653 start = PyNumber_AsSsize_t(key, NULL); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
654 if (start == -1 && PyErr_Occurred()) |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
655 return -1; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
656 if (start < 0) { |
|
50083
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
657 start += get_shape0(view); |
|
48782
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
658 } |
|
50083
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
659 if ((start < 0) || (start >= get_shape0(view))) { |
|
48782
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
660 PyErr_SetString(PyExc_IndexError, |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
661 "index out of bounds"); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
662 return -1; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
663 } |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
664 len = 1; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
665 } |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
666 else if (PySlice_Check(key)) { |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
667 Py_ssize_t stop, step; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
668 |
|
50083
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
669 if (PySlice_GetIndicesEx((PySliceObject*)key, get_shape0(view), |
|
48782
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
670 &start, &stop, &step, &len) < 0) { |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
671 return -1; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
672 } |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
673 if (step != 1) { |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
674 PyErr_SetNone(PyExc_NotImplementedError); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
675 return -1; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
676 } |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
677 } |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
678 else { |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
679 PyErr_Format(PyExc_TypeError, |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
680 "cannot index memory using \"%.200s\"", |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
681 key->ob_type->tp_name); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
682 return -1; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
683 } |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
684 if (PyObject_GetBuffer(value, &srcview, PyBUF_CONTIG_RO) == -1) { |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
685 return -1; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
686 } |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
687 /* XXX should we allow assignment of different item sizes |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
688 as long as the byte length is the same? |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
689 (e.g. assign 2 shorts to a 4-byte slice) */ |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
690 if (srcview.itemsize != view->itemsize) { |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
691 PyErr_Format(PyExc_TypeError, |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
692 "mismatching item sizes for \"%.200s\" and \"%.200s\"", |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
693 view->obj->ob_type->tp_name, srcview.obj->ob_type->tp_name); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
694 goto _error; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
695 } |
|
50083
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
696 bytelen = len * view->itemsize; |
|
725192c64c28
Merged revisions 67650 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
49136
diff
changeset
|
697 if (bytelen != srcview.len) { |
|
48782
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
698 PyErr_SetString(PyExc_ValueError, |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
699 "cannot modify size of memoryview object"); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
700 goto _error; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
701 } |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
702 /* Do the actual copy */ |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
703 destbuf = (char *) view->buf + start * view->itemsize; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
704 srcbuf = (char *) srcview.buf; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
705 if (destbuf + bytelen < srcbuf || srcbuf + bytelen < destbuf) |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
706 /* No overlapping */ |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
707 memcpy(destbuf, srcbuf, bytelen); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
708 else if (destbuf < srcbuf) { |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
709 /* Copy in ascending order */ |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
710 for (i = 0; i < bytelen; i++) |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
711 destbuf[i] = srcbuf[i]; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
712 } |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
713 else { |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
714 /* Copy in descencing order */ |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
715 for (i = bytelen - 1; i >= 0; i--) |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
716 destbuf[i] = srcbuf[i]; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
717 } |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
718 |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
719 PyBuffer_Release(&srcview); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
720 return 0; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
721 |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
722 _error: |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
723 PyBuffer_Release(&srcview); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
724 return -1; |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
725 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
726 |
|
48782
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
727 static PyObject * |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
728 memory_richcompare(PyObject *v, PyObject *w, int op) |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
729 { |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
730 Py_buffer vv, ww; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
731 int equal = 0; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
732 PyObject *res; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
733 |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
734 vv.obj = NULL; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
735 ww.obj = NULL; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
736 if (op != Py_EQ && op != Py_NE) |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
737 goto _notimpl; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
738 if (PyObject_GetBuffer(v, &vv, PyBUF_CONTIG_RO) == -1) { |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
739 PyErr_Clear(); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
740 goto _notimpl; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
741 } |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
742 if (PyObject_GetBuffer(w, &ww, PyBUF_CONTIG_RO) == -1) { |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
743 PyErr_Clear(); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
744 goto _notimpl; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
745 } |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
746 |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
747 if (vv.itemsize != ww.itemsize || vv.len != ww.len) |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
748 goto _end; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
749 |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
750 equal = !memcmp(vv.buf, ww.buf, vv.len * vv.itemsize); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
751 |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
752 _end: |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
753 PyBuffer_Release(&vv); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
754 PyBuffer_Release(&ww); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
755 if ((equal && op == Py_EQ) || (!equal && op == Py_NE)) |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
756 res = Py_True; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
757 else |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
758 res = Py_False; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
759 Py_INCREF(res); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
760 return res; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
761 |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
762 _notimpl: |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
763 PyBuffer_Release(&vv); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
764 PyBuffer_Release(&ww); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
765 Py_INCREF(Py_NotImplemented); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
766 return Py_NotImplemented; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
767 } |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
768 |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
769 |
|
48928
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
770 static int |
|
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
771 memory_traverse(PyMemoryViewObject *self, visitproc visit, void *arg) |
|
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
772 { |
|
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
773 if (self->base != NULL) |
|
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
774 Py_VISIT(self->base); |
|
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
775 if (self->view.obj != NULL) |
|
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
776 Py_VISIT(self->view.obj); |
|
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
777 return 0; |
|
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
778 } |
|
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
779 |
|
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
780 static int |
|
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
781 memory_clear(PyMemoryViewObject *self) |
|
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
782 { |
|
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
783 Py_CLEAR(self->base); |
|
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
784 PyBuffer_Release(&self->view); |
|
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
785 return 0; |
|
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
786 } |
|
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
787 |
|
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
788 |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
789 /* As mapping */ |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
790 static PyMappingMethods memory_as_mapping = { |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
791 (lenfunc)memory_length, /*mp_length*/ |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
792 (binaryfunc)memory_subscript, /*mp_subscript*/ |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
793 (objobjargproc)memory_ass_sub, /*mp_ass_subscript*/ |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
794 }; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
795 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
796 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
797 /* Buffer methods */ |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
798 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
799 static PyBufferProcs memory_as_buffer = { |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
800 (getbufferproc)memory_getbuf, /* bf_getbuffer */ |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
801 (releasebufferproc)memory_releasebuf, /* bf_releasebuffer */ |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
802 }; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
803 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
804 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
805 PyTypeObject PyMemoryView_Type = { |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
806 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
807 "memoryview", |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
808 sizeof(PyMemoryViewObject), |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
809 0, |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
810 (destructor)memory_dealloc, /* tp_dealloc */ |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
811 0, /* tp_print */ |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
812 0, /* tp_getattr */ |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
813 0, /* tp_setattr */ |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
814 0, /* tp_compare */ |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
815 (reprfunc)memory_repr, /* tp_repr */ |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
816 0, /* tp_as_number */ |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
817 0, /* tp_as_sequence */ |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
818 &memory_as_mapping, /* tp_as_mapping */ |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
819 0, /* tp_hash */ |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
820 0, /* tp_call */ |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
821 (reprfunc)memory_str, /* tp_str */ |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
822 PyObject_GenericGetAttr, /* tp_getattro */ |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
823 0, /* tp_setattro */ |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
824 &memory_as_buffer, /* tp_as_buffer */ |
|
48928
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
825 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
826 memory_doc, /* tp_doc */ |
|
48928
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
827 (traverseproc)memory_traverse, /* tp_traverse */ |
|
696f5d8419f2
#3712: The memoryview object had a reference leak and didn't support cyclic garbage collection.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48782
diff
changeset
|
828 (inquiry)memory_clear, /* tp_clear */ |
|
48782
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
829 memory_richcompare, /* tp_richcompare */ |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
830 0, /* tp_weaklistoffset */ |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
831 0, /* tp_iter */ |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
832 0, /* tp_iternext */ |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
833 memory_methods, /* tp_methods */ |
|
42665
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
834 0, /* tp_members */ |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
835 memory_getsetlist, /* tp_getset */ |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
836 0, /* tp_base */ |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
837 0, /* tp_dict */ |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
838 0, /* tp_descr_get */ |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
839 0, /* tp_descr_set */ |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
840 0, /* tp_dictoffset */ |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
841 0, /* tp_init */ |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
842 0, /* tp_alloc */ |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
843 memory_new, /* tp_new */ |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
844 }; |
