[Python-checkins] gh-103092: Isolate msvcrt (#103248)

erlend-aasland webhook-mailer at python.org
Wed Apr 12 06:41:58 EDT 2023


https://github.com/python/cpython/commit/dce2d38cb04b541bad477ccc1040a68fa70a9a69
commit: dce2d38cb04b541bad477ccc1040a68fa70a9a69
branch: main
author: AN Long <aisk at users.noreply.github.com>
committer: erlend-aasland <erlend.aasland at protonmail.com>
date: 2023-04-12T12:41:21+02:00
summary:

gh-103092: Isolate msvcrt (#103248)

files:
A Misc/NEWS.d/next/Library/2023-04-04-21-44-25.gh-issue-103092.Dz0_Xn.rst
M PC/msvcrtmodule.c

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 000000000000..7bd191e3c22b
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2023-04-04-21-44-25.gh-issue-103092.Dz0_Xn.rst
@@ -0,0 +1 @@
+Adapt the :mod:`msvcrt` extension module to :pep:`687`.
diff --git a/PC/msvcrtmodule.c b/PC/msvcrtmodule.c
index 6e8b423c3839..de9a88946aff 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,10 @@ insertptr(PyObject *d, char *name, void *value)
     }
 }
 
-PyMODINIT_FUNC
-PyInit_msvcrt(void)
+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 */
@@ -645,21 +628,21 @@ PyInit_msvcrt(void)
     st = PyModule_AddStringConstant(m, "VC_ASSEMBLY_PUBLICKEYTOKEN",
                                     _VC_ASSEMBLY_PUBLICKEYTOKEN);
     if (st < 0) {
-        goto error;
+        return -1;
     }
 #endif
 #ifdef _CRT_ASSEMBLY_VERSION
     st = PyModule_AddStringConstant(m, "CRT_ASSEMBLY_VERSION",
                                     _CRT_ASSEMBLY_VERSION);
     if (st < 0) {
-        goto error;
+        return -1;
     }
 #endif
 #ifdef __LIBRARIES_ASSEMBLY_NAME_PREFIX
     st = PyModule_AddStringConstant(m, "LIBRARIES_ASSEMBLY_NAME_PREFIX",
                                     __LIBRARIES_ASSEMBLY_NAME_PREFIX);
     if (st < 0) {
-        goto error;
+        return -1;
     }
 #endif
 
@@ -671,20 +654,34 @@ PyInit_msvcrt(void)
                                              _VC_CRT_BUILD_VERSION,
                                              _VC_CRT_RBUILD_VERSION);
     if (version == NULL) {
-        goto error;
+        return -1;
     }
     st = PyModule_AddObjectRef(m, "CRT_ASSEMBLY_VERSION", version);
     Py_DECREF(version);
     if (st < 0) {
-        goto error;
+        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}
+};
 
-error:
-    Py_DECREF(m);
-    return NULL;
+static struct PyModuleDef msvcrtmodule = {
+    .m_base = PyModuleDef_HEAD_INIT,
+    .m_name = "msvcrt",
+    .m_methods = msvcrt_functions,
+    .m_slots = msvcrt_slots,
+};
+
+PyMODINIT_FUNC
+PyInit_msvcrt(void)
+{
+    return PyModuleDef_Init(&msvcrtmodule);
 }



More information about the Python-checkins mailing list