diff options
author | Georg Brandl <georg@python.org> | 2011-01-02 22:33:43 (GMT) |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2011-01-02 22:33:43 (GMT) |
commit | e10608cf5de214985308a8d9eb6ace07bc9c590e (patch) | |
tree | c98ff816df5594b3c64311a0740184c75dfb1832 /Modules | |
parent | 3e913c9ecf5fc7e4a0daea116ac73d3fae4ab84c (diff) | |
download | cpython-e10608cf5de214985308a8d9eb6ace07bc9c590e.zip cpython-e10608cf5de214985308a8d9eb6ace07bc9c590e.tar.gz cpython-e10608cf5de214985308a8d9eb6ace07bc9c590e.tar.bz2 |
#8013 follow-up:
* In asctime and ctime, properly remove the newline if the year has more than four digits
* Consistent error message for both functions
* Fix the test comments and add a check for the removed newline
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/timemodule.c | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/Modules/timemodule.c b/Modules/timemodule.c index e8ea661..4a0bacb 100644 --- a/Modules/timemodule.c +++ b/Modules/timemodule.c @@ -611,7 +611,7 @@ time_asctime(PyObject *self, PyObject *args) { PyObject *tup = NULL; struct tm buf; - char *p; + char *p, *q; if (!PyArg_UnpackTuple(args, "asctime", 0, 1, &tup)) return NULL; if (tup == NULL) { @@ -621,11 +621,14 @@ time_asctime(PyObject *self, PyObject *args) return NULL; p = asctime(&buf); if (p == NULL) { - PyErr_SetString(PyExc_ValueError, "invalid time"); + PyErr_SetString(PyExc_ValueError, "unconvertible time"); return NULL; } - if (p[24] == '\n') - p[24] = '\0'; + /* Replace a terminating newline by a null byte, normally at position 24. + * It can occur later if the year has more than four digits. */ + for (q = p+24; *q != '\0'; q++) + if (*q == '\n') + *q = '\0'; return PyUnicode_FromString(p); } @@ -641,7 +644,7 @@ time_ctime(PyObject *self, PyObject *args) { PyObject *ot = NULL; time_t tt; - char *p; + char *p, *q; if (!PyArg_UnpackTuple(args, "ctime", 0, 1, &ot)) return NULL; @@ -660,8 +663,11 @@ time_ctime(PyObject *self, PyObject *args) PyErr_SetString(PyExc_ValueError, "unconvertible time"); return NULL; } - if (p[24] == '\n') - p[24] = '\0'; + /* Replace a terminating newline by a null byte, normally at position 24. + * It can occur later if the year has more than four digits. */ + for (q = p+24; *q != '\0'; q++) + if (*q == '\n') + *q = '\0'; return PyUnicode_FromString(p); } |