8000 gh-89039: call subclass constructors in datetime.*.replace by eltoder · Pull Request #114780 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-89039: call subclass constructors in datetime.*.replace #114780

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 56 additions & 6 deletions Lib/test/datetimetester.py
Original file line number Diff line number Diff line change
Expand Up @@ -1723,11 +1723,24 @@ def test_replace(self):

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)
self.assertIs(type(copy.replace(dt, year=2013)), DateSubclass)

test_cases = [
('self.replace', dt.replace(year=2013)),
('copy.replace', copy.replace(dt, year=2013)),
]

for name, res in test_cases:
with self.subTest(name):
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):

Expand Down Expand Up @@ -3025,6 +3038,26 @@ def __new__(cls, *args, **kwargs):
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),
('copy.replace', copy.replace(dt, year=2013), 0),
('copy.replace', copy.replace(dt2, 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 = [
Expand Down Expand Up @@ -3705,11 +3738,28 @@ def test_replace(self):

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)
self.assertIs(type(copy.replace(ctime, 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),
('copy.replace', copy.replace(ctime, hour=10), 0),
('copy.replace', copy.replace(ctime2, 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):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
When replace() method is called on a subclass of datetime, date or time,
properly call derived constructor. Previously, only the base class's
constructor was called.

Also, make sure to pass non-zero fold values when creating subclasses in
various methods. Previously, fold was silently ignored.
77 changes: 62 additions & 15 deletions Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,40 @@ new_datetime_ex(int year, int month, int day, int hour, int minute,
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,
Expand All @@ -1054,17 +1088,11 @@ new_datet 10000 ime_subclass_fold_ex(int year, int month, int day, int hour, int minute
// 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;
Expand Down Expand Up @@ -1120,6 +1148,24 @@ new_time_ex(int hour, int minute, int second, int usecond,
#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
Expand Down Expand Up @@ -3482,7 +3528,7 @@ datetime_date_replace_impl(PyDateTime_Date *self, int year, int month,
int day)
/*[clinic end generated code: output=2a9430d1e6318aeb input=0d1f02685b3e90f6]*/
{
return new_date_ex(year, month, day, Py_TYPE(self));
return new_date_subclass_ex(year, month, day, (PyObject *)Py_TYPE(self));
}

static Py_hash_t
Expand Down Expand Up @@ -4591,8 +4637,8 @@ datetime_time_replace_impl(PyDateTime_Time *self, int hour, int minute,
int fold)
/*[clinic end generated code: output=0b89a44c299e4f80 input=9b6a35b1e704b0ca]*/
{
return new_time_ex2(hour, minute, second, microsecond, tzinfo, fold,
Py_TYPE(self));
return new_time_subclass_fold_ex(hour, minute, second, microsecond, tzinfo,
fold, (PyObject *)Py_TYPE(self));
}

static PyObject *
Expand Down Expand Up @@ -6055,8 +6101,9 @@ datetime_datetime_replace_impl(PyDateTime_DateTime *self, int year,
int fold)
/*[clinic end generated code: output=00bc96536833fddb input=9b38253d56d9bcad]*/
{
return new_datetime_ex2(year, month, day, hour, minute, second,
microsecond, tzinfo, fold, Py_TYPE(self));
return new_datetime_subclass_fold_ex(year, month, day, hour, minute,
second, microsecond, tzinfo, fold,
(PyObject *)Py_TYPE(self));
}

static PyObject *
Expand Down
0