Skip to content

Commit 6e505ac

Browse files
committed
Use tp_new directly
1 parent 4c80b06 commit 6e505ac

File tree

1 file changed

+30
-36
lines changed

1 file changed

+30
-36
lines changed

Modules/posixmodule.c

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2077,12 +2077,16 @@ static PyStructSequence_Desc waitid_result_desc = {
20772077
#endif
20782078

20792079
static PyObject *
2080-
statresult_new(PyTypeObject *type, PyObject *args)
2080+
statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
20812081
{
20822082
PyObject *sequence, *kwds;
20832083
PyStructSequence *result;
20842084
int i;
20852085

2086+
if (!_PyArg_NoKeywords("StatResult", kwargs)) {
2087+
return NULL;
2088+
}
2089+
20862090
/* Remove the cls object from the argument list */
20872091
sequence = PyTuple_GetSlice(args, 1, PyTuple_Size(args));
20882092
if (!sequence) {
@@ -6249,24 +6253,20 @@ PyDoc_STRVAR(os_sched_param__doc__,
62496253
" A scheduling parameter.");
62506254

62516255
static PyObject *
6252-
os_sched_param(PyTypeObject *type, PyObject *args, PyObject *kwargs)
6256+
os_sched_param(PyObject *module, PyObject *args, PyObject *kwargs)
62536257
{
62546258
static char* _keywords[] = {"sched_priority", NULL};
62556259
static const char * _format = "O:sched_param";
6256-
PyObject *sequence, *sched_priority, *res;
6260+
PyObject *sched_priority, *res;
62576261

6258-
/* Remove the cls object from the argument list */
6259-
sequence = PyTuple_GetSlice(args, 1, PyTuple_Size(args));
6260-
if (!sequence) {
6261-
return NULL;
6262-
}
6263-
int result = PyArg_ParseTupleAndKeywords(sequence, kwargs, _format, _keywords,
6262+
int result = PyArg_ParseTupleAndKeywords(args, kwargs, _format, _keywords,
62646263
&sched_priority);
6265-
Py_DECREF(sequence);
62666264
if (!result) {
62676265
return NULL;
62686266
}
6269-
res = PyStructSequence_New((PyTypeObject *)type);
6267+
res = PyStructSequence_New(
6268+
(PyTypeObject*)_posixstate(module)->SchedParamType
6269+
);
62706270
if (!res) {
62716271
return NULL;
62726272
}
@@ -6275,7 +6275,21 @@ os_sched_param(PyTypeObject *type, PyObject *args, PyObject *kwargs)
62756275
return res;
62766276
}
62776277

6278-
PyDoc_VAR(os_sched_param__doc__);
6278+
static PyObject *
6279+
sched_param_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) {
6280+
PyObject *module = PyState_FindModule(&posixmodule);
6281+
if (!module) {
6282+
return NULL;
6283+
}
6284+
/* Remove the cls object from the argument list */
6285+
PyObject *sequence = PyTuple_GetSlice(args, 1, PyTuple_Size(args));
6286+
if (!sequence) {
6287+
return NULL;
6288+
}
6289+
PyObject *result = os_sched_param(module, args, kwargs);
6290+
Py_DECREF(sequence);
6291+
return result;
6292+
}
62796293

62806294
static PyStructSequence_Field sched_param_fields[] = {
62816295
{"sched_priority", "the scheduling priority"},
@@ -13579,16 +13593,6 @@ os__remove_dll_directory_impl(PyObject *module, PyObject *cookie)
1357913593

1358013594
#endif
1358113595

13582-
#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
13583-
static PyMethodDef SchedParamType_dunder_new = {
13584-
"__new__", (PyCFunction)os_sched_param, METH_VARARGS | METH_KEYWORDS
13585-
};
13586-
#endif
13587-
13588-
static PyMethodDef StatResultType_dunder_new = {
13589-
"__new__", (PyCFunction)statresult_new, METH_VARARGS
13590-
};
13591-
1359213596
static PyMethodDef posix_methods[] = {
1359313597

1359413598
OS_STAT_METHODDEF
@@ -14442,7 +14446,7 @@ static const char * const have_functions[] = {
1444214446
PyMODINIT_FUNC
1444314447
INITFUNC(void)
1444414448
{
14445-
PyObject *m, *v, *dunder_new;
14449+
PyObject *m, *v;
1444614450
PyObject *list;
1444714451
const char * const *trace;
1444814452

@@ -14502,13 +14506,8 @@ INITFUNC(void)
1450214506
_posixstate(m)->StatResultType = StatResultType;
1450314507

1450414508
/* Add a custom __new__ to the structsequence */
14505-
_posixstate(m)->structseq_new = (newfunc)PyType_GetSlot((PyTypeObject *)StatResultType, Py_tp_new);
14506-
dunder_new = PyDescr_NewClassMethod((PyTypeObject *)StatResultType, &StatResultType_dunder_new);
14507-
if (dunder_new == NULL) {
14508-
return NULL;
14509-
}
14510-
PyObject_SetAttrString(StatResultType, "__new__", dunder_new);
14511-
Py_DECREF(dunder_new);
14509+
_posixstate(m)->structseq_new = ((PyTypeObject *)StatResultType)->tp_new;
14510+
((PyTypeObject *)StatResultType)->tp_new = statresult_new;
1451214511

1451314512
statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */
1451414513
PyObject *StatVFSResultType = (PyObject *)PyStructSequence_NewType(&statvfs_result_desc);
@@ -14538,12 +14537,7 @@ INITFUNC(void)
1453814537
PyModule_AddObject(m, "sched_param", SchedParamType);
1453914538
_posixstate(m)->SchedParamType = SchedParamType;
1454014539
/* Add a custom __new__ to the structsequence */
14541-
dunder_new = PyDescr_NewClassMethod((PyTypeObject *)SchedParamType, &SchedParamType_dunder_new);
14542-
if (dunder_new == NULL) {
14543-
return NULL;
14544-
}
14545-
PyObject_SetAttrString((PyObject *)SchedParamType, "__new__", dunder_new);
14546-
Py_DECREF(dunder_new);
14540+
((PyTypeObject *)SchedParamType)->tp_new = sched_param_new;
1454714541
#endif
1454814542

1454914543
/* initialize TerminalSize_info */

0 commit comments

Comments
 (0)