mirror of https://github.com/python/cpython
The date class is now properly subclassable. (SF bug #720908)
(This is only the tip of the iceberg; the time and datetime classes need the same treatment.)
This commit is contained in:
parent
4dcdb78c6f
commit
8b7a9a38c6
|
@ -479,6 +479,12 @@ class TestDateOnly(unittest.TestCase):
|
||||||
dt2 = dt - delta
|
dt2 = dt - delta
|
||||||
self.assertEqual(dt2, dt - days)
|
self.assertEqual(dt2, dt - days)
|
||||||
|
|
||||||
|
def test_subclass_date(self):
|
||||||
|
class C(date):
|
||||||
|
theAnswer = 42
|
||||||
|
dt = C(2003, 4, 14)
|
||||||
|
self.assertEqual(dt.__class__, C)
|
||||||
|
|
||||||
class TestDate(HarmlessMixedComparison):
|
class TestDate(HarmlessMixedComparison):
|
||||||
# Tests here should pass for both dates and datetimes, except for a
|
# Tests here should pass for both dates and datetimes, except for a
|
||||||
# few tests that TestDateTime overrides.
|
# few tests that TestDateTime overrides.
|
||||||
|
|
|
@ -330,6 +330,8 @@ Extension modules
|
||||||
|
|
||||||
- datetime changes:
|
- datetime changes:
|
||||||
|
|
||||||
|
The date class is now properly subclassable. (SF bug #720908)
|
||||||
|
|
||||||
The datetime and datetimetz classes have been collapsed into a single
|
The datetime and datetimetz classes have been collapsed into a single
|
||||||
datetime class, and likewise the time and timetz classes into a single
|
datetime class, and likewise the time and timetz classes into a single
|
||||||
time class. Previously, a datetimetz object with tzinfo=None acted
|
time class. Previously, a datetimetz object with tzinfo=None acted
|
||||||
|
|
|
@ -1291,16 +1291,19 @@ set_date_fields(PyDateTime_Date *self, int y, int m, int d)
|
||||||
|
|
||||||
/* Create a date instance with no range checking. */
|
/* Create a date instance with no range checking. */
|
||||||
static PyObject *
|
static PyObject *
|
||||||
new_date(int year, int month, int day)
|
new_date_ex(int year, int month, int day, PyTypeObject *type)
|
||||||
{
|
{
|
||||||
PyDateTime_Date *self;
|
PyDateTime_Date *self;
|
||||||
|
|
||||||
self = PyObject_New(PyDateTime_Date, &PyDateTime_DateType);
|
self = (PyDateTime_Date *) (type->tp_alloc(type, 0));
|
||||||
if (self != NULL)
|
if (self != NULL)
|
||||||
set_date_fields(self, year, month, day);
|
set_date_fields(self, year, month, day);
|
||||||
return (PyObject *) self;
|
return (PyObject *) self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define new_date(year, month, day) \
|
||||||
|
(new_date_ex(year, month, day, &PyDateTime_DateType))
|
||||||
|
|
||||||
/* Create a datetime instance with no range checking. */
|
/* Create a datetime instance with no range checking. */
|
||||||
static PyObject *
|
static PyObject *
|
||||||
new_datetime(int year, int month, int day, int hour, int minute,
|
new_datetime(int year, int month, int day, int hour, int minute,
|
||||||
|
@ -2168,7 +2171,7 @@ date_new(PyTypeObject *type, PyObject *args, PyObject *kw)
|
||||||
{
|
{
|
||||||
PyDateTime_Date *me;
|
PyDateTime_Date *me;
|
||||||
|
|
||||||
me = PyObject_New(PyDateTime_Date, &PyDateTime_DateType);
|
me = PyObject_New(PyDateTime_Date, type);
|
||||||
if (me != NULL) {
|
if (me != NULL) {
|
||||||
char *pdata = PyString_AS_STRING(state);
|
char *pdata = PyString_AS_STRING(state);
|
||||||
memcpy(me->data, pdata, _PyDateTime_DATE_DATASIZE);
|
memcpy(me->data, pdata, _PyDateTime_DATE_DATASIZE);
|
||||||
|
@ -2181,7 +2184,7 @@ date_new(PyTypeObject *type, PyObject *args, PyObject *kw)
|
||||||
&year, &month, &day)) {
|
&year, &month, &day)) {
|
||||||
if (check_date_args(year, month, day) < 0)
|
if (check_date_args(year, month, day) < 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
self = new_date(year, month, day);
|
self = new_date_ex(year, month, day, type);
|
||||||
}
|
}
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
@ -2632,7 +2635,7 @@ static PyTypeObject PyDateTime_DateType = {
|
||||||
"datetime.date", /* tp_name */
|
"datetime.date", /* tp_name */
|
||||||
sizeof(PyDateTime_Date), /* tp_basicsize */
|
sizeof(PyDateTime_Date), /* tp_basicsize */
|
||||||
0, /* tp_itemsize */
|
0, /* tp_itemsize */
|
||||||
(destructor)PyObject_Del, /* tp_dealloc */
|
0, /* tp_dealloc */
|
||||||
0, /* tp_print */
|
0, /* tp_print */
|
||||||
0, /* tp_getattr */
|
0, /* tp_getattr */
|
||||||
0, /* tp_setattr */
|
0, /* tp_setattr */
|
||||||
|
|
Loading…
Reference in New Issue