-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Summary Implements Pylint rule [`subprocess-popen-preexec-fn` (`W1509`)](https://pylint.pycqa.org/en/latest/user_guide/messages/warning/subprocess-popen-preexec-fn.html) as `subprocess-popen-preexec-fn` (`PLW1509`). Includes documentation. Related to #970. ## Test Plan `cargo test`
- Loading branch information
Showing
8 changed files
with
139 additions
and
1 deletion.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
crates/ruff/resources/test/fixtures/pylint/subprocess_popen_preexec_fn.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import subprocess | ||
|
||
|
||
def foo(): | ||
pass | ||
|
||
|
||
# Errors. | ||
subprocess.Popen(preexec_fn=foo) | ||
subprocess.Popen(["ls"], preexec_fn=foo) | ||
subprocess.Popen(preexec_fn=lambda: print("Hello, world!")) | ||
subprocess.Popen(["ls"], preexec_fn=lambda: print("Hello, world!")) | ||
|
||
# Non-errors. | ||
subprocess.Popen() | ||
subprocess.Popen(["ls"]) | ||
subprocess.Popen(preexec_fn=None) # None is the default. | ||
subprocess.Popen(["ls"], preexec_fn=None) # None is the default. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
crates/ruff/src/rules/pylint/rules/subprocess_popen_preexec_fn.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::helpers::{find_keyword, is_const_none}; | ||
use rustpython_parser::ast::{Expr, Keyword, Ranged}; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks for uses of `subprocess.Popen` with a `preexec_fn` argument. | ||
/// | ||
/// ## Why is this bad? | ||
/// The `preexec_fn` argument is unsafe within threads as it can lead to | ||
/// deadlocks. Furthermore, `preexec_fn` is [targeted for deprecation]. | ||
/// | ||
/// Instead, consider using task-specific arguments such as `env`, | ||
/// `start_new_session`, and `process_group`. These are not prone to deadlocks | ||
/// and are more explicit. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// import os, subprocess | ||
/// | ||
/// subprocess.Popen(foo, preexec_fn=os.setsid) | ||
/// subprocess.Popen(bar, preexec_fn=os.setpgid(0, 0)) | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// import subprocess | ||
/// | ||
/// subprocess.Popen(foo, start_new_session=True) | ||
/// subprocess.Popen(bar, process_group=0) # Introduced in Python 3.11 | ||
/// ``` | ||
/// | ||
/// ## References | ||
/// - [Python documentation: `subprocess.Popen`](https://docs.python.org/3/library/subprocess.html#popen-constructor) | ||
/// - [Why `preexec_fn` in `subprocess.Popen` may lead to deadlock?](https://discuss.python.org/t/why-preexec-fn-in-subprocess-popen-may-lead-to-deadlock/16908/2) | ||
/// | ||
/// [targeted for deprecation]: https://github.com/python/cpython/issues/82616 | ||
#[violation] | ||
pub struct SubprocessPopenPreexecFn; | ||
|
||
impl Violation for SubprocessPopenPreexecFn { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("`preexec_fn` argument is unsafe when using threads") | ||
} | ||
} | ||
|
||
/// PLW1509 | ||
pub(crate) fn subprocess_popen_preexec_fn(checker: &mut Checker, func: &Expr, kwargs: &[Keyword]) { | ||
if checker | ||
.semantic() | ||
.resolve_call_path(func) | ||
.map_or(false, |call_path| { | ||
matches!(call_path.as_slice(), ["subprocess", "Popen"]) | ||
}) | ||
{ | ||
if let Some(keyword) = | ||
find_keyword(kwargs, "preexec_fn").filter(|keyword| !is_const_none(&keyword.value)) | ||
{ | ||
checker | ||
.diagnostics | ||
.push(Diagnostic::new(SubprocessPopenPreexecFn, keyword.range())); | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
.../pylint/snapshots/ruff__rules__pylint__tests__PLW1509_subprocess_popen_preexec_fn.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
--- | ||
source: crates/ruff/src/rules/pylint/mod.rs | ||
--- | ||
subprocess_popen_preexec_fn.py:9:18: PLW1509 `preexec_fn` argument is unsafe when using threads | ||
| | ||
8 | # Errors. | ||
9 | subprocess.Popen(preexec_fn=foo) | ||
| ^^^^^^^^^^^^^^ PLW1509 | ||
10 | subprocess.Popen(["ls"], preexec_fn=foo) | ||
11 | subprocess.Popen(preexec_fn=lambda: print("Hello, world!")) | ||
| | ||
|
||
subprocess_popen_preexec_fn.py:10:26: PLW1509 `preexec_fn` argument is unsafe when using threads | ||
| | ||
8 | # Errors. | ||
9 | subprocess.Popen(preexec_fn=foo) | ||
10 | subprocess.Popen(["ls"], preexec_fn=foo) | ||
| ^^^^^^^^^^^^^^ PLW1509 | ||
11 | subprocess.Popen(preexec_fn=lambda: print("Hello, world!")) | ||
12 | subprocess.Popen(["ls"], preexec_fn=lambda: print("Hello, world!")) | ||
| | ||
|
||
subprocess_popen_preexec_fn.py:11:18: PLW1509 `preexec_fn` argument is unsafe when using threads | ||
| | ||
9 | subprocess.Popen(preexec_fn=foo) | ||
10 | subprocess.Popen(["ls"], preexec_fn=foo) | ||
11 | subprocess.Popen(preexec_fn=lambda: print("Hello, world!")) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PLW1509 | ||
12 | subprocess.Popen(["ls"], preexec_fn=lambda: print("Hello, world!")) | ||
| | ||
|
||
subprocess_popen_preexec_fn.py:12:26: PLW1509 `preexec_fn` argument is unsafe when using threads | ||
| | ||
10 | subprocess.Popen(["ls"], preexec_fn=foo) | ||
11 | subprocess.Popen(preexec_fn=lambda: print("Hello, world!")) | ||
12 | subprocess.Popen(["ls"], preexec_fn=lambda: print("Hello, world!")) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PLW1509 | ||
13 | | ||
14 | # Non-errors. | ||
| | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.