@@ -36,6 +36,9 @@ class bytes "PyBytesObject *" "&PyBytes_Type"
36
36
/* Forward declaration */
37
37
Py_LOCAL_INLINE (Py_ssize_t ) _PyBytesWriter_GetSize (_PyBytesWriter * writer ,
38
38
char * str );
39
+ static void * _PyBytesWriter_ResizeAndUpdatePointer (PyBytesWriter * writer ,
40
+ Py_ssize_t size , void * data );
41
+ static Py_ssize_t _PyBytesWriter_GetAllocated (PyBytesWriter * writer );
39
42
40
43
41
44
#define CHARACTERS _Py_SINGLETON(bytes_characters)
@@ -2531,17 +2534,13 @@ bytes_fromhex_impl(PyTypeObject *type, PyObject *string)
2531
2534
PyObject *
2532
2535
_PyBytes_FromHex (PyObject * string , int use_bytearray )
2533
2536
{
2534
- char * buf ;
2535
2537
Py_ssize_t hexlen , invalid_char ;
2536
2538
unsigned int top , bot ;
2537
2539
const Py_UCS1 * str , * start , * end ;
2538
- _PyBytesWriter writer ;
2540
+ PyBytesWriter * writer = NULL ;
2539
2541
Py_buffer view ;
2540
2542
view .obj = NULL ;
2541
2543
2542
- _PyBytesWriter_Init (& writer );
2543
- writer .use_bytearray = use_bytearray ;
2544
-
2545
2544
if (PyUnicode_Check (string )) {
2546
2545
hexlen = PyUnicode_GET_LENGTH (string );
2547
2546
@@ -2577,10 +2576,16 @@ _PyBytes_FromHex(PyObject *string, int use_bytearray)
2577
2576
}
2578
2577
2579
2578
/* This overestimates if there are spaces */
2580
- buf = _PyBytesWriter_Alloc (& writer , hexlen / 2 );
2581
- if (buf == NULL ) {
2579
+ if (use_bytearray ) {
2580
+ writer = _PyBytesWriter_CreateByteArray (hexlen / 2 );
2581
+ }
2582
+ else {
2583
+ writer = PyBytesWriter_Create (hexlen / 2 );
2584
+ }
2585
+ if (writer == NULL ) {
2582
2586
goto release_buffer ;
2583
2587
}
2588
+ char * buf = PyBytesWriter_GetData (writer );
2584
2589
2585
2590
start = str ;
2586
2591
end = str + hexlen ;
@@ -2619,7 +2624,7 @@ _PyBytes_FromHex(PyObject *string, int use_bytearray)
2619
2624
if (view .obj != NULL ) {
2620
2625
PyBuffer_Release (& view );
2621
2626
}
2622
- return _PyBytesWriter_Finish ( & writer , buf );
2627
+ return PyBytesWriter_FinishWithPointer ( writer , buf );
2623
2628
2624
2629
error :
2625
2630
if (invalid_char == -1 ) {
@@ -2630,7 +2635,7 @@ _PyBytes_FromHex(PyObject *string, int use_bytearray)
2630
2635
"non-hexadecimal number found in "
2631
2636
"fromhex() arg at position %zd" , invalid_char );
2632
2637
}
2633
- _PyBytesWriter_Dealloc ( & writer );
2638
+ PyBytesWriter_Discard ( writer );
2634
2639
2635
2640
release_buffer :
2636
2641
if (view .obj != NULL ) {
@@ -2857,47 +2862,44 @@ bytes_new_impl(PyTypeObject *type, PyObject *x, const char *encoding,
2857
2862
static PyObject *
2858
2863
_PyBytes_FromBuffer (PyObject * x )
2859
2864
{
2860
- PyObject * new ;
2861
2865
Py_buffer view ;
2862
-
2863
2866
if (PyObject_GetBuffer (x , & view , PyBUF_FULL_RO ) < 0 )
2864
2867
return NULL ;
2865
2868
2866
- new = PyBytes_FromStringAndSize ( NULL , view .len );
2867
- if (! new )
2869
+ PyBytesWriter * writer = PyBytesWriter_Create ( view .len );
2870
+ if (writer == NULL ) {
2868
2871
goto fail ;
2869
- if (PyBuffer_ToContiguous (((PyBytesObject * )new )-> ob_sval ,
2870
- & view , view .len , 'C' ) < 0 )
2872
+ }
2873
+
2874
+ if (PyBuffer_ToContiguous (PyBytesWriter_GetData (writer ),
2875
+ & view , view .len , 'C' ) < 0 ) {
2871
2876
goto fail ;
2877
+ }
2878
+
2872
2879
PyBuffer_Release (& view );
2873
- return new ;
2880
+ return PyBytesWriter_Finish ( writer ) ;
2874
2881
2875
2882
fail :
2876
- Py_XDECREF ( new );
2883
+ PyBytesWriter_Discard ( writer );
2877
2884
PyBuffer_Release (& view );
2878
2885
return NULL ;
2879
2886
}
2880
2887
2881
2888
static PyObject *
2882
2889
_PyBytes_FromList (PyObject * x )
2883
2890
{
2884
- Py_ssize_t i , size = PyList_GET_SIZE (x );
2885
- Py_ssize_t value ;
2886
- char * str ;
2887
- PyObject * item ;
2888
- _PyBytesWriter writer ;
2889
-
2890
- _PyBytesWriter_Init (& writer );
2891
- str = _PyBytesWriter_Alloc (& writer , size );
2892
- if (str == NULL )
2891
+ Py_ssize_t size = PyList_GET_SIZE (x );
2892
+ PyBytesWriter * writer = PyBytesWriter_Create (size );
2893
+ if (writer == NULL ) {
2893
2894
return NULL ;
2894
- writer .overallocate = 1 ;
2895
- size = writer .allocated ;
2895
+ }
2896
+ char * str = PyBytesWriter_GetData (writer );
2897
+ size = _PyBytesWriter_GetAllocated (writer );
2896
2898
2897
- for (i = 0 ; i < PyList_GET_SIZE (x ); i ++ ) {
2898
- item = PyList_GET_ITEM (x , i );
2899
+ for (Py_ssize_t i = 0 ; i < PyList_GET_SIZE (x ); i ++ ) {
2900
+ PyObject * item = PyList_GET_ITEM (x , i );
2899
2901
Py_INCREF (item );
2900
- value = PyNumber_AsSsize_t (item , NULL );
2902
+ Py_ssize_t value = PyNumber_AsSsize_t (item , NULL );
2901
2903
Py_DECREF (item );
2902
2904
if (value == -1 && PyErr_Occurred ())
2903
2905
goto error ;
@@ -2909,33 +2911,33 @@ _PyBytes_FromList(PyObject *x)
2909
2911
}
2910
2912
2911
2913
if (i >= size ) {
2912
- str = _PyBytesWriter_Resize (& writer , str , size + 1 );
2913
- if (str == NULL )
2914
- return NULL ;
2915
- size = writer .allocated ;
2914
+ str = _PyBytesWriter_ResizeAndUpdatePointer (writer , size + 1 , str );
2915
+ if (str == NULL ) {
2916
+ goto error ;
2917
+ }
2918
+ size = _PyBytesWriter_GetAllocated (writer );
2916
2919
}
2917
2920
* str ++ = (char ) value ;
2918
2921
}
2919
- return _PyBytesWriter_Finish ( & writer , str );
2922
+ return PyBytesWriter_FinishWithPointer ( writer , str );
2920
2923
2921
- error :
2922
- _PyBytesWriter_Dealloc ( & writer );
2924
+ error :
2925
+ PyBytesWriter_Discard ( writer );
2923
2926
return NULL ;
2924
2927
}
2925
2928
2926
2929
static PyObject *
2927
2930
_PyBytes_FromTuple (PyObject * x )
2928
2931
{
2929
- PyObject * bytes ;
2930
2932
Py_ssize_t i , size = PyTuple_GET_SIZE (x );
2931
2933
Py_ssize_t value ;
2932
- char * str ;
2933
2934
PyObject * item ;
2934
2935
2935
- bytes = PyBytes_FromStringAndSize ( NULL , size );
2936
- if (bytes == NULL )
2936
+ PyBytesWriter * writer = PyBytesWriter_Create ( size );
2937
+ if (writer == NULL ) {
2937
2938
return NULL ;
2938
- str = ((PyBytesObject * )bytes )-> ob_sval ;
2939
+ }
2940
+ char * str = PyBytesWriter_GetData (writer );
2939
2941
2940
2942
for (i = 0 ; i < size ; i ++ ) {
2941
2943
item = PyTuple_GET_ITEM (x , i );
@@ -2950,31 +2952,29 @@ _PyBytes_FromTuple(PyObject *x)
2950
2952
}
2951
2953
* str ++ = (char ) value ;
2952
2954
}
2953
- return bytes ;
2955
+ return PyBytesWriter_Finish ( writer ) ;
2954
2956
2955
2957
error :
2956
- Py_DECREF ( bytes );
2958
+ PyBytesWriter_Discard ( writer );
2957
2959
return NULL ;
2958
2960
}
2959
2961
2960
2962
static PyObject *
2961
2963
_PyBytes_FromIterator (PyObject * it , PyObject * x )
2962
2964
{
2963
- char * str ;
2964
2965
Py_ssize_t i , size ;
2965
- _PyBytesWriter writer ;
2966
2966
2967
2967
/* For iterator version, create a bytes object and resize as needed */
2968
2968
size = PyObject_LengthHint (x , 64 );
2969
2969
if (size == -1 && PyErr_Occurred ())
2970
2970
return NULL ;
2971
2971
2972
- _PyBytesWriter_Init (& writer );
2973
- str = _PyBytesWriter_Alloc (& writer , size );
2974
- if (str == NULL )
2972
+ PyBytesWriter * writer = PyBytesWriter_Create (size );
2973
+ if (writer == NULL ) {
2975
2974
return NULL ;
2976
- writer .overallocate = 1 ;
2977
- size = writer .allocated ;
2975
+ }
2976
+ char * str = PyBytesWriter_GetData (writer );
2977
+ size = _PyBytesWriter_GetAllocated (writer );
2978
2978
2979
2979
/* Run the iterator to exhaustion */
2980
2980
for (i = 0 ; ; i ++ ) {
@@ -3004,18 +3004,18 @@ _PyBytes_FromIterator(PyObject *it, PyObject *x)
3004
3004
3005
3005
/* Append the byte */
3006
3006
if (i >= size ) {
3007
- str = _PyBytesWriter_Resize (& writer , str , size + 1 );
3008
- if (str == NULL )
3009
- return NULL ;
3010
- size = writer .allocated ;
3007
+ str = _PyBytesWriter_ResizeAndUpdatePointer (writer , size + 1 , str );
3008
+ if (str == NULL ) {
3009
+ goto error ;
3010
+ }
3011
+ size = _PyBytesWriter_GetAllocated (writer );
3011
3012
}
3012
3013
* str ++ = (char ) value ;
3013
3014
}
3014
-
3015
- return _PyBytesWriter_Finish (& writer , str );
3015
+ return PyBytesWriter_FinishWithPointer (writer , str );
3016
3016
3017
3017
error :
3018
- _PyBytesWriter_Dealloc ( & writer );
3018
+ PyBytesWriter_Discard ( writer );
3019
3019
return NULL ;
3020
3020
}
3021
3021
@@ -3983,6 +3983,13 @@ PyBytesWriter_GetSize(PyBytesWriter *writer)
3983
3983
}
3984
3984
3985
3985
3986
+ static Py_ssize_t
3987
+ _PyBytesWriter_GetAllocated (PyBytesWriter * writer )
3988
+ {
3989
+ return byteswriter_allocated (writer );
3990
+ }
3991
+
3992
+
3986
3993
int
3987
3994
PyBytesWriter_Resize (PyBytesWriter * writer , Py_ssize_t size )
3988
3995
{
@@ -3998,6 +4005,18 @@ PyBytesWriter_Resize(PyBytesWriter *writer, Py_ssize_t size)
3998
4005
}
3999
4006
4000
4007
4008
+ static void *
4009
+ _PyBytesWriter_ResizeAndUpdatePointer (PyBytesWriter * writer , Py_ssize_t size ,
4010
+ void * data )
4011
+ {
4012
+ Py_ssize_t pos = (char * )data - byteswriter_data (writer );
4013
+ if (PyBytesWriter_Resize (writer , size ) < 0 ) {
4014
+ return NULL ;
4015
+ }
4016
+ return byteswriter_data (writer ) + pos ;
4017
+ }
4018
+
4019
+
4001
4020
int
4002
4021
PyBytesWriter_Grow (PyBytesWriter * writer , Py_ssize_t size )
4003
4022
{
0 commit comments