From 3006696972fb0420aa24c2c421656208504d6ff0 Mon Sep 17 00:00:00 2001 From: OMOTO Kenji Date: Sat, 16 Sep 2017 17:12:06 +0900 Subject: [PATCH] Fixed wrong use of exception and ambiguous exception names --- execjs/__init__.py | 8 +++++++- execjs/_exceptions.py | 20 +++++++++++++------- execjs/_external_runtime.py | 22 ++++++++++++---------- execjs/_pyv8runtime.py | 2 +- 4 files changed, 33 insertions(+), 19 deletions(-) diff --git a/execjs/__init__.py b/execjs/__init__.py index 2b573df..98a4bea 100755 --- a/execjs/__init__.py +++ b/execjs/__init__.py @@ -22,7 +22,13 @@ ''' from __future__ import unicode_literals, division, with_statement -from execjs._exceptions import Error, RuntimeError, ProgramError, RuntimeUnavailableError +from execjs._exceptions import ( + Error, + RuntimeError, + ProgramError, + RuntimeUnavailableError, +) + import execjs._runtimes from execjs._external_runtime import ExternalRuntime from execjs._abstract_runtime import AbstractRuntime diff --git a/execjs/_exceptions.py b/execjs/_exceptions.py index e11f947..0723550 100644 --- a/execjs/_exceptions.py +++ b/execjs/_exceptions.py @@ -1,18 +1,24 @@ +# Abstract base error classes class Error(Exception): pass - +# Abstract class that represents errors of runtime engine. +# ex. Specified runtime engine is not installed, runtime engine aborted (by its bugs). +# By the way "RuntimeError" is bad name because it is confusing with the standard exception. class RuntimeError(Error): + pass + +# Concrete runtime error classes +class RuntimeUnavailableError(RuntimeError): pass + +class ProcessExitedWithNonZeroStatus(RuntimeError): def __init__(self, status, stdout, stderr): - Error.__init__(self, status, stdout, stderr) + RuntimeError.__init__(self, status, stdout, stderr) self.status = status self.stdout = stdout self.stderr = stderr - +# Errors due to JS script. +# ex. Script has syntax error, executed and raised exception. class ProgramError(Error): pass - - -class RuntimeUnavailableError(RuntimeError): - pass diff --git a/execjs/_external_runtime.py b/execjs/_external_runtime.py index 6489084..2b05c16 100644 --- a/execjs/_external_runtime.py +++ b/execjs/_external_runtime.py @@ -11,7 +11,12 @@ import six import execjs._json2 as _json2 import execjs._runner_sources as _runner_sources -import execjs._exceptions as exceptions + +from execjs._exceptions import ( + ProcessExitedWithNonZeroStatus, + ProgramError +) + from execjs._abstract_runtime import AbstractRuntime from execjs._abstract_runtime_context import AbstractRuntimeContext from execjs._misc import encode_unicode_codepoints @@ -126,7 +131,7 @@ def _exec_with_tempfile(self, source): def _fail_on_non_zero_status(self, status, stdoutdata, stderrdata): if status != 0: - raise exceptions.RuntimeError(status=status, stdout=stdoutdata, stderr=stderrdata) + raise ProcessExitedWithNonZeroStatus(status=status, stdout=stdoutdata, stderr=stderrdata) def _compile(self, source): runner_source = self._runtime._runner_source @@ -151,18 +156,15 @@ def _extract_result(self, output): output = output.replace("\r\n", "\n").replace("\r", "\n") output_last_line = output.split("\n")[-2] - if not output_last_line: - raise exceptions.ProgramError - else: - ret = json.loads(output_last_line) - if len(ret) == 1: - ret = [ret[0], None] - status, value = ret + ret = json.loads(output_last_line) + if len(ret) == 1: + ret = [ret[0], None] + status, value = ret if status == "ok": return value else: - raise exceptions.ProgramError(value) + raise ProgramError(value) def _is_windows(): diff --git a/execjs/_pyv8runtime.py b/execjs/_pyv8runtime.py index c5edcff..2f93a2c 100644 --- a/execjs/_pyv8runtime.py +++ b/execjs/_pyv8runtime.py @@ -53,7 +53,7 @@ def _exec_(self, source): try: script = engine.compile(source) except js_errors as e: - raise exceptions.RuntimeError(e) + raise exceptions.ProgramError(e) try: value = script.run() except js_errors as e: