Skip to content

Commit

Permalink
bpo-37372: Fix error unpickling datetime.time objects from Python 2 w…
Browse files Browse the repository at this point in the history
…ith seconds>=24. (GH-14307)

(cherry picked from commit 122376d)

Co-authored-by: Justin Blanchard <UncombedCoconut@gmail.com>
  • Loading branch information
miss-islington and UncombedCoconut committed Aug 29, 2019
1 parent 097eae5 commit d1d42bf
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
27 changes: 18 additions & 9 deletions Lib/test/datetimetester.py
Original file line number Diff line number Diff line change
Expand Up @@ -3324,16 +3324,25 @@ def test_pickling_subclass_time(self):

def test_compat_unpickle(self):
tests = [
b"cdatetime\ntime\n(S'\\x14;\\x10\\x00\\x10\\x00'\ntR.",
b'cdatetime\ntime\n(U\x06\x14;\x10\x00\x10\x00tR.',
b'\x80\x02cdatetime\ntime\nU\x06\x14;\x10\x00\x10\x00\x85R.',
(b"cdatetime\ntime\n(S'\\x14;\\x10\\x00\\x10\\x00'\ntR.",
(20, 59, 16, 64**2)),
(b'cdatetime\ntime\n(U\x06\x14;\x10\x00\x10\x00tR.',
(20, 59, 16, 64**2)),
(b'\x80\x02cdatetime\ntime\nU\x06\x14;\x10\x00\x10\x00\x85R.',
(20, 59, 16, 64**2)),
(b"cdatetime\ntime\n(S'\\x14;\\x19\\x00\\x10\\x00'\ntR.",
(20, 59, 25, 64**2)),
(b'cdatetime\ntime\n(U\x06\x14;\x19\x00\x10\x00tR.',
(20, 59, 25, 64**2)),
(b'\x80\x02cdatetime\ntime\nU\x06\x14;\x19\x00\x10\x00\x85R.',
(20, 59, 25, 64**2)),
]
args = 20, 59, 16, 64**2
expected = self.theclass(*args)
for data in tests:
for loads in pickle_loads:
derived = loads(data, encoding='latin1')
self.assertEqual(derived, expected)
for i, (data, args) in enumerate(tests):
with self.subTest(i=i):
expected = self.theclass(*args)
for loads in pickle_loads:
derived = loads(data, encoding='latin1')
self.assertEqual(derived, expected)

def test_bool(self):
# time is always True.
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ Roy Bixler
Daniel Black
Jonathan Black
Renaud Blanch
Justin Blanchard
Mike Bland
Martin Bless
Pablo Bleyer
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix error unpickling datetime.time objects from Python 2 with seconds>=24.
Patch by Justin Blanchard.
2 changes: 1 addition & 1 deletion Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -4081,7 +4081,7 @@ time_new(PyTypeObject *type, PyObject *args, PyObject *kw)
return NULL;
}
if (PyUnicode_GET_LENGTH(state) == _PyDateTime_TIME_DATASIZE &&
(0x7F & PyUnicode_READ_CHAR(state, 2)) < 24)
(0x7F & PyUnicode_READ_CHAR(state, 0)) < 24)
{
state = PyUnicode_AsLatin1String(state);
if (state == NULL) {
Expand Down

0 comments on commit d1d42bf

Please sign in to comment.