Permalink
Browse files

Simplifies loading pickled data via ``django.utils.encoding.smart_str…

…`` function.

Signed-off-by: Alexander Artemenko <svetlyak.40wt@gmail.com>
  • Loading branch information...
1 parent 13ced58 commit ef7b37445c5652ae2bd40e237f31b238250d839d @playpauseandstop playpauseandstop committed with svetlyak40wt Feb 17, 2009
Showing with 4 additions and 13 deletions.
  1. +4 −13 django_fields/fields.py
View
@@ -5,6 +5,7 @@
from django import forms
from django.db import models
from django.conf import settings
+from django.utils.encoding import smart_str
USE_CPICKLE = getattr(settings, 'USE_CPICKLE', False)
@@ -92,19 +93,9 @@ def to_python(self, value):
# Tries to convert unicode objects to string, cause loads pickle from
# unicode excepts ugly ``KeyError: '\x00'``.
- #
- # If not possible return this value, cause it's not pickled yet.
- if isinstance(value, unicode):
- try:
- str_value = str(value)
- except UnicodeEncodeError:
- return value
- else:
- str_value = value
-
try:
- return pickle.loads(str_value)
+ return pickle.loads(smart_str(value))
+ # If pickle could not loads from string it's means that it's Python
+ # string saved to PickleField.
except ValueError:
- # If pickle could not loads from string it's means that it's Python
- # string saved to PickleField.
return value

0 comments on commit ef7b374

Please sign in to comment.