From 274da20baf99bd9c00f189a56aa71b23e27e57b7 Mon Sep 17 00:00:00 2001 From: Hai Shi Date: Wed, 1 Apr 2020 00:04:24 +0800 Subject: [PATCH 1/8] Port resource extension module to multiphase initialization(PEP 489) --- ...2020-04-01-00-08-18.bpo-1635741.bhGWam.rst | 1 + Modules/resource.c | 113 ++++++++++-------- 2 files changed, 63 insertions(+), 51 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2020-04-01-00-08-18.bpo-1635741.bhGWam.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-04-01-00-08-18.bpo-1635741.bhGWam.rst b/Misc/NEWS.d/next/Core and Builtins/2020-04-01-00-08-18.bpo-1635741.bhGWam.rst new file mode 100644 index 00000000000000..0dce86b3710b48 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2020-04-01-00-08-18.bpo-1635741.bhGWam.rst @@ -0,0 +1 @@ +Port resource extension module to multiphase initialization (:pep:`489`). diff --git a/Modules/resource.c b/Modules/resource.c index afde03c6c7e559..12cb548731eb6c 100644 --- a/Modules/resource.c +++ b/Modules/resource.c @@ -340,144 +340,132 @@ resource_methods[] = { /* Module initialization */ -static struct PyModuleDef resourcemodule = { - PyModuleDef_HEAD_INIT, - "resource", - NULL, - -1, - resource_methods, - NULL, - NULL, - NULL, - NULL -}; - -PyMODINIT_FUNC -PyInit_resource(void) +static int resource_exec(PyObject *module) { - PyObject *m, *v; - - /* Create the module and add the functions */ - m = PyModule_Create(&resourcemodule); - if (m == NULL) - return NULL; + PyObject *v; /* Add some symbolic constants to the module */ Py_INCREF(PyExc_OSError); - PyModule_AddObject(m, "error", PyExc_OSError); + if (PyModule_AddObject(module, "error", PyExc_OSError) < 0) { + Py_DECREF(PyExc_OSError); + return -1; + } if (!initialized) { if (PyStructSequence_InitType2(&StructRUsageType, &struct_rusage_desc) < 0) - return NULL; + return -1; } Py_INCREF(&StructRUsageType); - PyModule_AddObject(m, "struct_rusage", - (PyObject*) &StructRUsageType); + if (PyModule_AddObject(module, "struct_rusage", + (PyObject*) &StructRUsageType) < 0) { + Py_DECREF(&StructRUsageType); + return -1; + } /* insert constants */ #ifdef RLIMIT_CPU - PyModule_AddIntMacro(m, RLIMIT_CPU); + PyModule_AddIntMacro(module, RLIMIT_CPU); #endif #ifdef RLIMIT_FSIZE - PyModule_AddIntMacro(m, RLIMIT_FSIZE); + PyModule_AddIntMacro(module, RLIMIT_FSIZE); #endif #ifdef RLIMIT_DATA - PyModule_AddIntMacro(m, RLIMIT_DATA); + PyModule_AddIntMacro(module, RLIMIT_DATA); #endif #ifdef RLIMIT_STACK - PyModule_AddIntMacro(m, RLIMIT_STACK); + PyModule_AddIntMacro(module, RLIMIT_STACK); #endif #ifdef RLIMIT_CORE - PyModule_AddIntMacro(m, RLIMIT_CORE); + PyModule_AddIntMacro(module, RLIMIT_CORE); #endif #ifdef RLIMIT_NOFILE - PyModule_AddIntMacro(m, RLIMIT_NOFILE); + PyModule_AddIntMacro(module, RLIMIT_NOFILE); #endif #ifdef RLIMIT_OFILE - PyModule_AddIntMacro(m, RLIMIT_OFILE); + PyModule_AddIntMacro(module, RLIMIT_OFILE); #endif #ifdef RLIMIT_VMEM - PyModule_AddIntMacro(m, RLIMIT_VMEM); + PyModule_AddIntMacro(module, RLIMIT_VMEM); #endif #ifdef RLIMIT_AS - PyModule_AddIntMacro(m, RLIMIT_AS); + PyModule_AddIntMacro(module, RLIMIT_AS); #endif #ifdef RLIMIT_RSS - PyModule_AddIntMacro(m, RLIMIT_RSS); + PyModule_AddIntMacro(module, RLIMIT_RSS); #endif #ifdef RLIMIT_NPROC - PyModule_AddIntMacro(m, RLIMIT_NPROC); + PyModule_AddIntMacro(module, RLIMIT_NPROC); #endif #ifdef RLIMIT_MEMLOCK - PyModule_AddIntMacro(m, RLIMIT_MEMLOCK); + PyModule_AddIntMacro(module, RLIMIT_MEMLOCK); #endif #ifdef RLIMIT_SBSIZE - PyModule_AddIntMacro(m, RLIMIT_SBSIZE); + PyModule_AddIntMacro(module, RLIMIT_SBSIZE); #endif /* Linux specific */ #ifdef RLIMIT_MSGQUEUE - PyModule_AddIntMacro(m, RLIMIT_MSGQUEUE); + PyModule_AddIntMacro(module, RLIMIT_MSGQUEUE); #endif #ifdef RLIMIT_NICE - PyModule_AddIntMacro(m, RLIMIT_NICE); + PyModule_AddIntMacro(module, RLIMIT_NICE); #endif #ifdef RLIMIT_RTPRIO - PyModule_AddIntMacro(m, RLIMIT_RTPRIO); + PyModule_AddIntMacro(module, RLIMIT_RTPRIO); #endif #ifdef RLIMIT_RTTIME - PyModule_AddIntMacro(m, RLIMIT_RTTIME); + PyModule_AddIntMacro(module, RLIMIT_RTTIME); #endif #ifdef RLIMIT_SIGPENDING - PyModule_AddIntMacro(m, RLIMIT_SIGPENDING); + PyModule_AddIntMacro(module, RLIMIT_SIGPENDING); #endif /* target */ #ifdef RUSAGE_SELF - PyModule_AddIntMacro(m, RUSAGE_SELF); + PyModule_AddIntMacro(module, RUSAGE_SELF); #endif #ifdef RUSAGE_CHILDREN - PyModule_AddIntMacro(m, RUSAGE_CHILDREN); + PyModule_AddIntMacro(module, RUSAGE_CHILDREN); #endif #ifdef RUSAGE_BOTH - PyModule_AddIntMacro(m, RUSAGE_BOTH); + PyModule_AddIntMacro(module, RUSAGE_BOTH); #endif #ifdef RUSAGE_THREAD - PyModule_AddIntMacro(m, RUSAGE_THREAD); + PyModule_AddIntMacro(module, RUSAGE_THREAD); #endif /* FreeBSD specific */ #ifdef RLIMIT_SWAP - PyModule_AddIntMacro(m, RLIMIT_SWAP); + PyModule_AddIntMacro(module, RLIMIT_SWAP); #endif #ifdef RLIMIT_SBSIZE - PyModule_AddIntMacro(m, RLIMIT_SBSIZE); + PyModule_AddIntMacro(module, RLIMIT_SBSIZE); #endif #ifdef RLIMIT_NPTS - PyModule_AddIntMacro(m, RLIMIT_NPTS); + PyModule_AddIntMacro(module, RLIMIT_NPTS); #endif if (sizeof(RLIM_INFINITY) > sizeof(long)) { @@ -487,8 +475,31 @@ PyInit_resource(void) v = PyLong_FromLong((long) RLIM_INFINITY); } if (v) { - PyModule_AddObject(m, "RLIM_INFINITY", v); + PyModule_AddObject(module, "RLIM_INFINITY", v); } initialized = 1; - return m; + return 0; +} + +static struct PyModuleDef_Slot resource_slots[] = { + {Py_mod_exec, resource_exec}, + {0, NULL} +}; + +static struct PyModuleDef resourcemodule = { + PyModuleDef_HEAD_INIT, + "resource", + NULL, + 0, + resource_methods, + resource_slots, + NULL, + NULL, + NULL +}; + +PyMODINIT_FUNC +PyInit_resource(void) +{ + return PyModuleDef_Init(&resourcemodule); } From 6b46c86f1d32a142f2da84e8db97276abc8a5af5 Mon Sep 17 00:00:00 2001 From: Hai Shi Date: Wed, 1 Apr 2020 01:09:51 +0800 Subject: [PATCH 2/8] update code --- .../2020-04-01-00-08-18.bpo-1635741.bhGWam.rst | 2 +- Modules/resource.c | 17 +++++------------ 2 files changed, 6 insertions(+), 13 deletions(-) diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-04-01-00-08-18.bpo-1635741.bhGWam.rst b/Misc/NEWS.d/next/Core and Builtins/2020-04-01-00-08-18.bpo-1635741.bhGWam.rst index 0dce86b3710b48..cacfed2f9fdb57 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2020-04-01-00-08-18.bpo-1635741.bhGWam.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2020-04-01-00-08-18.bpo-1635741.bhGWam.rst @@ -1 +1 @@ -Port resource extension module to multiphase initialization (:pep:`489`). +Port :mod:`resource` to multiphase initialization (:pep:`489`). diff --git a/Modules/resource.c b/Modules/resource.c index 12cb548731eb6c..dca5d68cad4ffd 100644 --- a/Modules/resource.c +++ b/Modules/resource.c @@ -356,10 +356,7 @@ static int resource_exec(PyObject *module) return -1; } - Py_INCREF(&StructRUsageType); - if (PyModule_AddObject(module, "struct_rusage", - (PyObject*) &StructRUsageType) < 0) { - Py_DECREF(&StructRUsageType); + if(PyModule_AddType(module, &StructRUsageType) < 0) { return -1; } @@ -488,14 +485,10 @@ static struct PyModuleDef_Slot resource_slots[] = { static struct PyModuleDef resourcemodule = { PyModuleDef_HEAD_INIT, - "resource", - NULL, - 0, - resource_methods, - resource_slots, - NULL, - NULL, - NULL + .m_name = "resource", + .m_size = 0, + .m_methods = resource_methods, + .m_slots = resource_slots, }; PyMODINIT_FUNC From 90504f75e463221e6898852b98deb9605a930dbd Mon Sep 17 00:00:00 2001 From: Hai Shi Date: Thu, 2 Apr 2020 00:09:09 +0800 Subject: [PATCH 3/8] fix some potential refleaks --- Modules/resource.c | 64 +++++++++++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 26 deletions(-) diff --git a/Modules/resource.c b/Modules/resource.c index dca5d68cad4ffd..71bd40d6b485a2 100644 --- a/Modules/resource.c +++ b/Modules/resource.c @@ -15,6 +15,13 @@ #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) +#define ADD_INT(module, value) \ + do { \ + if (PyModule_AddIntMacro(module, value) < 0) { \ + return -1; \ + } \ + } while (0) \ + /*[clinic input] module resource [clinic start generated code]*/ @@ -362,107 +369,107 @@ static int resource_exec(PyObject *module) /* insert constants */ #ifdef RLIMIT_CPU - PyModule_AddIntMacro(module, RLIMIT_CPU); + ADD_INT(module, RLIMIT_CPU); #endif #ifdef RLIMIT_FSIZE - PyModule_AddIntMacro(module, RLIMIT_FSIZE); + ADD_INT(module, RLIMIT_FSIZE); #endif #ifdef RLIMIT_DATA - PyModule_AddIntMacro(module, RLIMIT_DATA); + ADD_INT(module, RLIMIT_DATA); #endif #ifdef RLIMIT_STACK - PyModule_AddIntMacro(module, RLIMIT_STACK); + ADD_INT(module, RLIMIT_STACK); #endif #ifdef RLIMIT_CORE - PyModule_AddIntMacro(module, RLIMIT_CORE); + ADD_INT(module, RLIMIT_CORE); #endif #ifdef RLIMIT_NOFILE - PyModule_AddIntMacro(module, RLIMIT_NOFILE); + ADD_INT(module, RLIMIT_NOFILE); #endif #ifdef RLIMIT_OFILE - PyModule_AddIntMacro(module, RLIMIT_OFILE); + ADD_INT(module, RLIMIT_OFILE); #endif #ifdef RLIMIT_VMEM - PyModule_AddIntMacro(module, RLIMIT_VMEM); + ADD_INT(module, RLIMIT_VMEM); #endif #ifdef RLIMIT_AS - PyModule_AddIntMacro(module, RLIMIT_AS); + ADD_INT(module, RLIMIT_AS); #endif #ifdef RLIMIT_RSS - PyModule_AddIntMacro(module, RLIMIT_RSS); + ADD_INT(module, RLIMIT_RSS); #endif #ifdef RLIMIT_NPROC - PyModule_AddIntMacro(module, RLIMIT_NPROC); + ADD_INT(module, RLIMIT_NPROC); #endif #ifdef RLIMIT_MEMLOCK - PyModule_AddIntMacro(module, RLIMIT_MEMLOCK); + ADD_INT(module, RLIMIT_MEMLOCK); #endif #ifdef RLIMIT_SBSIZE - PyModule_AddIntMacro(module, RLIMIT_SBSIZE); + ADD_INT(module, RLIMIT_SBSIZE); #endif /* Linux specific */ #ifdef RLIMIT_MSGQUEUE - PyModule_AddIntMacro(module, RLIMIT_MSGQUEUE); + ADD_INT(module, RLIMIT_MSGQUEUE); #endif #ifdef RLIMIT_NICE - PyModule_AddIntMacro(module, RLIMIT_NICE); + ADD_INT(module, RLIMIT_NICE); #endif #ifdef RLIMIT_RTPRIO - PyModule_AddIntMacro(module, RLIMIT_RTPRIO); + ADD_INT(module, RLIMIT_RTPRIO); #endif #ifdef RLIMIT_RTTIME - PyModule_AddIntMacro(module, RLIMIT_RTTIME); + ADD_INT(module, RLIMIT_RTTIME); #endif #ifdef RLIMIT_SIGPENDING - PyModule_AddIntMacro(module, RLIMIT_SIGPENDING); + ADD_INT(module, RLIMIT_SIGPENDING); #endif /* target */ #ifdef RUSAGE_SELF - PyModule_AddIntMacro(module, RUSAGE_SELF); + ADD_INT(module, RUSAGE_SELF); #endif #ifdef RUSAGE_CHILDREN - PyModule_AddIntMacro(module, RUSAGE_CHILDREN); + ADD_INT(module, RUSAGE_CHILDREN); #endif #ifdef RUSAGE_BOTH - PyModule_AddIntMacro(module, RUSAGE_BOTH); + ADD_INT(module, RUSAGE_BOTH); #endif #ifdef RUSAGE_THREAD - PyModule_AddIntMacro(module, RUSAGE_THREAD); + ADD_INT(module, RUSAGE_THREAD); #endif /* FreeBSD specific */ #ifdef RLIMIT_SWAP - PyModule_AddIntMacro(module, RLIMIT_SWAP); + ADD_INT(module, RLIMIT_SWAP); #endif #ifdef RLIMIT_SBSIZE - PyModule_AddIntMacro(module, RLIMIT_SBSIZE); + ADD_INT(module, RLIMIT_SBSIZE); #endif #ifdef RLIMIT_NPTS - PyModule_AddIntMacro(module, RLIMIT_NPTS); + ADD_INT(module, RLIMIT_NPTS); #endif if (sizeof(RLIM_INFINITY) > sizeof(long)) { @@ -472,7 +479,12 @@ static int resource_exec(PyObject *module) v = PyLong_FromLong((long) RLIM_INFINITY); } if (v) { - PyModule_AddObject(module, "RLIM_INFINITY", v); + if (PyModule_AddObject(module, "RLIM_INFINITY", v) < 0) { + Py_DECREF(v); + return -1; + } + } else { + return -1; } initialized = 1; return 0; From 7a8297697712c364abac3433b2ff8a84ef7d0bd6 Mon Sep 17 00:00:00 2001 From: Hai Shi Date: Thu, 2 Apr 2020 13:16:37 +0800 Subject: [PATCH 4/8] Simplify the code --- Modules/resource.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/Modules/resource.c b/Modules/resource.c index 71bd40d6b485a2..d2af92bbae4869 100644 --- a/Modules/resource.c +++ b/Modules/resource.c @@ -349,6 +349,13 @@ resource_methods[] = { static int resource_exec(PyObject *module) { +#define ADD_INT(module, value) \ + do { \ + if (PyModule_AddIntMacro(module, value) < 0) { \ + return -1; \ + } \ + } while (0) \ + PyObject *v; /* Add some symbolic constants to the module */ @@ -478,16 +485,20 @@ static int resource_exec(PyObject *module) { v = PyLong_FromLong((long) RLIM_INFINITY); } - if (v) { - if (PyModule_AddObject(module, "RLIM_INFINITY", v) < 0) { - Py_DECREF(v); - return -1; - } - } else { + + if (!v) { return -1; } + + if (PyModule_AddObject(module, "RLIM_INFINITY", v) < 0) { + Py_DECREF(v); + return -1; + } + initialized = 1; return 0; + +#undef ADD_INT } static struct PyModuleDef_Slot resource_slots[] = { From a0dcd68a9f428266284be1edc2fc84fb73069d9e Mon Sep 17 00:00:00 2001 From: Hai Shi Date: Thu, 2 Apr 2020 13:20:07 +0800 Subject: [PATCH 5/8] remove file's ADD_INT macro --- Modules/resource.c | 7 ------- 1 file changed, 7 deletions(-) diff --git a/Modules/resource.c b/Modules/resource.c index d2af92bbae4869..dcd5006eadc53e 100644 --- a/Modules/resource.c +++ b/Modules/resource.c @@ -15,13 +15,6 @@ #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) -#define ADD_INT(module, value) \ - do { \ - if (PyModule_AddIntMacro(module, value) < 0) { \ - return -1; \ - } \ - } while (0) \ - /*[clinic input] module resource [clinic start generated code]*/ From 7add9ba111e66f65f850311c9ba5a3fad71c1942 Mon Sep 17 00:00:00 2001 From: Hai Shi Date: Thu, 2 Apr 2020 19:29:42 +0800 Subject: [PATCH 6/8] Use PyModule_AddIntConstant to replace PyModule_AddIntMacro --- Modules/resource.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Modules/resource.c b/Modules/resource.c index dcd5006eadc53e..b420a3b6eb6e6e 100644 --- a/Modules/resource.c +++ b/Modules/resource.c @@ -342,12 +342,12 @@ resource_methods[] = { static int resource_exec(PyObject *module) { -#define ADD_INT(module, value) \ - do { \ - if (PyModule_AddIntMacro(module, value) < 0) { \ - return -1; \ - } \ - } while (0) \ +#define ADD_INT(module, value) \ + do { \ + if (PyModule_AddIntConstant(module, #value, value) < 0) { \ + return -1; \ + } \ + } while (0) PyObject *v; From 41d6bf83c49e8c7a49082f751d1eb2535f221216 Mon Sep 17 00:00:00 2001 From: Hai Shi Date: Thu, 2 Apr 2020 19:34:37 +0800 Subject: [PATCH 7/8] move position of claim of v --- Modules/resource.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Modules/resource.c b/Modules/resource.c index b420a3b6eb6e6e..b375861a78b1fc 100644 --- a/Modules/resource.c +++ b/Modules/resource.c @@ -349,8 +349,6 @@ static int resource_exec(PyObject *module) } \ } while (0) - PyObject *v; - /* Add some symbolic constants to the module */ Py_INCREF(PyExc_OSError); if (PyModule_AddObject(module, "error", PyExc_OSError) < 0) { @@ -472,6 +470,8 @@ static int resource_exec(PyObject *module) ADD_INT(module, RLIMIT_NPTS); #endif + PyObject *v; + if (sizeof(RLIM_INFINITY) > sizeof(long)) { v = PyLong_FromLongLong((long long) RLIM_INFINITY); } else From 6fdb19aecf32b10d43aeb8778617afdabcbff8fc Mon Sep 17 00:00:00 2001 From: Hai Shi Date: Thu, 2 Apr 2020 20:05:26 +0800 Subject: [PATCH 8/8] Fix style issues --- Modules/resource.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Modules/resource.c b/Modules/resource.c index b375861a78b1fc..ddbf80be9c69eb 100644 --- a/Modules/resource.c +++ b/Modules/resource.c @@ -340,7 +340,8 @@ resource_methods[] = { /* Module initialization */ -static int resource_exec(PyObject *module) +static int +resource_exec(PyObject *module) { #define ADD_INT(module, value) \ do { \ @@ -471,14 +472,12 @@ static int resource_exec(PyObject *module) #endif PyObject *v; - if (sizeof(RLIM_INFINITY) > sizeof(long)) { v = PyLong_FromLongLong((long long) RLIM_INFINITY); } else { v = PyLong_FromLong((long) RLIM_INFINITY); } - if (!v) { return -1; } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy