Mercurial > cpython-lru_cache-weakref
annotate Objects/memoryobject.c @ 49961:e793fc3c8abe
More 3.0 -> 3.1 transistion in the Windows and OS/2 build files
| author | Christian Heimes <christian@cheimes.de> |
|---|---|
| date | Thu, 04 Dec 2008 14:27:25 +0000 |
| parents | 348a33048ebf |
| children | 725192c64c28 |
| 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 |
|
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
|
16 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
|
17 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
|
18 { |
|
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
|
19 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
|
20 /* 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
|
21 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
|
22 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
|
23 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
|
24 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
|
25 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
|
26 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
|
27 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
|
28 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
29 |
|
85c22b4d4455
Merged in py3k-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 static void |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
31 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
|
32 { |
|
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
|
33 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
|
34 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
35 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
36 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
|
37 "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
|
38 \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
|
39 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
|
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 PyObject * |
|
48770
7511f3e672a2
#3560: cleanup C memoryview API
Antoine Pitrou <solipsis@pitrou.net>
parents:
48683
diff
changeset
|
42 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
|
43 { |
|
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
|
44 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
|
45 |
|
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
|
46 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
|
47 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
|
48 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
|
49 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
|
50 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
|
51 dup_buffer(&mview->view, info); |
|
48782
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
52 /* 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
|
53 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
|
54 _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
|
55 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
|
56 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
57 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
58 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
|
59 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
|
60 { |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
61 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
|
62 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
63 if (!PyObject_CheckBuffer(base)) { |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
64 PyErr_SetString(PyExc_TypeError, |
|
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
65 "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
|
66 "not have the buffer interface"); |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
67 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
|
68 } |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
69 |
|
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
|
70 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
|
71 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
|
72 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
|
73 return NULL; |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
74 |
|
42671
fca354160458
SF #1777057, fix memoryview('test') so it works in debug mode too.
Neal Norwitz <nnorwitz@gmail.com>
parents:
42669
diff
changeset
|
75 mview->base = NULL; |
|
48623
bf50085fbcd3
#2538: bytes objects can only provide read-only buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
48330
diff
changeset
|
76 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
|
77 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
|
78 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
|
79 } |
|
85c22b4d4455
Merged in py3k-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 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
81 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
|
82 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
|
83 _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
|
84 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
|
85 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
86 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
87 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
|
88 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
|
89 { |
|
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
|
90 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
|
91 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
|
92 |
|
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
|
93 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
|
94 &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
|
95 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
|
96 } |
|
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
|
97 |
|
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
|
98 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
|
99 } |
|
85c22b4d4455
Merged in py3k-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 |
|
85c22b4d4455
Merged in py3k-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 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
102 static void |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
103 _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
|
104 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
|
105 { |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
106 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
|
107 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
|
108 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
109 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
|
110 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
|
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 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
|
113 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
|
114 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
|
115 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
|
116 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
|
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 } |
|
85c22b4d4455
Merged in py3k-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 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
|
120 if (fort == 'F') { |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
121 /* 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
|
122 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
|
123 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
|
124 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
|
125 */ |
|
85c22b4d4455
Merged in py3k-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 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
|
127 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
|
128 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
|
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 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
|
131 _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
|
132 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
|
133 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
|
134 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
|
135 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
136 } |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
137 |
|
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
|
138 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
|
139 /* 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
|
140 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
|
141 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
|
142 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
|
143 */ |
|
85c22b4d4455
Merged in py3k-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 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
|
145 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
|
146 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
|
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 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
|
149 _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
|
150 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
|
151 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
|
152 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
|
153 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
|
154 } |
|
85c22b4d4455
Merged in py3k-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 } |
|
85c22b4d4455
Merged in py3k-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 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
|
158 } |
|
85c22b4d4455
Merged in py3k-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 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
|
161 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
|
162 |
|
85c22b4d4455
Merged in py3k-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 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
|
164 _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
|
165 { |
|
85c22b4d4455
Merged in py3k-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 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
|
167 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
|
168 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
|
169 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
|
170 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
|
171 |
|
48330
ac854cde3135
Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47500
diff
changeset
|
172 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
|
173 PyErr_NoMemory(); |
|
ac854cde3135
Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47500
diff
changeset
|
174 return -1; |
|
ac854cde3135
Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47500
diff
changeset
|
175 } |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
176 |
|
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 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
|
178 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
|
179 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
|
180 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
|
181 } |
|
85c22b4d4455
Merged in py3k-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 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
|
183 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
|
184 } |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
185 |
|
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
|
186 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
|
187 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
|
188 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
|
189 } |
|
85c22b4d4455
Merged in py3k-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 (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
|
191 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
|
192 } |
|
85c22b4d4455
Merged in py3k-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 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
|
194 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
|
195 } |
|
85c22b4d4455
Merged in py3k-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 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
|
197 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
|
198 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
|
199 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
|
200 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
|
201 } |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
202 |
|
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
|
203 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
|
204 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
|
205 } |
|
85c22b4d4455
Merged in py3k-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 |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
207 /* |
|
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
|
208 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
|
209 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
|
210 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
|
211 |
|
85c22b4d4455
Merged in py3k-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 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
|
213 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
|
214 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
|
215 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
|
216 |
|
85c22b4d4455
Merged in py3k-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 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
|
218 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
219 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
|
220 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
|
221 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
|
222 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
|
223 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
|
224 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
|
225 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
|
226 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
|
227 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
|
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 |
|
85c22b4d4455
Merged in py3k-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 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
|
231 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
|
232 { |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
233 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
|
234 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
|
235 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
|
236 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
|
237 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
|
238 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
239 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
|
240 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
|
241 "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
|
242 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
|
243 } |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
244 |
|
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
|
245 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
|
246 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
|
247 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
|
248 |
|
48770
7511f3e672a2
#3560: cleanup C memoryview API
Antoine Pitrou <solipsis@pitrou.net>
parents:
48683
diff
changeset
|
249 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
|
250 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
|
251 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
|
252 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
|
253 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
|
254 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
|
255 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
256 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
257 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
|
258 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
|
259 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
|
260 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
261 |
|
85c22b4d4455
Merged in py3k-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 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
|
263 /* 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
|
264 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
|
265 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
|
266 _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
|
267 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
|
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 /* 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
|
270 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
|
271 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
|
272 PyErr_SetString(PyExc_BufferError, |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
273 "writable contiguous buffer requested " |
|
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
274 "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
|
275 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
|
276 } |
|
48782
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
277 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
|
278 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
|
279 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
|
280 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
|
281 } |
|
48782
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
282 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
|
283 /* 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
|
284 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
|
285 */ |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
286 /* 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
|
287 if (view->suboffsets==NULL) { |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
288 _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
|
289 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
|
290 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
291 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
|
292 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
|
293 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
|
294 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
|
295 return NULL; |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
296 } |
|
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
|
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 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
|
299 /* 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
|
300 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
|
301 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
|
302 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
|
303 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
|
304 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
|
305 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
|
306 } |
|
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 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
308 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
|
309 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
|
310 /* 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
|
311 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
|
312 } |
|
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
|
313 _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
|
314 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
|
315 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
316 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
317 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
318 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
|
319 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
|
320 { |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
321 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
|
322 } |
|
85c22b4d4455
Merged in py3k-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 |
|
85c22b4d4455
Merged in py3k-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 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
|
325 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
|
326 { |
|
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
|
327 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
|
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 _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
|
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 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
|
334 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
|
335 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
|
336 |
|
85c22b4d4455
Merged in py3k-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 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
|
338 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
|
339 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
|
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 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
|
342 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
|
343 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
|
344 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
|
345 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
|
346 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
|
347 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
|
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 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
|
350 } |
|
85c22b4d4455
Merged in py3k-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 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
|
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 |
|
85c22b4d4455
Merged in py3k-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 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
|
355 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
|
356 { |
|
85c22b4d4455
Merged in py3k-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 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
|
358 } |
|
85c22b4d4455
Merged in py3k-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 |
|
85c22b4d4455
Merged in py3k-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 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
|
361 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
|
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 _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
|
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_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
|
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.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
|
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_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
|
374 { |
|
42671
fca354160458
SF #1777057, fix memoryview('test') so it works in debug mode too.
Neal Norwitz <nnorwitz@gmail.com>
parents:
42669
diff
changeset
|
375 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
|
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_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
|
380 { |
|
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
|
381 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
|
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 |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
384 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
|
385 {"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
|
386 {"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
|
387 {"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
|
388 {"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
|
389 {"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
|
390 {"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
|
391 {"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
|
392 {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
|
393 }; |
|
85c22b4d4455
Merged in py3k-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 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
396 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
|
397 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
|
398 { |
|
48782
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
399 return PyObject_CallFunctionObjArgs( |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
400 (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
|
401 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
402 |
|
48782
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
403 /* 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
|
404 each buffer item */ |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
405 |
|
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
|
406 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
|
407 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
|
408 { |
|
48782
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
409 Py_buffer *view = &(mem->view); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
410 Py_ssize_t i; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
411 PyObject *res, *item; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
412 char *buf; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
413 |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
414 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
|
415 PyErr_SetString(PyExc_NotImplementedError, |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
416 "tolist() only supports byte views"); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
417 return NULL; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
418 } |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
419 if (view->ndim != 1) { |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
420 PyErr_SetString(PyExc_NotImplementedError, |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
421 "tolist() only supports one-dimensional objects"); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
422 return NULL; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
423 } |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
424 res = PyList_New(view->len); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
425 if (res == NULL) |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
426 return NULL; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
427 buf = view->buf; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
428 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
|
429 item = PyLong_FromUnsignedLong((unsigned char) *buf); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
430 if (item == NULL) { |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
431 Py_DECREF(res); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
432 return NULL; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
433 } |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
434 PyList_SET_ITEM(res, i, item); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
435 buf++; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
436 } |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
437 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
|
438 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
439 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
440 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
441 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
442 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
|
443 {"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
|
444 {"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
|
445 {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
|
446 }; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
447 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
448 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
449 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
|
450 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
|
451 { |
|
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
|
452 _PyObject_GC_UNTRACK(self); |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48623
diff
changeset
|
453 if (self->view.obj != NULL) { |
|
48782
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
454 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
|
455 /* 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
|
456 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
|
457 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
|
458 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
|
459 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
|
460 */ |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
461 |
|
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
|
462 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
|
463 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
|
464 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
465 /* 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
|
466 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
|
467 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
|
468 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
|
469 */ |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48623
diff
changeset
|
470 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
|
471 } |
|
fca354160458
SF #1777057, fix memoryview('test') so it works in debug mode too.
Neal Norwitz <nnorwitz@gmail.com>
parents:
42669
diff
changeset
|
472 else { |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48623
diff
changeset
|
473 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
|
474 } |
|
fca354160458
SF #1777057, fix memoryview('test') so it works in debug mode too.
Neal Norwitz <nnorwitz@gmail.com>
parents:
42669
diff
changeset
|
475 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
|
476 } |
|
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
|
477 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
|
478 } |
|
85c22b4d4455
Merged in py3k-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 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
480 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
|
481 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
|
482 { |
|
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
|
483 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
|
484 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
485 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
486 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
487 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
|
488 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
|
489 { |
|
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
|
490 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
|
491 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
|
492 |
|
85c22b4d4455
Merged in py3k-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 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
|
494 return NULL; |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
495 |
|
48782
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
496 res = PyBytes_FromStringAndSize(NULL, view.len); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
497 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
|
498 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
|
499 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
|
500 } |
|
85c22b4d4455
Merged in py3k-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 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
502 /* 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
|
503 |
|
85c22b4d4455
Merged in py3k-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 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
|
505 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
|
506 { |
|
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
|
507 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
|
508 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
509 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
|
510 return -1; |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48623
diff
changeset
|
511 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
|
512 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
|
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 |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
515 /* |
|
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
|
516 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
|
517 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
|
518 if it does not. |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
519 |
|
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
|
520 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
|
521 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
|
522 */ |
|
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
|
523 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
|
524 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
|
525 { |
|
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
|
526 Py_buffer *view; |
|
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
|
527 view = &(self->view); |
|
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 |
|
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 if (view->ndim == 0) { |
|
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 (key == Py_Ellipsis || |
|
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
|
531 (PyTuple_Check(key) && PyTuple_GET_SIZE(key)==0)) { |
|
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 Py_INCREF(self); |
|
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
|
533 return (PyObject *)self; |
|
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 } |
|
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
|
535 else { |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
536 PyErr_SetString(PyExc_IndexError, |
|
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
537 "invalid indexing of 0-dim memory"); |
|
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
|
538 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
|
539 } |
|
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
|
540 } |
|
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
|
541 if (PyIndex_Check(key)) { |
|
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
|
542 Py_ssize_t result; |
|
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 result = PyNumber_AsSsize_t(key, 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
|
544 if (result == -1 && PyErr_Occurred()) |
|
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
|
545 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
|
546 if (view->ndim == 1) { |
|
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
|
547 /* Return a bytes 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
|
548 char *ptr; |
|
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
|
549 ptr = (char *)view->buf; |
|
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
|
550 if (result < 0) { |
|
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
|
551 result += view->shape[0]; |
|
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
|
552 } |
|
48782
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
553 if ((result < 0) || (result >= view->shape[0])) { |
|
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
|
554 PyErr_SetString(PyExc_IndexError, |
|
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
|
555 "index out of bounds"); |
|
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
|
556 return NULL; |
|
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
|
557 } |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
558 if (view->strides == 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
|
559 ptr += view->itemsize * result; |
|
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
|
560 else |
|
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
|
561 ptr += view->strides[0] * result; |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
562 if (view->suboffsets != NULL && |
|
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
563 view->suboffsets[0] >= 0) |
|
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
564 { |
|
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
|
565 ptr = *((char **)ptr) + view->suboffsets[0]; |
|
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
|
566 } |
|
48782
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
567 return PyBytes_FromStringAndSize(ptr, view->itemsize); |
|
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
|
568 } |
|
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
|
569 else { |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
570 /* Return a new memory-view object */ |
|
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
|
571 Py_buffer newview; |
|
43485
074130a073b6
At least one of the buildbots was complaining about newview being used
Neal Norwitz <nnorwitz@gmail.com>
parents:
43439
diff
changeset
|
572 memset(&newview, 0, sizeof(newview)); |
|
48623
bf50085fbcd3
#2538: bytes objects can only provide read-only buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
48330
diff
changeset
|
573 /* 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
|
574 actually returns a sub-view |
|
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
|
575 */ |
|
48770
7511f3e672a2
#3560: cleanup C memoryview API
Antoine Pitrou <solipsis@pitrou.net>
parents:
48683
diff
changeset
|
576 return PyMemoryView_FromBuffer(&newview); |
|
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
|
577 } |
|
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
|
578 } |
|
48782
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
579 else if (PySlice_Check(key)) { |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
580 Py_ssize_t start, stop, step, slicelength; |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
581 |
|
48782
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
582 if (PySlice_GetIndicesEx((PySliceObject*)key, view->len, |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
583 &start, &stop, &step, &slicelength) < 0) { |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
584 return NULL; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
585 } |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
586 |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
587 if (step == 1 && view->ndim == 1) { |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
588 Py_buffer newview; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
589 void *newbuf = (char *) view->buf |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
590 + start * view->itemsize; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
591 int newflags = view->readonly |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
592 ? PyBUF_CONTIG_RO : PyBUF_CONTIG; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
593 |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
594 /* XXX There should be an API to create a subbuffer */ |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
595 if (view->obj != NULL) { |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
596 if (PyObject_GetBuffer(view->obj, |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
597 &newview, newflags) == -1) |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
598 return NULL; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
599 } |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
600 else { |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
601 newview = *view; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
602 } |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
603 newview.buf = newbuf; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
604 newview.len = slicelength; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
605 newview.format = view->format; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
606 if (view->shape == &(view->len)) |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
607 newview.shape = &(newview.len); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
608 if (view->strides == &(view->itemsize)) |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
609 newview.strides = &(newview.itemsize); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
610 return PyMemoryView_FromBuffer(&newview); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
611 } |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
612 PyErr_SetNone(PyExc_NotImplementedError); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
613 return NULL; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
614 } |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
615 PyErr_Format(PyExc_TypeError, |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
616 "cannot index memory using \"%.200s\"", |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
617 key->ob_type->tp_name); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
618 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
|
619 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
620 |
|
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
|
621 |
|
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
|
622 /* 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
|
623 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
|
624 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
|
625 { |
|
48782
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
626 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
|
627 Py_buffer srcview; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
628 Py_buffer *view = &(self->view); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
629 char *srcbuf, *destbuf; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
630 |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
631 if (view->readonly) { |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
632 PyErr_SetString(PyExc_TypeError, |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
633 "cannot modify read-only memory"); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
634 return -1; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
635 } |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
636 if (view->ndim != 1) { |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
637 PyErr_SetNone(PyExc_NotImplementedError); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
638 return -1; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
639 } |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
640 if (PyIndex_Check(key)) { |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
641 start = PyNumber_AsSsize_t(key, NULL); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
642 if (start == -1 && PyErr_Occurred()) |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
643 return -1; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
644 if (start < 0) { |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
645 start += view->shape[0]; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
646 } |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
647 if ((start < 0) || (start >= view->shape[0])) { |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
648 PyErr_SetString(PyExc_IndexError, |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
649 "index out of bounds"); |
|
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 len = 1; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
653 } |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
654 else if (PySlice_Check(key)) { |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
655 Py_ssize_t stop, step; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
656 |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
657 if (PySlice_GetIndicesEx((PySliceObject*)key, view->len, |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
658 &start, &stop, &step, &len) < 0) { |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
659 return -1; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
660 } |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
661 if (step != 1) { |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
662 PyErr_SetNone(PyExc_NotImplementedError); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
663 return -1; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
664 } |
|
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 { |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
667 PyErr_Format(PyExc_TypeError, |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
668 "cannot index memory using \"%.200s\"", |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
669 key->ob_type->tp_name); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
670 return -1; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
671 } |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
672 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
|
673 return -1; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
674 } |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
675 /* 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
|
676 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
|
677 (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
|
678 if (srcview.itemsize != view->itemsize) { |
|
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 "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
|
681 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
|
682 goto _error; |
|
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 (srcview.len != len) { |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
685 PyErr_SetString(PyExc_ValueError, |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
686 "cannot modify size of memoryview object"); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
687 goto _error; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
688 } |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
689 /* Do the actual copy */ |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
690 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
|
691 srcbuf = (char *) srcview.buf; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
692 bytelen = len * view->itemsize; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
693 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
|
694 /* No overlapping */ |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
695 memcpy(destbuf, srcbuf, bytelen); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
696 else if (destbuf < srcbuf) { |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
697 /* Copy in ascending order */ |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
698 for (i = 0; i < bytelen; i++) |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
699 destbuf[i] = srcbuf[i]; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
700 } |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
701 else { |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
702 /* Copy in descencing order */ |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
703 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
|
704 destbuf[i] = srcbuf[i]; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
705 } |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
706 |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
707 PyBuffer_Release(&srcview); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
708 return 0; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
709 |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
710 _error: |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
711 PyBuffer_Release(&srcview); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
712 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
|
713 } |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
714 |
|
48782
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
715 static PyObject * |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
716 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
|
717 { |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
718 Py_buffer vv, ww; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
719 int equal = 0; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
720 PyObject *res; |
|
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 vv.obj = NULL; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
723 ww.obj = NULL; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
724 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
|
725 goto _notimpl; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
726 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
|
727 PyErr_Clear(); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
728 goto _notimpl; |
|
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 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
|
731 PyErr_Clear(); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
732 goto _notimpl; |
|
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 |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
735 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
|
736 goto _end; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
737 |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
738 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
|
739 |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
740 _end: |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
741 PyBuffer_Release(&vv); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
742 PyBuffer_Release(&ww); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
743 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
|
744 res = Py_True; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
745 else |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
746 res = Py_False; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
747 Py_INCREF(res); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
748 return res; |
|
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 _notimpl: |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
751 PyBuffer_Release(&vv); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
752 PyBuffer_Release(&ww); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
753 Py_INCREF(Py_NotImplemented); |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
754 return Py_NotImplemented; |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
755 } |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
756 |
|
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
757 |
|
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
|
758 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
|
759 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
|
760 { |
|
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
|
761 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
|
762 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
|
763 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
|
764 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
|
765 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
|
766 } |
|
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
|
767 |
|
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
|
768 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
|
769 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
|
770 { |
|
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 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
|
772 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
|
773 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
|
774 } |
|
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 |
|
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 |
|
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
|
777 /* 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
|
778 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
|
779 (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
|
780 (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
|
781 (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
|
782 }; |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
783 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
784 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
785 /* 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
|
786 |
|
85c22b4d4455
Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents:
diff
changeset
|
787 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
|
788 (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
|
789 (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
|
790 }; |
|
85c22b4d4455
Merged in py3k-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 |
|
85c22b4d4455
Merged in py3k-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 |
|
85c22b4d4455
Merged in py3k-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 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
|
794 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
|
795 "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
|
796 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
|
797 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
|
798 (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
|
799 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
|
800 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
|
801 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
|
802 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
|
803 (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
|
804 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
|
805 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
|
806 &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
|
807 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
|
808 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
|
809 (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
|
810 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
|
811 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
|
812 &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
|
813 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
|
814 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
|
815 (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
|
816 (inquiry)memory_clear, /* tp_clear */ |
|
48782
501256c54c84
Issue #2394: implement more of the memoryview API.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48770
diff
changeset
|
817 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
|
818 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
|
819 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
|
820 0, /* tp_iternext */ |
|
43439
79b0db0356d3
Whitespace and line lengrth cleanup.
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
821 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
|
822 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
|
823 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
|
824 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
|
825 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
|
826 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
|
827 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
|
828 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
|
829 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
|
830 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
|
831 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
|
832 }; |
