From a848eaa08929128c0fe00907747f1cce477ecadd Mon Sep 17 00:00:00 2001 From: AN Long Date: Tue, 4 Apr 2023 21:45:31 +0800 Subject: [PATCH 1/7] gh-103092: isolate msvcrt --- ...-04-04-21-44-25.gh-issue-103092.Dz0_Xn.rst | 1 + PC/msvcrtmodule.c | 48 +++++++++++-------- 2 files changed, 29 insertions(+), 20 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2023-04-04-21-44-25.gh-issue-103092.Dz0_Xn.rst diff --git a/Misc/NEWS.d/next/Library/2023-04-04-21-44-25.gh-issue-103092.Dz0_Xn.rst b/Misc/NEWS.d/next/Library/2023-04-04-21-44-25.gh-issue-103092.Dz0_Xn.rst new file mode 100644 index 00000000000000..a5ba874136f64c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-04-04-21-44-25.gh-issue-103092.Dz0_Xn.rst @@ -0,0 +1 @@ +Adapt :mod:`!msvcrt` to :pep:`687`. diff --git a/PC/msvcrtmodule.c b/PC/msvcrtmodule.c index face4d03af9d4f..9e8a74eb354ae3 100644 --- a/PC/msvcrtmodule.c +++ b/PC/msvcrtmodule.c @@ -564,19 +564,6 @@ static struct PyMethodDef msvcrt_functions[] = { {NULL, NULL} }; - -static struct PyModuleDef msvcrtmodule = { - PyModuleDef_HEAD_INIT, - "msvcrt", - NULL, - -1, - msvcrt_functions, - NULL, - NULL, - NULL, - NULL -}; - static void insertint(PyObject *d, char *name, int value) { @@ -605,14 +592,11 @@ insertptr(PyObject *d, char *name, void *value) } } -PyMODINIT_FUNC -PyInit_msvcrt(void) +static int +exec_module(PyObject* m) { int st; PyObject *d, *version; - PyObject *m = PyModule_Create(&msvcrtmodule); - if (m == NULL) - return NULL; d = PyModule_GetDict(m); /* constants for the locking() function's mode argument */ @@ -664,10 +648,34 @@ PyInit_msvcrt(void) _VC_CRT_BUILD_VERSION, _VC_CRT_RBUILD_VERSION); st = PyModule_AddObject(m, "CRT_ASSEMBLY_VERSION", version); - if (st < 0) return NULL; + if (st < 0) return -1; #endif /* make compiler warning quiet if st is unused */ (void)st; - return m; + return 0; + +} + +static PyModuleDef_Slot msvcrt_slots[] = { + {Py_mod_exec, exec_module}, + {0, NULL} +}; + +static struct PyModuleDef msvcrtmodule = { + PyModuleDef_HEAD_INIT, + "msvcrt", + NULL, + 0, + msvcrt_functions, + msvcrt_slots, + NULL, + NULL, + NULL +}; + +PyMODINIT_FUNC +PyInit_msvcrt(void) +{ + return PyModuleDef_Init(&msvcrtmodule); } From bc8aa95924e2b725296bf4326c43f8339db0bb9c Mon Sep 17 00:00:00 2001 From: AN Long Date: Thu, 6 Apr 2023 15:24:42 +0800 Subject: [PATCH 2/7] Update PC/msvcrtmodule.c Co-authored-by: Oleg Iarygin --- PC/msvcrtmodule.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/PC/msvcrtmodule.c b/PC/msvcrtmodule.c index 9e8a74eb354ae3..93f48971d87611 100644 --- a/PC/msvcrtmodule.c +++ b/PC/msvcrtmodule.c @@ -663,15 +663,10 @@ static PyModuleDef_Slot msvcrt_slots[] = { }; static struct PyModuleDef msvcrtmodule = { - PyModuleDef_HEAD_INIT, - "msvcrt", - NULL, - 0, - msvcrt_functions, - msvcrt_slots, - NULL, - NULL, - NULL + .m_base = PyModuleDef_HEAD_INIT, + .m_name = "msvcrt", + .m_methods = msvcrt_functions, + .m_slots = msvcrt_slots }; PyMODINIT_FUNC From 8dd469f5854ad4e37aee43f5e077e046e7fec162 Mon Sep 17 00:00:00 2001 From: AN Long Date: Sun, 9 Apr 2023 13:05:40 +0800 Subject: [PATCH 3/7] Update Misc/NEWS.d/next/Library/2023-04-04-21-44-25.gh-issue-103092.Dz0_Xn.rst Co-authored-by: Erlend E. Aasland --- .../next/Library/2023-04-04-21-44-25.gh-issue-103092.Dz0_Xn.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2023-04-04-21-44-25.gh-issue-103092.Dz0_Xn.rst b/Misc/NEWS.d/next/Library/2023-04-04-21-44-25.gh-issue-103092.Dz0_Xn.rst index a5ba874136f64c..7bd191e3c22b2b 100644 --- a/Misc/NEWS.d/next/Library/2023-04-04-21-44-25.gh-issue-103092.Dz0_Xn.rst +++ b/Misc/NEWS.d/next/Library/2023-04-04-21-44-25.gh-issue-103092.Dz0_Xn.rst @@ -1 +1 @@ -Adapt :mod:`!msvcrt` to :pep:`687`. +Adapt the :mod:`msvcrt` extension module to :pep:`687`. From cdc37b7935bcfdb5d450ce2850965d24ec568db3 Mon Sep 17 00:00:00 2001 From: AN Long Date: Sun, 9 Apr 2023 13:05:46 +0800 Subject: [PATCH 4/7] Update PC/msvcrtmodule.c Co-authored-by: Erlend E. Aasland --- PC/msvcrtmodule.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PC/msvcrtmodule.c b/PC/msvcrtmodule.c index 93f48971d87611..38074c82a808c9 100644 --- a/PC/msvcrtmodule.c +++ b/PC/msvcrtmodule.c @@ -666,7 +666,7 @@ static struct PyModuleDef msvcrtmodule = { .m_base = PyModuleDef_HEAD_INIT, .m_name = "msvcrt", .m_methods = msvcrt_functions, - .m_slots = msvcrt_slots + .m_slots = msvcrt_slots, }; PyMODINIT_FUNC From 6119c5bdd9b797b29ad85ae83a8903a300dc9cff Mon Sep 17 00:00:00 2001 From: AN Long Date: Sun, 9 Apr 2023 13:07:24 +0800 Subject: [PATCH 5/7] Update PC/msvcrtmodule.c Co-authored-by: Erlend E. Aasland --- PC/msvcrtmodule.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/PC/msvcrtmodule.c b/PC/msvcrtmodule.c index 38074c82a808c9..ace2a91cb7991b 100644 --- a/PC/msvcrtmodule.c +++ b/PC/msvcrtmodule.c @@ -648,7 +648,9 @@ exec_module(PyObject* m) _VC_CRT_BUILD_VERSION, _VC_CRT_RBUILD_VERSION); st = PyModule_AddObject(m, "CRT_ASSEMBLY_VERSION", version); - if (st < 0) return -1; + if (st < 0) { + return -1; + } #endif /* make compiler warning quiet if st is unused */ (void)st; From b3f5edbb0287af6157468d2f6c9371fee80db5bf Mon Sep 17 00:00:00 2001 From: AN Long Date: Sun, 9 Apr 2023 13:11:13 +0800 Subject: [PATCH 6/7] fix error branch in exec_module --- PC/msvcrtmodule.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/PC/msvcrtmodule.c b/PC/msvcrtmodule.c index ace2a91cb7991b..3738f5f8c50c81 100644 --- a/PC/msvcrtmodule.c +++ b/PC/msvcrtmodule.c @@ -628,17 +628,23 @@ exec_module(PyObject* m) #ifdef _VC_ASSEMBLY_PUBLICKEYTOKEN st = PyModule_AddStringConstant(m, "VC_ASSEMBLY_PUBLICKEYTOKEN", _VC_ASSEMBLY_PUBLICKEYTOKEN); - if (st < 0) return NULL; + if (st < 0) { + return -1; +} #endif #ifdef _CRT_ASSEMBLY_VERSION st = PyModule_AddStringConstant(m, "CRT_ASSEMBLY_VERSION", _CRT_ASSEMBLY_VERSION); - if (st < 0) return NULL; + if (st < 0) { + return -1; + } #endif #ifdef __LIBRARIES_ASSEMBLY_NAME_PREFIX st = PyModule_AddStringConstant(m, "LIBRARIES_ASSEMBLY_NAME_PREFIX", __LIBRARIES_ASSEMBLY_NAME_PREFIX); - if (st < 0) return NULL; + if (st < 0) { + return -1; + } #endif /* constants for the 2010 crt versions */ From 49d91e1a43a3b96748bd5f72f56538bcfd0cb622 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Mon, 10 Apr 2023 21:30:59 +0200 Subject: [PATCH 7/7] Fix merge --- PC/msvcrtmodule.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/PC/msvcrtmodule.c b/PC/msvcrtmodule.c index e9c743934ce6c9..de9a88946aff3e 100644 --- a/PC/msvcrtmodule.c +++ b/PC/msvcrtmodule.c @@ -596,10 +596,6 @@ static int exec_module(PyObject* m) { int st; - PyObject *m = PyModule_Create(&msvcrtmodule); - if (m == NULL) { - return NULL; - } PyObject *d = PyModule_GetDict(m); // Borrowed ref. /* constants for the locking() function's mode argument */