Skip to content

Commit 07edc0d

Browse files
gh-132987: Support __index__() in the posix module (GH-133096)
Support it for the dev_t values in mknod(), major(), minor() and makedev(), CPU numbers in sched_setaffinity(), group numbers in setgroups(), configuration name in fpathconf(), pathconf(), confstr(), and sysconf().
1 parent bba14c3 commit 07edc0d

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

Modules/posixmodule.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,21 +1035,34 @@ _PyLong_FromDev(dev_t dev)
10351035
static int
10361036
_Py_Dev_Converter(PyObject *obj, void *p)
10371037
{
1038+
if (!PyLong_Check(obj)) {
1039+
obj = _PyNumber_Index(obj);
1040+
if (obj == NULL) {
1041+
return 0;
1042+
}
1043+
}
1044+
else {
1045+
Py_INCREF(obj);
1046+
}
1047+
assert(PyLong_Check(obj));
10381048
#ifdef NODEV
1039-
if (PyLong_Check(obj) && _PyLong_IsNegative((PyLongObject *)obj)) {
1049+
if (_PyLong_IsNegative((PyLongObject *)obj)) {
10401050
int overflow;
10411051
long long result = PyLong_AsLongLongAndOverflow(obj, &overflow);
10421052
if (result == -1 && PyErr_Occurred()) {
1053+
Py_DECREF(obj);
10431054
return 0;
10441055
}
10451056
if (!overflow && result == (long long)NODEV) {
10461057
*((dev_t *)p) = NODEV;
1058+
Py_DECREF(obj);
10471059
return 1;
10481060
}
10491061
}
10501062
#endif
10511063

10521064
unsigned long long result = PyLong_AsUnsignedLongLong(obj);
1065+
Py_DECREF(obj);
10531066
if (result == (unsigned long long)-1 && PyErr_Occurred()) {
10541067
return 0;
10551068
}
@@ -8499,7 +8512,7 @@ os_sched_setaffinity_impl(PyObject *module, pid_t pid, PyObject *mask)
84998512

85008513
while ((item = PyIter_Next(iterator))) {
85018514
long cpu;
8502-
if (!PyLong_Check(item)) {
8515+
if (!PyIndex_Check(item)) {
85038516
PyErr_Format(PyExc_TypeError,
85048517
"expected an iterator of ints, "
85058518
"but iterator yielded %R",
@@ -9874,7 +9887,7 @@ os_setgroups(PyObject *module, PyObject *groups)
98749887
PyMem_Free(grouplist);
98759888
return NULL;
98769889
}
9877-
if (!PyLong_Check(elem)) {
9890+
if (!PyIndex_Check(elem)) {
98789891
PyErr_SetString(PyExc_TypeError,
98799892
"groups must be integers");
98809893
Py_DECREF(elem);
@@ -13643,7 +13656,7 @@ conv_confname(PyObject *module, PyObject *arg, int *valuep, const char *tablenam
1364313656
}
1364413657

1364513658
int success = 0;
13646-
if (!PyLong_Check(arg)) {
13659+
if (!PyIndex_Check(arg)) {
1364713660
PyErr_SetString(PyExc_TypeError,
1364813661
"configuration names must be strings or integers");
1364913662
} else {

0 commit comments

Comments
 (0)