Skip to content

Commit 190ca24

Browse files
committed
use PyBytes in json:_match_number_unicode
1 parent 4e00e25 commit 190ca24

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

Modules/_json.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,17 +1092,22 @@ _match_number_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t start, Py_
10921092
/* Straight conversion to ASCII, to avoid costly conversion of
10931093
decimal unicode digits (which cannot appear here) */
10941094
n = idx - start;
1095-
numstr = PyBytes_FromStringAndSize(NULL, n);
1096-
if (numstr == NULL)
1095+
PyBytesWriter *writer = PyBytesWriter_Create(n);
1096+
if (writer == NULL) {
10971097
return NULL;
1098-
buf = PyBytes_AS_STRING(numstr);
1098+
}
1099+
buf = PyBytesWriter_GetData(writer);
10991100
for (i = 0; i < n; i++) {
11001101
buf[i] = (char) PyUnicode_READ(kind, str, i + start);
11011102
}
1103+
numstr = PyBytesWriter_Finish(writer);
1104+
if (numstr == NULL) {
1105+
return NULL;
1106+
}
11021107
if (is_float)
11031108
rval = PyFloat_FromString(numstr);
11041109
else
1105-
rval = PyLong_FromString(buf, NULL, 10);
1110+
rval = PyLong_FromString(PyBytes_AS_STRING(numstr), NULL, 10);
11061111
}
11071112
Py_DECREF(numstr);
11081113
*next_idx_ptr = idx;

0 commit comments

Comments
 (0)