| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,2 @@ | |||
| 1 | + Fix the implementation of PyStructSequence_NewType in order to create heap | ||
| 2 | + allocated StructSequences. | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3313,6 +3313,31 @@ test_decref_doesnt_leak(PyObject *ob, PyObject *Py_UNUSED(ignored)) | |||
| 3313 | 3313 | Py_RETURN_NONE; | |
| 3314 | 3314 | } | |
| 3315 | 3315 | ||
| 3316 | + static PyObject * | ||
| 3317 | + test_structseq_newtype_doesnt_leak(PyObject *Py_UNUSED(self), | ||
| 3318 | + PyObject *Py_UNUSED(args)) | ||
| 3319 | + { | ||
| 3320 | + PyStructSequence_Desc descr; | ||
| 3321 | + PyStructSequence_Field descr_fields[3]; | ||
| 3322 | + | ||
| 3323 | + descr_fields[0] = (PyStructSequence_Field){"foo", "foo value"}; | ||
| 3324 | + descr_fields[1] = (PyStructSequence_Field){NULL, "some hidden value"}; | ||
| 3325 | + descr_fields[2] = (PyStructSequence_Field){0, NULL}; | ||
| 3326 | + | ||
| 3327 | + descr.name = "_testcapi.test_descr"; | ||
| 3328 | + descr.doc = "This is used to test for memory leaks in NewType"; | ||
| 3329 | + descr.fields = descr_fields; | ||
| 3330 | + descr.n_in_sequence = 1; | ||
| 3331 | + | ||
| 3332 | + PyTypeObject* structseq_type = PyStructSequence_NewType(&descr); | ||
| 3333 | + assert(structseq_type != NULL); | ||
| 3334 | + assert(PyType_Check(structseq_type)); | ||
| 3335 | + assert(PyType_FastSubclass(structseq_type, Py_TPFLAGS_TUPLE_SUBCLASS)); | ||
| 3336 | + Py_DECREF(structseq_type); | ||
| 3337 | + | ||
| 3338 | + Py_RETURN_NONE; | ||
| 3339 | + } | ||
| 3340 | + | ||
| 3316 | 3341 | static PyObject * | |
| 3317 | 3342 | test_incref_decref_API(PyObject *ob, PyObject *Py_UNUSED(ignored)) | |
| 3318 | 3343 | { | |
@@ -4721,6 +4746,8 @@ static PyMethodDef TestMethods[] = { | |||
| 4721 | 4746 | {"test_incref_doesnt_leak", test_incref_doesnt_leak, METH_NOARGS}, | |
| 4722 | 4747 | {"test_xdecref_doesnt_leak",test_xdecref_doesnt_leak, METH_NOARGS}, | |
| 4723 | 4748 | {"test_decref_doesnt_leak", test_decref_doesnt_leak, METH_NOARGS}, | |
| 4749 | + {"test_structseq_newtype_doesnt_leak", | ||
| 4750 | + test_structseq_newtype_doesnt_leak, METH_NOARGS}, | ||
| 4724 | 4751 | {"test_incref_decref_API", test_incref_decref_API, METH_NOARGS}, | |
| 4725 | 4752 | {"test_long_and_overflow", test_long_and_overflow, METH_NOARGS}, | |
| 4726 | 4753 | {"test_long_as_double", test_long_as_double, METH_NOARGS}, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1948,14 +1948,14 @@ static PyStructSequence_Desc waitid_result_desc = { | |||
| 1948 | 1948 | waitid_result_fields, | |
| 1949 | 1949 | 5 | |
| 1950 | 1950 | }; | |
| 1951 | - static PyTypeObject WaitidResultType; | ||
| 1951 | + static PyTypeObject* WaitidResultType; | ||
| 1952 | 1952 | #endif | |
| 1953 | 1953 | ||
| 1954 | 1954 | static int initialized; | |
| 1955 | - static PyTypeObject StatResultType; | ||
| 1956 | - static PyTypeObject StatVFSResultType; | ||
| 1955 | + static PyTypeObject* StatResultType; | ||
| 1956 | + static PyTypeObject* StatVFSResultType; | ||
| 1957 | 1957 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM) | |
| 1958 | - static PyTypeObject SchedParamType; | ||
| 1958 | + static PyTypeObject* SchedParamType; | ||
| 1959 | 1959 | #endif | |
| 1960 | 1960 | static newfunc structseq_new; | |
| 1961 | 1961 | ||
@@ -2029,7 +2029,7 @@ static PyObject* | |||
| 2029 | 2029 | _pystat_fromstructstat(STRUCT_STAT *st) | |
| 2030 | 2030 | { | |
| 2031 | 2031 | unsigned long ansec, mnsec, cnsec; | |
| 2032 | - PyObject *v = PyStructSequence_New(&StatResultType); | ||
| 2032 | + PyObject *v = PyStructSequence_New(StatResultType); | ||
| 2033 | 2033 | if (v == NULL) | |
| 2034 | 2034 | return NULL; | |
| 2035 | 2035 | ||
@@ -4407,7 +4407,7 @@ static PyStructSequence_Desc uname_result_desc = { | |||
| 4407 | 4407 | 5 | |
| 4408 | 4408 | }; | |
| 4409 | 4409 | ||
| 4410 | - static PyTypeObject UnameResultType; | ||
| 4410 | + static PyTypeObject* UnameResultType; | ||
| 4411 | 4411 | ||
| 4412 | 4412 | ||
| 4413 | 4413 | #ifdef HAVE_UNAME | |
@@ -4435,7 +4435,7 @@ os_uname_impl(PyObject *module) | |||
| 4435 | 4435 | if (res < 0) | |
| 4436 | 4436 | return posix_error(); | |
| 4437 | 4437 | ||
| 4438 | - value = PyStructSequence_New(&UnameResultType); | ||
| 4438 | + value = PyStructSequence_New(UnameResultType); | ||
| 4439 | 4439 | if (value == NULL) | |
| 4440 | 4440 | return NULL; | |
| 4441 | 4441 | ||
@@ -5941,7 +5941,7 @@ os_sched_getscheduler_impl(PyObject *module, pid_t pid) | |||
| 5941 | 5941 | ||
| 5942 | 5942 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM) | |
| 5943 | 5943 | /*[clinic input] | |
| 5944 | - class os.sched_param "PyObject *" "&SchedParamType" | ||
| 5944 | + class os.sched_param "PyObject *" "SchedParamType" | ||
| 5945 | 5945 | ||
| 5946 | 5946 | @classmethod | |
| 5947 | 5947 | os.sched_param.__new__ | |
@@ -5954,7 +5954,7 @@ Current has only one field: sched_priority"); | |||
| 5954 | 5954 | ||
| 5955 | 5955 | static PyObject * | |
| 5956 | 5956 | os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority) | |
| 5957 | - /*[clinic end generated code: output=48f4067d60f48c13 input=73a4c22f7071fc62]*/ | ||
| 5957 | + /*[clinic end generated code: output=48f4067d60f48c13 input=ab4de35a9a7811f2]*/ | ||
| 5958 | 5958 | { | |
| 5959 | 5959 | PyObject *res; | |
| 5960 | 5960 | ||
@@ -5986,7 +5986,7 @@ convert_sched_param(PyObject *param, struct sched_param *res) | |||
| 5986 | 5986 | { | |
| 5987 | 5987 | long priority; | |
| 5988 | 5988 | ||
| 5989 | - if (Py_TYPE(param) != &SchedParamType) { | ||
| 5989 | + if (Py_TYPE(param) != SchedParamType) { | ||
| 5990 | 5990 | PyErr_SetString(PyExc_TypeError, "must have a sched_param object"); | |
| 5991 | 5991 | return 0; | |
| 5992 | 5992 | } | |
@@ -6057,7 +6057,7 @@ os_sched_getparam_impl(PyObject *module, pid_t pid) | |||
| 6057 | 6057 | ||
| 6058 | 6058 | if (sched_getparam(pid, ¶m)) | |
| 6059 | 6059 | return posix_error(); | |
| 6060 | - result = PyStructSequence_New(&SchedParamType); | ||
| 6060 | + result = PyStructSequence_New(SchedParamType); | ||
| 6061 | 6061 | if (!result) | |
| 6062 | 6062 | return NULL; | |
| 6063 | 6063 | priority = PyLong_FromLong(param.sched_priority); | |
@@ -7422,7 +7422,7 @@ os_waitid_impl(PyObject *module, idtype_t idtype, id_t id, int options) | |||
| 7422 | 7422 | if (si.si_pid == 0) | |
| 7423 | 7423 | Py_RETURN_NONE; | |
| 7424 | 7424 | ||
| 7425 | - result = PyStructSequence_New(&WaitidResultType); | ||
| 7425 | + result = PyStructSequence_New(WaitidResultType); | ||
| 7426 | 7426 | if (!result) | |
| 7427 | 7427 | return NULL; | |
| 7428 | 7428 | ||
@@ -7857,7 +7857,7 @@ static PyStructSequence_Desc times_result_desc = { | |||
| 7857 | 7857 | 5 | |
| 7858 | 7858 | }; | |
| 7859 | 7859 | ||
| 7860 | - static PyTypeObject TimesResultType; | ||
| 7860 | + static PyTypeObject* TimesResultType; | ||
| 7861 | 7861 | ||
| 7862 | 7862 | #ifdef MS_WINDOWS | |
| 7863 | 7863 | #define HAVE_TIMES /* mandatory, for the method table */ | |
@@ -7870,7 +7870,7 @@ build_times_result(double user, double system, | |||
| 7870 | 7870 | double children_user, double children_system, | |
| 7871 | 7871 | double elapsed) | |
| 7872 | 7872 | { | |
| 7873 | - PyObject *value = PyStructSequence_New(&TimesResultType); | ||
| 7873 | + PyObject *value = PyStructSequence_New(TimesResultType); | ||
| 7874 | 7874 | if (value == NULL) | |
| 7875 | 7875 | return NULL; | |
| 7876 | 7876 | ||
@@ -9950,7 +9950,7 @@ os_WSTOPSIG_impl(PyObject *module, int status) | |||
| 9950 | 9950 | ||
| 9951 | 9951 | static PyObject* | |
| 9952 | 9952 | _pystatvfs_fromstructstatvfs(struct statvfs st) { | |
| 9953 | - PyObject *v = PyStructSequence_New(&StatVFSResultType); | ||
| 9953 | + PyObject *v = PyStructSequence_New(StatVFSResultType); | ||
| 9954 | 9954 | if (v == NULL) | |
| 9955 | 9955 | return NULL; | |
| 9956 | 9956 | ||
@@ -11703,7 +11703,7 @@ os_urandom_impl(PyObject *module, Py_ssize_t size) | |||
| 11703 | 11703 | ||
| 11704 | 11704 | /* Terminal size querying */ | |
| 11705 | 11705 | ||
| 11706 | - static PyTypeObject TerminalSizeType; | ||
| 11706 | + static PyTypeObject* TerminalSizeType; | ||
| 11707 | 11707 | ||
| 11708 | 11708 | PyDoc_STRVAR(TerminalSize_docstring, | |
| 11709 | 11709 | "A tuple of (columns, lines) for holding terminal window size"); | |
@@ -11795,7 +11795,7 @@ get_terminal_size(PyObject *self, PyObject *args) | |||
| 11795 | 11795 | } | |
| 11796 | 11796 | #endif /* TERMSIZE_USE_CONIO */ | |
| 11797 | 11797 | ||
| 11798 | - termsize = PyStructSequence_New(&TerminalSizeType); | ||
| 11798 | + termsize = PyStructSequence_New(TerminalSizeType); | ||
| 11799 | 11799 | if (termsize == NULL) | |
| 11800 | 11800 | return NULL; | |
| 11801 | 11801 | PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns)); | |
@@ -13912,23 +13912,28 @@ INITFUNC(void) | |||
| 13912 | 13912 | if (!initialized) { | |
| 13913 | 13913 | #if defined(HAVE_WAITID) && !defined(__APPLE__) | |
| 13914 | 13914 | waitid_result_desc.name = MODNAME ".waitid_result"; | |
| 13915 | - if (PyStructSequence_InitType2(&WaitidResultType, &waitid_result_desc) < 0) | ||
| 13915 | + WaitidResultType = PyStructSequence_NewType(&waitid_result_desc); | ||
| 13916 | + if (WaitidResultType == NULL) { | ||
| 13916 | 13917 | return NULL; | |
| 13918 | + } | ||
| 13917 | 13919 | #endif | |
| 13918 | 13920 | ||
| 13919 | 13921 | stat_result_desc.name = "os.stat_result"; /* see issue #19209 */ | |
| 13920 | 13922 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; | |
| 13921 | 13923 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; | |
| 13922 | 13924 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; | |
| 13923 | - if (PyStructSequence_InitType2(&StatResultType, &stat_result_desc) < 0) | ||
| 13925 | + StatResultType = PyStructSequence_NewType(&stat_result_desc); | ||
| 13926 | + if (StatResultType == NULL) { | ||
| 13924 | 13927 | return NULL; | |
| 13925 | - structseq_new = StatResultType.tp_new; | ||
| 13926 | - StatResultType.tp_new = statresult_new; | ||
| 13928 | + } | ||
| 13929 | + structseq_new = StatResultType->tp_new; | ||
| 13930 | + StatResultType->tp_new = statresult_new; | ||
| 13927 | 13931 | ||
| 13928 | 13932 | statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */ | |
| 13929 | - if (PyStructSequence_InitType2(&StatVFSResultType, | ||
| 13930 | - &statvfs_result_desc) < 0) | ||
| 13933 | + StatVFSResultType = PyStructSequence_NewType(&statvfs_result_desc); | ||
| 13934 | + if (StatVFSResultType == NULL) { | ||
| 13931 | 13935 | return NULL; | |
| 13936 | + } | ||
| 13932 | 13937 | #ifdef NEED_TICKS_PER_SECOND | |
| 13933 | 13938 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) | |
| 13934 | 13939 | ticks_per_second = sysconf(_SC_CLK_TCK); | |
@@ -13941,15 +13946,18 @@ INITFUNC(void) | |||
| 13941 | 13946 | ||
| 13942 | 13947 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM) | |
| 13943 | 13948 | sched_param_desc.name = MODNAME ".sched_param"; | |
| 13944 | - if (PyStructSequence_InitType2(&SchedParamType, &sched_param_desc) < 0) | ||
| 13949 | + SchedParamType = PyStructSequence_NewType(&sched_param_desc); | ||
| 13950 | + if (SchedParamType == NULL) { | ||
| 13945 | 13951 | return NULL; | |
| 13946 | - SchedParamType.tp_new = os_sched_param; | ||
| 13952 | + } | ||
| 13953 | + SchedParamType->tp_new = os_sched_param; | ||
| 13947 | 13954 | #endif | |
| 13948 | 13955 | ||
| 13949 | 13956 | /* initialize TerminalSize_info */ | |
| 13950 | - if (PyStructSequence_InitType2(&TerminalSizeType, | ||
| 13951 | - &TerminalSize_desc) < 0) | ||
| 13957 | + TerminalSizeType = PyStructSequence_NewType(&TerminalSize_desc); | ||
| 13958 | + if (TerminalSizeType == NULL) { | ||
| 13952 | 13959 | return NULL; | |
| 13960 | + } | ||
| 13953 | 13961 | ||
| 13954 | 13962 | /* initialize scandir types */ | |
| 13955 | 13963 | if (PyType_Ready(&ScandirIteratorType) < 0) | |
@@ -13958,29 +13966,33 @@ INITFUNC(void) | |||
| 13958 | 13966 | return NULL; | |
| 13959 | 13967 | } | |
| 13960 | 13968 | #if defined(HAVE_WAITID) && !defined(__APPLE__) | |
| 13961 | - Py_INCREF((PyObject*) &WaitidResultType); | ||
| 13962 | - PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType); | ||
| 13969 | + Py_INCREF((PyObject*) WaitidResultType); | ||
| 13970 | + PyModule_AddObject(m, "waitid_result", (PyObject*) WaitidResultType); | ||
| 13963 | 13971 | #endif | |
| 13964 | - Py_INCREF((PyObject*) &StatResultType); | ||
| 13965 | - PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); | ||
| 13966 | - Py_INCREF((PyObject*) &StatVFSResultType); | ||
| 13972 | + Py_INCREF((PyObject*) StatResultType); | ||
| 13973 | + PyModule_AddObject(m, "stat_result", (PyObject*) StatResultType); | ||
| 13974 | + Py_INCREF((PyObject*) StatVFSResultType); | ||
| 13967 | 13975 | PyModule_AddObject(m, "statvfs_result", | |
| 13968 | - (PyObject*) &StatVFSResultType); | ||
| 13976 | + (PyObject*) StatVFSResultType); | ||
| 13969 | 13977 | ||
| 13970 | 13978 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) | |
| 13971 | - Py_INCREF(&SchedParamType); | ||
| 13972 | - PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType); | ||
| 13979 | + Py_INCREF(SchedParamType); | ||
| 13980 | + PyModule_AddObject(m, "sched_param", (PyObject *)SchedParamType); | ||
| 13973 | 13981 | #endif | |
| 13974 | 13982 | ||
| 13975 | 13983 | times_result_desc.name = MODNAME ".times_result"; | |
| 13976 | - if (PyStructSequence_InitType2(&TimesResultType, ×_result_desc) < 0) | ||
| 13984 | + TimesResultType = PyStructSequence_NewType(×_result_desc); | ||
| 13985 | + if (TimesResultType == NULL) { | ||
| 13977 | 13986 | return NULL; | |
| 13978 | - PyModule_AddObject(m, "times_result", (PyObject *)&TimesResultType); | ||
| 13987 | + } | ||
| 13988 | + PyModule_AddObject(m, "times_result", (PyObject *)TimesResultType); | ||
| 13979 | 13989 | ||
| 13980 | 13990 | uname_result_desc.name = MODNAME ".uname_result"; | |
| 13981 | - if (PyStructSequence_InitType2(&UnameResultType, &uname_result_desc) < 0) | ||
| 13991 | + UnameResultType = PyStructSequence_NewType(&uname_result_desc); | ||
| 13992 | + if (UnameResultType == NULL) { | ||
| 13982 | 13993 | return NULL; | |
| 13983 | - PyModule_AddObject(m, "uname_result", (PyObject *)&UnameResultType); | ||
| 13994 | + } | ||
| 13995 | + PyModule_AddObject(m, "uname_result", (PyObject *)UnameResultType); | ||
| 13984 | 13996 | ||
| 13985 | 13997 | #ifdef __APPLE__ | |
| 13986 | 13998 | /* | |
@@ -14020,8 +14032,8 @@ INITFUNC(void) | |||
| 14020 | 14032 | ||
| 14021 | 14033 | #endif /* __APPLE__ */ | |
| 14022 | 14034 | ||
| 14023 | - Py_INCREF(&TerminalSizeType); | ||
| 14024 | - PyModule_AddObject(m, "terminal_size", (PyObject*) &TerminalSizeType); | ||
| 14035 | + Py_INCREF(TerminalSizeType); | ||
| 14036 | + PyModule_AddObject(m, "terminal_size", (PyObject*)TerminalSizeType); | ||
| 14025 | 14037 | ||
| 14026 | 14038 | billion = PyLong_FromLong(1000000000); | |
| 14027 | 14039 | if (!billion) | |
| Back | FazBrowse Home | New Git URL |
0 commit comments