Skip to content
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

Ensure sys.argv is a unicode literal #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
13 changes: 10 additions & 3 deletions lib/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ def log(msg):
print(msg)


def to_unicode(text, encoding='utf-8', errors='strict'):
"""Force text to unicode"""
if isinstance(text, bytes):
return text.decode(encoding, errors=errors)
return text


class RoutingError(Exception):
pass

Expand All @@ -60,7 +67,7 @@ class Plugin:
def __init__(self, base_url=None):
self._rules = {} # function to list of rules
if sys.argv:
self.path = urlsplit(sys.argv[0]).path or '/'
self.path = urlsplit(to_unicode(sys.argv[0])).path or '/'
else:
self.path = '/'
if len(sys.argv) > 1 and sys.argv[1].isdigit():
Expand Down Expand Up @@ -123,10 +130,10 @@ def add_route(self, func, pattern):
def run(self, argv=None):
if argv is None:
argv = sys.argv
self.path = urlsplit(argv[0]).path
self.path = urlsplit(to_unicode(argv[0])).path
self.path = self.path.rstrip('/')
if len(argv) > 2:
self.args = parse_qs(argv[2].lstrip('?'))
self.args = parse_qs(to_unicode(argv[2]).lstrip('?'))
self._dispatch(self.path)

def redirect(self, path):
Expand Down