Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adds proper encoding to strings being passed to a request.

  • Loading branch information...
commit e0d604111ba9f09e4f161d4280ea132e0e4a1990 1 parent 86b54a1
wagnerrp authored
Showing with 57 additions and 14 deletions.
  1. +48 −11 tmdb3/locales.py
  2. +9 −3 tmdb3/request.py
59 tmdb3/locales.py
View
@@ -83,11 +83,12 @@ def __repr__(self):
return u"<Country '{0.name}' ({0.alpha2})>".format(self)
class Locale( LocaleBase ):
- __slots__ = ['language', 'country']
+ __slots__ = ['language', 'country', 'encoding']
- def __init__(self, language, country):
+ def __init__(self, language, country, encoding):
self.language = Language.getstored(language)
self.country = Country.getstored(country)
+ self.encoding = encoding if encoding else 'latin-1'
def __str__(self):
return u"{0}_{1}".format(self.language, self.country)
@@ -95,33 +96,69 @@ def __str__(self):
def __repr__(self):
return u"<Locale {0.language}_{0.country}>".format(self)
+ def encode(self, dat):
+ """Encode using system default encoding for network/file output."""
+ try:
+ return dat.encode(self.encoding)
+ except AttributeError:
+ # not a string type, pass along
+ return dat
+ except UnicodeDecodeError:
+ # just return unmodified and hope for the best
+ return dat
+
+ def decode(self, dat):
+ """Decode to system default encoding for internal use."""
+ try:
+ return dat.decode(self.encoding)
+ except AttributeError:
+ # not a string type, pass along
+ return dat
+ except UnicodeEncodeError:
+ # just return unmodified and hope for the best
+ return dat
+
def set_locale(language=None, country=None, fallthrough=False):
global syslocale
LocaleBase.fallthrough = fallthrough
+ sysloc, sysenc = locale.getdefaultlocale()
+
if (not language) or (not country):
dat = None
if syslocale is not None:
dat = (str(syslocale.language), str(syslocale.country))
else:
- res = locale.getdefaultlocale()[0]
- if (res is None) or ('_' not in res):
+ if (sysloc is None) or ('_' not in sysloc):
dat = ('en', 'US')
else:
- dat = res.split('_')
+ dat = sysloc.split('_')
if language is None:
language = dat[0]
if country is None:
country = dat[1]
- syslocale = Locale(language, country)
-def get_locale(language=None, country=None):
+ syslocale = Locale(language, country, sysenc)
+
+def get_locale(language=-1, country=-1):
+ """Output locale using provided attributes, or return system locale."""
global syslocale
- if language and country:
- return Locale(language, country)
+ # pull existing stored values
if syslocale is None:
- return Locale(None, None)
- return syslocale
+ loc = Locale(None, None, locale.getdefaultlocale()[1])
+ else:
+ loc = syslocale
+
+ # both options are default, return stored values
+ if language == country == -1:
+ return loc
+
+ # supplement default option with stored values
+ if language == -1:
+ language = loc.language
+ elif country == -1:
+ country = loc.country
+ return Locale(language, country, loc.encoding)
######## AUTOGENERATED LANGUAGE AND COUNTRY DATA BELOW HERE #########
12 tmdb3/request.py
View
@@ -9,10 +9,11 @@
#-----------------------
from tmdb_exceptions import *
+from locales import get_locale
from cache import Cache
+from urllib import urlencode
import urllib2
-import urllib
import json
import os
@@ -56,8 +57,13 @@ def __init__(self, url, **kwargs):
self._url = url.lstrip('/')
self._kwargs = dict([(kwa,kwv) for kwa,kwv in kwargs.items()
if kwv is not None])
- url = '{0}{1}?{2}'.format(self._base_url, self._url,
- urllib.urlencode(self._kwargs))
+
+ locale = get_locale()
+ kwargs = {}
+ for k,v in self._kwargs.items():
+ kwargs[k] = locale.encode(v)
+ url = '{0}{1}?{2}'.format(self._base_url, self._url, urlencode(kwargs))
+
urllib2.Request.__init__(self, url)
self.add_header('Accept', 'application/json')
self.lifetime = 3600 # 1hr
Please sign in to comment.
Something went wrong with that request. Please try again.