Skip to content

Commit 98921ae

Browse files
authored
bpo-35134: Add Include/cpython/bytesobject.h file (GH-18494)
Add Include/cpython/bytearrayobject.h and Include/cpython/bytesobject.h header files. Move CPython C API from Include/bytesobject.h into a new Include/cpython/bytesobject.h header file which is included by Include/bytesobject.h. Do a similar change for Include/bytearrayobject.h.
1 parent e9e7d28 commit 98921ae

File tree

7 files changed

+155
-140
lines changed

7 files changed

+155
-140
lines changed

Include/bytearrayobject.h

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,6 @@ extern "C" {
1818
* to contain a char pointer, not an unsigned char pointer.
1919
*/
2020

21-
/* Object layout */
22-
#ifndef Py_LIMITED_API
23-
typedef struct {
24-
PyObject_VAR_HEAD
25-
Py_ssize_t ob_alloc; /* How many bytes allocated in ob_bytes */
26-
char *ob_bytes; /* Physical backing buffer */
27-
char *ob_start; /* Logical start inside ob_bytes */
28-
Py_ssize_t ob_exports; /* How many buffer exports */
29-
} PyByteArrayObject;
30-
#endif
31-
3221
/* Type object */
3322
PyAPI_DATA(PyTypeObject) PyByteArray_Type;
3423
PyAPI_DATA(PyTypeObject) PyByteArrayIter_Type;
@@ -45,14 +34,10 @@ PyAPI_FUNC(Py_ssize_t) PyByteArray_Size(PyObject *);
4534
PyAPI_FUNC(char *) PyByteArray_AsString(PyObject *);
4635
PyAPI_FUNC(int) PyByteArray_Resize(PyObject *, Py_ssize_t);
4736

48-
/* Macros, trading safety for speed */
4937
#ifndef Py_LIMITED_API
50-
#define PyByteArray_AS_STRING(self) \
51-
(assert(PyByteArray_Check(self)), \
52-
Py_SIZE(self) ? ((PyByteArrayObject *)(self))->ob_start : _PyByteArray_empty_string)
53-
#define PyByteArray_GET_SIZE(self) (assert(PyByteArray_Check(self)), Py_SIZE(self))
54-
55-
PyAPI_DATA(char) _PyByteArray_empty_string[];
38+
# define Py_CPYTHON_BYTEARRAYOBJECT_H
39+
# include "cpython/bytearrayobject.h"
40+
# undef Py_CPYTHON_BYTEARRAYOBJECT_H
5641
#endif
5742

5843
#ifdef __cplusplus

Include/bytesobject.h

Lines changed: 4 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,6 @@ functions should be applied to nil objects.
2727
/* Caching the hash (ob_shash) saves recalculation of a string's hash value.
2828
This significantly speeds up dict lookups. */
2929

30-
#ifndef Py_LIMITED_API
31-
typedef struct {
32-
PyObject_VAR_HEAD
33-
Py_hash_t ob_shash;
34-
char ob_sval[1];
35-
36-
/* Invariants:
37-
* ob_sval contains space for 'ob_size+1' elements.
38-
* ob_sval[ob_size] == 0.
39-
* ob_shash is the hash of the string or -1 if not computed yet.
40-
*/
41-
} PyBytesObject;
42-
#endif
43-
4430
PyAPI_DATA(PyTypeObject) PyBytes_Type;
4531
PyAPI_DATA(PyTypeObject) PyBytesIter_Type;
4632

@@ -60,38 +46,9 @@ PyAPI_FUNC(char *) PyBytes_AsString(PyObject *);
6046
PyAPI_FUNC(PyObject *) PyBytes_Repr(PyObject *, int);
6147
PyAPI_FUNC(void) PyBytes_Concat(PyObject **, PyObject *);
6248
PyAPI_FUNC(void) PyBytes_ConcatAndDel(PyObject **, PyObject *);
63-
#ifndef Py_LIMITED_API
64-
PyAPI_FUNC(int) _PyBytes_Resize(PyObject **, Py_ssize_t);
65-
PyAPI_FUNC(PyObject*) _PyBytes_FormatEx(
66-
const char *format,
67-
Py_ssize_t format_len,
68-
PyObject *args,
69-
int use_bytearray);
70-
PyAPI_FUNC(PyObject*) _PyBytes_FromHex(
71-
PyObject *string,
72-
int use_bytearray);
73-
#endif
7449
PyAPI_FUNC(PyObject *) PyBytes_DecodeEscape(const char *, Py_ssize_t,
7550
const char *, Py_ssize_t,
7651
const char *);
77-
#ifndef Py_LIMITED_API
78-
/* Helper for PyBytes_DecodeEscape that detects invalid escape chars. */
79-
PyAPI_FUNC(PyObject *) _PyBytes_DecodeEscape(const char *, Py_ssize_t,
80-
const char *, const char **);
81-
#endif
82-
83-
/* Macro, trading safety for speed */
84-
#ifndef Py_LIMITED_API
85-
#define PyBytes_AS_STRING(op) (assert(PyBytes_Check(op)), \
86-
(((PyBytesObject *)(op))->ob_sval))
87-
#define PyBytes_GET_SIZE(op) (assert(PyBytes_Check(op)),Py_SIZE(op))
88-
#endif
89-
90-
/* _PyBytes_Join(sep, x) is like sep.join(x). sep must be PyBytesObject*,
91-
x must be an iterable object. */
92-
#ifndef Py_LIMITED_API
93-
PyAPI_FUNC(PyObject *) _PyBytes_Join(PyObject *sep, PyObject *x);
94-
#endif
9552

