Skip to content

Commit ee58fa4

Browse files
committed
#3560: cleanup C memoryview API
1 parent fd03645 commit ee58fa4

File tree

5 files changed

+30
-18
lines changed

5 files changed

+30
-18
lines changed

Include/memoryobject.h

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
1-
2-
/* Memory object interface */
1+
/* Memory view object. In Python this is available as "memoryview". */
32

43
#ifndef Py_MEMORYOBJECT_H
54
#define Py_MEMORYOBJECT_H
65
#ifdef __cplusplus
76
extern "C" {
87
#endif
98

10-
typedef struct {
11-
PyObject_HEAD
12-
PyObject *base;
13-
Py_buffer view;
14-
} PyMemoryViewObject;
15-
16-
179
PyAPI_DATA(PyTypeObject) PyMemoryView_Type;
1810

19-
#define PyMemory_Check(op) (Py_TYPE(op) == &PyMemoryView_Type)
20-
#define PyMemoryView(op) (((PyMemoryViewObject *)(op))->view)
11+
#define PyMemoryView_Check(op) (Py_TYPE(op) == &PyMemoryView_Type)
12+
13+
/* Get a pointer to the underlying Py_buffer of a memoryview object. */
14+
#define PyMemoryView_GET_BUFFER(op) (&((PyMemoryViewObject *)(op))->view)
15+
/* Get a pointer to the PyObject from which originates a memoryview object. */
16+
#define PyMemoryView_GET_BASE(op) (((PyMemoryViewObject *)(op))->view.obj)
2117

22-
#define Py_END_OF_MEMORY (-1)
2318

2419
PyAPI_FUNC(PyObject *) PyMemoryView_GetContiguous(PyObject *base,
2520
int buffertype,
@@ -58,10 +53,21 @@ PyAPI_FUNC(PyObject *) PyMemoryView_GetContiguous(PyObject *base,
5853

5954
PyAPI_FUNC(PyObject *) PyMemoryView_FromObject(PyObject *base);
6055

61-
PyAPI_FUNC(PyObject *) PyMemoryView_FromMemory(Py_buffer *info);
56+
PyAPI_FUNC(PyObject *) PyMemoryView_FromBuffer(Py_buffer *info);
6257
/* create new if bufptr is NULL
6358
will be a new bytesobject in base */
6459

60+
61+
/* The struct is declared here so that macros can work, but it shouldn't
62+
be considered public. Don't access those fields directly, use the macros
63+
and functions instead! */
64+
typedef struct {
65+
PyObject_HEAD
66+
PyObject *base;
67+
Py_buffer view;
68+
} PyMemoryViewObject;
69+
70+
6571
#ifdef __cplusplus
6672
}
6773
#endif

Misc/NEWS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ What's new in Python 3.0b3?
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue #3560: clean up the new C PyMemoryView API so that naming is
16+
internally consistent; add macros PyMemoryView_GET_BASE() and
17+
PyMemoryView_GET_BUFFER() to access useful properties of a memory views
18+
without relying on a particular implementation; remove the ill-named
19+
PyMemoryView() function (PyMemoryView_GET_BUFFER() can be used instead).
20+
1521
- Issue #1819: function calls with several named parameters are now on
1622
average 35% faster (as measured by pybench).
1723

Modules/_json.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ scanstring_str(PyObject *pystr, Py_ssize_t end, char *encoding, int strict)
264264
if (PyBuffer_FillInfo(&info, NULL, &buf[end], next - end, 1, 0) < 0) {
265265
goto bail;
266266
}
267-
strchunk = PyMemoryView_FromMemory(&info);
267+
strchunk = PyMemoryView_FromBuffer(&info);
268268
if (strchunk == NULL) {
269269
goto bail;
270270
}

Objects/memoryobject.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ PyDoc_STRVAR(memory_doc,
2929
Create a new memoryview object which references the given object.");
3030

3131
PyObject *
32-
PyMemoryView_FromMemory(Py_buffer *info)
32+
PyMemoryView_FromBuffer(Py_buffer *info)
3333
{
3434
PyMemoryViewObject *mview;
3535

@@ -231,7 +231,7 @@ PyMemoryView_GetContiguous(PyObject *obj, int buffertype, char fort)
231231
mem = PyObject_New(PyMemoryViewObject, &PyMemoryView_Type);
232232
if (mem == NULL) return NULL;
233233

234-
view = &PyMemoryView(mem);
234+
view = &mem->view;
235235
flags = PyBUF_FULL_RO;
236236
switch(buffertype) {
237237
case PyBUF_WRITE:
@@ -534,7 +534,7 @@ memory_subscript(PyMemoryViewObject *self, PyObject *key)
534534
/* XXX: This needs to be fixed so it
535535
actually returns a sub-view
536536
*/
537-
return PyMemoryView_FromMemory(&newview);
537+
return PyMemoryView_FromBuffer(&newview);
538538
}
539539
}
540540

Objects/unicodeobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1200,7 +1200,7 @@ PyObject *PyUnicode_Decode(const char *s,
12001200
buffer = NULL;
12011201
if (PyBuffer_FillInfo(&info, NULL, (void *)s, size, 1, PyBUF_SIMPLE) < 0)
12021202
goto onError;
1203-
buffer = PyMemoryView_FromMemory(&info);
1203+
buffer = PyMemoryView_FromBuffer(&info);
12041204
if (buffer == NULL)
12051205
goto onError;
12061206
unicode = PyCodec_Decode(buffer, encoding, errors);

0 commit comments

Comments
 (0)