kulpa / google-api-python-client

Automatically exported from code.google.com/p/google-api-python-client
Other
0 stars 0 forks source link

oauth2client.django_orm.CredentialsField raises pickle error on empty string #166

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
[Use this form for both apiclient and oauth2client issues]

What steps will reproduce the problem?
1. Create Django model with field of type 
oauth2client.django_orm.CredentialsField, eg:
    class GoogleApiCredentials(models.Model):
        credentials = CredentialsField()
2. Create new instance of this model with no parameters, like instance = 
models.GoogleApiCredentials()
3. Got exception:
Traceback (most recent call last):
  File "/opt/virtualenv/seoparser/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 111, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "/opt/virtualenv/seoparser/local/lib/python2.7/site-packages/django/contrib/auth/decorators.py", line 20, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "/home/seriy/workspace/seoparser/seoparser/analytics/views.py", line 24, in auth_return
    creds = models.GoogleApiCredentials()
  File "/opt/virtualenv/seoparser/local/lib/python2.7/site-packages/django/db/models/base.py", line 357, in __init__
    setattr(self, field.attname, val)
  File "/opt/virtualenv/seoparser/local/lib/python2.7/site-packages/django/db/models/fields/subclassing.py", line 34, in __set__
    obj.__dict__[self.field.name] = self.field.to_python(value)
  File "/opt/virtualenv/seoparser/local/lib/python2.7/site-packages/oauth2client/django_orm.py", line 42, in to_python
    return pickle.loads(base64.b64decode(value))
  File "/usr/lib/python2.7/pickle.py", line 1382, in loads
    return Unpickler(file).load()
  File "/usr/lib/python2.7/pickle.py", line 858, in load
    dispatch[key](self)
  File "/usr/lib/python2.7/pickle.py", line 880, in load_eof
    raise EOFError
EOFError

What is the expected output? What do you see instead?
instead I expect that `instance.credentials == None`

What version of the product are you using? On what operating system?
Linux, google-api-python-client==1.0c2, Django 1.4

Please provide any additional information below.

This is because, for some reason, empty value means empty string, not None, so 
you must check, for example:

def to_python(self, value):
    if value is None or value == '':
        return None  # or `return value`

Original issue reported on code.google.com by sergey.p...@ostrovok.ru on 19 Jul 2012 at 12:23

GoogleCodeExporter commented 9 years ago

Original comment by jcgregorio@google.com on 1 Aug 2012 at 6:32