Skip to content

Commit c1a6d03

Browse files
Add __new__ changes
1 parent b9a49aa commit c1a6d03

File tree

1 file changed

+34
-31
lines changed

1 file changed

+34
-31
lines changed

Modules/posixmodule.c

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

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

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

62556251
static PyObject *
6256-
os_sched_param(PyObject *module, PyObject *args, PyObject *kwargs)
6252+
os_sched_param(PyTypeObject *type, PyObject *args, PyObject *kwargs)
62576253
{
62586254
static char* _keywords[] = {"sched_priority", NULL};
62596255
static const char * _format = "O:sched_param";
6260-
PyObject *sched_priority, *res;
6256+
PyObject *sequence, *sched_priority, *res;
62616257

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+
}
62626263
int result = PyArg_ParseTupleAndKeywords(args, kwargs, _format, _keywords,
62636264
&sched_priority);
6265+
Py_DECREF(sequence);
62646266
if (!result) {
62656267
return NULL;
62666268
}
6267-
res = PyStructSequence_New(
6268-
(PyTypeObject*)_posixstate(module)->SchedParamType
6269-
);
6269+
res = PyStructSequence_New((PyTypeObject *)type);
62706270
if (!res) {
62716271
return NULL;
62726272
}
@@ -6275,22 +6275,6 @@ os_sched_param(PyObject *module, PyObject *args, PyObject *kwargs)
62756275
return res;
62766276
}
62776277

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-
}
6293-
62946278
static PyStructSequence_Field sched_param_fields[] = {
62956279
{"sched_priority", "the scheduling priority"},
62966280
{0}
@@ -13593,6 +13577,16 @@ os__remove_dll_directory_impl(PyObject *module, PyObject *cookie)
1359313577

1359413578
#endif
1359513579

13580+
#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
13581+
static PyMethodDef SchedParamType_dunder_new = {
13582+
"__new__", (PyCFunction)os_sched_param, METH_VARARGS | METH_KEYWORDS
13583+
};
13584+
#endif
13585+
13586+
static PyMethodDef StatResultType_dunder_new = {
13587+
"__new__", (PyCFunction)statresult_new, METH_VARARGS
13588+
};
13589+
1359613590
static PyMethodDef posix_methods[] = {
1359713591

1359813592
OS_STAT_METHODDEF
@@ -14446,7 +14440,7 @@ static const char * const have_functions[] = {
1444614440
PyMODINIT_FUNC
1444714441
INITFUNC(void)
1444814442
{
14449-
PyObject *m, *v;
14443+
PyObject *m, *v, *dunder_new;
1445014444
PyObject *list;
1445114445
const char * const *trace;
1445214446

@@ -14504,10 +14498,14 @@ INITFUNC(void)
1450414498
Py_INCREF(StatResultType);
1450514499
PyModule_AddObject(m, "stat_result", StatResultType);
1450614500
_posixstate(m)->StatResultType = StatResultType;
14507-
1450814501
/* Add a custom __new__ to the structsequence */
14509-
_posixstate(m)->structseq_new = ((PyTypeObject *)StatResultType)->tp_new;
14510-
((PyTypeObject *)StatResultType)->tp_new = statresult_new;
14502+
_posixstate(m)->structseq_new = (newfunc)PyType_GetSlot((PyTypeObject *)StatResultType, Py_tp_new);
14503+
dunder_new = PyDescr_NewClassMethod((PyTypeObject *)StatResultType, &StatResultType_dunder_new);
14504+
if (dunder_new == NULL) {
14505+
return NULL;
14506+
}
14507+
PyObject_SetAttrString(StatResultType, "__new__", dunder_new);
14508+
Py_DECREF(dunder_new);
1451114509

1451214510
statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */
1451314511
PyObject *StatVFSResultType = (PyObject *)PyStructSequence_NewType(&statvfs_result_desc);
@@ -14537,7 +14535,12 @@ INITFUNC(void)
1453714535
PyModule_AddObject(m, "sched_param", SchedParamType);
1453814536
_posixstate(m)->SchedParamType = SchedParamType;
1453914537
/* Add a custom __new__ to the structsequence */
14540-
((PyTypeObject *)SchedParamType)->tp_new = sched_param_new;
14538+
dunder_new = PyDescr_NewClassMethod((PyTypeObject *)SchedParamType, &SchedParamType_dunder_new);
14539+
if (dunder_new == NULL) {
14540+
return NULL;
14541+
}
14542+
PyObject_SetAttrString((PyObject *)SchedParamType, "__new__", dunder_new);
14543+
Py_DECREF(dunder_new);
1454114544
#endif
1454214545

1454314546
/* initialize TerminalSize_info */

0 commit comments

Comments
 (0)