@@ -6741,6 +6741,68 @@ datetime_clear(PyObject *module)
6741
6741
return 0 ;
6742
6742
}
6743
6743
6744
+ static PyObject *
6745
+ create_timezone_from_delta (int days , int sec , int ms , int normalize )
6746
+ {
6747
+ PyObject * delta = new_delta (days , sec , ms , normalize );
6748
+ if (delta == NULL ) {
6749
+ return NULL ;
6750
+ }
6751
+ PyObject * tz = create_timezone (delta , NULL );
6752
+ Py_DECREF (delta );
6753
+ return tz ;
6754
+ }
6755
+
6756
+ static int
6757
+ init_state (datetime_state * st )
6758
+ {
6759
+ st -> us_per_ms = PyLong_FromLong (1000 );
6760
+ if (st -> us_per_ms == NULL ) {
6761
+ return -1 ;
6762
+ }
6763
+ st -> us_per_second = PyLong_FromLong (1000000 );
6764
+ if (st -> us_per_second == NULL ) {
6765
+ return -1 ;
6766
+ }
6767
+ st -> us_per_minute = PyLong_FromLong (60000000 );
6768
+ if (st -> us_per_minute == NULL ) {
6769
+ return -1 ;
6770
+ }
6771
+ st -> seconds_per_day = PyLong_FromLong (24 * 3600 );
6772
+ if (st -> seconds_per_day == NULL ) {
6773
+ return -1 ;
6774
+ }
6775
+
6776
+ /* The rest are too big for 32-bit ints, but even
6777
+ * us_per_week fits in 40 bits, so doubles should be exact.
6778
+ */
6779
+ st -> us_per_hour = PyLong_FromDouble (3600000000.0 );
6780
+ if (st -> us_per_hour == NULL ) {
6781
+ return -1 ;
6782
+ }
6783
+ st -> us_per_day = PyLong_FromDouble (86400000000.0 );
6784
+ if (st -> us_per_day == NULL ) {
6785
+ return -1 ;
6786
+ }
6787
+ st -> us_per_week = PyLong_FromDouble (604800000000.0 );
6788
+ if (st -> us_per_week == NULL ) {
6789
+ return -1 ;
6790
+ }
6791
+
6792
+ /* Init UTC timezone */
6793
+ st -> utc = create_timezone_from_delta (0 , 0 , 0 , 0 );
6794
+ if (st -> utc == NULL ) {
6795
+ return -1 ;
6796
+ }
6797
+
6798
+ /* Init Unix epoch */
6799
+ st -> epoch = new_datetime (1970 , 1 , 1 , 0 , 0 , 0 , 0 , st -> utc , 0 );
6800
+ if (st -> epoch == NULL ) {
6801
+ return -1 ;
6802
+ }
6803
+ return 0 ;
6804
+ }
6805
+
6744
6806
static int
6745
6807
_datetime_exec (PyObject * module )
6746
6808
{
@@ -6762,23 +6824,23 @@ _datetime_exec(PyObject *module)
6762
6824
6763
6825
for (size_t i = 0 ; i < Py_ARRAY_LENGTH (types ); i ++ ) {
6764
6826
if (PyModule_AddType (module , types [i ]) < 0 ) {
6765
- return -1 ;
6827
+ goto error ;
6766
6828
}
6767
6829
}
6768
6830
6769
6831
if (PyType_Ready (& PyDateTime_IsoCalendarDateType ) < 0 ) {
6770
- return -1 ;
6832
+ goto error ;
6771
6833
}
6772
6834
6773
6835
#define DATETIME_ADD_MACRO (dict , c , value_expr ) \
6774
6836
do { \
6775
6837
PyObject *value = (value_expr); \
6776
6838
if (value == NULL) { \
6777
- return -1; \
6839
+ goto error; \
6778
6840
} \
6779
6841
if (PyDict_SetItemString(dict, c, value) < 0) { \
6780
6842
Py_DECREF(value); \
6781
- return -1; \
6843
+ goto error; \
6782
6844
} \
6783
6845
Py_DECREF(value); \
6784
6846
} while(0)
@@ -6810,73 +6872,53 @@ _datetime_exec(PyObject *module)
6810
6872
999999 , Py_None , 0 ));
6811
6873
DATETIME_ADD_MACRO (d , "resolution" , new_delta (0 , 0 , 1 , 0 ));
6812
6874
6813
- /* timezone values */
6814
- d = PyDateTime_TimeZoneType .tp_dict ;
6815
- PyObject * delta = new_delta (0 , 0 , 0 , 0 );
6816
- if (delta == NULL ) {
6817
- return -1 ;
6818
- }
6819
-
6820
6875
datetime_state * st = STATIC_STATE ();
6821
- st -> utc = create_timezone (delta , NULL );
6822
- Py_DECREF (delta );
6823
- if (st -> utc == NULL ) {
6876
+ if (init_state (st ) < 0 ) {
6824
6877
goto error ;
6825
6878
}
6879
+
6880
+ /* timezone values */
6881
+ d = PyDateTime_TimeZoneType .tp_dict ;
6826
6882
if (PyDict_SetItemString (d , "utc" , st -> utc ) < 0 ) {
6827
6883
goto error ;
6828
6884
}
6829
6885
6830
6886
/* bpo-37642: These attributes are rounded to the nearest minute for backwards
6831
6887
* compatibility, even though the constructor will accept a wider range of
6832
6888
* values. This may change in the future.*/
6833
- delta = new_delta (-1 , 60 , 0 , 1 ); /* -23:59 */
6834
- if (delta == NULL ) {
6835
- goto error ;
6836
- }
6837
-
6838
- PyObject * x = create_timezone (delta , NULL );
6839
- Py_DECREF (delta );
6840
- DATETIME_ADD_MACRO (d , "min" , x );
6841
6889
6842
- delta = new_delta (0 , (23 * 60 + 59 ) * 60 , 0 , 0 ); /* +23:59 */
6843
- if (delta == NULL ) {
6844
- goto error ;
6845
- }
6890
+ /* -23:59 */
6891
+ PyObject * min = create_timezone_from_delta (-1 , 60 , 0 , 1 );
6892
+ DATETIME_ADD_MACRO (d , "min" , min );
6846
6893
6847
- x = create_timezone ( delta , NULL );
6848
- Py_DECREF ( delta );
6849
- DATETIME_ADD_MACRO (d , "max" , x );
6894
+ /* 23:59 */
6895
+ PyObject * max = create_timezone_from_delta ( 0 , ( 23 * 60 + 59 ) * 60 , 0 , 0 );
6896
+ DATETIME_ADD_MACRO (d , "max" , max );
6850
6897
6851
- /* Epoch */
6852
- st -> epoch = new_datetime (1970 , 1 , 1 , 0 , 0 , 0 , 0 , st -> utc , 0 );
6853
- if (st -> epoch == NULL ) {
6854
- goto error ;
6855
- }
6856
-
6857
- /* module initialization */
6898
+ /* Add module level attributes */
6858
6899
if (PyModule_AddIntMacro (module , MINYEAR ) < 0 ) {
6859
6900
goto error ;
6860
6901
}
6861
6902
if (PyModule_AddIntMacro (module , MAXYEAR ) < 0 ) {
6862
6903
goto error ;
6863
6904
}
6905
+ if (PyModule_AddObjectRef (module , "UTC" , st -> utc ) < 0 ) {
6906
+ goto error ;
6907
+ }
6864
6908
6909
+ /* At last, set up and add the encapsulated C API */
6865
6910
PyDateTime_CAPI * capi = get_datetime_capi ();
6866
6911
if (capi == NULL ) {
6867
6912
goto error ;
6868
6913
}
6869
- x = PyCapsule_New (capi , PyDateTime_CAPSULE_NAME , datetime_destructor );
6870
- if (x == NULL ) {
6914
+ PyObject * capsule = PyCapsule_New (capi , PyDateTime_CAPSULE_NAME ,
6915
+ datetime_destructor );
6916
+ if (capsule == NULL ) {
6871
6917
PyMem_Free (capi );
6872
6918
goto error ;
6873
6919
}
6874
-
6875
- if (PyModule_Add (module , "datetime_CAPI" , x ) < 0 ) {
6876
- goto error ;
6877
- }
6878
-
6879
- if (PyModule_AddObjectRef (module , "UTC" , st -> utc ) < 0 ) {
6920
+ if (PyModule_Add (module , "datetime_CAPI" , capsule ) < 0 ) {
6921
+ PyMem_Free (capi );
6880
6922
goto error ;
6881
6923
}
6882
6924
@@ -6898,45 +6940,13 @@ _datetime_exec(PyObject *module)
6898
6940
static_assert (DI100Y == 25 * DI4Y - 1 , "DI100Y" );
6899
6941
assert (DI100Y == days_before_year (100 + 1 ));
6900
6942
6901
- st -> us_per_ms = PyLong_FromLong (1000 );
6902
- if (st -> us_per_ms == NULL ) {
6903
- goto error ;
6904
- }
6905
- st -> us_per_second = PyLong_FromLong (1000000 );
6906
- if (st -> us_per_second == NULL ) {
6907
- goto error ;
6908
- }
6909
- st -> us_per_minute = PyLong_FromLong (60000000 );
6910
- if (st -> us_per_minute == NULL ) {
6911
- goto error ;
6912
- }
6913
- st -> seconds_per_day = PyLong_FromLong (24 * 3600 );
6914
- if (st -> seconds_per_day == NULL ) {
6915
- goto error ;
6916
- }
6917
-
6918
- /* The rest are too big for 32-bit ints, but even
6919
- * us_per_week fits in 40 bits, so doubles should be exact.
6920
- */
6921
- st -> us_per_hour = PyLong_FromDouble (3600000000.0 );
6922
- if (st -> us_per_hour == NULL ) {
6923
- goto error ;
6924
- }
6925
- st -> us_per_day = PyLong_FromDouble (86400000000.0 );
6926
- if (st -> us_per_day == NULL ) {
6927
- goto error ;
6928
- }
6929
- st -> us_per_week = PyLong_FromDouble (604800000000.0 );
6930
- if (st -> us_per_week == NULL ) {
6931
- goto error ;
6932
- }
6933
-
6934
6943
return 0 ;
6935
6944
6936
6945
error :
6937
6946
datetime_clear (module );
6938
6947
return -1 ;
6939
6948
}
6949
+ #undef DATETIME_ADD_MACRO
6940
6950
6941
6951
static struct PyModuleDef datetimemodule = {
6942
6952
.m_base = PyModuleDef_HEAD_INIT ,
0 commit comments