Skip to content

Commit dce2d38

Browse files
authored
gh-103092: Isolate msvcrt (#103248)
1 parent f2b7ecb commit dce2d38

File tree

2 files changed

+26
-28
lines changed

2 files changed

+26
-28
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Adapt the :mod:`msvcrt` extension module to :pep:`687`.

PC/msvcrtmodule.c

+25-28
Original file line numberDiff line numberDiff line change
@@ -564,19 +564,6 @@ static struct PyMethodDef msvcrt_functions[] = {
564564
{NULL, NULL}
565565
};
566566

567-
568-
static struct PyModuleDef msvcrtmodule = {
569-
PyModuleDef_HEAD_INIT,
570-
"msvcrt",
571-
NULL,
572-
-1,
573-
msvcrt_functions,
574-
NULL,
575-
NULL,
576-
NULL,
577-
NULL
578-
};
579-
580567
static void
581568
insertint(PyObject *d, char *name, int value)
582569
{
@@ -605,14 +592,10 @@ insertptr(PyObject *d, char *name, void *value)
605592
}
606593
}
607594

608-
PyMODINIT_FUNC
609-
PyInit_msvcrt(void)
595+
static int
596+
exec_module(PyObject* m)
610597
{
611598
int st;
612-
PyObject *m = PyModule_Create(&msvcrtmodule);
613-
if (m == NULL) {
614-
return NULL;
615-
}
616599
PyObject *d = PyModule_GetDict(m); // Borrowed ref.
617600

618601
/* constants for the locking() function's mode argument */
@@ -645,21 +628,21 @@ PyInit_msvcrt(void)
645628
st = PyModule_AddStringConstant(m, "VC_ASSEMBLY_PUBLICKEYTOKEN",
646629
_VC_ASSEMBLY_PUBLICKEYTOKEN);
647630
if (st < 0) {
648-
goto error;
631+
return -1;
649632
}
650633
#endif
651634
#ifdef _CRT_ASSEMBLY_VERSION
652635
st = PyModule_AddStringConstant(m, "CRT_ASSEMBLY_VERSION",
653636
_CRT_ASSEMBLY_VERSION);
654637
if (st < 0) {
655-
goto error;
638+
return -1;
656639
}
657640
#endif
658641
#ifdef __LIBRARIES_ASSEMBLY_NAME_PREFIX
659642
st = PyModule_AddStringConstant(m, "LIBRARIES_ASSEMBLY_NAME_PREFIX",
660643
__LIBRARIES_ASSEMBLY_NAME_PREFIX);
661644
if (st < 0) {
662-
goto error;
645+
return -1;
663646
}
664647
#endif
665648

@@ -671,20 +654,34 @@ PyInit_msvcrt(void)
671654
_VC_CRT_BUILD_VERSION,
672655
_VC_CRT_RBUILD_VERSION);
673656
if (version == NULL) {
674-
goto error;
657+
return -1;
675658
}
676659
st = PyModule_AddObjectRef(m, "CRT_ASSEMBLY_VERSION", version);
677660
Py_DECREF(version);
678661
if (st < 0) {
679-
goto error;
662+
return -1;
680663
}
681664
#endif
682665
/* make compiler warning quiet if st is unused */
683666
(void)st;
684667

685-
return m;
668+
return 0;
669+
}
670+
671+
static PyModuleDef_Slot msvcrt_slots[] = {
672+
{Py_mod_exec, exec_module},
673+
{0, NULL}
674+
};
686675

687-
error:
688-
Py_DECREF(m);
689-
return NULL;
676+
static struct PyModuleDef msvcrtmodule = {
677+
.m_base = PyModuleDef_HEAD_INIT,
678+
.m_name = "msvcrt",
679+
.m_methods = msvcrt_functions,
680+
.m_slots = msvcrt_slots,
681+
};
682+
683+
PyMODINIT_FUNC
684+
PyInit_msvcrt(void)
685+
{
686+
return PyModuleDef_Init(&msvcrtmodule);
690687
}

0 commit comments

Comments
 (0)