Skip to content

Commit daea55d

Browse files
committed
Improve Python execution
1 parent ac2a9b6 commit daea55d

File tree

2 files changed

+158
-145
lines changed

2 files changed

+158
-145
lines changed

utile/src/python/function.rs

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ impl PythonFunction {
6363
Ok((value, output))
6464
}
6565

66-
pub fn into_map(self) -> io::Result<super::map::PythonMap> {
67-
super::map::PythonMap::new(&self)
66+
pub async fn into_map(self) -> io::Result<super::map::PythonMap> {
67+
super::map::PythonMap::new(&self).await
6868
}
6969
}
7070
#[cfg(not(target_arch = "wasm32"))]
@@ -75,18 +75,17 @@ impl PythonFunction {
7575
let function = &self.function;
7676

7777
let content = format!(
78-
r##"
79-
import sys
80-
import os
81-
import io
82-
import traceback
83-
import json
84-
from contextlib import redirect_stdout, redirect_stderr
85-
from pydantic import BaseModel
86-
87-
{function}
78+
r##"{function}
8879
8980
def main():
81+
import sys
82+
import os
83+
import io
84+
import traceback
85+
import json
86+
from contextlib import redirect_stdout, redirect_stderr
87+
from pydantic import BaseModel
88+
9089
class __InternalOutputModel(BaseModel):
9190
value: {output} | None
9291
error: str | None
@@ -231,7 +230,7 @@ impl PythonFunction {
231230
vec![Self::simple(), Self::dep(), Self::exception()]
232231
}
233232

234-
fn simple() -> (Self, Vec<TestValue>) {
233+
pub(super) fn simple() -> (Self, Vec<TestValue>) {
235234
const PYTHON_VERSION: &str = "==3.10";
236235
const FUNCTION: &str = "
237236
def process(input: int) -> int:
@@ -258,7 +257,7 @@ def process(input: int) -> int:
258257
)
259258
}
260259

261-
fn dep() -> (Self, Vec<TestValue>) {
260+
pub(super) fn dep() -> (Self, Vec<TestValue>) {
262261
const PYTHON_VERSION: &str = "==3.10";
263262
const FUNCTION: &str = "
264263
import numpy as np
@@ -285,14 +284,14 @@ def process(x: int) -> list[int]:
285284
)
286285
}
287286

288-
fn exception() -> (Self, Vec<TestValue>) {
287+
pub(super) fn exception() -> (Self, Vec<TestValue>) {
289288
const PYTHON_VERSION: &str = "==3.10";
290289
const FUNCTION: &str = "
291290
def process(input: int) -> int:
292291
print(\"hello\")
293292
raise Exception('This is a test')
294293
";
295-
const ERROR: &str = "Traceback (most recent call last):\n File \"/temp_folder/script.py\", line 47, in main\n result = process(input)\n File \"/temp_folder/script.py\", line 19, in process\n raise Exception('This is a test')\nException: This is a test\n";
294+
const ERROR: &str = "Traceback (most recent call last):\n File \"/temp_folder/script.py\", line 46, in main\n result = process(input)\n File \"/temp_folder/script.py\", line 10, in process\n raise Exception('This is a test')\nException: This is a test\n";
296295

297296
let values = vec![(
298297
Value::Number(40.into()),
@@ -344,7 +343,9 @@ mod tests {
344343
assert!(status.success());
345344
assert_eq!(
346345
stdout,
347-
b"{\"value\":42,\"error\":null,\"stdout\":\"hello\\n\",\"stderr\":\"\"}\n"
346+
b"{\"value\":42,\"error\":null,\"stdout\":\"hello\\n\",\"stderr\":\"\"}\n",
347+
"{:?}",
348+
String::from_utf8_lossy(stdout)
348349
);
349350
// assert_eq!(stderr, b"Installed 5 packages in 3ms\n");
350351
assert!(
@@ -374,7 +375,9 @@ mod tests {
374375
assert!(status.success());
375376
assert_eq!(
376377
stdout,
377-
b"{\"value\":42,\"error\":null,\"stdout\":\"hello\\n\",\"stderr\":\"\"}\n"
378+
b"{\"value\":42,\"error\":null,\"stdout\":\"hello\\n\",\"stderr\":\"\"}\n",
379+
"{:?}",
380+
String::from_utf8_lossy(stdout)
378381
);
379382
// assert_eq!(stderr, b"Installed 5 packages in 3ms\n");
380383
assert!(
@@ -405,7 +408,9 @@ mod tests {
405408
assert!(status.success());
406409
assert_eq!(
407410
stdout,
408-
b"{\"value\":42,\"error\":null,\"stdout\":\"hello\\n\",\"stderr\":\"\"}\n"
411+
b"{\"value\":42,\"error\":null,\"stdout\":\"hello\\n\",\"stderr\":\"\"}\n",
412+
"{:?}",
413+
String::from_utf8_lossy(stdout)
409414
);
410415
// assert_eq!(stderr, b"Installed 5 packages in 3ms\n");
411416
assert!(
@@ -435,7 +440,9 @@ mod tests {
435440
assert!(status.success());
436441
assert_eq!(
437442
stdout,
438-
b"{\"value\":42,\"error\":null,\"stdout\":\"hello\\n\",\"stderr\":\"\"}\n"
443+
b"{\"value\":42,\"error\":null,\"stdout\":\"hello\\n\",\"stderr\":\"\"}\n",
444+
"{:?}",
445+
String::from_utf8_lossy(stdout)
439446
);
440447
// assert_eq!(stderr, b"Installed 5 packages in 3ms\n");
441448
assert!(
@@ -458,8 +465,8 @@ mod tests {
458465
mod exception_tests {
459466
use super::*;
460467

461-
const STDOUT: &str = "{\"value\":null,\"error\":\"Traceback (most recent call last):\\n File \\\"/temp_folder/script.py\\\", line 47, in main\\n result = process(input)\\n File \\\"/temp_folder/script.py\\\", line 19, in process\\n raise Exception('This is a test')\\nException: This is a test\\n\",\"stdout\":\"hello\\n\",\"stderr\":\"\"}\n";
462-
const ERROR: &str = "Traceback (most recent call last):\n File \"/temp_folder/script.py\", line 47, in main\n result = process(input)\n File \"/temp_folder/script.py\", line 19, in process\n raise Exception('This is a test')\nException: This is a test\n";
468+
const STDOUT: &str = "{\"value\":null,\"error\":\"Traceback (most recent call last):\\n File \\\"/temp_folder/script.py\\\", line 46, in main\\n result = process(input)\\n File \\\"/temp_folder/script.py\\\", line 10, in process\\n raise Exception('This is a test')\\nException: This is a test\\n\",\"stdout\":\"hello\\n\",\"stderr\":\"\"}\n";
469+
const ERROR: &str = "Traceback (most recent call last):\n File \"/temp_folder/script.py\", line 46, in main\n result = process(input)\n File \"/temp_folder/script.py\", line 10, in process\n raise Exception('This is a test')\nException: This is a test\n";
463470

464471
#[tokio::test]
465472
async fn test_run_function_exception() {
@@ -472,7 +479,12 @@ mod exception_tests {
472479
stderr,
473480
} = &output;
474481
assert!(status.success());
475-
assert_eq!(stdout, STDOUT.as_bytes(),);
482+
assert_eq!(
483+
stdout,
484+
STDOUT.as_bytes(),
485+
"{:?}",
486+
String::from_utf8_lossy(stdout)
487+
);
476488
// assert_eq!(stderr, b"Installed 5 packages in 3ms\n");
477489
assert!(
478490
stderr
@@ -499,7 +511,12 @@ mod exception_tests {
499511
stderr,
500512
} = &output;
501513
assert!(status.success());
502-
assert_eq!(stdout, STDOUT.as_bytes());
514+
assert_eq!(
515+
stdout,
516+
STDOUT.as_bytes(),
517+
"{:?}",
518+
String::from_utf8_lossy(stdout)
519+
);
503520
// assert_eq!(stderr, b"Installed 5 packages in 3ms\n");
504521
assert!(
505522
stderr

0 commit comments

Comments
 (0)