@@ -1045,6 +1045,39 @@ 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
+ PyObject * kwargs = NULL , * res = NULL ;
1051
+ va_list va ;
1052
+
1053
+ va_start (va , format );
1054
+ PyObject * args = Py_VaBuildValue (format , va );
1055
+ va_end (va );
1056
+ if (args == NULL ) {
1057
+ return NULL ;
1058
+ }
1059
+ if (fold ) {
1060
+ kwargs = PyDict_New ();
1061
+ if (kwargs == NULL ) {
1062
+ goto Done ;
1063
+ }
1064
+ PyObject * obj = PyLong_FromLong (fold );
1065
+ if (obj == NULL ) {
1066
+ goto Done ;
1067
+ }
1068
+ int res = PyDict_SetItemString (kwargs , "fold" , obj );
1069
+ Py_DECREF (obj );
1070
+ if (res < 0 ) {
1071
+ goto Done ;
1072
+ }
1073
+ }
1074
+ res = PyObject_Call (cls , args , kwargs );
1075
+ Done :
1076
+ Py_DECREF (args );
1077
+ Py_XDECREF (kwargs );
1078
+ return res ;
1079
+ }
1080
+
1048
1081
static PyObject *
1049
1082
new_datetime_subclass_fold_ex (int year , int month , int day , int hour , int minute ,
1050
1083
int second , int usecond , PyObject * tzinfo ,
@@ -1056,15 +1089,8 @@ new_datetime_subclass_fold_ex(int year, int month, int day, int hour, int minute
1056
1089
tzinfo , fold );
1057
1090
} else {
1058
1091
// Subclass
1059
- dt = PyObject_CallFunction (cls , "iiiiiiiO" ,
1060
- year ,
1061
- month ,
1062
- day ,
1063
- hour ,
1064
- minute ,
1065
- second ,
1066
- usecond ,
1067
- tzinfo );
1092
+ dt = call_subclass_fold (cls , fold , "iiiiiiiO" , year , month , day ,
1093
+ hour , minute , second , usecond , tzinfo );
1068
1094
}
1069
1095
1070
1096
return dt ;
@@ -1120,6 +1146,23 @@ new_time_ex(int hour, int minute, int second, int usecond,
1120
1146
#define new_time (hh , mm , ss , us , tzinfo , fold ) \
1121
1147
new_time_ex2(hh, mm, ss, us, tzinfo, fold, &PyDateTime_TimeType)
1122
1148
1149
+ static PyObject *
1150
+ new_time_subclass_fold_ex (int hour , int minute , int second , int usecond ,
1151
+ PyObject * tzinfo , int fold , PyObject * cls )
1152
+ {
1153
+ PyObject * t ;
1154
+ if ((PyTypeObject * )cls == & PyDateTime_TimeType ) {
1155
+ // Use the fast path constructor
1156
+ t = new_time (hour , minute , second , usecond , tzinfo , fold );
1157
+ } else {
1158
+ // Subclass
1159
+ t = call_subclass_fold (cls , fold , "iiiiO" , hour , minute , second ,
1160
+ usecond , tzinfo );
1161
+ }
1162
+
1163
+ return t ;
1164
+ }
1165
+
1123
1166
/* Create a timedelta instance. Normalize the members iff normalize is
1124
1167
* true. Passing false is a speed optimization, if you know for sure
1125
1168
* that seconds and microseconds are already in their proper ranges. In any
@@ -3482,7 +3525,7 @@ datetime_date_replace_impl(PyDateTime_Date *self, int year, int month,
3482
3525
int day )
3483
3526
/*[clinic end generated code: output=2a9430d1e6318aeb input=0d1f02685b3e90f6]*/
3484
3527
{
3485
- return new_date_ex (year , month , day , Py_TYPE (self ));
3528
+ return new_date_subclass_ex (year , month , day , ( PyObject * ) Py_TYPE (self ));
3486
3529
}
3487
3530
3488
3531
static Py_hash_t
@@ -4591,8 +4634,8 @@ datetime_time_replace_impl(PyDateTime_Time *self, int hour, int minute,
4591
4634
int fold )
4592
4635
/*[clinic end generated code: output=0b89a44c299e4f80 input=9b6a35b1e704b0ca]*/
4593
4636
{
4594
- return new_time_ex2 (hour , minute , second , microsecond , tzinfo , fold ,
4595
- Py_TYPE (self ));
4637
+ return new_time_subclass_fold_ex (hour , minute , second , microsecond , tzinfo ,
4638
+ fold , ( PyObject * ) Py_TYPE (self ));
4596
4639
}
4597
4640
4598
4641
static PyObject *
@@ -6055,8 +6098,9 @@ datetime_datetime_replace_impl(PyDateTime_DateTime *self, int year,
6055
6098
int fold )
6056
6099
/*[clinic end generated code: output=00bc96536833fddb input=9b38253d56d9bcad]*/
6057
6100
{
6058
- return new_datetime_ex2 (year , month , day , hour , minute , second ,
6059
- microsecond , tzinfo , fold , Py_TYPE (self ));
6101
+ return new_datetime_subclass_fold_ex (year , month , day , hour , minute ,
6102
+ second , microsecond , tzinfo , fold ,
6103
+ (PyObject * )Py_TYPE (self ));
6060
6104
}
6061
6105
6062
6106
static PyObject *
0 commit comments