summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorUtkarsh Upadhyay <mail@musicallyut.in>2017-07-25 21:51:33 (GMT)
committerVictor Stinner <victor.stinner@gmail.com>2017-07-25 21:51:33 (GMT)
commitcc5a65cd9025280ea67ef4bbc2a8bfe31ced6c30 (patch)
tree4565cd48860cd2c8581225565509ea327293b7f9 /Modules
parent830080913c22a9834d310294b9f7653234dc6a59 (diff)
downloadcpython-cc5a65cd9025280ea67ef4bbc2a8bfe31ced6c30.zip
cpython-cc5a65cd9025280ea67ef4bbc2a8bfe31ced6c30.tar.gz
cpython-cc5a65cd9025280ea67ef4bbc2a8bfe31ced6c30.tar.bz2
bpo-30302 Make timedelta.__repr__ more informative. (#1493)
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_datetimemodule.c59
1 files changed, 44 insertions, 15 deletions
diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c
index 3439040..28805d1 100644
--- a/Modules/_datetimemodule.c
+++ b/Modules/_datetimemodule.c
@@ -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));
-
- return PyUnicode_FromFormat("%s(%d)",
- Py_TYPE(self)->tp_name,
- GET_TD_DAYS(self));
+ PyObject *args = PyUnicode_FromString("");
+
+ 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 *