diff options
author | Walter Dörwald <walter@livinglogic.de> | 2007-05-19 21:49:49 (GMT) |
---|---|---|
committer | Walter Dörwald <walter@livinglogic.de> | 2007-05-19 21:49:49 (GMT) |
commit | 7569dfe11d51a11bfb11002d31245b889916fb11 (patch) | |
tree | 7e4cbbb7a27a366ea0cdc8eeacc05a23f649e4cd /Modules/datetimemodule.c | |
parent | 94b59bb14474b6fe5a272a2c60b65f8375e827b3 (diff) | |
download | cpython-7569dfe11d51a11bfb11002d31245b889916fb11.zip cpython-7569dfe11d51a11bfb11002d31245b889916fb11.tar.gz cpython-7569dfe11d51a11bfb11002d31245b889916fb11.tar.bz2 |
Add a format specifier %R to PyUnicode_FromFormat(), which embeds
the result of a call to PyObject_Repr() into the string. This makes
it possible to simplify many repr implementations.
PyUnicode_FromFormat() uses two steps to create the final string: A first
pass through the format string determines the size of the final string and
a second pass creates the string. To avoid calling PyObject_Repr() twice
for each %R specifier, PyObject_Repr() is called during the size
calculation step and the results are stored in an array (whose size is
determined at the start by counting %R specifiers).
Diffstat (limited to 'Modules/datetimemodule.c')
-rw-r--r-- | Modules/datetimemodule.c | 50 |
1 files changed, 20 insertions, 30 deletions
diff --git a/Modules/datetimemodule.c b/Modules/datetimemodule.c index 5d3c679..e03148c 100644 --- a/Modules/datetimemodule.c +++ b/Modules/datetimemodule.c @@ -1973,19 +1973,19 @@ delta_repr(PyDateTime_Delta *self) { if (GET_TD_MICROSECONDS(self) != 0) return PyUnicode_FromFormat("%s(%d, %d, %d)", - self->ob_type->tp_name, - GET_TD_DAYS(self), - GET_TD_SECONDS(self), - GET_TD_MICROSECONDS(self)); + self->ob_type->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)", - self->ob_type->tp_name, - GET_TD_DAYS(self), - GET_TD_SECONDS(self)); + self->ob_type->tp_name, + GET_TD_DAYS(self), + GET_TD_SECONDS(self)); return PyUnicode_FromFormat("%s(%d)", - self->ob_type->tp_name, - GET_TD_DAYS(self)); + self->ob_type->tp_name, + GET_TD_DAYS(self)); } static PyObject * @@ -2402,15 +2402,9 @@ date_subtract(PyObject *left, PyObject *right) static PyObject * date_repr(PyDateTime_Date *self) { - char buffer[1028]; - const char *type_name; - - type_name = self->ob_type->tp_name; - PyOS_snprintf(buffer, sizeof(buffer), "%s(%d, %d, %d)", - type_name, - GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); - - return PyUnicode_FromString(buffer); + return PyUnicode_FromFormat("%s(%d, %d, %d)", + self->ob_type->tp_name, + GET_YEAR(self), GET_MONTH(self), GET_DAY(self)); } static PyObject * @@ -3114,7 +3108,6 @@ time_tzname(PyDateTime_Time *self, PyObject *unused) { static PyObject * time_repr(PyDateTime_Time *self) { - char buffer[100]; const char *type_name = self->ob_type->tp_name; int h = TIME_GET_HOUR(self); int m = TIME_GET_MINUTE(self); @@ -3123,15 +3116,13 @@ time_repr(PyDateTime_Time *self) PyObject *result = NULL; if (us) - PyOS_snprintf(buffer, sizeof(buffer), - "%s(%d, %d, %d, %d)", type_name, h, m, s, us); + result = PyUnicode_FromFormat("%s(%d, %d, %d, %d)", + type_name, h, m, s, us); else if (s) - PyOS_snprintf(buffer, sizeof(buffer), - "%s(%d, %d, %d)", type_name, h, m, s); + result = PyUnicode_FromFormat("%s(%d, %d, %d)", + type_name, h, m, s); else - PyOS_snprintf(buffer, sizeof(buffer), - "%s(%d, %d)", type_name, h, m); - result = PyUnicode_FromString(buffer); + result = PyUnicode_FromFormat("%s(%d, %d)", type_name, h, m); if (result != NULL && HASTZINFO(self)) result = append_keyword_tzinfo(result, self->tzinfo); return result; @@ -4020,7 +4011,7 @@ datetime_repr(PyDateTime_DateTime *self) PyObject *baserepr; if (DATE_GET_MICROSECOND(self)) { - PyOS_snprintf(buffer, sizeof(buffer), + baserepr = PyUnicode_FromFormat( "%s(%d, %d, %d, %d, %d, %d, %d)", type_name, GET_YEAR(self), GET_MONTH(self), GET_DAY(self), @@ -4029,7 +4020,7 @@ datetime_repr(PyDateTime_DateTime *self) DATE_GET_MICROSECOND(self)); } else if (DATE_GET_SECOND(self)) { - PyOS_snprintf(buffer, sizeof(buffer), + baserepr = PyUnicode_FromFormat( "%s(%d, %d, %d, %d, %d, %d)", type_name, GET_YEAR(self), GET_MONTH(self), GET_DAY(self), @@ -4037,13 +4028,12 @@ datetime_repr(PyDateTime_DateTime *self) DATE_GET_SECOND(self)); } else { - PyOS_snprintf(buffer, sizeof(buffer), + baserepr = PyUnicode_FromFormat( "%s(%d, %d, %d, %d, %d)", type_name, GET_YEAR(self), GET_MONTH(self), GET_DAY(self), DATE_GET_HOUR(self), DATE_GET_MINUTE(self)); } - baserepr = PyUnicode_FromString(buffer); if (baserepr == NULL || ! HASTZINFO(self)) return baserepr; return append_keyword_tzinfo(baserepr, self->tzinfo); |