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

gh-129813, PEP 782: Use PyBytesWriter in socket and mmap by vstinner · Pull Request #138831 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (2) 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
14 changes: 8 additions & 6 deletions Modules/mmapmodule.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 @@ -448,7 +448,8 @@ _safe_PyBytes_ReverseFind(Py_ssize_t *out, mmap_object *self,
}

PyObject *
_safe_PyBytes_FromStringAndSize(char *start, size_t num_bytes) {
_safe_PyBytes_FromStringAndSize(char *start, size_t num_bytes)
{
if (num_bytes == 1) {
char dest;
if (safe_byte_copy(&dest, start) < 0) {
Expand All @@ -459,14 +460,15 @@ _safe_PyBytes_FromStringAndSize(char *start, size_t num_bytes) {
}
}
else {
PyObject *result = PyBytes_FromStringAndSize(NULL, num_bytes);
if (result == NULL) {
PyBytesWriter *writer = PyBytesWriter_Create(num_bytes);
if (writer == NULL) {
return NULL;
}
if (safe_memcpy(PyBytes_AS_STRING(result), start, num_bytes) < 0) {
Py_CLEAR(result);
if (safe_memcpy(PyBytesWriter_GetData(writer), start, num_bytes) < 0) {
PyBytesWriter_Discard(writer);
return NULL;
}
return result;
return PyBytesWriter_Finish(writer);
}
}

Expand Down
13 changes: 6 additions & 7 deletions Modules/socketmodule.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 @@ -3467,7 +3467,6 @@ sock_getsockopt(PyObject *self, PyObject *args)
int level;
int optname;
int res;
PyObject *buf;
socklen_t buflen = 0;
int flag = 0;
socklen_t flagsize;
Expand Down Expand Up @@ -3512,17 +3511,17 @@ sock_getsockopt(PyObject *self, PyObject *args)
"getsockopt buflen out of range");
return NULL;
}
buf = PyBytes_FromStringAndSize((char *)NULL, buflen);
if (buf == NULL)
PyBytesWriter *writer = PyBytesWriter_Create(buflen);
if (writer == NULL) {
return NULL;
}
res = getsockopt(get_sock_fd(s), level, optname,
(void *)PyBytes_AS_STRING(buf), &buflen);
PyBytesWriter_GetData(writer), &buflen);
if (res < 0) {
Py_DECREF(buf);
PyBytesWriter_Discard(writer);
return s->errorhandler();
}
_PyBytes_Resize(&buf, buflen);
return buf;
return PyBytesWriter_FinishWithSize(writer, buflen);
}

PyDoc_STRVAR(getsockopt_doc,
Expand Down
Loading

Back | FazBrowse Home | New Git URL