Fixed #27138 -- Restored pre-Python 3.6 behavior of localtime() and make_naive() on Python 3.6.

Reverted test changes in a7a7ecd2b0 and
e43ea36b76 (refs #27025).
This commit is contained in:
Joachim Jablon 2016-11-05 12:33:22 +01:00 committed by Tim Graham
parent ee1bf0e8b5
commit fd78fb82d6
3 changed files with 15 additions and 29 deletions

View File

@ -364,6 +364,7 @@ answer newbie questions, and generally made Django that much better:
Jim Dalton <jim.dalton@gmail.com> Jim Dalton <jim.dalton@gmail.com>
Jimmy Song <jaejoon@gmail.com> Jimmy Song <jaejoon@gmail.com>
Jiri Barton Jiri Barton
Joachim Jablon <ewjoachim@gmail.com>
Joao Oliveira <joaoxsouls@gmail.com> Joao Oliveira <joaoxsouls@gmail.com>
Joe Heck <http://www.rhonabwy.com/wp/> Joe Heck <http://www.rhonabwy.com/wp/>
Joel Bohman <mail@jbohman.com> Joel Bohman <mail@jbohman.com>

View File

@ -209,8 +209,9 @@ def localtime(value=None, timezone=None):
value = now() value = now()
if timezone is None: if timezone is None:
timezone = get_current_timezone() timezone = get_current_timezone()
# If `value` is naive, astimezone() will raise a ValueError, # Emulate the behavior of astimezone() on Python < 3.6.
# so we don't need to perform a redundant check. if is_naive(value):
raise ValueError("localtime() cannot be applied to a naive datetime")
value = value.astimezone(timezone) value = value.astimezone(timezone)
if hasattr(timezone, 'normalize'): if hasattr(timezone, 'normalize'):
# This method is available for pytz time zones. # This method is available for pytz time zones.
@ -295,8 +296,9 @@ def make_naive(value, timezone=None):
""" """
if timezone is None: if timezone is None:
timezone = get_current_timezone() timezone = get_current_timezone()
# If `value` is naive, astimezone() will raise a ValueError, # Emulate the behavior of astimezone() on Python < 3.6.
# so we don't need to perform a redundant check. if is_naive(value):
raise ValueError("make_naive() cannot be applied to a naive datetime")
value = value.astimezone(timezone) value = value.astimezone(timezone)
if hasattr(timezone, 'normalize'): if hasattr(timezone, 'normalize'):
# This method is available for pytz time zones. # This method is available for pytz time zones.

View File

@ -1,6 +1,5 @@
import datetime import datetime
import pickle import pickle
import sys
import pytz import pytz
@ -11,8 +10,6 @@ CET = pytz.timezone("Europe/Paris")
EAT = timezone.get_fixed_timezone(180) # Africa/Nairobi EAT = timezone.get_fixed_timezone(180) # Africa/Nairobi
ICT = timezone.get_fixed_timezone(420) # Asia/Bangkok ICT = timezone.get_fixed_timezone(420) # Asia/Bangkok
PY36 = sys.version_info >= (3, 6)
class TimezoneTests(SimpleTestCase): class TimezoneTests(SimpleTestCase):
@ -24,14 +21,10 @@ class TimezoneTests(SimpleTestCase):
def test_localdate(self): def test_localdate(self):
naive = datetime.datetime(2015, 1, 1, 0, 0, 1) naive = datetime.datetime(2015, 1, 1, 0, 0, 1)
if PY36: with self.assertRaisesMessage(ValueError, 'localtime() cannot be applied to a naive datetime'):
self.assertEqual(timezone.localdate(naive), datetime.date(2015, 1, 1)) timezone.localdate(naive)
self.assertEqual(timezone.localdate(naive, timezone=EAT), datetime.date(2015, 1, 1)) with self.assertRaisesMessage(ValueError, 'localtime() cannot be applied to a naive datetime'):
else: timezone.localdate(naive, timezone=EAT)
with self.assertRaisesMessage(ValueError, 'astimezone() cannot be applied to a naive datetime'):
timezone.localdate(naive)
with self.assertRaisesMessage(ValueError, 'astimezone() cannot be applied to a naive datetime'):
timezone.localdate(naive, timezone=EAT)
aware = datetime.datetime(2015, 1, 1, 0, 0, 1, tzinfo=ICT) aware = datetime.datetime(2015, 1, 1, 0, 0, 1, tzinfo=ICT)
self.assertEqual(timezone.localdate(aware, timezone=EAT), datetime.date(2014, 12, 31)) self.assertEqual(timezone.localdate(aware, timezone=EAT), datetime.date(2014, 12, 31))
@ -133,12 +126,8 @@ class TimezoneTests(SimpleTestCase):
timezone.make_naive(datetime.datetime(2011, 9, 1, 17, 20, 30, tzinfo=ICT), EAT), timezone.make_naive(datetime.datetime(2011, 9, 1, 17, 20, 30, tzinfo=ICT), EAT),
datetime.datetime(2011, 9, 1, 13, 20, 30)) datetime.datetime(2011, 9, 1, 13, 20, 30))
args = (datetime.datetime(2011, 9, 1, 13, 20, 30), EAT) with self.assertRaisesMessage(ValueError, 'make_naive() cannot be applied to a naive datetime'):
if PY36: timezone.make_naive(datetime.datetime(2011, 9, 1, 13, 20, 30), EAT)
self.assertEqual(timezone.make_naive(*args), datetime.datetime(2011, 9, 1, 21, 20, 30))
else:
with self.assertRaisesMessage(ValueError, 'astimezone() cannot be applied to a naive datetime'):
timezone.make_naive(*args)
def test_make_naive_no_tz(self): def test_make_naive_no_tz(self):
self.assertEqual( self.assertEqual(
@ -168,14 +157,8 @@ class TimezoneTests(SimpleTestCase):
pytz.timezone("Asia/Bangkok").localize(datetime.datetime(2011, 9, 1, 17, 20, 30)), CET pytz.timezone("Asia/Bangkok").localize(datetime.datetime(2011, 9, 1, 17, 20, 30)), CET
), ),
datetime.datetime(2011, 9, 1, 12, 20, 30)) datetime.datetime(2011, 9, 1, 12, 20, 30))
if PY36: with self.assertRaisesMessage(ValueError, 'make_naive() cannot be applied to a naive datetime'):
self.assertEqual( timezone.make_naive(datetime.datetime(2011, 9, 1, 12, 20, 30), CET)
timezone.make_naive(datetime.datetime(2011, 9, 1, 12, 20, 30), CET),
datetime.datetime(2011, 9, 1, 19, 20, 30)
)
else:
with self.assertRaises(ValueError):
timezone.make_naive(datetime.datetime(2011, 9, 1, 12, 20, 30), CET)
def test_make_aware_pytz_ambiguous(self): def test_make_aware_pytz_ambiguous(self):
# 2:30 happens twice, once before DST ends and once after # 2:30 happens twice, once before DST ends and once after