Branch data Line data Source code
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_DecodeEscape(const char *, Py_ssize_t, 29 : : const char *, const char **); 30 : : 31 : : /* Macros and static inline functions, trading safety for speed */ 32 : : #define _PyBytes_CAST(op) \ 33 : : (assert(PyBytes_Check(op)), _Py_CAST(PyBytesObject*, op)) 34 : : 35 : 1849987 : static inline char* PyBytes_AS_STRING(PyObject *op) 36 : : { 37 [ # # ]: 1849987 : return _PyBytes_CAST(op)->ob_sval; 38 : : } 39 : : #define PyBytes_AS_STRING(op) PyBytes_AS_STRING(_PyObject_CAST(op)) 40 : : 41 : 1819818 : static inline Py_ssize_t PyBytes_GET_SIZE(PyObject *op) { 42 [ # # ]: 1819818 : PyBytesObject *self = _PyBytes_CAST(op); 43 : 1819818 : return Py_SIZE(self); 44 : : } 45 : : #define PyBytes_GET_SIZE(self) PyBytes_GET_SIZE(_PyObject_CAST(self)) 46 : : 47 : : /* _PyBytes_Join(sep, x) is like sep.join(x). sep must be PyBytesObject*, 48 : : x must be an iterable object. */ 49 : : 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);