33import logging
44import os
55import re
6+ import functools
7+ from threading import RLock
68
79import jedi
810
1517RE_END_WORD = re .compile ('^[A-Za-z_0-9]*' )
1618
1719
20+ def lock (method ):
21+ """Define an atomic region over a method."""
22+ @functools .wraps (method )
23+ def wrapper (self , * args , ** kwargs ):
24+ with self ._lock :
25+ return method (self , * args , ** kwargs )
26+ return wrapper
27+
28+
1829class Workspace (object ):
1930
2031 M_PUBLISH_DIAGNOSTICS = 'textDocument/publishDiagnostics'
@@ -131,6 +142,7 @@ def __init__(self, uri, workspace, source=None, version=None, local=True, extra_
131142 self ._source = source
132143 self ._extra_sys_path = extra_sys_path or []
133144 self ._rope_project_builder = rope_project_builder
145+ self ._lock = RLock ()
134146
135147 def __str__ (self ):
136148 return str (self .uri )
@@ -140,10 +152,12 @@ def _rope_resource(self, rope_config):
140152 return libutils .path_to_resource (self ._rope_project_builder (rope_config ), self .path )
141153
142154 @property
155+ @lock
143156 def lines (self ):
144157 return self .source .splitlines (True )
145158
146159 @property
160+ @lock
147161 def source (self ):
148162 if self ._source is None :
149163 with io .open (self .path , 'r' , encoding = 'utf-8' ) as f :
@@ -153,6 +167,7 @@ def source(self):
153167 def update_config (self , settings ):
154168 self ._config .update ((settings or {}).get ('pyls' , {}))
155169
170+ @lock
156171 def apply_change (self , change ):
157172 """Apply a change to the document."""
158173 text = change ['text' ]
@@ -218,11 +233,13 @@ def word_at_position(self, position):
218233
219234 return m_start [0 ] + m_end [- 1 ]
220235
236+ @lock
221237 def jedi_names (self , all_scopes = False , definitions = True , references = False ):
222238 script = self .jedi_script ()
223239 return script .get_names (all_scopes = all_scopes , definitions = definitions ,
224240 references = references )
225241
242+ @lock
226243 def jedi_script (self , position = None ):
227244 extra_paths = []
228245 environment_path = None
0 commit comments