Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Add type assertion to the cache lookup #123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 15, 2022
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 8 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 22 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()
Morty Proxy This is a proxified and sanitized view of the page, visit original site.