Skip to content

Commit

Permalink
Use Js2Py's new built-in way to disable pyimport.
Browse files Browse the repository at this point in the history
  • Loading branch information
carsonyl committed Jan 20, 2017
1 parent e6b2357 commit 44716ca
Showing 1 changed file with 4 additions and 11 deletions.
15 changes: 4 additions & 11 deletions pypac/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ def __init__(self, pac_js):
:raises PyImportError: If the JavaScript tries to use Js2Py's `pyimport` keyword,
which is not legitimate in the context of a PAC file.
"""
# Disallow parsing of the unsafe 'pyimport' statement in Js2Py by monkey-patching the parser.
# Try to be a good citizen by restoring original functionality once we're done.
# Don't want to make Mock a dependency just for this.
orig_pyimport_meth = js2py.translators.pyjsparser.PyJsParser.parsePyimportStatement
js2py.translators.pyjsparser.PyJsParser.parsePyimportStatement = _raise_pyimport_error
if 'pyimport' in pac_js:
raise PyimportError()
# Disallow parsing of the unsafe 'pyimport' statement in Js2Py.
js2py.disable_pyimport()
try:
context = js2py.EvalJs(function_injections)
context.execute(pac_js)
Expand All @@ -29,8 +28,6 @@ def __init__(self, pac_js):
self._func = context.FindProxyForURL
except PyJsException: # as e:
raise MalformedPacError() # from e
finally:
js2py.translators.pyjsparser.PyJsParser.parsePyimportStatement = orig_pyimport_meth

def find_proxy_for_url(self, url, host):
"""
Expand All @@ -44,10 +41,6 @@ def find_proxy_for_url(self, url, host):
return self._func(url, host)


def _raise_pyimport_error(*args, **kwargs):
raise PyimportError()


class MalformedPacError(Exception):
def __init__(self, msg=None):
if not msg:
Expand Down

0 comments on commit 44716ca

Please sign in to comment.