@@ -1045,6 +1045,40 @@ new_datetime_ex(int year, int month, int day, int hour, int minute,
1045
1045
new_datetime_ex2(y, m, d, hh, mm, ss, us, tzinfo, fold, \
1046
1046
&PyDateTime_DateTimeType)
1047
1047
1048
+ static PyObject *
1049
+ call_subclass_fold (PyObject * cls , int fold , const char * format , ...)
1050
+ {
1051
+ PyObject * kwargs = NULL , * res = NULL ;
1052
+ va_list va ;
1053
+
1054
+ va_start (va , format );
1055
+ PyObject * args = Py_VaBuildValue (format , va );
1056
+ va_end (va );
1057
+ if (args == NULL ) {
1058
+ return NULL ;
1059
+ }
1060
+ if (fold ) {
1061
+ kwargs = PyDict_New ();
1062
+ if (kwargs == NULL ) {
1063
+ goto Done ;
1064
+ }
1065
+ PyObject * obj = PyLong_FromLong (fold );
1066
+ if (obj == NULL ) {
1067
+ goto Done ;
1068
+ }
1069
+ int err = PyDict_SetItemString (kwargs , "fold" , obj );
1070
+ Py_DECREF (obj );
1071
+ if (err < 0 ) {
1072
+ goto Done ;
1073
+ }
1074
+ }
1075
+ res = PyObject_Call (cls , args , kwargs );
1076
+ Done :
1077
+ Py_DECREF (args );
1078
+ Py_XDECREF (kwargs );
1079
+ return res ;
1080
+ }
1081
+
1048
1082
static PyObject *
1049
1083
new_datetime_subclass_fold_ex (int year , int month , int day , int hour , int minute ,
1050
1084
int second , int usecond , PyObject * tzinfo ,
@@ -1054,17 +1088,11 @@ new_datetime_subclass_fold_ex(int year, int month, int day, int hour, int minute
1054
1088
// Use the fast path constructor
1055
1089
dt = new_datetime (year , month , day , hour , minute , second , usecond ,
1056
1090
tzinfo , fold );
1057
- } else {
1091
+ }
1092
+ else {
1058
1093
// Subclass
1059
- dt = PyObject_CallFunction (cls , "iiiiiiiO" ,
1060
- year ,
1061
- month ,
1062
- day ,
1063
- hour ,
1064
- minute ,
1065
- second ,
1066
- usecond ,
1067
- tzinfo );
1094
+ dt = call_subclass_fold (cls , fold , "iiiiiiiO" , year , month , day ,
1095
+ hour , minute , second , usecond , tzinfo );
1068
1096
}
1069
1097
1070
1098
return dt ;
@@ -1120,6 +1148,24 @@ new_time_ex(int hour, int minute, int second, int usecond,
1120
1148
#define new_time (hh , mm , ss , us , tzinfo , fold ) \
1121
1149
new_time_ex2(hh, mm, ss, us, tzinfo, fold, &PyDateTime_TimeType)
1122
1150
1151
+ static PyObject *
1152
+ new_time_subclass_fold_ex (int hour , int minute , int second , int usecond ,
1153
+ PyObject * tzinfo , int fold , PyObject * cls )
1154
+ {
1155
+ PyObject * t ;
1156
+ if ((PyTypeObject * )cls == & PyDateTime_TimeType ) {
1157
+ // Use the fast path constructor
1158
+ t = new_time (hour , minute , second , usecond , tzinfo , fold );
1159
+ }
1160
+ else {
1161
+ // Subclass
1162
+ t = call_subclass_fold (cls , fold , "iiiiO" , hour , minute , second ,
1163
+ usecond , tzinfo );
1164
+ }
1165
+
1166
+ return t ;
1167
+ }
1168
+
1123
1169
/* Create a timedelta instance. Normalize the members iff normalize is
1124
1170
* true. Passing false is a speed optimization, if you know for sure
1125
1171
* that seconds and microseconds are already in their proper ranges. In any
@@ -3480,7 +3526,7 @@ datetime_date_replace_impl(PyDateTime_Date *self, int year, int month,
3480
3526
int day )
3481
3527
/*[clinic end generate
D0E4
d code: output=2a9430d1e6318aeb input=0d1f02685b3e90f6]*/
3482
3528
{
3483
- return new_date_ex (year , month , day , Py_TYPE (self ));
3529
+ return new_date_subclass_ex (year , month , day , ( PyObject * ) Py_TYPE (self ));
3484
3530
}
3485
3531
3486
3532
static Py_hash_t
@@ -4589,8 +4635,8 @@ datetime_time_replace_impl(PyDateTime_Time *self, int hour, int minute,
4589
4635
int fold )
4590
4636
/*[clinic end generated code: output=0b89a44c299e4f80 input=9b6a35b1e704b0ca]*/
4591
4637
{
4592
- return new_time_ex2 (hour , minute , second , microsecond , tzinfo , fold ,
4593
- Py_TYPE (self ));
4638
+ return new_time_subclass_fold_ex (hour , minute , second , microsecond , tzinfo ,
4639
+ fold , ( PyObject * ) Py_TYPE (self ));
4594
4640
}
4595
4641
4596
4642
static PyObject *
@@ -6039,8 +6085,9 @@ datetime_datetime_replace_impl(PyDateTime_DateTime *self, int year,
6039
6085
int fold )
6040
6086
/*[clinic end generated code: output=00bc96536833fddb input=9b38253d56d9bcad]*/
6041
6087
{
6042
- return new_datetime_ex2 (year , month , day , hour , minute , second ,
6043
- microsecond , tzinfo , fold , Py_TYPE (self ));
6088
+ return new_datetime_subclass_fold_ex (year , month , day , hour , minute ,
6089
+ second , microsecond , tzinfo , fold ,
6090
+ (PyObject * )Py_TYPE (self ));
6044
6091
}
6045
6092
6046
6093
static PyObject *
0 commit comments