Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

bytesobject.h 8.3 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
  1. /* Bytes (String) object interface */
  2. #ifndef Py_BYTESOBJECT_H
  3. #define Py_BYTESOBJECT_H
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. #include <stdarg.h>
  8. /*
  9. Type PyBytesObject represents a character string. An extra zero byte is
  10. reserved at the end to ensure it is zero-terminated, but a size is
  11. present so strings with null bytes in them can be represented. This
  12. is an immutable object type.
  13. There are functions to create new string objects, to test
  14. an object for string-ness, and to get the
  15. string value. The latter function returns a null pointer
  16. if the object is not of the proper type.
  17. There is a variant that takes an explicit size as well as a
  18. variant that assumes a zero-terminated string. Note that none of the
  19. functions should be applied to nil objects.
  20. */
  21. /* Caching the hash (ob_shash) saves recalculation of a string's hash value.
  22. This significantly speeds up dict lookups. */
  23. #ifndef Py_LIMITED_API
  24. typedef struct {
  25. PyObject_VAR_HEAD
  26. Py_hash_t ob_shash;
  27. char ob_sval[1];
  28. /* Invariants:
  29. * ob_sval contains space for 'ob_size+1' elements.
  30. * ob_sval[ob_size] == 0.
  31. * ob_shash is the hash of the string or -1 if not computed yet.
  32. */
  33. } PyBytesObject;
  34. #endif
  35. PyAPI_DATA(PyTypeObject) PyBytes_Type;
  36. PyAPI_DATA(PyTypeObject) PyBytesIter_Type;
  37. #define PyBytes_Check(op) \
  38. PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_BYTES_SUBCLASS)
  39. #define PyBytes_CheckExact(op) (Py_TYPE(op) == &PyBytes_Type)
  40. PyAPI_FUNC(PyObject *) PyBytes_FromStringAndSize(const char *, Py_ssize_t);
  41. PyAPI_FUNC(PyObject *) PyBytes_FromString(const char *);
  42. PyAPI_FUNC(PyObject *) PyBytes_FromObject(PyObject *);
  43. PyAPI_FUNC(PyObject *) PyBytes_FromFormatV(const char*, va_list)
  44. Py_GCC_ATTRIBUTE((format(printf, 1, 0)));
  45. PyAPI_FUNC(PyObject *) PyBytes_FromFormat(const char*, ...)
  46. Py_GCC_ATTRIBUTE((format(printf, 1, 2)));
  47. PyAPI_FUNC(Py_ssize_t) PyBytes_Size(PyObject *);
  48. PyAPI_FUNC(char *) PyBytes_AsString(PyObject *);
  49. PyAPI_FUNC(PyObject *) PyBytes_Repr(PyObject *, int);
  50. PyAPI_FUNC(void) PyBytes_Concat(PyObject **, PyObject *);
  51. PyAPI_FUNC(void) PyBytes_ConcatAndDel(PyObject **, PyObject *);
  52. #ifndef Py_LIMITED_API
  53. PyAPI_FUNC(int) _PyBytes_Resize(PyObject **, Py_ssize_t);
  54. PyAPI_FUNC(PyObject*) _PyBytes_FormatEx(
  55. const char *format,
  56. Py_ssize_t format_len,
  57. PyObject *args,
  58. int use_bytearray);
  59. PyAPI_FUNC(PyObject*) _PyBytes_FromHex(
  60. PyObject *string,
  61. int use_bytearray);
  62. #endif
  63. PyAPI_FUNC(PyObject *) PyBytes_DecodeEscape(const char *, Py_ssize_t,
  64. const char *, Py_ssize_t,
  65. const char *);
  66. #ifndef Py_LIMITED_API
  67. /* Helper for PyBytes_DecodeEscape that detects invalid escape chars. */
  68. PyAPI_FUNC(PyObject *) _PyBytes_DecodeEscape(const char *, Py_ssize_t,
  69. const char *, Py_ssize_t,
  70. const char *,
  71. const char **);
  72. #endif
  73. /* Macro, trading safety for speed */
  74. #ifndef Py_LIMITED_API
  75. #define PyBytes_AS_STRING(op) (assert(PyBytes_Check(op)), \
  76. (((PyBytesObject *)(op))->ob_sval))
  77. #define PyBytes_GET_SIZE(op) (assert(PyBytes_Check(op)),Py_SIZE(op))
  78. #endif
  79. /* _PyBytes_Join(sep, x) is like sep.join(x). sep must be PyBytesObject*,
  80. x must be an iterable object. */
  81. #ifndef Py_LIMITED_API
  82. PyAPI_FUNC(PyObject *) _PyBytes_Join(PyObject *sep, PyObject *x);
  83. #endif
  84. /* Provides access to the internal data buffer and size of a string
  85. object or the default encoded version of a Unicode object. Passing
  86. NULL as *len parameter will force the string buffer to be
  87. 0-terminated (passing a string with embedded NULL characters will
  88. cause an exception). */
  89. PyAPI_FUNC(int) PyBytes_AsStringAndSize(
  90. PyObject *obj, /* string or Unicode object */
  91. char **s, /* pointer to buffer variable */
  92. Py_ssize_t *len /* pointer to length variable or NULL
  93. (only possible for 0-terminated
  94. strings) */
  95. );
  96. /* Using the current locale, insert the thousands grouping
  97. into the string pointed to by buffer. For the argument descriptions,
  98. see Objects/stringlib/localeutil.h */
  99. #ifndef Py_LIMITED_API
  100. PyAPI_FUNC(Py_ssize_t) _PyBytes_InsertThousandsGroupingLocale(char *buffer,
  101. Py_ssize_t n_buffer,
  102. char *digits,
  103. Py_ssize_t n_digits,
  104. Py_ssize_t min_width);
  105. /* Using explicit passed-in values, insert the thousands grouping
  106. into the string pointed to by buffer. For the argument descriptions,
  107. see Objects/stringlib/localeutil.h */
  108. PyAPI_FUNC(Py_ssize_t) _PyBytes_InsertThousandsGrouping(char *buffer,
  109. Py_ssize_t n_buffer,
  110. char *digits,
  111. Py_ssize_t n_digits,
  112. Py_ssize_t min_width,
  113. const char *grouping,
  114. const char *thousands_sep);
  115. #endif
  116. /* Flags used by string formatting */
  117. #define F_LJUST (1<<0)
  118. #define F_SIGN (1<<1)
  119. #define F_BLANK (1<<2)
  120. #define F_ALT (1<<3)
  121. #define F_ZERO (1<<4)
  122. #ifndef Py_LIMITED_API
  123. /* The _PyBytesWriter structure is big: it contains an embedded "stack buffer".
  124. A _PyBytesWriter variable must be declared at the end of variables in a
  125. function to optimize the memory allocation on the stack. */
  126. typedef struct {
  127. /* bytes, bytearray or NULL (when the small buffer is used) */
  128. PyObject *buffer;
  129. /* Number of allocated size. */
  130. Py_ssize_t allocated;
  131. /* Minimum number of allocated bytes,
  132. incremented by _PyBytesWriter_Prepare() */
  133. Py_ssize_t min_size;
  134. /* If non-zero, use a bytearray instead of a bytes object for buffer. */
  135. int use_bytearray;
  136. /* If non-zero, overallocate the buffer (default: 0).
  137. This flag must be zero if use_bytearray is non-zero. */
  138. int overallocate;
  139. /* Stack buffer */
  140. int use_small_buffer;
  141. char small_buffer[512];
  142. } _PyBytesWriter;
  143. /* Initialize a bytes writer
  144. By default, the overallocation is disabled. Set the overallocate attribute
  145. to control the allocation of the buffer. */
  146. PyAPI_FUNC(void) _PyBytesWriter_Init(_PyBytesWriter *writer);
  147. /* Get the buffer content and reset the writer.
  148. Return a bytes object, or a bytearray object if use_bytearray is non-zero.
  149. Raise an exception and return NULL on error. */
  150. PyAPI_FUNC(PyObject *) _PyBytesWriter_Finish(_PyBytesWriter *writer,
  151. void *str);
  152. /* Deallocate memory of a writer (clear its internal buffer). */
  153. PyAPI_FUNC(void) _PyBytesWriter_Dealloc(_PyBytesWriter *writer);
  154. /* Allocate the buffer to write size bytes.
  155. Return the pointer to the beginning of buffer data.
  156. Raise an exception and return NULL on error. */
  157. PyAPI_FUNC(void*) _PyBytesWriter_Alloc(_PyBytesWriter *writer,
  158. Py_ssize_t size);
  159. /* Ensure that the buffer is large enough to write *size* bytes.
  160. Add size to the writer minimum size (min_size attribute).
  161. str is the current pointer inside the buffer.
  162. Return the updated current pointer inside the buffer.
  163. Raise an exception and return NULL on error. */
  164. PyAPI_FUNC(void*) _PyBytesWriter_Prepare(_PyBytesWriter *writer,
  165. void *str,
  166. Py_ssize_t size);
  167. /* Resize the buffer to make it larger.
  168. The new buffer may be larger than size bytes because of overallocation.
  169. Return the updated current pointer inside the buffer.
  170. Raise an exception and return NULL on error.
  171. Note: size must be greater than the number of allocated bytes in the writer.
  172. This function doesn't use the writer minimum size (min_size attribute).
  173. See also _PyBytesWriter_Prepare().
  174. */
  175. PyAPI_FUNC(void*) _PyBytesWriter_Resize(_PyBytesWriter *writer,
  176. void *str,
  177. Py_ssize_t size);
  178. /* Write bytes.
  179. Raise an exception and return NULL on error. */
  180. PyAPI_FUNC(void*) _PyBytesWriter_WriteBytes(_PyBytesWriter *writer,
  181. void *str,
  182. const void *bytes,
  183. Py_ssize_t size);
  184. #endif /* Py_LIMITED_API */
  185. #ifdef __cplusplus
  186. }
  187. #endif
  188. #endif /* !Py_BYTESOBJECT_H */
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...