Skip to content

Commit a1110f3

Browse files
jbosboomvstinner
andcommitted
Apply suggestions from code review
Co-authored-by: Victor Stinner <[email protected]>
1 parent c2e6f81 commit a1110f3

File tree

4 files changed

+69
-68
lines changed

4 files changed

+69
-68
lines changed

Doc/library/os.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3386,9 +3386,11 @@ features:
33863386
.. function:: statx(path, mask, *, dir_fd=None, follow_symlinks=True, sync=None)
33873387

33883388
Get the status of a file or file descriptor by performing a :c:func:`!statx`
3389-
system call on the given path. *path* may be specified as either a string or
3390-
bytes -- directly or indirectly through the :class:`PathLike` interface --
3391-
or as an open file descriptor. *mask* is a combination of the module-level
3389+
system call on the given path.
3390+
3391+
*path* may be specified as either a string or bytes -- directly or
3392+
indirectly through the :class:`PathLike` interface -- or as an open file
3393+
descriptor. *mask* is a combination of the module-level
33923394
:const:`STATX_* <STATX_TYPE>` constants specifying the information to
33933395
retrieve. Returns a :class:`statx_result` object whose
33943396
:attr:`~os.statx_result.stx_mask` attribute specifies the information

Lib/test/test_os.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -644,8 +644,8 @@ def check_timestamp_agreement(self, result, names):
644644
# Make sure that the st_?time and st_?time_ns fields roughly agree
645645
# (they should always agree up to around tens-of-microseconds)
646646
for name in names:
647-
floaty = int(getattr(result, name) * 100000)
648-
nanosecondy = getattr(result, name + "_ns") // 10000
647+
floaty = int(getattr(result, name) * 100_000)
648+
nanosecondy = getattr(result, name + "_ns") // 10_000
649649
self.assertAlmostEqual(floaty, nanosecondy, delta=2, msg=name)
650650

651651
def check_stat_attributes(self, fname):

Lib/test/test_posix.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1661,7 +1661,8 @@ def test_stat_dir_fd(self):
16611661

16621662
@unittest.skipUnless(hasattr(posix, 'statx'), "test needs os.statx()")
16631663
def test_statx_dir_fd(self):
1664-
func = lambda path, **kwargs: posix.statx(path, os.STATX_INO, **kwargs)
1664+
def func(path, **kwargs):
1665+
return posix.statx(path, os.STATX_INO, **kwargs)
16651666
self.check_statlike_dir_fd(func)
16661667

16671668
@unittest.skipUnless(os.utime in os.supports_dir_fd, "test needs dir_fd support in os.utime()")

Modules/posixmodule.c

Lines changed: 60 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@ extern char *ctermid_r(char *);
410410
#endif
411411

412412
#ifdef HAVE_STATX
413+
/* until we can assume glibc 2.28 at runtime, we must weakly link */
413414
# pragma weak statx
414415
/* provide constants introduced later than statx itself */
415416
# ifndef STATX_MNT_ID
@@ -2624,7 +2625,8 @@ _posix_free(void *module)
26242625

26252626
#define SEC_TO_NS (1000000000LL)
26262627
static PyObject *
2627-
nanosecond_timestamp(_posixstate *state, time_t sec, unsigned long nsec) {
2628+
stat_nanosecond_timestamp(_posixstate *state, time_t sec, unsigned long nsec)
2629+
{
26282630
/* 1677-09-21 00:12:44 to 2262-04-11 23:47:15 UTC inclusive */
26292631
if ((LLONG_MIN/SEC_TO_NS) <= sec && sec <= (LLONG_MAX/SEC_TO_NS - 1)) {
26302632
return PyLong_FromLongLong(sec * SEC_TO_NS + nsec);
@@ -2680,7 +2682,7 @@ fill_time(_posixstate *state, PyObject *v, int s_index, int f_index,
26802682
}
26812683

26822684
if (ns_index >= 0) {
2683-
PyObject *ns_total = nanosecond_timestamp(state, sec, nsec);
2685+
PyObject *ns_total = stat_nanosecond_timestamp(state, sec, nsec);
26842686
if (ns_total == NULL) {
26852687
return -1;
26862688
}
@@ -3322,18 +3324,18 @@ typedef struct {
33223324
struct statx stx;
33233325
double atime_sec, btime_sec, ctime_sec, mtime_sec;
33243326
dev_t rdev, dev;
3325-
} statx_result;
3327+
} Py_statx_result;
33263328

33273329
#define M(attr, type, offset, doc) \
33283330
{attr, type, offset, Py_READONLY, PyDoc_STR(doc)}
33293331
#define MO(attr, type, offset, doc) \
3330-
M(#attr, type, offsetof(statx_result, stx) + offset, doc)
3332+
M(#attr, type, offsetof(Py_statx_result, stx) + offset, doc)
33313333
#define MM(attr, type, member, doc) \
3332-
M(#attr, type, offsetof(statx_result, stx.stx_##member), doc)
3334+
M(#attr, type, offsetof(Py_statx_result, stx.stx_##member), doc)
33333335
#define MX(attr, type, member, doc) \
3334-
M(#attr, type, offsetof(statx_result, member), doc)
3336+
M(#attr, type, offsetof(Py_statx_result, member), doc)
33353337

3336-
static PyMemberDef statx_result_members[] = {
3338+
static PyMemberDef pystatx_result_members[] = {
33373339
MM(stx_mask, Py_T_UINT, mask, "member validity mask"),
33383340
MM(st_blksize, Py_T_UINT, blksize, "blocksize for filesystem I/O"),
33393341
MM(stx_attributes, Py_T_ULONGLONG, attributes, "Linux inode attribute bits"),
@@ -3370,42 +3372,40 @@ static PyMemberDef statx_result_members[] = {
33703372
#undef MO
33713373
#undef M
33723374

3373-
#define DECLARE_GET(name, type, func) \
3374-
static PyObject * \
3375-
statx_result_get_##name(PyObject *op, void *context) { \
3376-
statx_result *self = (statx_result *) op; \
3377-
uint16_t offset = (uintptr_t)context; \
3378-
type val; \
3379-
memcpy(&val, (void *)self + offset, sizeof(val)); \
3380-
return func(val); \
3381-
}
3382-
DECLARE_GET(u32, uint32_t, PyLong_FromUInt32)
3383-
#undef DECLARE_GET
3375+
static PyObject *
3376+
pystatx_result_get_u32(PyObject *op, void *context) {
3377+
Py_statx_result *self = (Py_statx_result *) op;
3378+
uint16_t offset = (uintptr_t)context;
3379+
uint32_t val;
3380+
memcpy(&val, (void *)self + offset, sizeof(val));
3381+
return PyLong_FromUInt32(val);
3382+
}
33843383

33853384
static PyObject *
3386-
statx_result_get_nsec(PyObject *op, void *context) {
3387-
statx_result *self = (statx_result *) op;
3385+
pystatx_result_get_nsec(PyObject *op, void *context)
3386+
{
3387+
Py_statx_result *self = (Py_statx_result *) op;
33883388
uint16_t offset = (uintptr_t)context;
33893389
struct statx_timestamp val;
33903390
memcpy(&val, (void *)self + offset, sizeof(val));
33913391
_posixstate *state = PyType_GetModuleState(Py_TYPE(op));
33923392
assert(state != NULL);
3393-
return nanosecond_timestamp(state, val.tv_sec, val.tv_nsec);
3393+
return stat_nanosecond_timestamp(state, val.tv_sec, val.tv_nsec);
33943394
}
33953395

33963396
/* The low 16 bits of the context pointer are the offset from the start of
3397-
statx_result to the struct statx member. */
3398-
#define OFFSET_CONTEXT(offset) (void *)(offsetof(statx_result, stx) + offset)
3397+
Py_statx_result to the struct statx member. */
3398+
#define OFFSET_CONTEXT(offset) (void *)(offsetof(Py_statx_result, stx) + offset)
33993399
#define MEMBER_CONTEXT(name) OFFSET_CONTEXT(offsetof(struct statx, stx_##name))
34003400

34013401
#define G(attr, type, doc, context) \
3402-
{attr, statx_result_get_##type, NULL, PyDoc_STR(doc), context}
3402+
{attr, pystatx_result_get_##type, NULL, PyDoc_STR(doc), context}
34033403
#define GM(attr, type, member, doc) \
34043404
G(#attr, type, doc, MEMBER_CONTEXT(member))
34053405
#define GO(attr, type, offset, doc) \
34063406
G(#attr, type, doc, OFFSET_CONTEXT(offset))
34073407

3408-
static PyGetSetDef statx_result_getset[] = {
3408+
static PyGetSetDef pystatx_result_getset[] = {
34093409
GM(st_atime_ns, nsec, atime, "time of last access in nanoseconds"),
34103410
GM(st_birthtime_ns, nsec, btime, "time of creation in nanoseconds"),
34113411
GM(st_ctime_ns, nsec, ctime, "time of last change in nanoseconds"),
@@ -3426,74 +3426,72 @@ static PyGetSetDef statx_result_getset[] = {
34263426
};
34273427

34283428
#undef GO
3429-
#undef GOC
34303429
#undef GM
3431-
#undef GMC
34323430
#undef G
34333431
#undef MEMBER_CONTEXT
3434-
#undef MEMBER_CACHE_CONTEXT
34353432
#undef OFFSET_CONTEXT
3436-
#undef OFFSET_CACHE_CONTEXT
34373433

34383434
static PyObject *
3439-
statx_result_repr(PyObject *op) {
3435+
pystatx_result_repr(PyObject *op) {
34403436
PyUnicodeWriter *writer = PyUnicodeWriter_Create(0);
34413437
if (writer == NULL) {
34423438
return NULL;
34433439
}
3444-
#define WRITE_ASCII(s, n) \
3440+
#define WRITE_ASCII(s) \
34453441
do { \
3446-
if (PyUnicodeWriter_WriteASCII(writer, s, n) < 0) { \
3442+
if (PyUnicodeWriter_WriteASCII(writer, s, strlen(s)) < 0) { \
34473443
goto error; \
34483444
} \
34493445
} while (0)
34503446

3451-
WRITE_ASCII("os.statx_result(", -1);
3447+
WRITE_ASCII("os.statx_result(");
34523448

3453-
for (size_t i = 0; i < Py_ARRAY_LENGTH(statx_result_members) - 1; ++i) {
3449+
for (size_t i = 0; i < Py_ARRAY_LENGTH(pystatx_result_members) - 1; ++i) {
34543450
if (i > 0) {
3455-
WRITE_ASCII(", ", 2);
3451+
WRITE_ASCII(", ");
34563452
}
34573453

3458-
PyMemberDef *d = &statx_result_members[i];
3459-
WRITE_ASCII(d->name, -1);
3460-
WRITE_ASCII("=", 1);
3454+
PyMemberDef *d = &pystatx_result_members[i];
3455+
WRITE_ASCII(d->name);
3456+
WRITE_ASCII("=");
34613457

34623458
PyObject *o = PyMember_GetOne((const char *)op, d);
34633459
if (o == NULL) {
34643460
goto error;
34653461
}
34663462
if (PyUnicodeWriter_WriteRepr(writer, o) < 0) {
3463+
Py_DECREF(o);
34673464
goto error;
34683465
}
34693466
Py_DECREF(o);
34703467
}
34713468

3472-
if (Py_ARRAY_LENGTH(statx_result_members) > 1
3473-
&& Py_ARRAY_LENGTH(statx_result_getset) > 1) {
3474-
WRITE_ASCII(", ", 2);
3469+
if (Py_ARRAY_LENGTH(pystatx_result_members) > 1
3470+
&& Py_ARRAY_LENGTH(pystatx_result_getset) > 1) {
3471+
WRITE_ASCII(", ");
34753472
}
34763473

3477-
for (size_t i = 0; i < Py_ARRAY_LENGTH(statx_result_getset) - 1; ++i) {
3474+
for (size_t i = 0; i < Py_ARRAY_LENGTH(pystatx_result_getset) - 1; ++i) {
34783475
if (i > 0) {
3479-
WRITE_ASCII(", ", 2);
3476+
WRITE_ASCII(", ");
34803477
}
34813478

3482-
PyGetSetDef *d = &statx_result_getset[i];
3483-
WRITE_ASCII(d->name, -1);
3484-
WRITE_ASCII("=", 1);
3479+
PyGetSetDef *d = &pystatx_result_getset[i];
3480+
WRITE_ASCII(d->name);
3481+
WRITE_ASCII("=");
34853482

34863483
PyObject *o = d->get(op, d->closure);
34873484
if (o == NULL) {
34883485
goto error;
34893486
}
34903487
if (PyUnicodeWriter_WriteRepr(writer, o) < 0) {
3488+
Py_DECREF(o);
34913489
goto error;
34923490
}
34933491
Py_DECREF(o);
34943492
}
34953493

3496-
WRITE_ASCII(")", 1);
3494+
WRITE_ASCII(")");
34973495
return PyUnicodeWriter_Finish(writer);
34983496
#undef WRITE_ASCII
34993497

@@ -3503,35 +3501,35 @@ statx_result_repr(PyObject *op) {
35033501
}
35043502

35053503
static int
3506-
statx_result_traverse(PyObject *self, visitproc visit, void *arg) {
3504+
pystatx_result_traverse(PyObject *self, visitproc visit, void *arg) {
35073505
Py_VISIT(Py_TYPE(self));
35083506
return 0;
35093507
}
35103508

35113509
static void
3512-
statx_result_dealloc(PyObject *op) {
3513-
statx_result *self = (statx_result *) op;
3510+
pystatx_result_dealloc(PyObject *op) {
3511+
Py_statx_result *self = (Py_statx_result *) op;
35143512
PyTypeObject *tp = Py_TYPE(self);
35153513
PyObject_GC_UnTrack(self);
35163514
tp->tp_free(self);
35173515
Py_DECREF(tp);
35183516
}
35193517

3520-
static PyType_Slot statx_result_slots[] = {
3521-
{Py_tp_repr, statx_result_repr},
3522-
{Py_tp_traverse, statx_result_traverse},
3523-
{Py_tp_dealloc, statx_result_dealloc},
3524-
{Py_tp_members, statx_result_members},
3525-
{Py_tp_getset, statx_result_getset},
3518+
static PyType_Slot pystatx_result_slots[] = {
3519+
{Py_tp_repr, pystatx_result_repr},
3520+
{Py_tp_traverse, pystatx_result_traverse},
3521+
{Py_tp_dealloc, pystatx_result_dealloc},
3522+
{Py_tp_members, pystatx_result_members},
3523+
{Py_tp_getset, pystatx_result_getset},
35263524
{0, NULL},
35273525
};
35283526

3529-
static PyType_Spec statx_result_spec = {
3527+
static PyType_Spec pystatx_result_spec = {
35303528
.name = "statx_result",
3531-
.basicsize = sizeof(statx_result),
3529+
.basicsize = sizeof(Py_statx_result),
35323530
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE | Py_TPFLAGS_HAVE_GC |
35333531
Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_DISALLOW_INSTANTIATION,
3534-
.slots = statx_result_slots,
3532+
.slots = pystatx_result_slots,
35353533
};
35363534

35373535
static int
@@ -3612,7 +3610,7 @@ os_statx_impl(PyObject *module, path_t *path, unsigned int mask, int dir_fd,
36123610

36133611
_posixstate *state = get_posix_state(module);
36143612
PyTypeObject *tp = (PyTypeObject *)state->StatxResultType;
3615-
statx_result *v = (statx_result *)tp->tp_alloc(tp, 0);
3613+
Py_statx_result *v = (Py_statx_result *)tp->tp_alloc(tp, 0);
36163614
if (v == NULL) {
36173615
return NULL;
36183616
}
@@ -18558,8 +18556,8 @@ posixmodule_exec(PyObject *m)
1855818556
}
1855918557
}
1856018558
else {
18561-
statx_result_spec.name = "os.statx_result";
18562-
state->StatxResultType = PyType_FromModuleAndSpec(m, &statx_result_spec, NULL);
18559+
pystatx_result_spec.name = "os.statx_result";
18560+
state->StatxResultType = PyType_FromModuleAndSpec(m, &pystatx_result_spec, NULL);
1856318561
if (PyModule_AddObjectRef(m, "statx_result", state->StatxResultType) < 0) {
1856418562
return -1;
1856518563
}

0 commit comments

Comments
 (0)