mirror of
https://github.com/python/cpython.git
synced 2025-07-24 11:44:31 +00:00
bpo-30302 Make timedelta.__repr__ more informative. (#1493)
This commit is contained in:
parent
830080913c
commit
cc5a65cd90
6 changed files with 73 additions and 32 deletions
|
@ -2284,21 +2284,50 @@ delta_bool(PyDateTime_Delta *self)
|
|||
static PyObject *
|
||||
delta_repr(PyDateTime_Delta *self)
|
||||
{
|
||||
if (GET_TD_MICROSECONDS(self) != 0)
|
||||
return PyUnicode_FromFormat("%s(%d, %d, %d)",
|
||||
Py_TYPE(self)->tp_name,
|
||||
GET_TD_DAYS(self),
|
||||
GET_TD_SECONDS(self),
|
||||
GET_TD_MICROSECONDS(self));
|
||||
if (GET_TD_SECONDS(self) != 0)
|
||||
return PyUnicode_FromFormat("%s(%d, %d)",
|
||||
Py_TYPE(self)->tp_name,
|
||||
GET_TD_DAYS(self),
|
||||
GET_TD_SECONDS(self));
|
||||
PyObject *args = PyUnicode_FromString("");
|
||||
|
||||
return PyUnicode_FromFormat("%s(%d)",
|
||||
Py_TYPE(self)->tp_name,
|
||||
GET_TD_DAYS(self));
|
||||
if (args == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const char *sep = "";
|
||||
|
||||
if (GET_TD_DAYS(self) != 0) {
|
||||
Py_SETREF(args, PyUnicode_FromFormat("days=%d", GET_TD_DAYS(self)));
|
||||
if (args == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
sep = ", ";
|
||||
}
|
||||
|
||||
if (GET_TD_SECONDS(self) != 0) {
|
||||
Py_SETREF(args, PyUnicode_FromFormat("%U%sseconds=%d", args, sep,
|
||||
GET_TD_SECONDS(self)));
|
||||
if (args == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
sep = ", ";
|
||||
}
|
||||
|
||||
if (GET_TD_MICROSECONDS(self) != 0) {
|
||||
Py_SETREF(args, PyUnicode_FromFormat("%U%smicroseconds=%d", args, sep,
|
||||
GET_TD_MICROSECONDS(self)));
|
||||
if (args == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
if (PyUnicode_GET_LENGTH(args) == 0) {
|
||||
Py_SETREF(args, PyUnicode_FromString("0"));
|
||||
if (args == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
PyObject *repr = PyUnicode_FromFormat("%s(%S)", Py_TYPE(self)->tp_name,
|
||||
args);
|
||||
Py_DECREF(args);
|
||||
return repr;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue