@@ -4897,33 +4897,27 @@ _PyUnicode_EncodeUTF7(PyObject *str,
4897
4897
int base64WhiteSpace ,
4898
4898
const char * errors )
4899
4899
{
4900
- int kind ;
4901
- const void * data ;
4902
- Py_ssize_t len ;
4903
- PyObject * v ;
4904
- int inShift = 0 ;
4905
- Py_ssize_t i ;
4906
- unsigned int base64bits = 0 ;
4907
- unsigned long base64buffer = 0 ;
4908
- char * out ;
4909
- const char * start ;
4910
-
4911
- kind = PyUnicode_KIND (str );
4912
- data = PyUnicode_DATA (str );
4913
- len = PyUnicode_GET_LENGTH (str );
4914
-
4915
- if (len == 0 )
4900
+ Py_ssize_t len = PyUnicode_GET_LENGTH (str );
4901
+ if (len == 0 ) {
4916
4902
return Py_GetConstant (Py_CONSTANT_EMPTY_BYTES );
4903
+ }
4904
+ int kind = PyUnicode_KIND (str );
4905
+ const void * data = PyUnicode_DATA (str );
4917
4906
4918
4907
/* It might be possible to tighten this worst case */
4919
- if (len > PY_SSIZE_T_MAX / 8 )
4908
+ if (len > PY_SSIZE_T_MAX / 8 ) {
4920
4909
return PyErr_NoMemory ();
4921
- v = PyBytes_FromStringAndSize (NULL , len * 8 );
4922
- if (v == NULL )
4910
+ }
4911
+ PyBytesWriter * writer = PyBytesWriter_Create (len * 8 );
4912
+ if (writer == NULL ) {
4923
4913
return NULL ;
4914
+ }
4924
4915
4925
- start = out = PyBytes_AS_STRING (v );
4926
- for (i = 0 ; i < len ; ++ i ) {
4916
+ int inShift = 0 ;
4917
+ unsigned int base64bits = 0 ;
4918
+ unsigned long base64buffer = 0 ;
4919
+ char * out = PyBytesWriter_GetData (writer );
4920
+ for (Py_ssize_t i = 0 ; i < len ; ++ i ) {
4927
4921
Py_UCS4 ch = PyUnicode_READ (kind , data , i );
4928
4922
4929
4923
if (inShift ) {
@@ -4986,9 +4980,7 @@ _PyUnicode_EncodeUTF7(PyObject *str,
4986
4980
* out ++ = TO_BASE64 (base64buffer << (6 - base64bits ) );
4987
4981
if (inShift )
4988
4982
* out ++ = '-' ;
4989
- if (_PyBytes_Resize (& v , out - start ) < 0 )
4990
- return NULL ;
4991
- return v ;
4983
+ return PyBytesWriter_FinishWithPointer (writer , out );
4992
4984
}
4993
4985
4994
4986
#undef IS_BASE64
0 commit comments