def test_subclass_replace(self):
class DateSubclass(self.theclass):
- pass
+ def __new__(cls, *args, **kwargs):
+ result = self.theclass.__new__(cls, *args, **kwargs)
+ result.extra = 7
+ return result
dt = DateSubclass(2012, 1, 1)
- self.assertIs(type(dt.replace(year=2013)), DateSubclass)
+ res = dt.replace(year=2013)
+ self.assertIs(type(res), DateSubclass)
+ self.assertEqual(res.year, 2013)
+ self.assertEqual(res.month, 1)
+ self.assertEqual(res.extra, 7)
def test_subclass_date(self):
self.assertIsInstance(dt, DateTimeSubclass)
self.assertEqual(dt.extra, 7)
+ def test_subclass_replace_fold(self):
+ class DateTimeSubclass(self.theclass):
+ pass
+
+ dt = DateTimeSubclass(2012, 1, 1)
+ dt2 = DateTimeSubclass(2012, 1, 1, fold=1)
+
+ test_cases = [
+ ('self.replace', dt.replace(year=2013), 0),
+ ('self.replace', dt2.replace(year=2013), 1),
+ ]
+
+ for name, res, fold in test_cases:
+ with self.subTest(name, fold=fold):
+ self.assertIs(type(res), DateTimeSubclass)
+ self.assertEqual(res.year, 2013)
+ self.assertEqual(res.fold, fold)
+
def test_fromisoformat_datetime(self):
# Test that isoformat() is reversible
base_dates = [
def test_subclass_replace(self):
class TimeSubclass(self.theclass):
- pass
+ def __new__(cls, *args, **kwargs):
+ result = self.theclass.__new__(cls, *args, **kwargs)
+ result.extra = 7
+ return result
ctime = TimeSubclass(12, 30)
- self.assertIs(type(ctime.replace(hour=10)), TimeSubclass)
+ ctime2 = TimeSubclass(12, 30, fold=1)
+
+ test_cases = [
+ ('self.replace', ctime.replace(hour=10), 0),
+ ('self.replace', ctime2.replace(hour=10), 1),
+ ]
+
+ for name, res, fold in test_cases:
+ with self.subTest(name, fold=fold):
+ self.assertIs(type(res), TimeSubclass)
+ self.assertEqual(res.hour, 10)
+ self.assertEqual(res.minute, 30)
+ self.assertEqual(res.extra, 7)
+ self.assertEqual(res.fold, fold)
def test_subclass_time(self):
new_datetime_ex2(y, m, d, hh, mm, ss, us, tzinfo, fold, \
&PyDateTime_DateTimeType)
+static PyObject *
+call_subclass_fold(PyObject *cls, int fold, const char *format, ...)
+{
+ PyObject *kwargs = NULL, *res = NULL;
+ va_list va;
+
+ va_start(va, format);
+ PyObject *args = Py_VaBuildValue(format, va);
+ va_end(va);
+ if (args == NULL) {
+ return NULL;
+ }
+ if (fold) {
+ kwargs = PyDict_New();
+ if (kwargs == NULL) {
+ goto Done;
+ }
+ PyObject *obj = PyLong_FromLong(fold);
+ if (obj == NULL) {
+ goto Done;
+ }
+ int err = PyDict_SetItemString(kwargs, "fold", obj);
+ Py_DECREF(obj);
+ if (err < 0) {
+ goto Done;
+ }
+ }
+ res = PyObject_Call(cls, args, kwargs);
+Done:
+ Py_DECREF(args);
+ Py_XDECREF(kwargs);
+ return res;
+}
+
static PyObject *
new_datetime_subclass_fold_ex(int year, int month, int day, int hour, int minute,
int second, int usecond, PyObject *tzinfo,
// Use the fast path constructor
dt = new_datetime(year, month, day, hour, minute, second, usecond,
tzinfo, fold);
- } else {
+ }
+ else {
// Subclass
- dt = PyObject_CallFunction(cls, "iiiiiiiO",
- year,
- month,
- day,
- hour,
- minute,
- second,
- usecond,
- tzinfo);
+ dt = call_subclass_fold(cls, fold, "iiiiiiiO", year, month, day,
+ hour, minute, second, usecond, tzinfo);
}
return dt;
#define new_time(hh, mm, ss, us, tzinfo, fold) \
new_time_ex2(hh, mm, ss, us, tzinfo, fold, &PyDateTime_TimeType)
+static PyObject *
+new_time_subclass_fold_ex(int hour, int minute, int second, int usecond,
+ PyObject *tzinfo, int fold, PyObject *cls)
+{
+ PyObject *t;
+ if ((PyTypeObject*)cls == &PyDateTime_TimeType) {
+ // Use the fast path constructor
+ t = new_time(hour, minute, second, usecond, tzinfo, fold);
+ }
+ else {
+ // Subclass
+ t = call_subclass_fold(cls, fold, "iiiiO", hour, minute, second,
+ usecond, tzinfo);
+ }
+
+ return t;
+}
+
/* Create a timedelta instance. Normalize the members iff normalize is
* true. Passing false is a speed optimization, if you know for sure
* that seconds and microseconds are already in their proper ranges. In any
static PyObject *
date_replace(PyDateTime_Date *self, PyObject *args, PyObject *kw)
{
- PyObject *clone;
- PyObject *tuple;
int year = GET_YEAR(self);
int month = GET_MONTH(self);
int day = GET_DAY(self);
if (! PyArg_ParseTupleAndKeywords(args, kw, "|iii:replace", date_kws,
&year, &month, &day))
return NULL;
- tuple = Py_BuildValue("iii", year, month, day);
- if (tuple == NULL)
- return NULL;
- clone = date_new(Py_TYPE(self), tuple, NULL);
- Py_DECREF(tuple);
- return clone;
+ return new_date_subclass_ex(year, month, day, (PyObject *)Py_TYPE(self));
}
static Py_hash_t
static PyObject *
time_replace(PyDateTime_Time *self, PyObject *args, PyObject *kw)
{
- PyObject *clone;
- PyObject *tuple;
int hh = TIME_GET_HOUR(self);
int mm = TIME_GET_MINUTE(self);
int ss = TIME_GET_SECOND(self);
"fold must be either 0 or 1");
return NULL;
}
- tuple = Py_BuildValue("iiiiO", hh, mm, ss, us, tzinfo);
- if (tuple == NULL)
- return NULL;
- clone = time_new(Py_TYPE(self), tuple, NULL);
- if (clone != NULL) {
- TIME_SET_FOLD(clone, fold);
- }
- Py_DECREF(tuple);
- return clone;
+ return new_time_subclass_fold_ex(hh, mm, ss, us, tzinfo, fold,
+ (PyObject *)Py_TYPE(self));
}
static PyObject *
static PyObject *
datetime_replace(PyDateTime_DateTime *self, PyObject *args, PyObject *kw)
{
- PyObject *clone;
- PyObject *tuple;
int y = GET_YEAR(self);
int m = GET_MONTH(self);
int d = GET_DAY(self);
"fold must be either 0 or 1");
return NULL;
}
- tuple = Py_BuildValue("iiiiiiiO", y, m, d, hh, mm, ss, us, tzinfo);
- if (tuple == NULL)
- return NULL;
- clone = datetime_new(Py_TYPE(self), tuple, NULL);
- if (clone != NULL) {
- DATE_SET_FOLD(clone, fold);
- }
- Py_DECREF(tuple);
- return clone;
+ return new_datetime_subclass_fold_ex(y, m, d, hh, mm, ss, us, tzinfo, fold,
+ (PyObject *)Py_TYPE(self));
}
static PyObject *