Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
gh-83714: Set os.statx() members to None if missing from stx_mask#140216
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes from1 commit
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -3319,6 +3319,8 @@ typedef struct { | ||
| struct statx stx; | ||
| } Py_statx_result; | ||
| #define Py_statx_result_CAST(op) _Py_CAST(Py_statx_result*, (op)) | ||
| #define M(attr, type, offset, doc) \ | ||
| {attr, type, offset, Py_READONLY, PyDoc_STR(doc)} | ||
| #define MM(attr, type, member, doc) \ | ||
| @@ -3330,85 +3332,166 @@ static PyMemberDef pystatx_result_members[] = { | ||
| MM(stx_mask, Py_T_UINT, mask, "member validity mask"), | ||
| MM(st_blksize, Py_T_UINT, blksize, "blocksize for filesystem I/O"), | ||
| MM(stx_attributes, Py_T_ULONGLONG, attributes, "Linux inode attribute bits"), | ||
| MM(st_mode, Py_T_USHORT, mode, "protection bits"), | ||
| MM(stx_attributes_mask, Py_T_ULONGLONG, attributes_mask, | ||
| "Mask of supported bits in stx_attributes"), | ||
| MM(stx_rdev_major, Py_T_UINT, rdev_major, "represented device major number"), | ||
| MM(stx_rdev_minor, Py_T_UINT, rdev_minor, "represented device minor number"), | ||
| MX(st_rdev, Py_T_ULONGLONG, rdev, "device type (if inode device)"), | ||
| MM(stx_dev_major, Py_T_UINT, dev_major, "containing device major number"), | ||
| MM(stx_dev_minor, Py_T_UINT, dev_minor, "containing device minor number"), | ||
| MX(st_dev, Py_T_ULONGLONG, dev, "device"), | ||
| {NULL}, | ||
| }; | ||
| #undef MX | ||
| #undef MM | ||
| #undef M | ||
| #define STATX_GET_UINT(ATTR, MEMBER, MASK) \ | ||
| static PyObject* \ | ||
| pystatx_result_get_##ATTR(PyObject *op, void *Py_UNUSED(context)) \ | ||
| { \ | ||
| Py_statx_result *self = Py_statx_result_CAST(op); \ | ||
| if (!(self->stx.stx_mask & MASK)) { \ | ||
| Py_RETURN_NONE; \ | ||
| } \ | ||
| unsigned long value = self->stx.MEMBER; \ | ||
| return PyLong_FromUnsignedLong(value); \ | ||
| } | ||
| STATX_GET_UINT(st_uid, stx_uid, STATX_UID) | ||
| STATX_GET_UINT(st_gid, stx_gid, STATX_GID) | ||
| STATX_GET_UINT(st_nlink, stx_nlink, STATX_NLINK) | ||
| #ifdef STATX_DIOALIGN | ||
| STATX_GET_UINT(stx_dio_mem_align, stx_dio_mem_align, STATX_DIOALIGN) | ||
| STATX_GET_UINT(stx_dio_offset_align, stx_dio_offset_align, STATX_DIOALIGN) | ||
| #endif | ||
| #ifdef STATX_DIO_READ_ALIGN | ||
| STATX_GET_UINT(stx_dio_read_offset_align, stx_dio_read_offset_align, | ||
| STATX_DIO_READ_ALIGN) | ||
| #endif | ||
| #ifdef STATX_WRITE_ATOMIC | ||
| STATX_GET_UINT(stx_atomic_write_unit_min,stx_atomic_write_unit_min, | ||
| STATX_WRITE_ATOMIC) | ||
| STATX_GET_UINT(stx_atomic_write_unit_max,stx_atomic_write_unit_max, | ||
| STATX_WRITE_ATOMIC) | ||
| STATX_GET_UINT(stx_atomic_write_segments_max,stx_atomic_write_segments_max, | ||
| STATX_WRITE_ATOMIC) | ||
| #endif | ||
| #ifdef HAVE_STRUCT_STATX_STX_ATOMIC_WRITE_UNIT_MAX_OPT | ||
| STATX_GET_UINT(stx_atomic_write_unit_max_opt,stx_atomic_write_unit_max_opt, | ||
| STATX_WRITE_ATOMIC) | ||
| #endif | ||
| #define STATX_GET_ULONGLONG(ATTR, MEMBER, MASK) \ | ||
| static PyObject* \ | ||
| pystatx_result_get_##ATTR(PyObject *op, void *Py_UNUSED(context)) \ | ||
| { \ | ||
| Py_statx_result *self = Py_statx_result_CAST(op); \ | ||
| if (!(self->stx.stx_mask & MASK)) { \ | ||
| Py_RETURN_NONE; \ | ||
| } \ | ||
| unsigned long long value = self->stx.MEMBER; \ | ||
| return PyLong_FromUnsignedLongLong(value); \ | ||
| } | ||
| STATX_GET_ULONGLONG(st_blocks, stx_blocks, STATX_BLOCKS) | ||
| STATX_GET_ULONGLONG(st_ino, stx_ino, STATX_INO) | ||
| STATX_GET_ULONGLONG(st_size, stx_size, STATX_SIZE) | ||
| #ifdef STATX_MNT_ID | ||
| STATX_GET_ULONGLONG(stx_mnt_id, stx_mnt_id, STATX_MNT_ID) | ||
| #endif | ||
| #ifdef STATX_SUBVOL | ||
| STATX_GET_ULONGLONG(stx_subvol, stx_subvol, STATX_SUBVOL) | ||
| #endif | ||
| #define STATX_GET_DOUBLE(ATTR, MEMBER, MASK) \ | ||
| static PyObject* \ | ||
| pystatx_result_get_##ATTR(PyObject *op, void *Py_UNUSED(context)) \ | ||
| { \ | ||
| Py_statx_result *self = Py_statx_result_CAST(op); \ | ||
| if (!(self->stx.stx_mask & MASK)) { \ | ||
| Py_RETURN_NONE; \ | ||
| } \ | ||
| double sec = self->MEMBER; \ | ||
| return PyFloat_FromDouble(sec); \ | ||
| } | ||
| STATX_GET_DOUBLE(st_atime, atime_sec, STATX_ATIME) | ||
| STATX_GET_DOUBLE(st_birthtime, btime_sec, STATX_BTIME) | ||
| STATX_GET_DOUBLE(st_ctime, ctime_sec, STATX_CTIME) | ||
| STATX_GET_DOUBLE(st_mtime, mtime_sec, STATX_MTIME) | ||
| #define STATX_GET_NSEC(ATTR, MEMBER, MASK) \ | ||
| static PyObject* \ | ||
| pystatx_result_get_##ATTR(PyObject *op, void *context) \ | ||
| { \ | ||
| Py_statx_result *self = Py_statx_result_CAST(op); \ | ||
| if (!(self->stx.stx_mask & MASK)) { \ | ||
| Py_RETURN_NONE; \ | ||
| } \ | ||
| struct statx_timestamp *ts = &self->stx.MEMBER; \ | ||
| _posixstate *state = PyType_GetModuleState(Py_TYPE(op)); \ | ||
| assert(state != NULL); \ | ||
| return stat_nanosecond_timestamp(state, ts->tv_sec, ts->tv_nsec); \ | ||
| } | ||
| STATX_GET_NSEC(st_atime_ns, stx_atime, STATX_ATIME) | ||
| STATX_GET_NSEC(st_birthtime_ns, stx_atime, STATX_BTIME) | ||
vstinner marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| STATX_GET_NSEC(st_ctime_ns, stx_ctime, STATX_CTIME) | ||
| STATX_GET_NSEC(st_mtime_ns, stx_mtime, STATX_MTIME) | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I know the mask bit itself won't fit in the context pointer on 32-bit, but you could store the mask bitindex in the upper 16 bits. For example, Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. (You probably already know, but just in case, you can get the bit index with MemberAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I prefer to avoid bad surprises on 32-bit systems when Linux will get more bits in mask. If all bits of mask are used, there will be no remaining place for an offset. It sounds more future proof to do as i did, one function per member and put the mask and "offset" there. Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Perhaps I wasn't clear. I'm not proposing to store the mask in the context pointer; I'm proposing to store the index of the mask bit, which is a number from 0 to 31. Even if the mask were somehow extended to 64 bits in the future, the index would then be from 0 to 63, which would still easily fit. CPython doesn't really care about code size, so I'm fine with the way you did it. MemberAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Oh ok. The problem is that I'm not sure that all compilers supported by Python on Linux can compute the bit position at build time. I don't think that code size is an issue here. | ||
| #define G(attr, doc) \ | ||
| {#attr, pystatx_result_get_##attr, NULL, PyDoc_STR(doc), NULL} | ||
| static PyGetSetDef pystatx_result_getset[] = { | ||
| G(st_nlink, "number of hard links"), | ||
| G(st_uid, "user ID of owner"), | ||
| G(st_gid, "group ID of owner"), | ||
| G(st_ino, "inode"), | ||
| G(st_size, "total size, in bytes"), | ||
| G(st_blocks, "number of blocks allocated"), | ||
| G(st_atime, "time of last access"), | ||
| G(st_atime_ns, "time of last access in nanoseconds"), | ||
| G(st_birthtime, "time of creation"), | ||
| G(st_birthtime_ns, "time of creation in nanoseconds"), | ||
| G(st_ctime, "time of last modification"), | ||
vstinner marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page.
vstinner marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| G(st_ctime_ns, "time of last change in nanoseconds"), | ||
| G(st_mtime, "time of last modification"), | ||
| G(st_mtime_ns, "time of last modification in nanoseconds"), | ||
| #ifdef STATX_MNT_ID | ||
| G(stx_mnt_id, "mount ID"), | ||
| #endif | ||
| #ifdef STATX_DIOALIGN | ||
| G(stx_dio_mem_align, "direct I/O memory buffer alignment"), | ||
| G(stx_dio_offset_align, "direct I/O file offset alignment"), | ||
| #endif | ||
| #ifdef STATX_SUBVOL | ||
| G(stx_subvol, "subvolume ID"), | ||
| #endif | ||
| #ifdef STATX_WRITE_ATOMIC | ||
| G(stx_atomic_write_unit_min, | ||
| "minimum size for direct I/O with torn-write protection"), | ||
| G(stx_atomic_write_unit_max, | ||
| "maximum size for direct I/O with torn-write protection"), | ||
| G(stx_atomic_write_segments_max, | ||
| "maximum iovecs for direct I/O with torn-write protection"), | ||
| #endif | ||
| #ifdef STATX_DIO_READ_ALIGN | ||
| G(stx_dio_read_offset_align, "direct I/O file offset alignment for reads"), | ||
| #endif | ||
| #ifdef HAVE_STRUCT_STATX_STX_ATOMIC_WRITE_UNIT_MAX_OPT | ||
| G(stx_atomic_write_unit_max_opt, | ||
| "maximum optimized size for direct I/O with torn-write protection"), | ||
| #endif | ||
| {NULL}, | ||
| }; | ||
| #undefG | ||
| static PyObject * | ||
| pystatx_result_repr(PyObject *op) | ||
| @@ -3452,23 +3535,25 @@ pystatx_result_repr(PyObject *op) | ||
| } | ||
| for (size_t i = 0; i < Py_ARRAY_LENGTH(pystatx_result_getset) - 1; ++i) { | ||
| PyGetSetDef *d = &pystatx_result_getset[i]; | ||
| PyObject *o = d->get(op, d->closure); | ||
| if (o == NULL) { | ||
| goto error; | ||
| } | ||
| if (o != Py_None) { | ||
| if (i > 0) { | ||
| WRITE_ASCII(", "); | ||
vstinner marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| } | ||
| WRITE_ASCII(d->name); | ||
| WRITE_ASCII("="); | ||
| if (PyUnicodeWriter_WriteRepr(writer, o) < 0) { | ||
| Py_DECREF(o); | ||
| goto error; | ||
| } | ||
| Py_DECREF(o); | ||
| } | ||
vstinner marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| } | ||
| WRITE_ASCII(")"); | ||
Uh oh!
There was an error while loading.Please reload this page.