From 89211b6ca669035334a314df01a8bd648de4bb72 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 30 Nov 2015 13:55:44 +0100 Subject: [PATCH] Port _validate_time() to Python 3 The change Ib342ef6640efbc3d76c4bafc622ccfdfa3d16ade added a new _validate_time() function for API v1. On Python 2, the function relies on the datetime module to get a ValueError for datetime with year older than 1900. On Python 3, the datetime module accepts years before 1900 and so we have to test manually. Change-Id: Id1dbf98ecf23e401f696a575dfea80fac2e297ed --- glance/api/v1/images.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/glance/api/v1/images.py b/glance/api/v1/images.py index a50aa51602..2915c47544 100644 --- a/glance/api/v1/images.py +++ b/glance/api/v1/images.py @@ -78,6 +78,12 @@ def _validate_time(req, values): if time_field in values and values[time_field]: try: time = timeutils.parse_isotime(values[time_field]) + # On Python 2, datetime.datetime.strftime() raises a ValueError + # for years older than 1900. On Python 3, years older than 1900 + # are accepted. But we explicitly want to reject timestamps + # older than January 1st, 1900 for Glance API v1. + if time.year < 1900: + raise ValueError values[time_field] = time.strftime( timeutils.PERFECT_TIME_FORMAT) except ValueError: