Thanks to visit codestin.com
Credit goes to github.com

Skip to content

bpo-35066: _dateime.datetime.strftime copies trailing '%' #10692

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jan 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Lib/test/datetimetester.py
Original file line number Diff line number Diff line change
Expand Up @@ -1350,6 +1350,17 @@ def test_strftime(self):
#check that this standard extension works
t.strftime("%f")

def test_strftime_trailing_percent(self):
# bpo-35066: make sure trailing '%' doesn't cause
# datetime's strftime to complain
t = self.theclass(2005, 3, 2)
try:
_time.strftime('%')
except ValueError:
self.skipTest('time module does not support trailing %')
self.assertEqual(t.strftime('%'), '%')
self.assertEqual(t.strftime("m:%m d:%d y:%y %"), "m:03 d:02 y:05 %")

def test_format(self):
dt = self.theclass(2007, 9, 10)
self.assertEqual(dt.__format__(''), str(dt))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Previously, calling the strftime() method on a datetime object with a
trailing '%' in the format string would result in an exception. However,
this only occured when the datetime C module was being used; the python
implementation did not match this behavior. Datetime is now PEP-399
compliant, and will not throw an exception on a trailing '%'.
13 changes: 8 additions & 5 deletions Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1528,10 +1528,13 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple,
ntoappend = 1;
}
else if ((ch = *pin++) == '\0') {
/* There's a lone trailing %; doesn't make sense. */
PyErr_SetString(PyExc_ValueError, "strftime format "
"ends with raw %");
goto Done;
/* Null byte follows %, copy only '%'.
*
* Back the pin up one char so that we catch the null check
* the next time through the loop.*/
pin--;
ptoappend = pin - 1;
ntoappend = 1;
}
/* A % has been seen and ch is the character after it. */
else if (ch == 'z') {
Expand Down Expand Up @@ -1616,7 +1619,7 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple,
usednew += ntoappend;
assert(usednew <= totalnew);
} /* end while() */

if (_PyBytes_Resize(&newfmt, usednew) < 0)
goto Done;
{
Expand Down