Skip to content

Commit

Permalink
Error eagerly if an interpreter binary doesn't exist. (#886)
Browse files Browse the repository at this point in the history
The exception class already existed, it just wasn't used.

Due to caching, the interpreter may or may not fail
in find_binary(), so the calling code might fail at some
later stage.

This change causes us to fail eagerly and consistently if
the binary doesn't exist on disk.
  • Loading branch information
benjyw committed Feb 13, 2020
1 parent 918794d commit ce0485d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pex/interpreter.py
Expand Up @@ -404,6 +404,8 @@ def hashbang_matches(fn):
@classmethod
def _spawn_from_binary(cls, binary):
normalized_binary = cls._normalize_path(binary)
if not os.path.exists(normalized_binary):
raise cls.InterpreterNotFound(normalized_binary)

# N.B.: The CACHE is written as the last step in PythonInterpreter instance initialization.
cached_interpreter = cls._PYTHON_INTERPRETER_BY_NORMALIZED_PATH.get(normalized_binary)
Expand Down
4 changes: 4 additions & 0 deletions tests/test_interpreter.py
Expand Up @@ -57,6 +57,10 @@ def test_interpreter_caching(self, test_interpreter1, test_interpreter2):
py_interpreter3 = interpreter.PythonInterpreter.from_binary(test_interpreter1)
assert py_interpreter1 is py_interpreter3

def test_nonexistent_interpreter(self):
with pytest.raises(interpreter.PythonInterpreter.InterpreterNotFound):
interpreter.PythonInterpreter.from_binary('/nonexistent/path')

def test_binary_name_matching(self):
valid_binary_names = (
'jython',
Expand Down

0 comments on commit ce0485d

Please sign in to comment.