implicitly convert unicode objects to str for form values

This commit is contained in:
Romain Bignon 2013-04-01 17:24:20 +02:00
commit 09b0ae72d4

View file

@ -44,6 +44,7 @@ from urlparse import urlsplit
import mimetypes import mimetypes
from contextlib import closing from contextlib import closing
from gzip import GzipFile from gzip import GzipFile
import warnings
from weboob.tools.decorators import retry from weboob.tools.decorators import retry
from weboob.tools.log import getLogger from weboob.tools.log import getLogger
@ -122,6 +123,11 @@ class NoHistory(object):
class BrokenPageError(Exception): class BrokenPageError(Exception):
pass pass
class FormFieldConversionWarning(UserWarning):
"""
A value has been set to a form's field and has been implicitly converted.
"""
class BasePage(object): class BasePage(object):
""" """
@ -441,6 +447,16 @@ class StandardBrowser(mechanize.Browser):
if certhash not in hsh: if certhash not in hsh:
raise ssl.SSLError() raise ssl.SSLError()
def __setitem__(self, key, value):
if isinstance(value, unicode):
value = value.encode(self.ENCODING or 'utf-8')
warnings.warn('Implicit conversion of form field %r from unicode to str' % key,
FormFieldConversionWarning, stacklevel=2)
if self.form is None:
raise AttributeError('Please select a form before setting values to fields')
return self.form.__setitem__(key, value)
class BaseBrowser(StandardBrowser): class BaseBrowser(StandardBrowser):
""" """