gh-87804: Fix error handling and style in _pystatvfs_fromstructstatfs (#115236)

This commit is contained in:
Nikita Sobolev 2024-02-12 10:27:12 +03:00 committed by GitHub
parent e1552fd19d
commit 54bde5dcc3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -12894,7 +12894,7 @@ os_WSTOPSIG_impl(PyObject *module, int status)
#ifdef __APPLE__ #ifdef __APPLE__
/* On macOS struct statvfs uses 32-bit integers for block counts, /* On macOS struct statvfs uses 32-bit integers for block counts,
* resulting in overflow when filesystems are larger tan 4TB. Therefore * resulting in overflow when filesystems are larger than 4TB. Therefore
* os.statvfs is implemented in terms of statfs(2). * os.statvfs is implemented in terms of statfs(2).
*/ */
@ -12902,8 +12902,9 @@ static PyObject*
_pystatvfs_fromstructstatfs(PyObject *module, struct statfs st) { _pystatvfs_fromstructstatfs(PyObject *module, struct statfs st) {
PyObject *StatVFSResultType = get_posix_state(module)->StatVFSResultType; PyObject *StatVFSResultType = get_posix_state(module)->StatVFSResultType;
PyObject *v = PyStructSequence_New((PyTypeObject *)StatVFSResultType); PyObject *v = PyStructSequence_New((PyTypeObject *)StatVFSResultType);
if (v == NULL) if (v == NULL) {
return NULL; return NULL;
}
long flags = 0; long flags = 0;
if (st.f_flags & MNT_RDONLY) { if (st.f_flags & MNT_RDONLY) {
@ -12915,28 +12916,29 @@ _pystatvfs_fromstructstatfs(PyObject *module, struct statfs st) {
_Static_assert(sizeof(st.f_blocks) == sizeof(long long), "assuming large file"); _Static_assert(sizeof(st.f_blocks) == sizeof(long long), "assuming large file");
PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_iosize)); #define SET_ITEM(v, index, item) \
PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_bsize)); do { \
PyStructSequence_SET_ITEM(v, 2, if (item == NULL) { \
PyLong_FromLongLong((long long) st.f_blocks)); Py_DECREF(v); \
PyStructSequence_SET_ITEM(v, 3, return NULL; \
PyLong_FromLongLong((long long) st.f_bfree)); } \
PyStructSequence_SET_ITEM(v, 4, PyStructSequence_SET_ITEM(v, index, item); \
PyLong_FromLongLong((long long) st.f_bavail)); } while (0) \
PyStructSequence_SET_ITEM(v, 5,
PyLong_FromLongLong((long long) st.f_files));
PyStructSequence_SET_ITEM(v, 6,
PyLong_FromLongLong((long long) st.f_ffree));
PyStructSequence_SET_ITEM(v, 7,
PyLong_FromLongLong((long long) st.f_ffree));
PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) flags));
PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) NAME_MAX)); SET_ITEM(v, 0, PyLong_FromLong((long) st.f_iosize));
PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid.val[0])); SET_ITEM(v, 1, PyLong_FromLong((long) st.f_bsize));
if (PyErr_Occurred()) { SET_ITEM(v, 2, PyLong_FromLongLong((long long) st.f_blocks));
Py_DECREF(v); SET_ITEM(v, 3, PyLong_FromLongLong((long long) st.f_bfree));
return NULL; SET_ITEM(v, 4, PyLong_FromLongLong((long long) st.f_bavail));
} SET_ITEM(v, 5, PyLong_FromLongLong((long long) st.f_files));
SET_ITEM(v, 6, PyLong_FromLongLong((long long) st.f_ffree));
SET_ITEM(v, 7, PyLong_FromLongLong((long long) st.f_ffree));
SET_ITEM(v, 8, PyLong_FromLong((long) flags));
SET_ITEM(v, 9, PyLong_FromLong((long) NAME_MAX));
SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid.val[0]));
#undef SET_ITEM
return v; return v;
} }