Commit 638c1ccd authored by Aaron Hill's avatar Aaron Hill
Browse files

Merge pull request #142 from danfairs/unicode-track-follow

Encode track and follow parameters to UTF8. Blindly calling str()
will cause a UnicodeEncodeError as the ascii codec is used.
parents 012da2d2 2d5db811
...@@ -68,6 +68,21 @@ class TweepyStreamTests(unittest.TestCase): ...@@ -68,6 +68,21 @@ class TweepyStreamTests(unittest.TestCase):
self.assertEquals(self.listener.status_count, self.assertEquals(self.listener.status_count,
self.listener.status_stop_count) self.listener.status_stop_count)
def test_track_encoding(self):
s = Stream(None, None)
s._start = lambda async: None
s.filter(track=[u'Caf\xe9'])
# Should be UTF-8 encoded
self.assertEqual(u'Caf\xe9'.encode('utf8'), s.parameters['track'])
def test_follow_encoding(self):
s = Stream(None, None)
s._start = lambda async: None
s.filter(follow=[u'Caf\xe9'])
# Should be UTF-8 encoded
self.assertEqual(u'Caf\xe9'.encode('utf8'), s.parameters['follow'])
class TweepyStreamBackoffTests(unittest.TestCase): class TweepyStreamBackoffTests(unittest.TestCase):
def setUp(self): def setUp(self):
......
...@@ -272,17 +272,19 @@ class Stream(object): ...@@ -272,17 +272,19 @@ class Stream(object):
self.url += '&count=%s' % count self.url += '&count=%s' % count
self._start(async) self._start(async)
def filter(self, follow=None, track=None, async=False, locations=None, def filter(self, follow=None, track=None, async=False, locations=None,
count = None, stall_warnings=False, languages=None): count=None, stall_warnings=False, languages=None, encoding='utf8'):
self.parameters = {} self.parameters = {}
self.headers['Content-type'] = "application/x-www-form-urlencoded" self.headers['Content-type'] = "application/x-www-form-urlencoded"
if self.running: if self.running:
raise TweepError('Stream object already connected!') raise TweepError('Stream object already connected!')
self.url = '/%s/statuses/filter.json?delimited=length' % STREAM_VERSION self.url = '/%s/statuses/filter.json?delimited=length' % STREAM_VERSION
if follow: if follow:
self.parameters['follow'] = ','.join(map(str, follow)) encoded_follow = [s.encode(encoding) for s in follow]
self.parameters['follow'] = ','.join(encoded_follow)
if track: if track:
self.parameters['track'] = ','.join(map(str, track)) encoded_track = [s.encode(encoding) for s in track]
self.parameters['track'] = ','.join(encoded_track)
if locations and len(locations) > 0: if locations and len(locations) > 0:
assert len(locations) % 4 == 0 assert len(locations) % 4 == 0
self.parameters['locations'] = ','.join(['%.2f' % l for l in locations]) self.parameters['locations'] = ','.join(['%.2f' % l for l in locations])
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment