@@ -6494,32 +6494,9 @@ static PyDateTime_CAPI CAPI = {
6494
6494
new_time_ex2
6495
6495
};
6496
6496
6497
-
6498
-
6499
- static struct PyModuleDef datetimemodule = {
6500
- PyModuleDef_HEAD_INIT ,
6501
- "_datetime" ,
6502
- "Fast implementation of the datetime type." ,
6503
- -1 ,
6504
- module_methods ,
6505
- NULL ,
6506
- NULL ,
6507
- NULL ,
6508
- NULL
6509
- };
6510
-
6511
- PyMODINIT_FUNC
6512
- PyInit__datetime (void )
6497
+ static int
6498
+ _datetime_exec (PyObject * module )
6513
6499
{
6514
- PyObject * m ; /* a module object */
6515
- PyObject * d ; /* its dict */
6516
- PyObject * x ;
6517
- PyObject * delta ;
6518
-
6519
- m = PyModule_Create (& datetimemodule );
6520
- if (m == NULL )
6521
- return NULL ;
6522
-
6523
6500
// `&...` is not a constant expression according to a strict reading
6524
6501
// of C standards. Fill tp_base at run-time rather than statically.
6525
6502
// See https://bugs.python.org/issue40777
@@ -6537,136 +6514,131 @@ PyInit__datetime(void)
6537
6514
};
6538
6515
6539
6516
for (size_t i = 0 ; i < Py_ARRAY_LENGTH (types ); i ++ ) {
6540
- if (PyModule_AddType (m , types [i ]) < 0 ) {
6541
- return NULL ;
6517
+ if (PyModule_AddType (module , types [i ]) < 0 ) {
6518
+ return -1 ;
6542
6519
}
6543
6520
}
6544
6521
6545
6522
if (PyType_Ready (& PyDateTime_IsoCalendarDateType ) < 0 ) {
6546
- return NULL ;
6523
+ return -1 ;
6547
6524
}
6548
6525
Py_INCREF (& PyDateTime_IsoCalendarDateType );
6549
6526
6550
- /* timedelta values */
6551
- d = PyDateTime_DeltaType .tp_dict ;
6552
6527
6553
- x = new_delta (0 , 0 , 1 , 0 );
6554
- if (x == NULL || PyDict_SetItemString (d , "resolution" , x ) < 0 )
6555
- return NULL ;
6556
- Py_DECREF (x );
6528
+ PyObject * x = NULL ;
6557
6529
6558
- x = new_delta (- MAX_DELTA_DAYS , 0 , 0 , 0 );
6559
- if (x == NULL || PyDict_SetItemString (d , "min" , x ) < 0 )
6560
- return NULL ;
6561
- Py_DECREF (x );
6530
+ #define DATETIME_ADD_MACRO (x , c ) \
6531
+ do { \
6532
+ if (x == NULL) { \
6533
+ return -1; \
6534
+ } \
6535
+ if (PyDict_SetItemString(d, c, x) < 0) { \
6536
+ Py_DECREF(x); \
6537
+ return -1; \
6538
+ } \
6539
+ Py_DECREF(x);
67E6
\
6540
+ } while(0)
6562
6541
6542
+ /* timedelta values */
6543
+ PyObject * d = PyDateTime_DeltaType .tp_dict ;
6544
+ x = new_delta (0 , 0 , 1 , 0 );
6545
+ DATETIME_ADD_MACRO (x , "resolution" );
6546
+ x = new_delta (- MAX_DELTA_DAYS , 0 , 0 , 0 );
6547
+ DATETIME_ADD_MACRO (x , "min" );
6563
6548
x = new_delta (MAX_DELTA_DAYS , 24 * 3600 - 1 , 1000000 - 1 , 0 );
6564
- if (x == NULL || PyDict_SetItemString (d , "max" , x ) < 0 )
6565
- return NULL ;
6566
- Py_DECREF (x );
6549
+ DATETIME_ADD_MACRO (x , "max" );
6567
6550
6568
6551
/* date values */
6569
6552
d = PyDateTime_DateType .tp_dict ;
6570
-
6571
6553
x = new_date (1 , 1 , 1 );
6572
- if (x == NULL || PyDict_SetItemString (d , "min" , x ) < 0 )
6573
- return NULL ;
6574
- Py_DECREF (x );
6575
-
6554
+ DATETIME_ADD_MACRO (x , "min" );
6576
6555
x = new_date (MAXYEAR , 12 , 31 );
6577
- if (x == NULL || PyDict_SetItemString (d , "max" , x ) < 0 )
6578
- return NULL ;
6579
- Py_DECREF (x );
6580
-
6556
+ DATETIME_ADD_MACRO (x , "max" );
6581
6557
x = new_delta (1 , 0 , 0 , 0 );
6582
- if (x == NULL || PyDict_SetItemString (d , "resolution" , x ) < 0 )
6583
- return NULL ;
6584
- Py_DECREF (x );
6558
+ DATETIME_ADD_MACRO (x , "resolution" );
6585
6559
6586
6560
/* time values */
6587
6561
d = PyDateTime_TimeType .tp_dict ;
6588
-
6589
6562
x = new_time (0 , 0 , 0 , 0 , Py_None , 0 );
6590
- if (x == NULL || PyDict_SetItemString (d , "min" , x ) < 0 )
6591
- return NULL ;
6592
- Py_DECREF (x );
6593
-
6563
+ DATETIME_ADD_MACRO (x , "min" );
6594
6564
x = new_time (23 , 59 , 59 , 999999 , Py_None , 0 );
6595
- if (x == NULL || PyDict_SetItemString (d , "max" , x ) < 0 )
6596
- return NULL ;
6597
- Py_DECREF (x );
6598
-
6565
+ DATETIME_ADD_MACRO (x , "max" );
6599
6566
x = new_delta (0 , 0 , 1 , 0 );
6600
- if (x == NULL || PyDict_SetItemString (d , "resolution" , x ) < 0 )
6601
- return NULL ;
6602
- Py_DECREF (x );
6567
+ DATETIME_ADD_MACRO (x , "resolution" );
6603
6568
6604
6569
/* datetime values */
6605
6570
d = PyDateTime_DateTimeType .tp_dict ;
6606
-
6607
6571
x = new_datetime (1 , 1 , 1 , 0 , 0 , 0 , 0 , Py_None , 0 );
6608
- if (x == NULL || PyDict_SetItemString (d , "min" , x ) < 0 )
6609
- return NULL ;
6610
- Py_DECREF (x );
6611
-
6572
+ DATETIME_ADD_MACRO (x , "min" );
6612
6573
x = new_datetime (MAXYEAR , 12 , 31 , 23 , 59 , 59 , 999999 , Py_None , 0 );
6613
- if (x == NULL || PyDict_SetItemString (d , "max" , x ) < 0 )
6614
- return NULL ;
6615
- Py_DECREF (x );
6616
-
6574
+ DATETIME_ADD_MACRO (x , "max" );
6617
6575
x = new_delta (0 , 0 , 1 , 0 );
6618
- if (x == NULL || PyDict_SetItemString (d , "resolution" , x ) < 0 )
6619
- return NULL ;
6620
- Py_DECREF (x );
6576
+ DATETIME_ADD_MACRO (x , "resolution" );
6621
6577
6622
6578
/* timezone values */
6623
6579
d = PyDateTime_TimeZoneType .tp_dict ;
6580
+ PyObject * delta = new_delta (0 , 0 , 0 , 0 );
6581
+ if (delta == NULL ) {
6582
+ return -1 ;
6583
+ }
6624
6584
6625
- delta = new_delta (0 , 0 , 0 , 0 );
6626
- if (delta == NULL )
6627
- return NULL ;
6628
6585
x = create_timezone (delta , NULL);
6629
6586
Py_DECREF (delta );
6630
- if (x == NULL || PyDict_SetItemString (d , "utc" , x ) < 0 )
6631
- return NULL ;
6587
+ if (x == NULL ) {
6588
+ return -1 ;
6589
+ }
6590
+ if (PyDict_SetItemString (d , "utc" , x ) < 0 ) {
6591
+ Py_DECREF (x );
6592
+ return -1 ;
6593
+ }
6594
+
6632
6595
PyDateTime_TimeZone_UTC = x ;
6633
6596
CAPI .TimeZone_UTC = PyDateTime_TimeZone_UTC ;
6634
6597
6635
- /* bpo-37642: These attributes are rounded to the nearest minute for backwards
6636
- * compatibility, even though the constructor will accept a wider range of
6637
- * values. This may change in the future.*/
6598
+ /* bpo-37642: These attributes are rounded to the nearest minute for
6599
+ * backwards compatibility, even though the constructor will accept a
6600
+ * wider range of values. This may change in the future.
6601
+ */
6638
6602
delta = new_delta (-1 , 60 , 0 , 1 ); /* -23:59 */
6639
- if (delta == NULL )
6640
- return NULL ;
6603
+ if (delta == NULL ) {
6604
+ return -1 ;
6605
+ }
6641
6606
x = create_timezone (delta , NULL );
6642
6607
Py_DECREF (delta );
6643
- if (x == NULL || PyDict_SetItemString (d , "min" , x ) < 0 )
6644
- return NULL ;
6645
- Py_DECREF (x );
6608
+ DATETIME_ADD_MACRO (x , "min" );
6646
6609
6647
6610
delta = new_delta (0 , (23 * 60 + 59 ) * 60 , 0 , 0 ); /* +23:59 */
6648
- if (delta == NULL )
6649
- return NULL ;
6611
+ if (delta == NULL ) {
6612
+ return -1 ;
6613
+ }
6650
6614
x = create_timezone (delta , NULL );
6651
6615
Py_DECREF (delta );
6652
- if (x == NULL || PyDict_SetItemString (d , "max" , x ) < 0 )
6653
- return NULL ;
6654
- Py_DECREF (x );
6616
+ DATETIME_ADD_MACRO (x , "max" );
6655
6617
6656
6618
/* Epoch */
6657
6619
PyDateTime_Epoch = new_datetime (1970 , 1 , 1 , 0 , 0 , 0 , 0 ,
6658
6620
PyDateTime_TimeZone_UTC , 0 );
6659
- if (PyDateTime_Epoch == NULL )
6660
- return NULL ;
6621
+ if (PyDateTime_Epoch == NULL ) {
6622
+ return -1 ;
6623
+ }
6661
6624
6662
6625
/* module initialization */
6663
- PyModule_AddIntMacro (m , MINYEAR );
6664
- PyModule_AddIntMacro (m , MAXYEAR );
6626
+ if (PyModule_AddIntMacro (module , MINYEAR ) < 0 ) {
6627
+ return -1 ;
6628
+ }
6629
+ if (PyModule_AddIntMacro (module , MAXYEAR ) < 0 ) {
6630
+ return -1 ;
6631
+ }
6665
6632
6666
6633
x = PyCapsule_New (& CAPI , PyDateTime_CAPSULE_NAME , NULL );
6667
- if (x == NULL )
6668
- return NULL ;
6669
- PyModule_AddObject (m , "datetime_CAPI" , x );
6634
+ if (x == NULL ) {
6635
+ return -1 ;
6636
+ }
6637
+
6638
+ if (PyModule_AddObject (module , "datetime_CAPI" , x ) < 0 ) {
6639
+ Py_DECREF (x );
6640
+ return -1 ;
6641
+ }
6670
6642
6671
6643
/* A 4-year cycle has an extra leap day over what we'd get from
6672
6644
* pasting together 4 single years.
@@ -6691,18 +6663,43 @@ PyInit__datetime(void)
6691
6663
us_per_minute = PyLong_FromLong (60000000 );
6692
6664
seconds_per_day = PyLong_FromLong (24 * 3600 );
6693
6665
if (us_per_ms == NULL || us_per_second == NULL ||
6694
- us_per_minute == NULL || seconds_per_day == NULL )
6695
- return NULL ;
6666
<
179B
td data-grid-cell-id="diff-3ee250e3806e884518fd872e9148baf532de6ec54c1cdb4e7679fbb2869d9c47-6695-6666-2" data-line-anchor="diff-3ee250e3806e884518fd872e9148baf532de6ec54c1cdb4e7679fbb2869d9c47R6666" data-selected="false" role="gridcell" style="background-color:var(--diffBlob-additionLine-bgColor, var(--diffBlob-addition-bgColor-line));padding-right:24px" tabindex="-1" valign="top" class="focusable-grid-cell diff-text-cell right-side-diff-cell left-side">+ us_per_minute == NULL || seconds_per_day == NULL ) {
6667
+ return -1 ;
6668
+ }
6696
6669
6697
6670
/* The rest are too big for 32-bit ints, but even
6698
6671
* us_per_week fits in 40 bits, so doubles should be exact.
6699
6672
*/
6700
6673
us_per_hour = PyLong_FromDouble (3600000000.0 );
6701
6674
us_per_day = PyLong_FromDouble (86400000000.0 );
6702
6675
us_per_week = PyLong_FromDouble (604800000000.0 );
6703
- if (us_per_hour == NULL || us_per_day == NULL || us_per_week == NULL )
6676
+ if (us_per_hour == NULL || us_per_day == NULL || us_per_week == NULL ) {
6677
+ return -1 ;
6678
+ }
6679
+ return 0 ;
6680
+ }
6681
+
6682
+ static struct PyModuleDef datetimemodule = {
6683
+ PyModuleDef_HEAD_INIT ,
6684
+ .m_name = "_datetime" ,
6685
+ .m_doc = "Fast implementation of the datetime type." ,
6686
+ .m_size = -1 ,
6687
+ .m_methods = module_methods ,
6688
+ };
6689
+
6690
+ PyMODINIT_FUNC
6691
+ PyInit__datetime (void )
6692
+ {
6693
+ PyObject * mod = PyModule_Create (& datetimemodule );
6694
+ if (mod == NULL )
6704
6695
return NULL ;
6705
- return m ;
6696
+
6697
+ if (_datetime_exec (mod ) < 0 ) {
6698
+ Py_DECREF (mod );
6699
+ return NULL ;
6700
+ }
6701
+
6702
+ return mod ;
6706
6703
}
6707
6704
6708
6705
/* ---------------------------------------------------------------------------
0 commit comments