Skip to content

Commit 769195a

Browse files
committed
clippy
1 parent 3609057 commit 769195a

File tree

3 files changed

+35
-27
lines changed

3 files changed

+35
-27
lines changed

Cargo.toml

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ dunce = "1.0.4"
1515
serde_json = "1.0.114"
1616
thiserror = "2.0"
1717
once_cell = "1.19"
18-
tokio = { version = "1.36.0", features = ["sync", "macros"] }
18+
tokio = { version = "1.47.0", features = ["sync", "macros", "rt", "rt-multi-thread"] }
1919

2020
[features]
2121
default = ["pyo3"]
@@ -28,19 +28,16 @@ version = "0.26.0"
2828
features = ["auto-initialize"]
2929
optional = true
3030

31-
32-
[dev-dependencies]
33-
tempfile = "3"
3431
[dependencies.rustpython-vm]
3532
version = "0.4.0"
3633
optional = true
3734
features = ["threading", "serde", "importlib"]
35+
3836
[dependencies.rustpython-stdlib]
3937
version = "0.4.0"
4038
optional = true
4139
features = ["threading"]
4240

43-
# The `full` feature for tokio is needed for the tests
44-
[dev-dependencies.tokio]
45-
version = "1"
46-
features = ["full"]
41+
[dev-dependencies]
42+
tempfile = "3"
43+
tokio = { version = "1.47.0", features = ["full"] }

src/lib.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@ pub struct PyRunner {
9292
sender: mpsc::Sender<PyCommand>,
9393
}
9494

95+
96+
impl Default for PyRunner {
97+
fn default() -> Self {
98+
PyRunner::new()
99+
}
100+
}
101+
95102
impl PyRunner {
96103
/// Creates a new `PyRunner` and spawns a dedicated thread for Python execution.
97104
///
@@ -153,14 +160,13 @@ impl PyRunner {
153160
let (tx, rx) = std_mpsc::channel();
154161
let sender = self.sender.clone();
155162

156-
let cmd_type_clone = cmd_type; // Clone is implicit as CmdType is Copy
157163
let task = Box::new(move |rt: &Runtime| {
158164
let result = rt.block_on(async {
159165
// This is the async `send_command` logic, but we can't call it
160166
// directly because of `&self` lifetime issues inside the closure.
161167
let (responder, receiver) = oneshot::channel();
162168
let cmd = PyCommand {
163-
cmd_type: cmd_type_clone,
169+
cmd_type,
164170
responder,
165171
};
166172
sender
@@ -186,6 +192,7 @@ impl PyRunner {
186192
/// Asynchronously executes a block of Python code.
187193
///
188194
/// * `code`: A string slice containing the Python code to execute.
195+
///
189196
/// This is equivalent to Python's `exec()` function.
190197
pub async fn run(&self, code: &str) -> Result<(), PyRunnerError> {
191198
self.send_command(CmdType::RunCode(code.into()))
@@ -208,6 +215,7 @@ impl PyRunner {
208215

209216
/// Asynchronously runs a python file.
210217
/// * `file`: Absolute path to a python file to execute.
218+
///
211219
/// Also loads the path of the file to sys.path for imports.
212220
pub async fn run_file(&self, file: &Path) -> Result<(), PyRunnerError> {
213221
self.send_command(CmdType::RunFile(file.to_path_buf()))
@@ -230,8 +238,8 @@ impl PyRunner {
230238

231239
/// Asynchronously evaluates a single Python expression.
232240
///
233-
/// * `code`: A string slice containing the Python expression to evaluate.
234-
/// Must not contain definitions or multiple lines.
241+
/// * `code`: A string slice containing the Python expression to evaluate. Must not contain definitions or multiple lines.
242+
///
235243
/// Returns a `Result` containing the expression's result as a `serde_json::Value` on success,
236244
/// or a `PyRunnerError` on failure. This is equivalent to Python's `eval()` function.
237245
pub async fn eval(&self, code: &str) -> Result<Value, PyRunnerError> {
@@ -254,6 +262,7 @@ impl PyRunner {
254262
///
255263
/// * `var_name`: The name of the variable to read. It can be a dot-separated path
256264
/// to access attributes of objects (e.g., "my_module.my_variable").
265+
///
257266
/// Returns the variable's value as a `serde_json::Value` on success.
258267
pub async fn read_variable(&self, var_name: &str) -> Result<Value, PyRunnerError> {
259268
self.send_command(CmdType::ReadVariable(var_name.into()))
@@ -277,6 +286,7 @@ impl PyRunner {
277286
/// * `name`: The name of the function to call. It can be a dot-separated path
278287
/// to access functions within modules (e.g., "my_module.my_function").
279288
/// * `args`: A vector of `serde_json::Value` to pass as arguments to the function.
289+
///
280290
/// Returns the function's return value as a `serde_json::Value` on success.
281291
/// Does not release GIL during await.
282292
pub async fn call_function(
@@ -313,6 +323,7 @@ impl PyRunner {
313323
/// * `name`: The name of the function to call. It can be a dot-separated path
314324
/// to access functions within modules (e.g., "my_module.my_function").
315325
/// * `args`: A vector of `serde_json::Value` to pass as arguments to the function.
326+
///
316327
/// Returns the function's return value as a `serde_json::Value` on success.
317328
/// Will release GIL during await.
318329
pub async fn call_async_function(
@@ -374,7 +385,7 @@ impl PyRunner {
374385
)));
375386
}
376387
let set_venv_code = include_str!("set_venv.py");
377-
self.run(&set_venv_code).await?;
388+
self.run(set_venv_code).await?;
378389

379390
let site_packages = if cfg!(target_os = "windows") {
380391
venv_path.join("Lib").join("site-packages")
@@ -415,7 +426,7 @@ impl PyRunner {
415426
)));
416427
}
417428
let set_venv_code = include_str!("set_venv.py");
418-
self.run_sync(&set_venv_code)?;
429+
self.run_sync(set_venv_code)?;
419430

420431
let site_packages = if cfg!(target_os = "windows") {
421432
venv_path.join("Lib").join("site-packages")

src/pyo3_runner.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,22 @@ pub(crate) async fn python_thread_main(mut receiver: mpsc::Receiver<PyCommand>)
2525
let result = match std::mem::replace(&mut cmd.cmd_type, CmdType::Stop) {
2626
CmdType::RunCode(code) => {
2727
let c_code = CString::new(code).expect("CString::new failed");
28-
py.run(&c_code, Some(&globals), None).map(|_| Value::Null)
28+
py.run(&c_code, Some(globals), None).map(|_| Value::Null)
2929
}
3030
CmdType::EvalCode(code) => {
3131
let c_code = CString::new(code).expect("CString::new failed");
32-
py.eval(&c_code, Some(&globals), None)
33-
.and_then(|obj| py_any_to_json(py, &obj))
32+
py.eval(&c_code, Some(globals), None)
33+
.and_then(|obj| py_any_to_json(&obj))
3434
}
35-
CmdType::RunFile(file) => handle_run_file(py, &globals, file),
35+
CmdType::RunFile(file) => handle_run_file(py, globals, file),
3636
CmdType::ReadVariable(var_name) => {
37-
get_py_object(&globals, &var_name).and_then(|obj| py_any_to_json(py, &obj))
37+
get_py_object(globals, &var_name).and_then(|obj| py_any_to_json(&obj))
3838
}
3939
CmdType::CallFunction { name, args } => {
40-
handle_call_function(py, &globals, name, args)
40+
handle_call_function(py, globals, name, args)
4141
}
4242
CmdType::CallAsyncFunction { name, args } => {
43-
let func = get_py_object(&globals, &name).unwrap(); // TODO;
43+
let func = get_py_object(globals, &name).unwrap(); // TODO;
4444
check_func_callable(&func, &name).unwrap(); // TODO
4545
let func = func.unbind();
4646

@@ -110,7 +110,7 @@ with open({}, 'r') as f:
110110
print_path_for_python(&file.to_path_buf())
111111
);
112112
let c_code = CString::new(code).expect("CString::new failed");
113-
py.run(&c_code, Some(&globals), None).map(|_| Value::Null)
113+
py.run(&c_code, Some(globals), None).map(|_| Value::Null)
114114
}
115115

116116
/// Handles the `CallFunction` command.
@@ -124,7 +124,7 @@ fn handle_call_function(
124124
check_func_callable(&func, &name)?;
125125
let t_args = vec_to_py_tuple(&py, args)?;
126126
let result = func.call1(t_args)?;
127-
py_any_to_json(py, &result)
127+
py_any_to_json(&result)
128128
}
129129

130130
fn vec_to_py_tuple<'py>(
@@ -155,13 +155,13 @@ async fn handle_call_async_function(
155155
let result = loop_obj.call_method1("run_until_complete", (coroutine,))?;
156156
loop_obj.call_method0("close")?;
157157

158-
py_any_to_json(py, &result)
158+
py_any_to_json(&result)
159159
});
160160
let _ = responder.send(result.map_err(|e| e.to_string()));
161161
}
162162

163163
/// Recursively converts a Python object to a `serde_json::Value`.
164-
fn py_any_to_json(py: Python, obj: &pyo3::Bound<'_, PyAny>) -> PyResult<Value> {
164+
fn py_any_to_json(obj: &pyo3::Bound<'_, PyAny>) -> PyResult<Value> {
165165
if obj.is_none() {
166166
return Ok(Value::Null);
167167
}
@@ -186,13 +186,13 @@ fn py_any_to_json(py: Python, obj: &pyo3::Bound<'_, PyAny>) -> PyResult<Value> {
186186
}
187187
if let Ok(list) = obj.cast::<PyList>() {
188188
let items: PyResult<Vec<Value>> =
189-
list.iter().map(|item| py_any_to_json(py, &item)).collect();
189+
list.iter().map(|item| py_any_to_json(&item)).collect();
190190
return Ok(Value::Array(items?));
191191
}
192192
if let Ok(dict) = obj.cast::<PyDict>() {
193193
let mut map = serde_json::Map::new();
194194
for (key, value) in dict.iter() {
195-
map.insert(key.to_string(), py_any_to_json(py, &value)?);
195+
map.insert(key.to_string(), py_any_to_json(&value)?);
196196
}
197197
return Ok(Value::Object(map));
198198
}

0 commit comments

Comments
 (0)