8000 gh-106320: Remove _PyBytesWriter C API (#106399) · python/cpython@ec931fc · GitHub
[go: up one dir, main page]

Skip to content

Commit ec931fc

Browse files
authored
gh-106320: Remove _PyBytesWriter C API (#106399)
Remove the _PyBytesWriter C API: move it to the internal C API (pycore_bytesobject.h).
1 parent d8c5d76 commit ec931fc

File tree

5 files changed

+86
-82
lines changed

5 files changed

+86
-82
lines changed

Include/cpython/bytesobject.h

Lines changed: 0 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -47,83 +47,3 @@ static inline Py_ssize_t PyBytes_GET_SIZE(PyObject *op) {
4747
/* _PyBytes_Join(sep, x) is like sep.join(x). sep must be PyBytesObject*,
4848
x must be an iterable object. */
4949
PyAPI_FUNC(PyObject *) _PyBytes_Join(PyObject *sep, PyObject *x);
50-
51-
52-
/* The _PyBytesWriter structure is big: it contains an embedded "stack buffer".
53-
A _PyBytesWriter variable must be declared at the end of variables in a
54-
function to optimize the memory allocation on the stack. */
55-
typedef struct {
56-
/* bytes, bytearray or NULL (when the small buffer is used) */
57-
PyObject *buffer;
58-
59-
/* Number of allocated size. */
60-
Py_ssize_t allocated;
61-
62-
/* Minimum number of allocated bytes,
63-
incremented by _PyBytesWriter_Prepare() */
64-
Py_ssize_t min_size;
65-
66-
/* If non-zero, use a bytearray instead of a bytes object for buffer. */
67-
int use_bytearray;
68-
69-
/* If non-zero, overallocate the buffer (default: 0).
70-
This flag must be zero if use_bytearray is non-zero. */
71-
int overallocate;
72-
73-
/* Stack buffer */
74-
int use_small_buffer;
75-
char small_buffer[512];
76-
} _PyBytesWriter;
77-
78-
/* Initialize a bytes writer
79-
80-
By default, the overallocation is disabled. Set the overallocate attribute
81-
to control the allocation of the buffer. */
82-
PyAPI_FUNC(void) _PyBytesWriter_Init(_PyBytesWriter *writer);
83-
84-
/* Get the buffer content and reset the writer.
85-
Return a bytes object, or a bytearray object if use_bytearray is non-zero.
86-
Raise an exception and return NULL on error. */
87-
PyAPI_FUNC(PyObject *) _PyBytesWriter_Finish(_PyBytesWriter *writer,
88-
void *str);
89-
90-
/* Deallocate memory of a writer (clear its internal buffer). */
91-
PyAPI_FUNC(void) _PyBytesWriter_Dealloc(_PyBytesWriter *writer);
92-
93-
/* Allocate the buffer to write size bytes.
94-
Return the pointer to the beginning of buffer data.
95-
Raise an exception and return NULL on error. */
96-
PyAPI_FUNC(void*) _PyBytesWriter_Alloc(_PyBytesWriter *writer,
97-
Py_ssize_t size);
98-
99-
/* Ensure that the buffer is large enough to write *size* bytes.
100-
Add size to the writer minimum size (min_size attribute).
101-
102-
str is the current pointer inside the buffer.
103-
Return the updated current pointer inside the buffer.
104-
Raise an exception and return NULL on error. */
105-
PyAPI_FUNC(void*) _PyBytesWriter_Prepare(_PyBytesWriter *writer,
106-
void *str,
107-
Py_ssize_t size);
108-
109-
/* Resize the buffer to make it larger.
110-
The new buffer may be larger than size bytes because of overallocation.
111-
Return the updated current pointer inside the buffer.
112-
Raise an exception and return NULL on error.
113-
114-
Note: size must be greater than the number of allocated bytes in the writer.
115-
116-
This function doesn't use the writer minimum size (min_size attribute).
117-
118-
See also _PyBytesWriter_Prepare().
119-
*/
120-
PyAPI_FUNC(void*) _PyBytesWriter_Resize(_PyBytesWriter *writer,
121-
void *str,
122-
Py_ssize_t size);
123-
124-
/* Write bytes.
125-
Raise an exception and return NULL on error. */
126-
PyAPI_FUNC(void*) _PyBytesWriter_WriteBytes(_PyBytesWriter *writer,
127-
void *str,
128-
const void *bytes,
129-
Py_ssize_t size);

Include/internal/pycore_bytesobject.h

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,87 @@ PyAPI_FUNC(void)
4141
_PyBytes_Repeat(char* dest, Py_ssize_t len_dest,
4242
const char* src, Py_ssize_t len_src);
4343

44+
/* --- _PyBytesWriter ----------------------------------------------------- */
45+
46+
/* The _PyBytesWriter structure is big: it contains an embedded "stack buffer".
47+
A _PyBytesWriter variable must be declared at the end of variables in a
48+
function to optimize the memory allocation on the stack. */
49+
typedef struct {
50+
/* bytes, bytearray or NULL (when the small buffer is used) */
51+
PyObject *buffer;
52+
53+
/* Number of allocated size. */
54+
Py_ssize_t allocated;
55+
56+
/* Minimum number of allocated bytes,
57+
incremented by _PyBytesWriter_Prepare() */
58+
Py_ssize_t min_size;
59+
60+
/* If non-zero, use a bytearray instead of a bytes object for buffer. */
61+
int use_bytearray;
62+
63+
/* If non-zero, overallocate the buffer (default: 0).
64+
This flag must be zero if use_bytearray is non-zero. */
65+
int overallocate;
66+
67+
/* Stack buffer */
68+
int use_small_buffer;
69+
char small_buffer[512];
70+
} _PyBytesWriter;
71+
72+
/* Initialize a bytes writer
73+
74+
By default, the overallocation is disabled. Set the overallocate attribute
75+
to control the allocation of the buffer. */
76+
PyAPI_FUNC(void) _PyBytesWriter_Init(_PyBytesWriter *writer);
77+
78+
/* Get the buffer content and reset the writer.
79+
Return a bytes object, or a bytearray object if use_bytearray is non-zero.
80+
Raise an exception and return NULL on error. */
81+
PyAPI_FUNC(PyObject *) _PyBytesWriter_Finish(_PyBytesWriter *writer,
82+
void *str);
83+
84+
/* Deallocate memory of a writer (clear its internal buffer). */
85+
PyAPI_FUNC(void) _PyBytesWriter_Dealloc(_PyBytesWriter *writer);
86+
87+
/* Allocate the buffer to write size bytes.
88+
Return the pointer to the beginning of buffer data.
89+
Raise an exception and return NULL on error. */
90+
PyAPI_FUNC(void*) _PyBytesWriter_Alloc(_PyBytesWriter *writer,
91+
Py_ssize_t size);
92+
93+
/* Ensure that the buffer is large enough to write *size* bytes.
94+
Add size to the writer minimum size (min_size attribute).
95+
96+
str is the current pointer inside the buffer.
97+
Return the updated current pointer inside the buffer.
98+
Raise an exception and return NULL on error. */
99+
PyAPI_FUNC(void*) _PyBytesWriter_Prepare(_PyBytesWriter *writer,
100+
void *str,
101+
Py_ssize_t size);
102+
103+
/* Resize the buffer to make it larger.
104+
The new buffer may be larger than size bytes because of overallocation.
105+
Return the updated current pointer inside the buffer.
106+
Raise an exception and return NULL on error.
107+
108+
Note: size must be greater than the number of allocated bytes in the writer.
109+
110+
This function doesn't use the writer minimum size (min_size attribute).
111+
112+
See also _PyBytesWriter_Prepare().
113+
*/
114+
PyAPI_FUNC(void*) _PyBytesWriter_Resize(_PyBytesWriter *writer,
115+
void *str,
116+
Py_ssize_t size);
117+
118+
/* Write bytes.
119+
Raise an exception and return NULL on error. */
120+
PyAPI_FUNC(void*) _PyBytesWriter_WriteBytes(_PyBytesWriter *writer,
121+
void *str,
122+
const void *bytes,
123+
Py_ssize_t size);
124+
44125
#ifdef __cplusplus
45126
}
46127
#endif

Include/internal/pycore_long.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ extern "C" {
88
# error "this header requires Py_BUILD_CORE define"
99
#endif
1010

11-
#include "pycore_global_objects.h" // _PY_NSMALLNEGINTS
11+
#include "pycore_bytesobject.h" // _PyBytesWriter
12+
#include "pycore_global_objects.h"// _PY_NSMALLNEGINTS
1213
#include "pycore_runtime.h" // _PyRuntime
1314

1415
/*

Modules/_pickle.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99
#endif
1010

1111
#include "Python.h"
12+
#include "pycore_bytesobject.h" // _PyBytesWriter
1213
#include "pycore_ceval.h" // _Py_EnterRecursiveCall()
1314
#include "pycore_moduleobject.h" // _PyModule_GetState()
14-
#include "pycore_runtime.h" // _Py_ID()
1515
#include "pycore_pystate.h" // _PyThreadState_GET()
16+
#include "pycore_runtime.h" // _Py_ID()
1617
#include "structmember.h" // PyMemberDef
1718

1819
#include <stdlib.h> // strtol()

Modules/_struct.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#endif
99

1010
#include "Python.h"
11+
#include "pycore_bytesobject.h" // _PyBytesWriter
1112
#include "pycore_moduleobject.h" // _PyModule_GetState()
1213
#include "structmember.h" // PyMemberDef
1314
#include <ctype.h>

0 commit comments

Comments
 (0)
0