/src/cpython/Objects/picklebufobject.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* PickleBuffer object implementation */ |
2 | | |
3 | | #include "Python.h" |
4 | | #include "pycore_weakref.h" // FT_CLEAR_WEAKREFS() |
5 | | #include <stddef.h> |
6 | | |
7 | | typedef struct { |
8 | | PyObject_HEAD |
9 | | /* The view exported by the original object */ |
10 | | Py_buffer view; |
11 | | PyObject *weakreflist; |
12 | | } PyPickleBufferObject; |
13 | | |
14 | | /* C API */ |
15 | | |
16 | | PyObject * |
17 | | PyPickleBuffer_FromObject(PyObject *base) |
18 | 0 | { |
19 | 0 | PyTypeObject *type = &PyPickleBuffer_Type; |
20 | 0 | PyPickleBufferObject *self; |
21 | |
|
22 | 0 | self = (PyPickleBufferObject *) type->tp_alloc(type, 0); |
23 | 0 | if (self == NULL) { |
24 | 0 | return NULL; |
25 | 0 | } |
26 | 0 | self->view.obj = NULL; |
27 | 0 | self->weakreflist = NULL; |
28 | 0 | if (PyObject_GetBuffer(base, &self->view, PyBUF_FULL_RO) < 0) { |
29 | 0 | Py_DECREF(self); |
30 | 0 | return NULL; |
31 | 0 | } |
32 | 0 | return (PyObject *) self; |
33 | 0 | } |
34 | | |
35 | | const Py_buffer * |
36 | | PyPickleBuffer_GetBuffer(PyObject *obj) |
37 | 0 | { |
38 | 0 | PyPickleBufferObject *self = (PyPickleBufferObject *) obj; |
39 | |
|
40 | 0 | if (!PyPickleBuffer_Check(obj)) { |
41 | 0 | PyErr_Format(PyExc_TypeError, |
42 | 0 | "expected PickleBuffer, %.200s found", |
43 | 0 | Py_TYPE(obj)->tp_name); |
44 | 0 | return NULL; |
45 | 0 | } |
46 | 0 | if (self->view.obj == NULL) { |
47 | 0 | PyErr_SetString(PyExc_ValueError, |
48 | 0 | "operation forbidden on released PickleBuffer object"); |
49 | 0 | return NULL; |
50 | 0 | } |
51 | 0 | return &self->view; |
52 | 0 | } |
53 | | |
54 | | int |
55 | | PyPickleBuffer_Release(PyObject *obj) |
56 | 0 | { |
57 | 0 | PyPickleBufferObject *self = (PyPickleBufferObject *) obj; |
58 | |
|
59 | 0 | if (!PyPickleBuffer_Check(obj)) { |
60 | 0 | PyErr_Format(PyExc_TypeError, |
61 | 0 | "expected PickleBuffer, %.200s found", |
62 | 0 | Py_TYPE(obj)->tp_name); |
63 | 0 | return -1; |
64 | 0 | } |
65 | 0 | PyBuffer_Release(&self->view); |
66 | 0 | return 0; |
67 | 0 | } |
68 | | |
69 | | static PyObject * |
70 | | picklebuf_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
71 | 0 | { |
72 | 0 | PyPickleBufferObject *self; |
73 | 0 | PyObject *base; |
74 | 0 | char *keywords[] = {"", NULL}; |
75 | |
|
76 | 0 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:PickleBuffer", |
77 | 0 | keywords, &base)) { |
78 | 0 | return NULL; |
79 | 0 | } |
80 | | |
81 | 0 | self = (PyPickleBufferObject *) type->tp_alloc(type, 0); |
82 | 0 | if (self == NULL) { |
83 | 0 | return NULL; |
84 | 0 | } |
85 | 0 | self->view.obj = NULL; |
86 | 0 | self->weakreflist = NULL; |
87 | 0 | if (PyObject_GetBuffer(base, &self->view, PyBUF_FULL_RO) < 0) { |
88 | 0 | Py_DECREF(self); |
89 | 0 | return NULL; |
90 | 0 | } |
91 | 0 | return (PyObject *) self; |
92 | 0 | } |
93 | | |
94 | | static int |
95 | | picklebuf_traverse(PyObject *op, visitproc visit, void *arg) |
96 | 0 | { |
97 | 0 | PyPickleBufferObject *self = (PyPickleBufferObject*)op; |
98 | 0 | Py_VISIT(self->view.obj); |
99 | 0 | return 0; |
100 | 0 | } |
101 | | |
102 | | static int |
103 | | picklebuf_clear(PyObject *op) |
104 | 0 | { |
105 | 0 | PyPickleBufferObject *self = (PyPickleBufferObject*)op; |
106 | 0 | PyBuffer_Release(&self->view); |
107 | 0 | return 0; |
108 | 0 | } |
109 | | |
110 | | static void |
111 | | picklebuf_dealloc(PyObject *op) |
112 | 0 | { |
113 | 0 | PyPickleBufferObject *self = (PyPickleBufferObject*)op; |
114 | 0 | PyObject_GC_UnTrack(self); |
115 | 0 | FT_CLEAR_WEAKREFS(op, self->weakreflist); |
116 | 0 | PyBuffer_Release(&self->view); |
117 | 0 | Py_TYPE(self)->tp_free((PyObject *) self); |
118 | 0 | } |
119 | | |
120 | | /* Buffer API */ |
121 | | |
122 | | static int |
123 | | picklebuf_getbuf(PyObject *op, Py_buffer *view, int flags) |
124 | 0 | { |
125 | 0 | PyPickleBufferObject *self = (PyPickleBufferObject*)op; |
126 | 0 | if (self->view.obj == NULL) { |
127 | 0 | PyErr_SetString(PyExc_ValueError, |
128 | 0 | "operation forbidden on released PickleBuffer object"); |
129 | 0 | return -1; |
130 | 0 | } |
131 | 0 | return PyObject_GetBuffer(self->view.obj, view, flags); |
132 | 0 | } |
133 | | |
134 | | static void |
135 | | picklebuf_releasebuf(PyObject *self, Py_buffer *view) |
136 | 0 | { |
137 | | /* Since our bf_getbuffer redirects to the original object, this |
138 | | * implementation is never called. It only exists to signal that |
139 | | * buffers exported by PickleBuffer have non-trivial releasing |
140 | | * behaviour (see check in Python/getargs.c). |
141 | | */ |
142 | 0 | } |
143 | | |
144 | | static PyBufferProcs picklebuf_as_buffer = { |
145 | | .bf_getbuffer = picklebuf_getbuf, |
146 | | .bf_releasebuffer = picklebuf_releasebuf, |
147 | | }; |
148 | | |
149 | | /* Methods */ |
150 | | |
151 | | static PyObject * |
152 | | picklebuf_raw(PyObject *op, PyObject *Py_UNUSED(ignored)) |
153 | 0 | { |
154 | 0 | PyPickleBufferObject *self = (PyPickleBufferObject*)op; |
155 | 0 | if (self->view.obj == NULL) { |
156 | 0 | PyErr_SetString(PyExc_ValueError, |
157 | 0 | "operation forbidden on released PickleBuffer object"); |
158 | 0 | return NULL; |
159 | 0 | } |
160 | 0 | if (self->view.suboffsets != NULL |
161 | 0 | || !PyBuffer_IsContiguous(&self->view, 'A')) { |
162 | 0 | PyErr_SetString(PyExc_BufferError, |
163 | 0 | "cannot extract raw buffer from non-contiguous buffer"); |
164 | 0 | return NULL; |
165 | 0 | } |
166 | 0 | PyObject *m = PyMemoryView_FromObject((PyObject *) self); |
167 | 0 | if (m == NULL) { |
168 | 0 | return NULL; |
169 | 0 | } |
170 | 0 | PyMemoryViewObject *mv = (PyMemoryViewObject *) m; |
171 | 0 | assert(mv->view.suboffsets == NULL); |
172 | | /* Mutate memoryview instance to make it a "raw" memoryview */ |
173 | 0 | mv->view.format = "B"; |
174 | 0 | mv->view.ndim = 1; |
175 | 0 | mv->view.itemsize = 1; |
176 | | /* shape = (length,) */ |
177 | 0 | mv->view.shape = &mv->view.len; |
178 | | /* strides = (1,) */ |
179 | 0 | mv->view.strides = &mv->view.itemsize; |
180 | | /* Fix memoryview state flags */ |
181 | | /* XXX Expose memoryobject.c's init_flags() instead? */ |
182 | 0 | mv->flags = _Py_MEMORYVIEW_C | _Py_MEMORYVIEW_FORTRAN; |
183 | 0 | return m; |
184 | 0 | } |
185 | | |
186 | | PyDoc_STRVAR(picklebuf_raw_doc, |
187 | | "raw($self, /)\n--\n\ |
188 | | \n\ |
189 | | Return a memoryview of the raw memory underlying this buffer.\n\ |
190 | | Will raise BufferError is the buffer isn't contiguous."); |
191 | | |
192 | | static PyObject * |
193 | | picklebuf_release(PyObject *op, PyObject *Py_UNUSED(ignored)) |
194 | 0 | { |
195 | 0 | PyPickleBufferObject *self = (PyPickleBufferObject*)op; |
196 | 0 | PyBuffer_Release(&self->view); |
197 | 0 | Py_RETURN_NONE; |
198 | 0 | } |
199 | | |
200 | | PyDoc_STRVAR(picklebuf_release_doc, |
201 | | "release($self, /)\n--\n\ |
202 | | \n\ |
203 | | Release the underlying buffer exposed by the PickleBuffer object."); |
204 | | |
205 | | static PyMethodDef picklebuf_methods[] = { |
206 | | {"raw", picklebuf_raw, METH_NOARGS, picklebuf_raw_doc}, |
207 | | {"release", picklebuf_release, METH_NOARGS, picklebuf_release_doc}, |
208 | | {NULL, NULL} |
209 | | }; |
210 | | |
211 | | PyTypeObject PyPickleBuffer_Type = { |
212 | | PyVarObject_HEAD_INIT(NULL, 0) |
213 | | .tp_name = "pickle.PickleBuffer", |
214 | | .tp_doc = PyDoc_STR("Wrapper for potentially out-of-band buffers"), |
215 | | .tp_basicsize = sizeof(PyPickleBufferObject), |
216 | | .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, |
217 | | .tp_new = picklebuf_new, |
218 | | .tp_dealloc = picklebuf_dealloc, |
219 | | .tp_traverse = picklebuf_traverse, |
220 | | .tp_clear = picklebuf_clear, |
221 | | .tp_weaklistoffset = offsetof(PyPickleBufferObject, weakreflist), |
222 | | .tp_as_buffer = &picklebuf_as_buffer, |
223 | | .tp_methods = picklebuf_methods, |
224 | | }; |