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_DEPRECATED(3.11) 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 byte 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_DecodeEscape2(const char *, Py_ssize_t,
29 const char *,
30 int *, const char **);
31 // Export for binary compatibility.
32 PyAPI_FUNC(PyObject *) _PyBytes_DecodeEscape(const char *, Py_ssize_t,
33 const char *, const char **);
34
35 /* Macros and static inline functions, trading safety for speed */
36 #define _PyBytes_CAST(op) \
37 (assert(PyBytes_Check(op)), _Py_CAST(PyBytesObject*, op))
38
PyBytes_AS_STRING(PyObject * op)39 static inline char* PyBytes_AS_STRING(PyObject *op)
40 {
41 return _PyBytes_CAST(op)->ob_sval;
42 }
43 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
44 # define PyBytes_AS_STRING(op) PyBytes_AS_STRING(_PyObject_CAST(op))
45 #endif
46
PyBytes_GET_SIZE(PyObject * op)47 static inline Py_ssize_t PyBytes_GET_SIZE(PyObject *op) {
48 PyBytesObject *self = _PyBytes_CAST(op);
49 return Py_SIZE(self);
50 }
51 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
52 # define PyBytes_GET_SIZE(self) PyBytes_GET_SIZE(_PyObject_CAST(self))
53 #endif
54
55 /* _PyBytes_Join(sep, x) is like sep.join(x). sep must be PyBytesObject*,
56 x must be an iterable object. */
57 PyAPI_FUNC(PyObject *) _PyBytes_Join(PyObject *sep, PyObject *x);
58
59
60 /* The _PyBytesWriter structure is big: it contains an embedded "stack buffer".
61 A _PyBytesWriter variable must be declared at the end of variables in a
62 function to optimize the memory allocation on the stack. */
63 typedef struct {
64 /* bytes, bytearray or NULL (when the small buffer is used) */
65 PyObject *buffer;
66
67 /* Number of allocated size. */
68 Py_ssize_t allocated;
69
70 /* Minimum number of allocated bytes,
71 incremented by _PyBytesWriter_Prepare() */
72 Py_ssize_t min_size;
73
74 /* If non-zero, use a bytearray instead of a bytes object for buffer. */
75 int use_bytearray;
76
77 /* If non-zero, overallocate the buffer (default: 0).
78 This flag must be zero if use_bytearray is non-zero. */
79 int overallocate;
80
81 /* Stack buffer */
82 int use_small_buffer;
83 char small_buffer[512];
84 } _PyBytesWriter;
85
86 /* Initialize a bytes writer
87
88 By default, the overallocation is disabled. Set the overallocate attribute
89 to control the allocation of the buffer. */
90 PyAPI_FUNC(void) _PyBytesWriter_Init(_PyBytesWriter *writer);
91
92 /* Get the buffer content and reset the writer.
93 Return a bytes object, or a bytearray object if use_bytearray is non-zero.
94 Raise an exception and return NULL on error. */
95 PyAPI_FUNC(PyObject *) _PyBytesWriter_Finish(_PyBytesWriter *writer,
96 void *str);
97
98 /* Deallocate memory of a writer (clear its internal buffer). */
99 PyAPI_FUNC(void) _PyBytesWriter_Dealloc(_PyBytesWriter *writer);
100
101 /* Allocate the buffer to write size bytes.
102 Return the pointer to the beginning of buffer data.
103 Raise an exception and return NULL on error. */
104 PyAPI_FUNC(void*) _PyBytesWriter_Alloc(_PyBytesWriter *writer,
105 Py_ssize_t size);
106
107 /* Ensure that the buffer is large enough to write *size* bytes.
108 Add size to the writer minimum size (min_size attribute).
109
110 str is the current pointer inside the buffer.
111 Return the updated current pointer inside the buffer.
112 Raise an exception and return NULL on error. */
113 PyAPI_FUNC(void*) _PyBytesWriter_Prepare(_PyBytesWriter *writer,
114 void *str,
115 Py_ssize_t size);
116
117 /* Resize the buffer to make it larger.
118 The new buffer may be larger than size bytes because of overallocation.
119 Return the updated current pointer inside the buffer.
120 Raise an exception and return NULL on error.
121
122 Note: size must be greater than the number of allocated bytes in the writer.
123
124 This function doesn't use the writer minimum size (min_size attribute).
125
126 See also _PyBytesWriter_Prepare().
127 */
128 PyAPI_FUNC(void*) _PyBytesWriter_Resize(_PyBytesWriter *writer,
129 void *str,
130 Py_ssize_t size);
131
132 /* Write bytes.
133 Raise an exception and return NULL on error. */
134 PyAPI_FUNC(void*) _PyBytesWriter_WriteBytes(_PyBytesWriter *writer,
135 void *str,
136 const void *bytes,
137 Py_ssize_t size);
138