Skip to content

Commit 0d534df

Browse files
committed
bpo-1635741: Port _stat module to multiphase initialization
1 parent 6d65087 commit 0d534df

File tree

2 files changed

+249
-100
lines changed

2 files changed

+249
-100
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Port _stat module to multiphase initialization (:pep:`489`).

Modules/_stat.c

+248-100
Original file line numberDiff line numberDiff line change
@@ -492,113 +492,261 @@ ST_CTIME\n\
492492
");
493493

494494

495+
static int
496+
stat_exec(PyObject *module)
497+
{
498+
if (PyModule_AddIntMacro(module, S_IFDIR) < 0) {
499+
return -1;
500+
}
501+
if (PyModule_AddIntMacro(module, S_IFCHR) < 0) {
502+
return -1;
503+
}
504+
if (PyModule_AddIntMacro(module, S_IFBLK) < 0) {
505+
return -1;
506+
}
507+
if (PyModule_AddIntMacro(module, S_IFREG) < 0) {
508+
return -1;
509+
}
510+
if (PyModule_AddIntMacro(module, S_IFIFO) < 0) {
511+
return -1;
512+
}
513+
if (PyModule_AddIntMacro(module, S_IFLNK) < 0) {
514+
return -1;
515+
}
516+
if (PyModule_AddIntMacro(module, S_IFSOCK) < 0) {
517+
return -1;
518+
}
519+
if (PyModule_AddIntMacro(module, S_IFDOOR) < 0) {
520+
return -1;
521+
}
522+
if (PyModule_AddIntMacro(module, S_IFPORT) < 0) {
523+
return -1;
524+
}
525+
if (PyModule_AddIntMacro(module, S_IFWHT) < 0) {
526+
return -1;
527+
}
528+
529+
if (PyModule_AddIntMacro(module, S_ISUID) < 0) {
530+
return -1;
531+
}
532+
if (PyModule_AddIntMacro(module, S_ISGID) < 0) {
533+
return -1;
534+
}
535+
if (PyModule_AddIntMacro(module, S_ISVTX) < 0) {
536+
return -1;
537+
}
538+
if (PyModule_AddIntMacro(module, S_ENFMT) < 0) {
539+
return -1;
540+
}
541+
542+
if (PyModule_AddIntMacro(module, S_IREAD) < 0) {
543+
return -1;
544+
}
545+
if (PyModule_AddIntMacro(module, S_IWRITE) < 0) {
546+
return -1;
547+
}
548+
if (PyModule_AddIntMacro(module, S_IEXEC) < 0) {
549+
return -1;
550+
}
551+
552+
if (PyModule_AddIntMacro(module, S_IRWXU) < 0) {
553+
return -1;
554+
}
555+
if (PyModule_AddIntMacro(module, S_IRUSR) < 0) {
556+
return -1;
557+
}
558+
if (PyModule_AddIntMacro(module, S_IWUSR) < 0) {
559+
return -1;
560+
}
561+
if (PyModule_AddIntMacro(module, S_IXUSR) < 0) {
562+
return -1;
563+
}
564+
565+
if (PyModule_AddIntMacro(module, S_IRWXG) < 0) {
566+
return -1;
567+
}
568+
if (PyModule_AddIntMacro(module, S_IRGRP) < 0) {
569+
return -1;
570+
}
571+
if (PyModule_AddIntMacro(module, S_IWGRP) < 0) {
572+
return -1;
573+
}
574+
if (PyModule_AddIntMacro(module, S_IXGRP) < 0) {
575+
return -1;
576+
}
577+
578+
if (PyModule_AddIntMacro(module, S_IRWXO) < 0) {
579+
return -1;
580+
}
581+
if (PyModule_AddIntMacro(module, S_IROTH) < 0) {
582+
return -1;
583+
}
584+
if (PyModule_AddIntMacro(module, S_IWOTH) < 0) {
585+
return -1;
586+
}
587+
if (PyModule_AddIntMacro(module, S_IXOTH) < 0) {
588+
return -1;
589+
}
590+
591+
if (PyModule_AddIntMacro(module, UF_NODUMP) < 0) {
592+
return -1;
593+
}
594+
if (PyModule_AddIntMacro(module, UF_IMMUTABLE) < 0) {
595+
return -1;
596+
}
597+
if (PyModule_AddIntMacro(module, UF_APPEND) < 0) {
598+
return -1;
599+
}
600+
if (PyModule_AddIntMacro(module, UF_OPAQUE) < 0) {
601+
return -1;
602+
}
603+
if (PyModule_AddIntMacro(module, UF_NOUNLINK) < 0) {
604+
return -1;
605+
}
606+
if (PyModule_AddIntMacro(module, UF_COMPRESSED) < 0) {
607+
return -1;
608+
}
609+
if (PyModule_AddIntMacro(module, UF_HIDDEN) < 0) {
610+
return -1;
611+
}
612+
if (PyModule_AddIntMacro(module, SF_ARCHIVED) < 0) {
613+
return -1;
614+
}
615+
if (PyModule_AddIntMacro(module, SF_IMMUTABLE) < 0) {
616+
return -1;
617+
}
618+
if (PyModule_AddIntMacro(module, SF_APPEND) < 0) {
619+
return -1;
620+
}
621+
if (PyModule_AddIntMacro(module, SF_NOUNLINK) < 0) {
622+
return -1;
623+
}
624+
if (PyModule_AddIntMacro(module, SF_SNAPSHOT) < 0) {
625+
return -1;
626+
}
627+
628+
if (PyModule_AddIntConstant(module, "ST_MODE", 0) < 0) {
629+
return -1;
630+
}
631+
if (PyModule_AddIntConstant(module, "ST_INO", 1) < 0) {
632+
return -1;
633+
}
634+
if (PyModule_AddIntConstant(module, "ST_DEV", 2) < 0) {
635+
return -1;
636+
}
637+
if (PyModule_AddIntConstant(module, "ST_NLINK", 3) < 0) {
638+
return -1;
639+
}
640+
if (PyModule_AddIntConstant(module, "ST_UID", 4) < 0) {
641+
return -1;
642+
}
643+
if (PyModule_AddIntConstant(module, "ST_GID", 5) < 0) {
644+
return -1;
645+
}
646+
if (PyModule_AddIntConstant(module, "ST_SIZE", 6) < 0) {
647+
return -1;
648+
}
649+
if (PyModule_AddIntConstant(module, "ST_ATIME", 7) < 0) {
650+
return -1;
651+
}
652+
if (PyModule_AddIntConstant(module, "ST_MTIME", 8) < 0) {
653+
return -1;
654+
}
655+
if (PyModule_AddIntConstant(module, "ST_CTIME", 9) < 0) {
656+
return -1;
657+
}
658+
659+
#ifdef MS_WINDOWS
660+
if (PyModule_AddIntMacro(module, FILE_ATTRIBUTE_ARCHIVE) < 0) {
661+
return -1;
662+
}
663+
if (PyModule_AddIntMacro(module, FILE_ATTRIBUTE_COMPRESSED) < 0) {
664+
return -1;
665+
}
666+
if (PyModule_AddIntMacro(module, FILE_ATTRIBUTE_DEVICE) < 0) {
667+
return -1;
668+
}
669+
if (PyModule_AddIntMacro(module, FILE_ATTRIBUTE_DIRECTORY) < 0) {
670+
return -1;
671+
}
672+
if (PyModule_AddIntMacro(module, FILE_ATTRIBUTE_ENCRYPTED) < 0) {
673+
return -1;
674+
}
675+
if (PyModule_AddIntMacro(module, FILE_ATTRIBUTE_HIDDEN) < 0) {
676+
return -1;
677+
}
678+
if (PyModule_AddIntMacro(module, FILE_ATTRIBUTE_INTEGRITY_STREAM) < 0) {
679+
return -1;
680+
}
681+
if (PyModule_AddIntMacro(module, FILE_ATTRIBUTE_NORMAL) < 0) {
682+
return -1;
683+
}
684+
if (PyModule_AddIntMacro(module, FILE_ATTRIBUTE_NOT_CONTENT_INDEXED) < 0) {
685+
return -1;
686+
}
687+
if (PyModule_AddIntMacro(module, FILE_ATTRIBUTE_NO_SCRUB_DATA) < 0) {
688+
return -1;
689+
}
690+
if (PyModule_AddIntMacro(module, FILE_ATTRIBUTE_OFFLINE) < 0) {
691+
return -1;
692+
}
693+
if (PyModule_AddIntMacro(module, FILE_ATTRIBUTE_READONLY) < 0) {
694+
return -1;
695+
}
696+
if (PyModule_AddIntMacro(module, FILE_ATTRIBUTE_REPARSE_POINT) < 0) {
697+
return -1;
698+
}
699+
if (PyModule_AddIntMacro(module, FILE_ATTRIBUTE_SPARSE_FILE) < 0) {
700+
return -1;
701+
}
702+
if (PyModule_AddIntMacro(module, FILE_ATTRIBUTE_SYSTEM) < 0) {
703+
return -1;
704+
}
705+
if (PyModule_AddIntMacro(module, FILE_ATTRIBUTE_TEMPORARY) < 0) {
706+
return -1;
707+
}
708+
if (PyModule_AddIntMacro(module, FILE_ATTRIBUTE_VIRTUAL) < 0) {
709+
return -1;
710+
}
711+
712+
if (PyModule_AddObject(module, "IO_REPARSE_TAG_SYMLINK",
713+
PyLong_FromUnsignedLong(IO_REPARSE_TAG_SYMLINK)) < 0) {
714+
return -1;
715+
}
716+
if (PyModule_AddObject(module, "IO_REPARSE_TAG_MOUNT_POINT",
717+
PyLong_FromUnsignedLong(IO_REPARSE_TAG_MOUNT_POINT)) < 0) {
718+
return -1;
719+
}
720+
if (PyModule_AddObject(module, "IO_REPARSE_TAG_APPEXECLINK",
721+
PyLong_FromUnsignedLong(IO_REPARSE_TAG_APPEXECLINK)) < 0) {
722+
return -1;
723+
}
724+
#endif
725+
726+
return 0;
727+
}
728+
729+
730+
static PyModuleDef_Slot stat_slots[] = {
731+
{Py_mod_exec, stat_exec},
732+
{0, NULL}
733+
};
734+
735+
495736
static struct PyModuleDef statmodule = {
496737
PyModuleDef_HEAD_INIT,
497-
"_stat",
498-
module_doc,
499-
-1,
500-
stat_methods,
501-
NULL,
502-
NULL,
503-
NULL,
504-
NULL
738+
.m_name = "_stat",
739+
.m_doc = module_doc,
740+
.m_size = 0,
741+
.m_methods = stat_methods,
742+
.m_slots = stat_slots,
505743
};
506744

745+
507746
PyMODINIT_FUNC
508747
PyInit__stat(void)
509748
{
510-
PyObject *m;
511-
m = PyModule_Create(&statmodule);
512-
if (m == NULL)
513-
return NULL;
514-
515-
if (PyModule_AddIntMacro(m, S_IFDIR)) return NULL;
516-
if (PyModule_AddIntMacro(m, S_IFCHR)) return NULL;
517-
if (PyModule_AddIntMacro(m, S_IFBLK)) return NULL;
518-
if (PyModule_AddIntMacro(m, S_IFREG)) return NULL;
519-
if (PyModule_AddIntMacro(m, S_IFIFO)) return NULL;
520-
if (PyModule_AddIntMacro(m, S_IFLNK)) return NULL;
521-
if (PyModule_AddIntMacro(m, S_IFSOCK)) return NULL;
522-
if (PyModule_AddIntMacro(m, S_IFDOOR)) return NULL;
523-
if (PyModule_AddIntMacro(m, S_IFPORT)) return NULL;
524-
if (PyModule_AddIntMacro(m, S_IFWHT)) return NULL;
525-
526-
if (PyModule_AddIntMacro(m, S_ISUID)) return NULL;
527-
if (PyModule_AddIntMacro(m, S_ISGID)) return NULL;
528-
if (PyModule_AddIntMacro(m, S_ISVTX)) return NULL;
529-
if (PyModule_AddIntMacro(m, S_ENFMT)) return NULL;
530-
531-
if (PyModule_AddIntMacro(m, S_IREAD)) return NULL;
532-
if (PyModule_AddIntMacro(m, S_IWRITE)) return NULL;
533-
if (PyModule_AddIntMacro(m, S_IEXEC)) return NULL;
534-
535-
if (PyModule_AddIntMacro(m, S_IRWXU)) return NULL;
536-
if (PyModule_AddIntMacro(m, S_IRUSR)) return NULL;
537-
if (PyModule_AddIntMacro(m, S_IWUSR)) return NULL;
538-
if (PyModule_AddIntMacro(m, S_IXUSR)) return NULL;
539-
540-
if (PyModule_AddIntMacro(m, S_IRWXG)) return NULL;
541-
if (PyModule_AddIntMacro(m, S_IRGRP)) return NULL;
542-
if (PyModule_AddIntMacro(m, S_IWGRP)) return NULL;
543-
if (PyModule_AddIntMacro(m, S_IXGRP)) return NULL;
544-
545-
if (PyModule_AddIntMacro(m, S_IRWXO)) return NULL;
546-
if (PyModule_AddIntMacro(m, S_IROTH)) return NULL;
547-
if (PyModule_AddIntMacro(m, S_IWOTH)) return NULL;
548-
if (PyModule_AddIntMacro(m, S_IXOTH)) return NULL;
549-
550-
if (PyModule_AddIntMacro(m, UF_NODUMP)) return NULL;
551-
if (PyModule_AddIntMacro(m, UF_IMMUTABLE)) return NULL;
552-
if (PyModule_AddIntMacro(m, UF_APPEND)) return NULL;
553-
if (PyModule_AddIntMacro(m, UF_OPAQUE)) return NULL;
554-
if (PyModule_AddIntMacro(m, UF_NOUNLINK)) return NULL;
555-
if (PyModule_AddIntMacro(m, UF_COMPRESSED)) return NULL;
556-
if (PyModule_AddIntMacro(m, UF_HIDDEN)) return NULL;
557-
if (PyModule_AddIntMacro(m, SF_ARCHIVED)) return NULL;
558-
if (PyModule_AddIntMacro(m, SF_IMMUTABLE)) return NULL;
559-
if (PyModule_AddIntMacro(m, SF_APPEND)) return NULL;
560-
if (PyModule_AddIntMacro(m, SF_NOUNLINK)) return NULL;
561-
if (PyModule_AddIntMacro(m, SF_SNAPSHOT)) return NULL;
562-
563-
if (PyModule_AddIntConstant(m, "ST_MODE", 0)) return NULL;
564-
if (PyModule_AddIntConstant(m, "ST_INO", 1)) return NULL;
565-
if (PyModule_AddIntConstant(m, "ST_DEV", 2)) return NULL;
566-
if (PyModule_AddIntConstant(m, "ST_NLINK", 3)) return NULL;
567-
if (PyModule_AddIntConstant(m, "ST_UID", 4)) return NULL;
568-
if (PyModule_AddIntConstant(m, "ST_GID", 5)) return NULL;
569-
if (PyModule_AddIntConstant(m, "ST_SIZE", 6)) return NULL;
570-
if (PyModule_AddIntConstant(m, "ST_ATIME", 7)) return NULL;
571-
if (PyModule_AddIntConstant(m, "ST_MTIME", 8)) return NULL;
572-
if (PyModule_AddIntConstant(m, "ST_CTIME", 9)) return NULL;
573-
574-
#ifdef MS_WINDOWS
575-
if (PyModule_AddIntMacro(m, FILE_ATTRIBUTE_ARCHIVE)) return NULL;
576-
if (PyModule_AddIntMacro(m, FILE_ATTRIBUTE_COMPRESSED)) return NULL;
577-
if (PyModule_AddIntMacro(m, FILE_ATTRIBUTE_DEVICE)) return NULL;
578-
if (PyModule_AddIntMacro(m, FILE_ATTRIBUTE_DIRECTORY)) return NULL;
579-
if (PyModule_AddIntMacro(m, FILE_ATTRIBUTE_ENCRYPTED)) return NULL;
580-
if (PyModule_AddIntMacro(m, FILE_ATTRIBUTE_HIDDEN)) return NULL;
581-
if (PyModule_AddIntMacro(m, FILE_ATTRIBUTE_INTEGRITY_STREAM)) return NULL;
582-
if (PyModule_AddIntMacro(m, FILE_ATTRIBUTE_NORMAL)) return NULL;
583-
if (PyModule_AddIntMacro(m, FILE_ATTRIBUTE_NOT_CONTENT_INDEXED)) return NULL;
584-
if (PyModule_AddIntMacro(m, FILE_ATTRIBUTE_NO_SCRUB_DATA)) return NULL;
585-
if (PyModule_AddIntMacro(m, FILE_ATTRIBUTE_OFFLINE)) return NULL;
586-
if (PyModule_AddIntMacro(m, FILE_ATTRIBUTE_READONLY)) return NULL;
587-
if (PyModule_AddIntMacro(m, FILE_ATTRIBUTE_REPARSE_POINT)) return NULL;
588-
if (PyModule_AddIntMacro(m, FILE_ATTRIBUTE_SPARSE_FILE)) return NULL;
589-
if (PyModule_AddIntMacro(m, FILE_ATTRIBUTE_SYSTEM)) return NULL;
590-
if (PyModule_AddIntMacro(m, FILE_ATTRIBUTE_TEMPORARY)) return NULL;
591-
if (PyModule_AddIntMacro(m, FILE_ATTRIBUTE_VIRTUAL)) return NULL;
592-
593-
if (PyModule_AddObject(m, "IO_REPARSE_TAG_SYMLINK",
594-
PyLong_FromUnsignedLong(IO_REPARSE_TAG_SYMLINK))) return NULL;
595-
if (PyModule_AddObject(m, "IO_REPARSE_TAG_MOUNT_POINT",
596-
PyLong_FromUnsignedLong(IO_REPARSE_TAG_MOUNT_POINT))) return NULL;
597-
if (PyModule_AddObject(m, "IO_REPARSE_TAG_APPEXECLINK",
598-
PyLong_FromUnsignedLong(IO_REPARSE_TAG_APPEXECLINK))) return NULL;
599-
#endif
600-
601-
return m;
749+
return PyModuleDef_Init(&statmodule);
602750
}
603751

604752
#ifdef __cplusplus

0 commit comments

Comments
 (0)