Skip to content

Support chained attributes in thread unsafe detection #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 83 additions & 35 deletions src/pytest_run_parallel/thread_unsafe_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,47 +56,95 @@ def __init__(self, fn, skip_set, level=0):

super().__init__()

def _recursive_analyze_attribute(self, node):
current = node
while isinstance(current.value, ast.Attribute):
current = current.value
if not isinstance(current.value, ast.Name):
return
id = current.value.id

def _get_child_fn(mod, node):
if isinstance(node.value, ast.Attribute):
submod = _get_child_fn(mod, node.value)
return getattr(submod, node.attr, None)

if not isinstance(node.value, ast.Name):
return None
return getattr(mod, node.attr, None)

if id in getattr(self.fn, "__globals__", {}):
mod = self.fn.__globals__[id]
child_fn = _get_child_fn(mod, node)
if child_fn is not None:
self.thread_unsafe, self.thread_unsafe_reason = (
identify_thread_unsafe_nodes(
child_fn, self.skip_set, self.level + 1
)
)

def _build_attribute_chain(self, node):
chain = []
current = node

while isinstance(current, ast.Attribute):
chain.insert(0, current.attr)
current = current.value

if isinstance(current, ast.Name):
chain.insert(0, current.id)

return chain

def _visit_attribute_call(self, node):
if isinstance(node.value, ast.Name):
real_mod = node.value.id
if real_mod in self.modules_aliases:
real_mod = self.modules_aliases[real_mod]
if (real_mod, node.attr) in self.blacklist:
self.thread_unsafe = True
self.thread_unsafe_reason = (
"calls thread-unsafe function: " f"{real_mod}.{node.attr}"
)
elif self.level < 2:
self._recursive_analyze_attribute(node)
elif isinstance(node.value, ast.Attribute):
chain = self._build_attribute_chain(node)
module_part = ".".join(chain[:-1])
func_part = chain[-1]
if (module_part, func_part) in self.blacklist:
self.thread_unsafe = True
self.thread_unsafe_reason = (
f"calls thread-unsafe function: {'.'.join(chain)}"
)
elif self.level < 2:
self._recursive_analyze_attribute(node)

def _recursive_analyze_name(self, node):
if node.id in getattr(self.fn, "__globals__", {}):
child_fn = self.fn.__globals__[node.id]
self.thread_unsafe, self.thread_unsafe_reason = (
identify_thread_unsafe_nodes(child_fn, self.skip_set, self.level + 1)
)

def _visit_name_call(self, node):
recurse = True
if node.id in self.func_aliases:
if self.func_aliases[node.id] in self.blacklist:
self.thread_unsafe = True
self.thread_unsafe_reason = f"calls thread-unsafe function: {node.id}"
recurse = False
if recurse and self.level < 2:
self._recursive_analyze_name(node)

def visit_Call(self, node):
if self.thread_unsafe:
return

if isinstance(node.func, ast.Attribute):
if isinstance(node.func.value, ast.Name):
real_mod = node.func.value.id
if real_mod in self.modules_aliases:
real_mod = self.modules_aliases[real_mod]
if (real_mod, node.func.attr) in self.blacklist:
self.thread_unsafe = True
self.thread_unsafe_reason = (
"calls thread-unsafe function: " f"{real_mod}.{node.func.attr}"
)
elif self.level < 2:
if node.func.value.id in getattr(self.fn, "__globals__", {}):
mod = self.fn.__globals__[node.func.value.id]
child_fn = getattr(mod, node.func.attr, None)
if child_fn is not None:
self.thread_unsafe, self.thread_unsafe_reason = (
identify_thread_unsafe_nodes(
child_fn, self.skip_set, self.level + 1
)
)
self._visit_attribute_call(node.func)
elif isinstance(node.func, ast.Name):
recurse = True
if node.func.id in self.func_aliases:
if self.func_aliases[node.func.id] in self.blacklist:
self.thread_unsafe = True
self.thread_unsafe_reason = (
f"calls thread-unsafe function: {node.func.id}"
)
recurse = False
if recurse and self.level < 2:
if node.func.id in getattr(self.fn, "__globals__", {}):
child_fn = self.fn.__globals__[node.func.id]
self.thread_unsafe, self.thread_unsafe_reason = (
identify_thread_unsafe_nodes(
child_fn, self.skip_set, self.level + 1
)
)
self._visit_name_call(node.func)

def visit_Assign(self, node):
if self.thread_unsafe:
Expand Down
43 changes: 43 additions & 0 deletions tests/test_thread_unsafe_detection.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import textwrap

import pytest

Expand Down Expand Up @@ -324,3 +325,45 @@ def test1():
"*1 failed*",
]
)


def test_chained_attribute_import(pytester):
pytester.makepyfile("""
import _pytest.recwarn

def test_chained_attribute_thread_unsafe_detection(num_parallel_threads):
_pytest.recwarn.warns()
assert num_parallel_threads == 1
""")

result = pytester.runpytest("--parallel-threads=10", "-v")
result.stdout.fnmatch_lines(
[
"*::test_chained_attribute_thread_unsafe_detection PASSED*",
]
)


def test_chained_attribute_thread_safe_assignment(pytester):
pytester.mkpydir("mod")
file = pytester.path / "mod" / "submod.py"
file.write_text(
textwrap.dedent("""
def to_skip():
__thread_safe__ = False
""")
)
pytester.makepyfile("""
import mod.submod

def test_chained_attribute_thread_safe_assignment(num_parallel_threads):
mod.submod.to_skip()
assert num_parallel_threads == 1
""")

result = pytester.runpytest("--parallel-threads=10", "-v")
result.stdout.fnmatch_lines(
[
"*::test_chained_attribute_thread_safe_assignment PASSED*",
]
)