Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#21404 closed Cleanup/optimization (fixed)

session.set_expiry() causes a TypeError when using JSONSerializer

Reported by: René Puls Owned by: nobody
Component: Documentation Version: 1.6
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

I am using django.contrib.sessions.backends.cached_db as the SESSION_ENGINE and django.contrib.sessions.serializers.JSONSerializer as the SESSION_SERIALIZER.

Setting a session expiration date using session.set_expiry() results in a TypeError when the session is saved:

Traceback:
File "/Users/pwr/Library/Python/2.7/lib/python/site-packages/django/core/handlers/base.py" in get_response
  201.                 response = middleware_method(request, response)
File "/Users/pwr/Library/Python/2.7/lib/python/site-packages/django/contrib/sessions/middleware.py" in process_response
  38.                     request.session.save()
File "/Users/pwr/Library/Python/2.7/lib/python/site-packages/django/contrib/sessions/backends/cached_db.py" in save
  62.         super(SessionStore, self).save(must_create)
File "/Users/pwr/Library/Python/2.7/lib/python/site-packages/django/contrib/sessions/backends/db.py" in save
  57.             session_data=self.encode(self._get_session(no_load=must_create)),
File "/Users/pwr/Library/Python/2.7/lib/python/site-packages/django/contrib/sessions/backends/base.py" in encode
  87.         serialized = self.serializer().dumps(session_dict)
File "/Users/pwr/Library/Python/2.7/lib/python/site-packages/django/core/signing.py" in dumps
  88.         return json.dumps(obj, separators=(',', ':')).encode('latin-1')
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py" in dumps
  250.         sort_keys=sort_keys, **kw).encode(obj)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py" in encode
  207.         chunks = self.iterencode(o, _one_shot=True)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py" in iterencode
  270.         return _iterencode(o, 0)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py" in default
  184.         raise TypeError(repr(o) + " is not JSON serializable")

Exception Type: TypeError at /users/login/
Exception Value: datetime.datetime(2013, 11, 9, 5, 0, tzinfo=<DstTzInfo 'Europe/Berlin' CET+1:00:00 STD>) is not JSON serializable

The documentation already notes that JSONSerializer cannot handle datetime values, which is working as intended according to #21124. The documentation for set_expiry also notes that "datetime and timedelta values are only serializable if you are using the PickleSerializer".

If this is indeed intentional, I suggest that the 1.6 release notes should at least mention that session expiration times can no longer be set in the default configuration.

Attachments (1)

21404.diff (1.4 KB) - added by Tim Graham 3 years ago.

Download all attachments as: .zip

Change History (7)

comment:1 Changed 3 years ago by Tim Graham

Component: contrib.sessionsDocumentation
Easy pickings: set
Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization

Accepting as a documentation addition.

comment:2 Changed 3 years ago by G.Z.

I can confirm that this seems like a relevant issue - there should be at least clear warning about a possibility of such errors coming up just from using default settings and methods.

Changed 3 years ago by Tim Graham

Attachment: 21404.diff added

comment:3 Changed 3 years ago by Tim Graham

Has patch: set

Attached is a proposed patch. Please let me know if you would add anything else.

comment:4 Changed 3 years ago by René Puls

Looks good to me. Thank you!

comment:5 Changed 3 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: newclosed

In ffdae5b66d681145d6e9288fd98718e6c7b263e4:

Fixed #21404 -- Added session.set_expiry() note to 1.6 release notes.

Thanks pwr for the suggestion.

comment:6 Changed 3 years ago by Tim Graham <timograham@…>

In a776e3f42088a5a82bd0ec9c5353981a1a23e10c:

[1.6.x] Fixed #21404 -- Added session.set_expiry() note to 1.6 release notes.

Thanks pwr for the suggestion.

Backport of ffdae5b66d from master

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