@@ -564,88 +564,81 @@ static struct PyMethodDef msvcrt_functions[] = {
564
564
{NULL , NULL }
565
565
};
566
566
567
- static void
568
- insertint (PyObject * d , char * name , int value )
569
- {
570
- PyObject * v = PyLong_FromLong ((long ) value );
571
- if (v == NULL ) {
572
- /* Don't bother reporting this error */
573
- PyErr_Clear ();
574
- }
575
- else {
576
- PyDict_SetItemString (d , name , v );
577
- Py_DECREF (v );
578
- }
579
- }
580
-
581
- static void
582
- insertptr (PyObject * d , char * name , void * value )
567
+ static int
568
+ insertptr (PyObject * mod , char * name , void * value )
583
569
{
584
570
PyObject * v = PyLong_FromVoidPtr (value );
585
571
if (v == NULL ) {
586
- /* Don't bother reporting this error */
587
- PyErr_Clear ();
588
- }
589
- else {
590
- PyDict_SetItemString (d , name , v );
591
- Py_DECREF (v );
572
+ return -1 ;
592
573
}
574
+ int rc = PyModule_AddObjectRef (mod , name , v );
575
+ Py_DECREF (v );
576
+ return rc ;
593
577
}
594
578
579
+ #define INSERTINT (MOD , NAME , VAL ) do { \
580
+ if (PyModule_AddIntConstant(MOD, NAME, VAL) < 0) { \
581
+ return -1; \
582
+ } \
583
+ } while (0)
584
+
585
+ #define INSERTPTR (MOD , NAME , PTR ) do { \
586
+ if (insertptr(MOD, NAME, PTR) < 0) { \
587
+ return -1; \
588
+ } \
589
+ } while (0)
590
+
591
+ #define INSERTSTR (MOD , NAME , CONST ) do { \
592
+ if (PyModule_AddStringConstant(MOD, NAME, CONST) < 0) { \
593
+ return -1; \
594
+ } \
595
+ } while (0)
596
+
595
597
static int
596
598
exec_module (PyObject * m )
597
599
{
598
- int st ;
599
- PyObject * d = PyModule_GetDict (m ); // Borrowed ref.
600
-
601
600
/* constants for the locking() function's mode argument */
602
- insertint ( d , "LK_LOCK" , _LK_LOCK );
603
- insertint ( d , "LK_NBLCK" , _LK_NBLCK );
604
- insertint ( d , "LK_NBRLCK" , _LK_NBRLCK );
605
- insertint ( d , "LK_RLCK" , _LK_RLCK );
606
- insertint ( d , "LK_UNLCK" , _LK_UNLCK );
601
+ INSERTINT ( m , "LK_LOCK" , _LK_LOCK );
602
+ INSERTINT ( m , "LK_NBLCK" , _LK_NBLCK );
603
+ INSERTINT ( m , "LK_NBRLCK" , _LK_NBRLCK );
604
+ INSERTINT ( m , "LK_RLCK" , _LK_RLCK );
605
+ INSERTINT ( m , "LK_UNLCK" , _LK_UNLCK );
607
606
#ifdef MS_WINDOWS_DESKTOP
608
- insertint ( d , "SEM_FAILCRITICALERRORS" , SEM_FAILCRITICALERRORS );
609
- insertint ( d , "SEM_NOALIGNMENTFAULTEXCEPT" , SEM_NOALIGNMENTFAULTEXCEPT );
610
- insertint ( d , "SEM_NOGPFAULTERRORBOX" , SEM_NOGPFAULTERRORBOX );
611
- insertint ( d , "SEM_NOOPENFILEERRORBOX" , SEM_NOOPENFILEERRORBOX );
607
+ INSERTINT ( m , "SEM_FAILCRITICALERRORS" , SEM_FAILCRITICALERRORS );
608
+ INSERTINT ( m , "SEM_NOALIGNMENTFAULTEXCEPT" , SEM_NOALIGNMENTFAULTEXCEPT );
609
+ INSERTINT ( m , "SEM_NOGPFAULTERRORBOX" , SEM_NOGPFAULTERRORBOX );
610
+ INSERTINT ( m , "SEM_NOOPENFILEERRORBOX" , SEM_NOOPENFILEERRORBOX );
612
611
#endif
613
612
#ifdef _DEBUG
614
- insertint ( d , "CRT_WARN" , _CRT_WARN );
615
- insertint ( d , "CRT_ERROR" , _CRT_ERROR );
616
- insertint ( d , "CRT_ASSERT" , _CRT_ASSERT );
617
- insertint ( d , "CRTDBG_MODE_DEBUG" , _CRTDBG_MODE_DEBUG );
618
- insertint ( d , "CRTDBG_MODE_FILE" , _CRTDBG_MODE_FILE );
619
- insertint ( d , "CRTDBG_MODE_WNDW" , _CRTDBG_MODE_WNDW );
620
- insertint ( d , "CRTDBG_REPORT_MODE" , _CRTDBG_REPORT_MODE );
621
- insertptr ( d , "CRTDBG_FILE_STDERR" , _CRTDBG_FILE_STDERR );
622
- insertptr ( d , "CRTDBG_FILE_STDOUT" , _CRTDBG_FILE_STDOUT );
623
- insertptr ( d , "CRTDBG_REPORT_FILE" , _CRTDBG_REPORT_FILE );
613
+ INSERTINT ( m , "CRT_WARN" , _CRT_WARN );
614
+ INSERTINT ( m , "CRT_ERROR" , _CRT_ERROR );
615
+ INSERTINT ( m , "CRT_ASSERT" , _CRT_ASSERT );
616
+ INSERTINT ( m , "CRTDBG_MODE_DEBUG" , _CRTDBG_MODE_DEBUG );
617
+ INSERTINT ( m , "CRTDBG_MODE_FILE" , _CRTDBG_MODE_FILE );
618
+ INSERTINT ( m , "CRTDBG_MODE_WNDW" , _CRTDBG_MODE_WNDW );
619
+ INSERTINT ( m , "CRTDBG_REPORT_MODE" , _CRTDBG_REPORT_MODE );
620
+ INSERTPTR ( m , "CRTDBG_FILE_STDERR" , _CRTDBG_FILE_STDERR );
621
+ INSERTPTR ( m , "CRTDBG_FILE_STDOUT" , _CRTDBG_FILE_STDOUT );
622
+ INSERTPTR ( m , "CRTDBG_REPORT_FILE" , _CRTDBG_REPORT_FILE );
624
623
#endif
625
624
625
+ #undef INSERTINT
626
+ #undef INSERTPTR
627
+
626
628
/* constants for the crt versions */
627
629
#ifdef _VC_ASSEMBLY_PUBLICKEYTOKEN
628
- st = PyModule_AddStringConstant (m , "VC_ASSEMBLY_PUBLICKEYTOKEN" ,
629
- _VC_ASSEMBLY_PUBLICKEYTOKEN );
630
- if (st < 0 ) {
631
- return -1 ;
632
- }
630
+ INSERTSTR (m , "VC_ASSEMBLY_PUBLICKEYTOKEN" , _VC_ASSEMBLY_PUBLICKEYTOKEN );
633
631
#endif
634
632
#ifdef _CRT_ASSEMBLY_VERSION
635
- st = PyModule_AddStringConstant (m , "CRT_ASSEMBLY_VERSION" ,
636
- _CRT_ASSEMBLY_VERSION );
637
- if (st < 0 ) {
638
- return -1 ;
639
- }
633
+ INSERTSTR (m , "CRT_ASSEMBLY_VERSION" , _CRT_ASSEMBLY_VERSION );
640
634
#endif
641
635
#ifdef __LIBRARIES_ASSEMBLY_NAME_PREFIX
642
- st = PyModule_AddStringConstant (m , "LIBRARIES_ASSEMBLY_NAME_PREFIX" ,
643
- __LIBRARIES_ASSEMBLY_NAME_PREFIX );
644
- if (st < 0 ) {
645
- return -1 ;
646
- }
636
+ INSERTSTR (m , "LIBRARIES_ASSEMBLY_NAME_PREFIX" ,
637
+ __LIBRARIES_ASSEMBLY_NAME_PREFIX );
647
638
#endif
648
639
640
+ #undef INSERTSTR
641
+
649
642
/* constants for the 2010 crt versions */
650
643
#if defined(_VC_CRT_MAJOR_VERSION ) && defined (_VC_CRT_MINOR_VERSION ) && defined(_VC_CRT_BUILD_VERSION ) && defined(_VC_CRT_RBUILD_VERSION )
651
644
PyObject * version = PyUnicode_FromFormat ("%d.%d.%d.%d" ,
@@ -656,14 +649,12 @@ exec_module(PyObject* m)
656
649
if (version == NULL ) {
657
650
return -1 ;
658
651
}
659
- st = PyModule_AddObjectRef (m , "CRT_ASSEMBLY_VERSION" , version );
652
+ int st = PyModule_AddObjectRef (m , "CRT_ASSEMBLY_VERSION" , version );
660
653
Py_DECREF (version );
661
654
if (st < 0 ) {
662
655
return -1 ;
663
656
}
664
657
#endif
665
- /* make compiler warning quiet if st is unused */
666
- (void )st ;
667
658
668
659
return 0 ;
669
660
}
0 commit comments