Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions ua_parser/user_agent_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import os
import re
import sys
import warnings

__author__ = "Lindsey Simon <elsigh@gmail.com>"
Expand Down Expand Up @@ -218,8 +219,15 @@ def Parse(self, user_agent_string):
MAX_CACHE_SIZE = 200
_PARSE_CACHE = {}

_UA_TYPES = str
if sys.version_info < (3,):
_UA_TYPES = (str, unicode)


def _lookup(ua, args):
if not isinstance(ua, _UA_TYPES):
raise TypeError("Expected user agent to be a string, got %r" % ua)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't that be type(ua) at the end?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't really matter, this prints the value which I assumed would be more relevant / understandable.


key = (ua, tuple(sorted(args.items())))
entry = _PARSE_CACHE.get(key)
if entry is not None:
Expand Down
22 changes: 22 additions & 0 deletions ua_parser/user_agent_parser_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import os
import platform
import re
import sys
import unittest
import warnings
import yaml
Expand Down Expand Up @@ -301,5 +302,26 @@ def test_js_bits_deprecation(self):
self.assertEqual(w.category, DeprecationWarning)


class ErrTest(unittest.TestCase):
@unittest.skipIf(
sys.version_info < (3,), "bytes and str are not differentiated in P2"
)
def test_bytes(self):
with self.assertRaises(TypeError):
user_agent_parser.Parse(b"")

def test_int(self):
with self.assertRaises(TypeError):
user_agent_parser.Parse(0)

def test_list(self):
with self.assertRaises(TypeError):
user_agent_parser.Parse([])

def test_tuple(self):
with self.assertRaises(TypeError):
user_agent_parser.Parse(())


if __name__ == "__main__":
unittest.main()