Skip to content

Commit 55bad19

Browse files
authored
gh-79315: Add Include/cpython/memoryobject.h header (#99723)
Move non-limited C API from Include/memoryobject.h to a new Include/cpython/memoryobject.h header file.
1 parent 81f7359 commit 55bad19

File tree

5 files changed

+50
-41
lines changed

5 files changed

+50
-41
lines changed

Include/cpython/memoryobject.h

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#ifndef Py_CPYTHON_MEMORYOBJECT_H
2+
# error "this header file must not be included directly"
3+
#endif
4+
5+
PyAPI_DATA(PyTypeObject) _PyManagedBuffer_Type;
6+
7+
/* The structs are declared here so that macros can work, but they shouldn't
8+
be considered public. Don't access their fields directly, use the macros
9+
and functions instead! */
10+
#define _Py_MANAGED_BUFFER_RELEASED 0x001 /* access to exporter blocked */
11+
#define _Py_MANAGED_BUFFER_FREE_FORMAT 0x002 /* free format */
12+
13+
typedef struct {
14+
PyObject_HEAD
15+
int flags; /* state flags */
16+
Py_ssize_t exports; /* number of direct memoryview exports */
17+
Py_buffer master; /* snapshot buffer obtained from the original exporter */
18+
} _PyManagedBufferObject;
19+
20+
21+
/* memoryview state flags */
22+
#define _Py_MEMORYVIEW_RELEASED 0x001 /* access to master buffer blocked */
23+
#define _Py_MEMORYVIEW_C 0x002 /* C-contiguous layout */
24+
#define _Py_MEMORYVIEW_FORTRAN 0x004 /* Fortran contiguous layout */
25+
#define _Py_MEMORYVIEW_SCALAR 0x008 /* scalar: ndim = 0 */
26+
#define _Py_MEMORYVIEW_PIL 0x010 /* PIL-style layout */
27+
28+
typedef struct {
29+
PyObject_VAR_HEAD
30+
_PyManagedBufferObject *mbuf; /* managed buffer */
31+
Py_hash_t hash; /* hash value for read-only views */
32+
int flags; /* state flags */
33+
Py_ssize_t exports; /* number of buffer re-exports */
34+
Py_buffer view; /* private copy of the exporter's view */
35+
PyObject *weakreflist;
36+
Py_ssize_t ob_array[1]; /* shape, strides, suboffsets */
37+
} PyMemoryViewObject;
38+
39+
/* Get a pointer to the memoryview's private copy of the exporter's buffer. */
40+
#define PyMemoryView_GET_BUFFER(op) (&((PyMemoryViewObject *)(op))->view)
41+
/* Get a pointer to the exporting object (this may be NULL!). */
42+
#define PyMemoryView_GET_BASE(op) (((PyMemoryViewObject *)(op))->view.obj)

Include/memoryobject.h

+3-41
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,10 @@
66
extern "C" {
77
#endif
88

9-
#ifndef Py_LIMITED_API
10-
PyAPI_DATA(PyTypeObject) _PyManagedBuffer_Type;
11-
#endif
129
PyAPI_DATA(PyTypeObject) PyMemoryView_Type;
1310

1411
#define PyMemoryView_Check(op) Py_IS_TYPE((op), &PyMemoryView_Type)
1512

16-
#ifndef Py_LIMITED_API
17-
/* Get a pointer to the memoryview's private copy of the exporter's buffer. */
18-
#define PyMemoryView_GET_BUFFER(op) (&((PyMemoryViewObject *)(op))->view)
19-
/* Get a pointer to the exporting object (this may be NULL!). */
20-
#define PyMemoryView_GET_BASE(op) (((PyMemoryViewObject *)(op))->view.obj)
21-
#endif
22-
2313
PyAPI_FUNC(PyObject *) PyMemoryView_FromObject(PyObject *base);
2414
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
2515
PyAPI_FUNC(PyObject *) PyMemoryView_FromMemory(char *mem, Py_ssize_t size,
@@ -32,38 +22,10 @@ PyAPI_FUNC(PyObject *) PyMemoryView_GetContiguous(PyObject *base,
3222
int buffertype,
3323
char order);
3424

35-
36-
/* The structs are declared here so that macros can work, but they shouldn't
37-
be considered public. Don't access their fields directly, use the macros
38-
and functions instead! */
3925
#ifndef Py_LIMITED_API
40-
#define _Py_MANAGED_BUFFER_RELEASED 0x001 /* access to exporter blocked */
41-
#define _Py_MANAGED_BUFFER_FREE_FORMAT 0x002 /* free format */
42-
typedef struct {
43-
PyObject_HEAD
44-
int flags; /* state flags */
45-
Py_ssize_t exports; /* number of direct memoryview exports */
46-
Py_buffer master; /* snapshot buffer obtained from the original exporter */
47-
} _PyManagedBufferObject;
48-
49-
50-
/* memoryview state flags */
51-
#define _Py_MEMORYVIEW_RELEASED 0x001 /* access to master buffer blocked */
52-
#define _Py_MEMORYVIEW_C 0x002 /* C-contiguous layout */
53-
#define _Py_MEMORYVIEW_FORTRAN 0x004 /* Fortran contiguous layout */
54-
#define _Py_MEMORYVIEW_SCALAR 0x008 /* scalar: ndim = 0 */
55-
#define _Py_MEMORYVIEW_PIL 0x010 /* PIL-style layout */
56-
57-
typedef struct {
58-
PyObject_VAR_HEAD
59-
_PyManagedBufferObject *mbuf; /* managed buffer */
60-
Py_hash_t hash; /* hash value for read-only views */
61-
int flags; /* state flags */
62-
Py_ssize_t exports; /* number of buffer re-exports */
63-
Py_buffer view; /* private copy of the exporter's view */
64-
PyObject *weakreflist;
65-
Py_ssize_t ob_array[1]; /* shape, strides, suboffsets */
66-
} PyMemoryViewObject;
26+
# define Py_CPYTHON_MEMORYOBJECT_H
27+
# include "cpython/memoryobject.h"
28+
# undef Py_CPYTHON_MEMORYOBJECT_H
6729
#endif
6830

6931
#ifdef __cplusplus

Makefile.pre.in

+1
Original file line numberDiff line numberDiff line change
@@ -1585,6 +1585,7 @@ PYTHON_HEADERS= \
15851585
$(srcdir)/Include/cpython/listobject.h \
15861586
$(srcdir)/Include/cpython/longintrepr.h \
15871587
$(srcdir)/Include/cpython/longobject.h \
1588+
$(srcdir)/Include/cpython/memoryobject.h \
15881589
$(srcdir)/Include/cpython/methodobject.h \
15891590
$(srcdir)/Include/cpython/modsupport.h \
15901591
$(srcdir)/Include/cpython/object.h \

PCbuild/pythoncore.vcxproj

+1
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@
155155
<ClInclude Include="..\Include\cpython\listobject.h" />
156156
<ClInclude Include="..\Include\cpython\longintrepr.h" />
157157
<ClInclude Include="..\Include\cpython\longobject.h" />
158+
<ClInclude Include="..\Include\cpython\memoryobject.h" />
158159
<ClInclude Include="..\Include\cpython\methodobject.h" />
159160
<ClInclude Include="..\Include\cpython\modsupport.h" />
160161
<ClInclude Include="..\Include\cpython\object.h" />

PCbuild/pythoncore.vcxproj.filters

+3
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,9 @@
384384
<ClInclude Include="..\Include\cpython\longobject.h">
385385
<Filter>Include</Filter>
386386
</ClInclude>
387+
<ClInclude Include="..\Include\cpython\memoryobject.h">
388+
<Filter>Include</Filter>
389+
</ClInclude>
387390
<ClInclude Include="..\Include\cpython\odictobject.h">
388391
<Filter>Include</Filter>
389392
</ClInclude>

0 commit comments

Comments
 (0)