11test_that(" newKaleidoScope does not inline Windows temp paths into Python code" , {
22 skip_if_not_installed(" reticulate" )
33 skip_if_not(suppressWarnings(reticulate :: py_available(TRUE )))
4+ withr :: defer({
5+ py <- reticulate :: py
6+ for (name in c(" fig" , " tmp_json_path" )) {
7+ if (reticulate :: py_has_attr(py , name )) {
8+ reticulate :: py_del_attr(py , name )
9+ }
10+ }
11+ })
412
513 win_path <- " C:\\ users\\ name\\ AppData\\ Local\\ Temp\\ Rtmp\\ file.json"
614 py_calls <- character ()
@@ -52,6 +60,8 @@ test_that("newKaleidoScope does not inline Windows temp paths into Python code",
5260})
5361
5462test_that(" py_run_string_with_context rejects invalid Python identifiers" , {
63+ skip_if_not_installed(" reticulate" )
64+
5565 expect_error(
5666 plotly ::: .py_run_string_with_context(
5767 " value = 1" ,
@@ -60,3 +70,38 @@ test_that("py_run_string_with_context rejects invalid Python identifiers", {
6070 " `context` names must be valid Python identifiers\\ ."
6171 )
6272})
73+
74+ test_that(" py_run_string_with_context cleans up partial assignments" , {
75+ skip_if_not_installed(" reticulate" )
76+ skip_if_not(suppressWarnings(reticulate :: py_available(TRUE )))
77+
78+ deleted <- character ()
79+
80+ testthat :: local_mocked_bindings(
81+ py_has_attr = function (x , name ) FALSE ,
82+ py_get_attr = function (x , name , silent = FALSE ) stop(" unexpected py_get_attr call" ),
83+ py_set_attr = function (x , name , value ) {
84+ if (identical(name , " second" )) {
85+ stop(" boom" )
86+ }
87+ invisible (NULL )
88+ },
89+ py_del_attr = function (x , name ) {
90+ deleted <<- c(deleted , name )
91+ invisible (NULL )
92+ },
93+ py_run_string = function (code , local = FALSE , convert = TRUE ) {
94+ stop(" unexpected py_run_string call" )
95+ },
96+ .package = " reticulate"
97+ )
98+
99+ expect_error(
100+ plotly ::: .py_run_string_with_context(
101+ " value = 1" ,
102+ context = list (first = 1 , second = 2 )
103+ ),
104+ " boom"
105+ )
106+ expect_identical(deleted , " first" )
107+ })
0 commit comments