summaryrefslogtreecommitdiffstats
path: root/Lib/datetime.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2018-12-07 12:56:02 (GMT)
committerGitHub <noreply@github.com>2018-12-07 12:56:02 (GMT)
commit0d5730e6437b157f4aeaf5d2e67abca23448c29a (patch)
tree42a1f8633486dbbe36131f7ea522c72b69438426 /Lib/datetime.py
parent602d307ac5e8a2da38a193dca3bdfef5994dfe67 (diff)
downloadcpython-0d5730e6437b157f4aeaf5d2e67abca23448c29a.zip
cpython-0d5730e6437b157f4aeaf5d2e67abca23448c29a.tar.gz
cpython-0d5730e6437b157f4aeaf5d2e67abca23448c29a.tar.bz2
[3.7] bpo-22005: Fixed unpickling instances of datetime classes pickled by Python 2. (GH-11017) (GH-11022)
encoding='latin1' should be used for successful decoding. (cherry picked from commit 8452ca15f41061c8a6297d7956df22ab476d4df4)
Diffstat (limited to 'Lib/datetime.py')
-rw-r--r--Lib/datetime.py38
1 files changed, 34 insertions, 4 deletions
diff --git a/Lib/datetime.py b/Lib/datetime.py
index 8bffbef..a964b20 100644
--- a/Lib/datetime.py
+++ b/Lib/datetime.py
@@ -808,9 +808,19 @@ class date:
year, month, day (required, base 1)
"""
- if month is None and isinstance(year, bytes) and len(year) == 4 and \
- 1 <= year[2] <= 12:
+ if (month is None and
+ isinstance(year, (bytes, str)) and len(year) == 4 and
+ 1 <= ord(year[2:3]) <= 12):
# Pickle support
+ if isinstance(year, str):
+ try:
+ year = year.encode('latin1')
+ except UnicodeEncodeError:
+ # More informative error message.
+ raise ValueError(
+ "Failed to encode latin1 string when unpickling "
+ "a date object. "
+ "pickle.load(data, encoding='latin1') is assumed.")
self = object.__new__(cls)
self.__setstate(year)
self._hashcode = -1
@@ -1184,8 +1194,18 @@ class time:
tzinfo (default to None)
fold (keyword only, default to zero)
"""
- if isinstance(hour, bytes) and len(hour) == 6 and hour[0]&0x7F < 24:
+ if (isinstance(hour, (bytes, str)) and len(hour) == 6 and
+ ord(hour[0:1])&0x7F < 24):
# Pickle support
+ if isinstance(hour, str):
+ try:
+ hour = hour.encode('latin1')
+ except UnicodeEncodeError:
+ # More informative error message.
+ raise ValueError(
+ "Failed to encode latin1 string when unpickling "
+ "a time object. "
+ "pickle.load(data, encoding='latin1') is assumed.")
self = object.__new__(cls)
self.__setstate(hour, minute or None)
self._hashcode = -1
@@ -1496,8 +1516,18 @@ class datetime(date):
def __new__(cls, year, month=None, day=None, hour=0, minute=0, second=0,
microsecond=0, tzinfo=None, *, fold=0):
- if isinstance(year, bytes) and len(year) == 10 and 1 <= year[2]&0x7F <= 12:
+ if (isinstance(year, (bytes, str)) and len(year) == 10 and
+ 1 <= ord(year[2:3])&0x7F <= 12):
# Pickle support
+ if isinstance(year, str):
+ try:
+ year = bytes(year, 'latin1')
+ except UnicodeEncodeError:
+ # More informative error message.
+ raise ValueError(
+ "Failed to encode latin1 string when unpickling "
+ "a datetime object. "
+ "pickle.load(data, encoding='latin1') is assumed.")
self = object.__new__(cls)
self.__setstate(year, month)
self._hashcode = -1