1
1
# Copyright 2017 Palantir Technologies, Inc.
2
+ from distutils .version import LooseVersion
2
3
import functools
3
4
import inspect
4
5
import logging
5
6
import os
6
7
import sys
7
8
import threading
8
9
10
+ import jedi
11
+
9
12
PY2 = sys .version_info .major == 2
13
+ JEDI_VERSION = jedi .__version__
10
14
11
15
if PY2 :
12
16
import pathlib2 as pathlib
@@ -136,7 +140,8 @@ def format_docstring(contents):
136
140
"""
137
141
contents = contents .replace ('\t ' , u'\u00A0 ' * 4 )
138
142
contents = contents .replace (' ' , u'\u00A0 ' * 2 )
139
- contents = contents .replace ('*' , '\\ *' )
143
+ if LooseVersion (JEDI_VERSION ) < LooseVersion ('0.15.0' ):
144
+ contents = contents .replace ('*' , '\\ *' )
140
145
return contents
141
146
142
147
@@ -147,19 +152,49 @@ def clip_column(column, lines, line_number):
147
152
return min (column , max_column )
148
153
149
154
150
- def is_process_alive ( pid ) :
151
- """ Check whether the process with the given pid is still alive.
155
+ if os . name == 'nt' :
156
+ import ctypes
152
157
153
- Args:
154
- pid (int): process ID
158
+ kernel32 = ctypes . windll . kernel32
159
+ PROCESS_QUERY_INFROMATION = 0x1000
155
160
156
- Returns:
157
- bool: False if the process is not alive or don't have permission to check, True otherwise.
158
- """
159
- try :
160
- os .kill (pid , 0 )
161
- except OSError :
162
- # no such process or process is already dead
161
+ def is_process_alive (pid ):
162
+ """Check whether the process with the given pid is still alive.
163
+
164
+ Running `os.kill()` on Windows always exits the process, so it can't be used to check for an alive process.
165
+ see: https://docs.python.org/3/library/os.html?highlight=os%20kill#os.kill
166
+
167
+ Hence ctypes is used to check for the process directly via windows API avoiding any other 3rd-party dependency.
168
+
169
+ Args:
170
+ pid (int): process ID
171
+
172
+ Returns:
173
+ bool: False if the process is not alive or don't have permission to check, True otherwise.
174
+ """
175
+ process = kernel32 .OpenProcess (PROCESS_QUERY_INFROMATION , 0 , pid )
176
+ if process != 0 :
177
+ kernel32 .CloseHandle (process )
178
+ return True
163
179
return False
164
- else :
165
- return True
180
+
181
+ else :
182
+ import errno
183
+
184
+ def is_process_alive (pid ):
185
+ """Check whether the process with the given pid is still alive.
186
+
187
+ Args:
188
+ pid (int): process ID
189
+
190
+ Returns:
191
+ bool: False if the process is not alive or don't have permission to check, True otherwise.
192
+ """
193
+ if pid < 0 :
194
+ return False
195
+ try :
196
+ os .kill (pid , 0 )
197
+ except OSError as e :
198
+ return e .errno == errno .EPERM
199
+ else :
200
+ return True
0 commit comments