FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-155742: Use PyBytesWriter in json _match_number_unicode() by vstinner · Pull Request #155744 · python/cpython · GitHub

Repository navigation

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
55 changes: 40 additions & 15 deletions Modules/_json.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,6 @@ _match_number_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t start, Py_
Py_ssize_t idx = start;
int is_float = 0;
PyObject *rval;
PyObject *numstr = NULL;
PyObject *custom_func;

str = PyUnicode_DATA(pystr);
Expand Down Expand Up @@ -1064,32 +1063,58 @@ _match_number_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t start, Py_

if (custom_func) {
/* copy the section we determined to be a number */
numstr = PyUnicode_FromKindAndData(kind,
(char*)str + kind * start,
idx - start);
if (numstr == NULL)
PyObject *numstr = PyUnicode_FromKindAndData(kind,
(char*)str + kind * start,
idx - start);
if (numstr == NULL) {
return NULL;
}
rval = PyObject_CallOneArg(custom_func, numstr);
Py_DECREF(numstr);
}
else {
Py_ssize_t i, n;
char *buf;
/* Straight conversion to ASCII, to avoid costly conversion of
decimal unicode digits (which cannot appear here) */
n = idx - start;
numstr = PyBytes_FromStringAndSize(NULL, n);
if (numstr == NULL)
return NULL;
buf = PyBytes_AS_STRING(numstr);
PyBytesWriter *writer;
char *buf;
if (is_float) {
writer = PyBytesWriter_Create(n);
if (writer == NULL) {
return NULL;
}
buf = PyBytesWriter_GetData(writer);
}
else {
writer = NULL;
buf = PyMem_Malloc(n + 1);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

PyLong_FromString() doesn't need a bytes object, just a C string (char*). But maybe the PyLong code path should also use PyBytesWriter to simplify the code.

if (buf == NULL) {
PyErr_NoMemory();
return NULL;
}
}
for (i = 0; i < n; i++) {
buf[i] = (char) PyUnicode_READ(kind, str, i + start);
Py_UCS4 ch = PyUnicode_READ(kind, str, i + start);
assert(ch <= 127);
buf[i] = (char)ch;
}
if (is_float)
rval = PyFloat_FromString(numstr);
else
if (is_float) {
PyObject *numstr = PyBytesWriter_Finish(writer);
if (numstr != NULL) {
rval = PyFloat_FromString(numstr);
Py_DECREF(numstr);
}
else {
rval = NULL;
}
}
else {
buf[n] = '\0';
rval = PyLong_FromString(buf, NULL, 10);
PyMem_Free(buf);
}
}
Py_DECREF(numstr);
*next_idx_ptr = idx;
return rval;
}
Expand Down
Loading

Back | FazBrowse Home | New Git URL