Skip to content

Commit 01509ac

Browse files
committed
Merge branch 'szhu-master'
2 parents 6b143d2 + 0e8faa6 commit 01509ac

1 file changed

Lines changed: 49 additions & 18 deletions

File tree

sublime_python.py

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,10 @@ def restart(self):
128128
elif SERVER_DEBUGGING:
129129
# debug mode two
130130
self.port = self.get_free_port()
131-
proc_args = [self.python, SERVER_SCRIPT,
131+
proc_args = self.python + [SERVER_SCRIPT,
132132
str(self.port), " --debug"]
133133
self.proc = subprocess.Popen(
134-
proc_args, cwd=os.path.dirname(self.python),
134+
proc_args, cwd=os.path.dirname(self.python[0]),
135135
stderr=subprocess.PIPE, creationflags=CREATION_FLAGS)
136136
self.queue = Queue()
137137
self.stderr_reader = AsynchronousFileReader(
@@ -144,9 +144,9 @@ def restart(self):
144144
else:
145145
# standard run of the server in end-user mode
146146
self.port = self.get_free_port()
147-
proc_args = [self.python, SERVER_SCRIPT, str(self.port)]
147+
proc_args = self.python + [SERVER_SCRIPT, str(self.port)]
148148
self.proc = subprocess.Popen(
149-
proc_args, cwd=os.path.dirname(self.python),
149+
proc_args, cwd=os.path.dirname(self.python[0]),
150150
creationflags=CREATION_FLAGS)
151151
print("started server on port %i with %s" %
152152
(self.port, self.python))
@@ -300,35 +300,66 @@ def project_venv_python(view):
300300
return python
301301

302302

303+
def shebang_line_python(view):
304+
shebang_line = view.substr(view.line(0))
305+
if shebang_line.startswith('#!'):
306+
return shebang_line[2:].split(None, 1)
307+
308+
309+
def normalize_path(args, make_abs=False):
310+
if not args:
311+
return None
312+
elif type(args) is str:
313+
args = [args]
314+
elif type(args) is not list:
315+
args = list(args)
316+
317+
# args is guaranteed to be a non-empty list at this point
318+
if make_abs:
319+
args[0] = os.path.abspath(os.path.realpath(os.path.expanduser(args[0])))
320+
return args
321+
322+
303323
def proxy_for(view):
304324
'''retrieve an existing proxy for an external Python process.
305-
will automatically create a new proxy if non exists for the
325+
will automatically create a new proxy if none exists for the
306326
requested interpreter'''
307327
proxy = None
308328
with PROXY_LOCK:
309-
python = get_setting("python_interpreter", view, "")
310-
if python == "":
311-
python = project_venv_python(view) or system_python()
312-
else:
313-
python = os.path.abspath(
314-
os.path.realpath(os.path.expanduser(python)))
315-
316-
if not os.path.exists(python):
329+
python = normalize_path(get_setting("python_interpreter", view, ""), True)
330+
if not python:
331+
python = normalize_path(project_venv_python(view))
332+
if not python:
333+
python = normalize_path(shebang_line_python(view))
334+
if not python:
335+
python = normalize_path(system_python())
336+
337+
if not python or not os.path.exists(python[0]):
317338
raise OSError("""
318339
--------------------------------------------------------------------------------------------------------------
319340
Could not detect python, please set the python_interpreter (see README) using an absolute path or make sure a
320-
system python is installed and is reachable on the PATH.
321-
--------------------------------------------------------------------------------------------------------------""")
341+
system python is installed and is reachable on the PATH. Here is the order in which paths are tried:
342+
343+
- By "python_interpreter" Sublime settings: %r
344+
- By auto-detecting venv: %r
345+
- By #! (shebang) line in this file: %r
346+
- By auto-detecting system Python: %r
347+
348+
We use the first non-None value and ensure that the path exists before proceeding.
349+
--------------------------------------------------------------------------------------------------------------"""
350+
% pythons)
322351

323-
if python in PROXIES:
324-
proxy = PROXIES[python]
352+
# Since lists cannot be used as keys, a temporary tuple version of this is created.
353+
python_as_key = tuple(python)
354+
if python_as_key in PROXIES:
355+
proxy = PROXIES[python_as_key]
325356
else:
326357
try:
327358
proxy = Proxy(python)
328359
except OSError:
329360
pass
330361
else:
331-
PROXIES[python] = proxy
362+
PROXIES[python_as_key] = proxy
332363
return proxy
333364

334365

0 commit comments

Comments
 (0)