Skip to content

Commit 4ab1c75

Browse files
authored
Merge pull request #164 from ngoldbaum/only-fail-gil-enable-in-parallel
Reset warning filter during thread-unsafe detection
2 parents 1a58146 + 1b6076a commit 4ab1c75

1 file changed

Lines changed: 25 additions & 18 deletions

File tree

src/pytest_run_parallel/thread_unsafe_detection.py

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,27 @@ def _is_source_indented(src):
272272
return is_indented
273273

274274

275+
def _visit_node(visitor, fn):
276+
try:
277+
src = inspect.getsource(fn)
278+
except (OSError, TypeError):
279+
# if we can't get the source code (e.g. builtin function) then give up
280+
# and don't attempt detection but default to assuming thread safety
281+
return False, None
282+
if _is_source_indented(src):
283+
# This test was extracted from a class or indented area, and Python
284+
# needs to be told to expect indentation.
285+
src = "if True:\n" + src
286+
try:
287+
tree = ast.parse(src)
288+
except (SyntaxError, ValueError):
289+
# AST parsing failed because the AST is invalid. Who knows why but that
290+
# means we can't run thread safety detection. Bail and assume
291+
# thread-safe.
292+
return False, None
293+
visitor.visit(tree)
294+
295+
275296
def _identify_thread_unsafe_nodes(
276297
fn, skip_set, unsafe_warnings, unsafe_ctypes, unsafe_hypothesis, level=0
277298
):
@@ -294,24 +315,10 @@ def _identify_thread_unsafe_nodes(
294315
visitor = ThreadUnsafeNodeVisitor(
295316
fn, skip_set, unsafe_warnings, unsafe_ctypes, unsafe_hypothesis, level=level
296317
)
297-
try:
298-
src = inspect.getsource(fn)
299-
except (OSError, TypeError):
300-
# if we can't get the source code (e.g. builtin function)
301-
# then give up and don't attempt detection but default to assuming
302-
# thread safety
303-
return False, None
304-
if _is_source_indented(src):
305-
# This test was extracted from a class or indented area, and Python needs
306-
# to be told to expect indentation.
307-
src = "if True:\n" + src
308-
try:
309-
tree = ast.parse(src)
310-
except (SyntaxError, ValueError):
311-
# AST parsing failed because the AST is invalid. Who knows why but that means
312-
# we can't run thread safety detection. Bail and assume thread-safe.
313-
return False, None
314-
visitor.visit(tree)
318+
with warnings.catch_warnings():
319+
# in case pytest is configured to treat warnings as errors.
320+
warnings.simplefilter("default")
321+
_visit_node(visitor, fn)
315322
except Exception as e:
316323
tb = traceback.format_exc()
317324
msg = (

0 commit comments

Comments
 (0)