Skip to content

Commit 4de5ccb

Browse files
committed
gh-129813, PEP 782: Use PyBytesWriter in _PyBytes_DecodeEscape2()
Replace the private _PyBytesWriter API with the new public PyBytesWriter API.
1 parent c3fca5d commit 4de5ccb

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

Objects/bytesobject.c

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,22 +1090,16 @@ PyObject *_PyBytes_DecodeEscape2(const char *s,
10901090
int *first_invalid_escape_char,
10911091
const char **first_invalid_escape_ptr)
10921092
{
1093-
int c;
1094-
char *p;
1095-
const char *end;
1096-
_PyBytesWriter writer;
1097-
1098-
_PyBytesWriter_Init(&writer);
1099-
1100-
p = _PyBytesWriter_Alloc(&writer, len);
1101-
if (p == NULL)
1093+
PyBytesWriter *writer = PyBytesWriter_Create(len);
1094+
if (writer == NULL) {
11021095
return NULL;
1103-
writer.overallocate = 1;
1096+
}
1097+
char *p = PyBytesWriter_GetData(writer);
11041098

11051099
*first_invalid_escape_char = -1;
11061100
*first_invalid_escape_ptr = NULL;
11071101

1108-
end = s + len;
1102+
const char *end = s + len;
11091103
while (s < end) {
11101104
if (*s != '\\') {
11111105
*p++ = *s++;
@@ -1134,7 +1128,8 @@ PyObject *_PyBytes_DecodeEscape2(const char *s,
11341128
case 'a': *p++ = '\007'; break; /* BEL, not classic C */
11351129
case '0': case '1': case '2': case '3':
11361130
case '4': case '5': case '6': case '7':
1137-
c = s[-1] - '0';
1131+
{
1132+
int c = s[-1] - '0';
11381133
if (s < end && '0' <= *s && *s <= '7') {
11391134
c = (c<<3) + *s++ - '0';
11401135
if (s < end && '0' <= *s && *s <= '7')
@@ -1149,6 +1144,7 @@ PyObject *_PyBytes_DecodeEscape2(const char *s,
11491144
}
11501145
*p++ = c;
11511146
break;
1147+
}
11521148
case 'x':
11531149
if (s+1 < end) {
11541150
int digit1, digit2;
@@ -1195,10 +1191,10 @@ PyObject *_PyBytes_DecodeEscape2(const char *s,
11951191
}
11961192
}
11971193

1198-
return _PyBytesWriter_Finish(&writer, p);
1194+
return PyBytesWriter_FinishWithPointer(writer, p);
11991195

12001196
failed:
1201-
_PyBytesWriter_Dealloc(&writer);
1197+
PyBytesWriter_Discard(writer);
12021198
return NULL;
12031199
}
12041200

0 commit comments

Comments
 (0)