@@ -227,17 +227,6 @@ def python_executable_prefix(v: str) -> List[str]:
227
227
return ['python{}' .format (v )]
228
228
229
229
230
- def _python_version_from_executable (python_executable : str ) -> Tuple [int , int ]:
231
- try :
232
- check = subprocess .check_output ([python_executable , '-c' ,
233
- 'import sys; print(repr(sys.version_info[:2]))' ],
234
- stderr = subprocess .STDOUT ).decode ()
235
- return ast .literal_eval (check )
236
- except (subprocess .CalledProcessError , FileNotFoundError ):
237
- raise PythonExecutableInferenceError (
238
- 'invalid Python executable {}' .format (python_executable ))
239
-
240
-
241
230
def _python_executable_from_version (python_version : Tuple [int , int ]) -> str :
242
231
if sys .version_info [:2 ] == python_version :
243
232
return sys .executable
@@ -253,37 +242,25 @@ def _python_executable_from_version(python_version: Tuple[int, int]) -> str:
253
242
' perhaps try --python-executable, or --no-site-packages?' .format (python_version ))
254
243
255
244
256
- def infer_python_version_and_executable (options : Options ,
257
- special_opts : argparse .Namespace ) -> None :
258
- """Infer the Python version or executable from each other. Check they are consistent .
245
+ def infer_python_executable (options : Options ,
246
+ special_opts : argparse .Namespace ) -> None :
247
+ """Infer the Python executable from the given version .
259
248
260
- This function mutates options based on special_opts to infer the correct Python version and
261
- executable to use.
249
+ This function mutates options based on special_opts to infer the correct Python executable
250
+ to use.
262
251
"""
263
- # Infer Python version and/or executable if one is not given
264
-
265
252
# TODO: (ethanhs) Look at folding these checks and the site packages subprocess calls into
266
253
# one subprocess call for speed.
267
- if special_opts .python_executable is not None and special_opts .python_version is not None :
268
- py_exe_ver = _python_version_from_executable (special_opts .python_executable )
269
- if py_exe_ver != special_opts .python_version :
270
- raise PythonExecutableInferenceError (
271
- 'Python version {} did not match executable {}, got version {}.' .format (
272
- special_opts .python_version , special_opts .python_executable , py_exe_ver
273
- ))
274
- else :
275
- options .python_version = special_opts .python_version
276
- options .python_executable = special_opts .python_executable
277
- elif special_opts .python_executable is None and special_opts .python_version is not None :
278
- options .python_version = special_opts .python_version
279
- py_exe = None
254
+
255
+ # Use the command line specified executable, or fall back to one set in the
256
+ # config file. If an executable is not specified, infer it from the version
257
+ # (unless no_executable is set)
258
+ python_executable = special_opts .python_executable or options .python_executable
259
+
260
+ if python_executable is None :
280
261
if not special_opts .no_executable :
281
- py_exe = _python_executable_from_version (special_opts .python_version )
282
- options .python_executable = py_exe
283
- elif special_opts .python_version is None and special_opts .python_executable is not None :
284
- options .python_version = _python_version_from_executable (
285
- special_opts .python_executable )
286
- options .python_executable = special_opts .python_executable
262
+ python_executable = _python_executable_from_version (options .python_version )
263
+ options .python_executable = python_executable
287
264
288
265
289
266
HEADER = """%(prog)s [-h] [-v] [-V] [more options; see below]
@@ -747,8 +724,11 @@ def add_invertible_flag(flag: str,
747
724
print ("Warning: --quick-and-dirty is deprecated. It will disappear in the next release." ,
748
725
file = sys .stderr )
749
726
727
+ # The python_version is either the default, which can be overridden via a config file,
728
+ # or stored in special_opts and is passed via the command line.
729
+ options .python_version = special_opts .python_version or options .python_version
750
730
try :
751
- infer_python_version_and_executable (options , special_opts )
731
+ infer_python_executable (options , special_opts )
752
732
except PythonExecutableInferenceError as e :
753
733
parser .error (str (e ))
754
734
0 commit comments