[issue10563] Spurious newline in time.ctime

Eric Smith report at bugs.python.org
Sun Nov 28 17:17:34 CET 2010


Eric Smith <eric at trueblade.com> added the comment:

Can you try this diff and see if it solves the problem:

Index: Modules/timemodule.c
===================================================================
--- Modules/timemodule.c        (revision 86848)
+++ Modules/timemodule.c        (working copy)
@@ -639,6 +639,7 @@
     PyObject *ot = NULL;
     time_t tt;
     char *p;
+    Py_ssize_t len;
 
     if (!PyArg_UnpackTuple(args, "ctime", 0, 1, &ot))
         return NULL;
@@ -657,8 +658,10 @@
         PyErr_SetString(PyExc_ValueError, "unconvertible time");
         return NULL;
     }
-    if (p[24] == '\n')
-        p[24] = '\0';
+    len = strlen(p);
+    if (len > 0 && p[len-1] == '\n')
+        /* Remove the trailing newline, if present. */
+        p[len-1] = '\0';
     return PyUnicode_FromString(p);
 }

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue10563>
_______________________________________


More information about the Python-bugs-list mailing list