9653
/* Provides access to the internal data buffer and size of a string
9754
object or the default encoded version of a Unicode object. Passing
@@ -114,85 +71,10 @@ PyAPI_FUNC(int) PyBytes_AsStringAndSize(
11471
#define F_ZERO (1<<4)
11572

11673
#ifndef Py_LIMITED_API
117-
/* The _PyBytesWriter structure is big: it contains an embedded "stack buffer".
118-
A _PyBytesWriter variable must be declared at the end of variables in a
119-
function to optimize the memory allocation on the stack. */
120-
typedef struct {
121-
/* bytes, bytearray or NULL (when the small buffer is used) */
122-
PyObject *buffer;
123-
124-
/* Number of allocated size. */
125-
Py_ssize_t allocated;
126-
127-
/* Minimum number of allocated bytes,
128-
incremented by _PyBytesWriter_Prepare() */
129-
Py_ssize_t min_size;
130-
131-
/* If non-zero, use a bytearray instead of a bytes object for buffer. */
132-
int use_bytearray;
133-
134-
/* If non-zero, overallocate the buffer (default: 0).
135-
This flag must be zero if use_bytearray is non-zero. */
136-
int overallocate;
137-
138-
/* Stack buffer */
139-
int use_small_buffer;
140-
char small_buffer[512];
141-
} _PyBytesWriter;
142-
143-
/* Initialize a bytes writer
144-
145-
By default, the overallocation is disabled. Set the overallocate attribute
146-
to control the allocation of the buffer. */
147-
PyAPI_FUNC(void) _PyBytesWriter_Init(_PyBytesWriter *writer);
148-
149-
/* Get the buffer content and reset the writer.
150-
Return a bytes object, or a bytearray object if use_bytearray is non-zero.
151-
Raise an exception and return NULL on error. */
152-
PyAPI_FUNC(PyObject *) _PyBytesWriter_Finish(_PyBytesWriter *writer,
153-
void *str);
154-
155-
/* Deallocate memory of a writer (clear its internal buffer). */
156-
PyAPI_FUNC(void) _PyBytesWriter_Dealloc(_PyBytesWriter *writer);
157-
158-
/* Allocate the buffer to write size bytes.
159-
Return the pointer to the beginning of buffer data.
160-
Raise an exception and return NULL on error. */
161-
PyAPI_FUNC(void*) _PyBytesWriter_Alloc(_PyBytesWriter *writer,
162-
Py_ssize_t size);
163-
164-
/* Ensure that the buffer is large enough to write *size* bytes.
165-
Add size to the writer minimum size (min_size attribute).
166-
167-
str is the current pointer inside the buffer.
168-
Return the updated current pointer inside the buffer.
169-
Raise an exception and return NULL on error. */
170-
PyAPI_FUNC(void*) _PyBytesWriter_Prepare(_PyBytesWriter *writer,
171-
void *str,
172-
Py_ssize_t size);
173-
174-
/* Resize the buffer to make it larger.
175-
The new buffer may be larger than size bytes because of overallocation.
176-
Return the updated current pointer inside the buffer.
177-
Raise an exception and return NULL on error.
178-
179-
Note: size must be greater than the number of allocated bytes in the writer.
180-
181-
This function doesn't use the writer minimum size (min_size attribute).
182-
183-
See also _PyBytesWriter_Prepare().
184-
*/
185-
PyAPI_FUNC(void*) _PyBytesWriter_Resize(_PyBytesWriter *writer,
186-
void *str,
187-
Py_ssize_t size);
188-
189-
/* Write bytes.
190-
Raise an exception and return NULL on error. */
191-
PyAPI_FUNC(void*) _PyBytesWriter_WriteBytes(_PyBytesWriter *writer,
192-
void *str,
193-
const void *bytes,
194-
Py_ssize_t size);
195-
#endif /* Py_LIMITED_API */
74+
# define Py_CPYTHON_BYTESOBJECT_H
75+
# include "cpython/bytesobject.h"
76+
# undef Py_CPYTHON_BYTESOBJECT_H
77+
#endif
19678

