@@ -8,12 +8,9 @@ static PyObject *_Py_strhex_impl(const char* argbuf, const Py_ssize_t arglen,
8
8
const PyObject * sep , int bytes_per_sep_group ,
9
9
const int return_bytes )
10
10
{
11
- PyObject * retval ;
12
- Py_UCS1 * retbuf ;
13
- Py_ssize_t i , j , resultlen = 0 ;
14
- Py_UCS1 sep_char = 0 ;
15
- unsigned int abs_bytes_per_sep ;
11
+ assert (arglen >= 0 );
16
12
13
+ Py_UCS1 sep_char = 0 ;
17
14
if (sep ) {
18
15
Py_ssize_t seplen = PyObject_Length ((PyObject * )sep );
19
16
if (seplen < 0 ) {
@@ -31,22 +28,25 @@ static PyObject *_Py_strhex_impl(const char* argbuf, const Py_ssize_t arglen,
31
28
return NULL ;
32
29
}
33
30
sep_char = PyUnicode_READ_CHAR (sep , 0 );
34
- } else if (PyBytes_Check (sep )) {
31
+ }
32
+ else if (PyBytes_Check (sep )) {
35
33
sep_char = PyBytes_AS_STRING (sep )[0 ];
36
- } else {
34
+ }
35
+ else {
37
36
PyErr_SetString (PyExc_TypeError , "sep must be str or bytes." );
38
37
return NULL ;
39
38
}
40
39
if (sep_char > 127 && !return_bytes ) {
41
40
PyErr_SetString (PyExc_ValueError , "sep must be ASCII." );
42
41
return NULL ;
43
42
}
44
- } else {
43
+ }
44
+ else {
45
45
bytes_per_sep_group = 0 ;
46
46
}
47
47
48
- assert ( arglen >= 0 );
49
- abs_bytes_per_sep = abs ( bytes_per_sep_group ) ;
48
+ unsigned int abs_bytes_per_sep = abs ( bytes_per_sep_group );
49
+ Py_ssize_t resultlen = 0 ;
50
50
if (bytes_per_sep_group && arglen > 0 ) {
51
51
/* How many sep characters we'll be inserting. */
52
52
resultlen = (arglen - 1 ) / abs_bytes_per_sep ;
@@ -62,26 +62,32 @@ static PyObject *_Py_strhex_impl(const char* argbuf, const Py_ssize_t arglen,
62
62
abs_bytes_per_sep = 0 ;
63
63
}
64
64
65
+ PyObject * retval ;
66
+ Py_UCS1 * retbuf ;
65
67
if (return_bytes ) {
66
68
/* If _PyBytes_FromSize() were public we could avoid malloc+copy. */
67
- retbuf = (Py_UCS1 * ) PyMem_Malloc (resultlen );
68
- if (!retbuf )
69
- return PyErr_NoMemory ();
70
- retval = NULL ; /* silence a compiler warning, assigned later. */
71
- } else {
69
+ retval = PyBytes_FromStringAndSize (NULL , resultlen );
70
+ if (!retval ) {
71
+ return NULL ;
72
+ }
73
+ retbuf = (Py_UCS1 * )PyBytes_AS_STRING (retval );
74
+ }
75
+ else {
72
76
retval = PyUnicode_New (resultlen , 127 );
73
- if (!retval )
77
+ if (!retval ) {
74
78
return NULL ;
79
+ }
75
80
retbuf = PyUnicode_1BYTE_DATA (retval );
76
81
}
77
82
78
83
/* Hexlify */
84
+ Py_ssize_t i , j ;
79
85
for (i = j = 0 ; i < arglen ; ++ i ) {
80
- assert (j < resultlen );
86
+ assert (( j + 1 ) < resultlen );
81
87
unsigned char c ;
82
- c = (argbuf [i ] >> 4 ) & 0xf ;
88
+ c = (argbuf [i ] >> 4 ) & 0x0f ;
83
89
retbuf [j ++ ] = Py_hexdigits [c ];
84
- c = argbuf [i ] & 0xf ;
90
+ c = argbuf [i ] & 0x0f ;
85
91
retbuf [j ++ ] = Py_hexdigits [c ];
86
92
if (bytes_per_sep_group && i < arglen - 1 ) {
87
93
Py_ssize_t anchor ;
@@ -93,12 +99,8 @@ static PyObject *_Py_strhex_impl(const char* argbuf, const Py_ssize_t arglen,
93
99
}
94
100
assert (j == resultlen );
95
101
96
- if (return_bytes ) {
97
- retval = PyBytes_FromStringAndSize ((const char * )retbuf , resultlen );
98
- PyMem_Free (retbuf );
99
- }
100
102
#ifdef Py_DEBUG
101
- else {
103
+ if (! return_bytes ) {
102
104
assert (_PyUnicode_CheckConsistency (retval , 1 ));
103
105
}
104
106
#endif
0 commit comments