Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#22625 closed Bug (fixed)

Installing pytz breaks code already using django's timezone module

Reported by: tom.michaelis@… Owned by: Aymeric Augustin
Component: Utilities Version: 1.6
Severity: Normal Keywords: pytz, timezone
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

pytz is an optional module for Django, used in django.utils.timezone (and possibly elsewhere). The documentation recommends that pytz is installed, but it's not a requirement.

The bug is that after installing pytz, methods in django's timezone module do not present the same interface as before installation. Installing an optional module should not adjust the interface presented.

The following code works differently before and after installation of pytz:

from django.utils import timezone

now = timezone.now()
timezone.make_aware(now, timezone.utc())

Before installing pytz, timezone.make_aware essentially does nothing (the variable is already timezone aware). After installing pytz, timezone.make_aware makes a call to pytz which raises 'ValueError: Not naive datetime (tzinfo is already set)', and causes everything to break.

While I discovered this in django 1.5, I've also been able to replicate the bug in django 1.6, so I am filing the bug report under that.

Change History (7)

comment:1 by Aymeric Augustin, 10 years ago

Owner: changed from nobody to Aymeric Augustin
Status: newassigned

comment:2 by Tim Graham, 10 years ago

Triage Stage: UnreviewedAccepted

comment:3 by Aymeric Augustin, 10 years ago

There are two possible semantics when make_aware receives an aware datetime (or make_naive receives a naive datetime):

  • raise an exception
  • return the value unchanged

Since Django uses the first one when pytz is installed, and the docs recommend to install pytz, it makes sense to enforce that behavior.

comment:4 by Aymeric Augustin <aymeric.augustin@…>, 10 years ago

Resolution: fixed
Status: assignedclosed

In fa89acf1d0a4adbf0802c1275494f5159a912c9e:

Fixed #22625 -- Normalized make_aware/naive errors.

Also added tests for is/make_aware/naive.

Thanks Tom Michaelis for the report.

comment:5 by Aymeric Augustin <aymeric.augustin@…>, 10 years ago

In 6f8301d6cd3815409c7c2c8468dbfd239d39f468:

[1.7.x] Fixed #22625 -- Normalized make_aware/naive errors.

Also added tests for is/make_aware/naive.

Thanks Tom Michaelis for the report.

Backport of fa89acf from master

comment:6 by Aymeric Augustin <aymeric.augustin@…>, 10 years ago

In 1109ebd7b373e14b68616346ea5dd3e89a8ae2fe:

Optimized make_aware/naive by removing redundant checks. Refs #22625.

Also added tests with pytz and removed misplaced tests.

comment:7 by Aymeric Augustin <aymeric.augustin@…>, 10 years ago

In 62de03ee337e085dbaa9d94a43a2aedbd3cdef84:

[1.7.x] Optimized make_aware/naive by removing redundant checks. Refs #22625.

Also added tests with pytz and removed misplaced tests.

Backport of 1109ebd from master

Note: See TracTickets for help on using tickets.
Back to Top