19779
#ifdef __cplusplus
19880
}

Include/cpython/bytearrayobject.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef Py_CPYTHON_BYTEARRAYOBJECT_H
2+
# error "this header file must not be included directly"
3+
#endif
4+
5+
/* Object layout */
6+
typedef struct {
7+
PyObject_VAR_HEAD
8+
Py_ssize_t ob_alloc; /* How many bytes allocated in ob_bytes */
9+
char *ob_bytes; /* Physical backing buffer */
10+
char *ob_start; /* Logical start inside ob_bytes */
11+
Py_ssize_t ob_exports; /* How many buffer exports */
12+
} PyByteArrayObject;
13+
14+
/* Macros, trading safety for speed */
15+
#define PyByteArray_AS_STRING(self) \
16+
(assert(PyByteArray_Check(self)), \
17+
Py_SIZE(self) ? ((PyByteArrayObject *)(self))->ob_start : _PyByteArray_empty_string)
18+
#define PyByteArray_GET_SIZE(self) (assert(PyByteArray_Check(self)), Py_SIZE(self))
19+
20+
PyAPI_DATA(char) _PyByteArray_empty_string[];

Include/cpython/bytesobject.h

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#ifndef Py_CPYTHON_BYTESOBJECT_H
2+
# error "this header file must not be included directly"
3+
#endif
4+
5+
typedef struct {
6+
PyObject_VAR_HEAD
7+
Py_hash_t ob_shash;
8+
char ob_sval[1];
9+
10+
/* Invariants:
11+
* ob_sval contains space for 'ob_size+1' elements.
12+
* ob_sval[ob_size] == 0.
13+
* ob_shash is the hash of the string or -1 if not computed yet.
14+
*/
15+
} PyBytesObject;
16+
17+
PyAPI_FUNC(int) _PyBytes_Resize(PyObject **, Py_ssize_t);
18+
PyAPI_FUNC(PyObject*) _PyBytes_FormatEx(
19+
const char *format,
20+
Py_ssize_t format_len,
21+
PyObject *args,
22+
int use_bytearray);
23+
PyAPI_FUNC(PyObject*) _PyBytes_FromHex(
24+
PyObject *string,
25+
int use_bytearray);
26+
27+
/* Helper for PyBytes_DecodeEscape that detects invalid escape chars. */
28+
PyAPI_FUNC(PyObject *) _PyBytes_DecodeEscape(const char *, Py_ssize_t,
29+
const char *, const char **);
30+
31+
/* Macro, trading safety for speed */
32+
#define PyBytes_AS_STRING(op) (assert(PyBytes_Check(op)), \
33+
(((PyBytesObject *)(op))->ob_sval))
34+
#define PyBytes_GET_SIZE(op) (assert(PyBytes_Check(op)),Py_SIZE(op))
35+
36+
/* _PyBytes_Join(sep, x) is like sep.join(x). sep must be PyBytesObject*,
37+
x must be an iterable object. */
38+
PyAPI_FUNC(PyObject *) _PyBytes_Join(PyObject *sep, PyObject *x);
39+
40+
41+
/* The _PyBytesWriter structure is big: it contains an embedded "stack buffer".
42+
A _PyBytesWriter variable must be declared at the end of variables in a
43+
function to optimize the memory allocation on the stack. */
44+
typedef struct {
45+
/* bytes, bytearray or NULL (when the small buffer is used) */
46+
PyObject *buffer;
47+
48+
/* Number of allocated size. */
49+
Py_ssize_t allocated;
50+
51+
/* Minimum number of allocated bytes,
52+
incremented by _PyBytesWriter_Prepare() */
53+
Py_ssize_t min_size;
54+
55+
/* If non-zero, use a bytearray instead of a bytes object for buffer. */
56+
int use_bytearray;
57+
58+
/* If non-zero, overallocate the buffer (default: 0).
59+
This flag must be zero if use_bytearray is non-zero. */
60+
int overallocate;
61+
62+
/* Stack buffer */
63+
int use_small_buffer;
64+
char small_buffer[512];
65+
} _PyBytesWriter;
66+
67+
/* Initialize a bytes writer
68+
69+
By default, the overallocation is disabled. Set the overallocate attribute
70+
to control the allocation of the buffer. */
71+
PyAPI_FUNC(void) _PyBytesWriter_Init(_PyBytesWriter *writer);
72+
73+
/* Get the buffer content and reset the writer.
74+
Return a bytes object, or a bytearray object if use_bytearray is non-zero.
75+
Raise an exception and return NULL on error. */
76+
PyAPI_FUNC(PyObject *) _PyBytesWriter_Finish(_PyBytesWriter *writer,
77+
void *str);
78+
79+
/* Deallocate memory of a writer (clear its internal buffer). */
80+
PyAPI_FUNC(void) _PyBytesWriter_Dealloc(_PyBytesWriter *writer);
81+
82+
/* Allocate the buffer to write size bytes.
83+
Return the pointer to the beginning of buffer data.
84+
Raise an exception and return NULL on error. */
85+
PyAPI_FUNC(void*) _PyBytesWriter_Alloc(_PyBytesWriter *writer,
86+
Py_ssize_t size);
87+
88+
/* Ensure that the buffer is large enough to write *size* bytes.
89+
Add size to the writer minimum size (min_size attribute).
90+
91+
str is the current pointer inside the buffer.
92+
Return the updated current pointer inside the buffer.
93+
Raise an exception and return NULL on error. */
94+
PyAPI_FUNC(void*) _PyBytesWriter_Prepare(_PyBytesWriter *writer,
95+
void *str,
96+
Py_ssize_t size);
97+
98+
/* Resize the buffer to make it larger.
99+
The new buffer may be larger than size bytes because of overallocation.
100+
Return the updated current pointer inside the buffer.
101+
Raise an exception and return NULL on error.
102+
103+
Note: size must be greater than the number of allocated bytes in the writer.
104+
105+
This function doesn't use the writer minimum size (min_size attribute).
106+
107+
See also _PyBytesWriter_Prepare().
108+
*/
109+
PyAPI_FUNC(void*) _PyBytesWriter_Resize(_PyBytesWriter *writer,
110+
void *str,
111+
Py_ssize_t size);
112+
113+
/* Write bytes.
114+
Raise an exception and return NULL on error. */
115+
PyAPI_FUNC(void*) _PyBytesWriter_WriteBytes(_PyBytesWriter *writer,
116+
void *str,
117+
const void *bytes,
118+
Py_ssize_t size);

Makefile.pre.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,6 +1055,8 @@ PYTHON_HEADERS= \
10551055
$(srcdir)/Include/Python-ast.h \
10561056
\
10571057
$(srcdir)/Include/cpython/abstract.h \
1058+
$(srcdir)/Include/cpython/bytearrayobject.h \
1059+
$(srcdir)/Include/cpython/bytesobject.h \
10581060
$(srcdir)/Include/cpython/ceval.h \
10591061
$(srcdir)/Include/cpython/dictobject.h \
10601062
$(srcdir)/Include/cpython/fileobject.h \

PCbuild/pythoncore.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@
126126
<ClInclude Include="..\Include\complexobject.h" />
127127
<ClInclude Include="..\Include\context.h" />
128128
<ClInclude Include="..\Include\cpython\abstract.h" />
129+
<ClInclude Include="..\Include\cpython\bytearrayobject.h" />
130+
<ClInclude Include="..\Include\cpython\bytesobject.h" />
129131
<ClInclude Include="..\Include\cpython\ceval.h" />
130132
<ClInclude Include="..\Include\cpython\dictobject.h" />
131133
<ClInclude Include="..\Include\cpython\fileobject.h" />

PCbuild/pythoncore.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@
8181
<ClInclude Include="..\Include\cpython\abstract.h">
8282
<Filter>Include</Filter>
8383
</ClInclude>
84+
<ClInclude Include="..\Include\cpython\bytearrayobject.h">
85+
<Filter>Include</Filter>
86+
</ClInclude>
87+
<ClInclude Include="..\Include\cpython\bytesobject.h">
88+
<Filter>Include</Filter>
89+
</ClInclude>
8490
<ClInclude Include="..\Include\cpython\ceval.h">
8591
<Filter>Include</Filter>
8692
</ClInclude>

0 commit comments

Comments
 (0)