interpretthis 0.3.0

Sandboxed Python AST interpreter for untrusted and LLM-generated code
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
// Copyright 2026 Thomas Santerre and Moderately AI Inc.
//
// SPDX-License-Identifier: MIT OR Apache-2.0

use rustpython_parser::ast::{self, Expr};

use crate::{
    error::{EvalError, EvalResult, InterpreterError},
    eval::{
        eval_expr,
        functions::{VariableCheckpoint, resolve_proxy},
        literals::value_to_key,
    },
    state::InterpreterState,
    tools::Tools,
    value::{Value, ValueKey, shared_list},
};

/// Collect every name introduced by a comprehension's `for X in ...`
/// generator targets (e.g. `x, y` in `[... for x, y in pairs]`). These
/// names are scoped to the comprehension in CPython; in our flat-state
/// model we snapshot their pre-comp values and restore on exit so the
/// names don't leak. Walrus targets are deliberately NOT collected
/// here — PEP 572 binds them to the comprehension's *enclosing* scope.
fn collect_generator_target_names(generators: &[ast::Comprehension]) -> Vec<String> {
    let mut names = Vec::new();
    for g in generators {
        collect_target_names(&g.target, &mut names);
    }
    names
}

/// Same `collect_target_names` walker the function-frame checkpoint uses,
/// inlined here so the comprehension module doesn't need a public export
/// from the `functions` module. Kept private — this isn't a stable API.
fn collect_target_names(target: &ast::Expr, out: &mut Vec<String>) {
    match target {
        Expr::Name(n) => {
            let s = n.id.as_str().to_string();
            if !out.contains(&s) {
                out.push(s);
            }
        }
        Expr::Tuple(t) => {
            for e in &t.elts {
                collect_target_names(e, out);
            }
        }
        Expr::List(l) => {
            for e in &l.elts {
                collect_target_names(e, out);
            }
        }
        Expr::Starred(s) => collect_target_names(&s.value, out),
        _ => {}
    }
}

/// Evaluate a list comprehension [expr for x in iterable if cond].
///
/// The comprehension target names are scoped to the comprehension —
/// snapshotted on entry and restored on exit. Walrus targets inside
/// the comprehension are deliberately NOT included in the checkpoint,
/// so they propagate to the enclosing function scope (PEP 572).
pub async fn eval_list_comp(
    state: &mut InterpreterState,
    node: &ast::ExprListComp,
    tools: &Tools,
) -> EvalResult {
    let checkpoint =
        VariableCheckpoint::capture(state, collect_generator_target_names(&node.generators));
    let mut results = Vec::new();

    let outcome = eval_list_generators(ListGenContext {
        state,
        generators: &node.generators,
        index: 0,
        elt_expr: &node.elt,
        results: &mut results,
        tools,
    })
    .await;

    checkpoint.restore(state);
    outcome?;

    Ok(Value::List(shared_list(results)))
}

/// Evaluate a generator expression `(expr for x in iterable if cond)`.
///
/// The interpreter has no coroutine/`yield` machinery, so a generator is
/// materialised eagerly into a `Value::List`. Every consumer in this sandbox
/// (`sum`/`all`/`any`/`min`/`max`/`sorted`/`list`/`set` and `for` loops) treats
/// the result as a plain iterable, so eager materialisation is observably
/// identical to lazy iteration for bounded inputs — and the operation/loop
/// limits already bound the input. The cost is that side effects run at
/// construction time rather than on demand and that unbounded generators are
/// not representable; both are acceptable in a sandbox that forbids I/O and
/// caps iteration counts.
pub async fn eval_generator_exp(
    state: &mut InterpreterState,
    node: &ast::ExprGeneratorExp,
    tools: &Tools,
) -> EvalResult {
    let checkpoint =
        VariableCheckpoint::capture(state, collect_generator_target_names(&node.generators));
    let mut results = Vec::new();

    let outcome = eval_list_generators(ListGenContext {
        state,
        generators: &node.generators,
        index: 0,
        elt_expr: &node.elt,
        results: &mut results,
        tools,
    })
    .await;

    checkpoint.restore(state);
    outcome?;

    Ok(Value::List(shared_list(results)))
}

/// Evaluate a dict comprehension {key: val for x in iterable if cond}.
pub async fn eval_dict_comp(
    state: &mut InterpreterState,
    node: &ast::ExprDictComp,
    tools: &Tools,
) -> EvalResult {
    let checkpoint =
        VariableCheckpoint::capture(state, collect_generator_target_names(&node.generators));
    let mut result_map = indexmap::IndexMap::new();

    let outcome = eval_dict_generators(DictGenContext {
        state,
        generators: &node.generators,
        index: 0,
        key_expr: &node.key,
        value_expr: &node.value,
        result_map: &mut result_map,
        tools,
    })
    .await;

    checkpoint.restore(state);
    outcome?;

    Ok(Value::Dict(result_map))
}

/// Evaluate a set comprehension {expr for x in iterable if cond}.
pub async fn eval_set_comp(
    state: &mut InterpreterState,
    node: &ast::ExprSetComp,
    tools: &Tools,
) -> EvalResult {
    let checkpoint =
        VariableCheckpoint::capture(state, collect_generator_target_names(&node.generators));
    let mut results = Vec::new();

    let outcome = eval_list_generators(ListGenContext {
        state,
        generators: &node.generators,
        index: 0,
        elt_expr: &node.elt,
        results: &mut results,
        tools,
    })
    .await;

    checkpoint.restore(state);
    outcome?;

    // Deduplicate for set semantics
    let mut deduped = Vec::new();
    for item in results {
        let key = value_to_key(&item).ok();
        let already = deduped.iter().any(|r: &Value| value_to_key(r).ok() == key);
        if !already {
            deduped.push(item);
        }
    }

    Ok(Value::Set(deduped))
}

/// Per-call context for [`eval_list_generators`].
struct ListGenContext<'a> {
    state: &'a mut InterpreterState,
    generators: &'a [ast::Comprehension],
    index: usize,
    elt_expr: &'a Expr,
    results: &'a mut Vec<Value>,
    tools: &'a Tools,
}

/// Recursively evaluate generators for list/set comprehensions.
fn eval_list_generators<'a>(
    ctx: ListGenContext<'a>,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<(), EvalError>> + Send + 'a>> {
    Box::pin(async move {
        let ListGenContext { state, generators, index, elt_expr, results, tools } = ctx;

        if index >= generators.len() {
            // Base case: evaluate the element expression
            let val = eval_expr(state, elt_expr, tools).await?;
            results.push(val);
            return Ok(());
        }

        let generator = &generators[index];
        let iterable = eval_expr(state, &generator.iter, tools).await?;
        let iterable = resolve_proxy(&iterable).await?;

        // Range fast path: walk (start, stop, step) without materializing
        // 10k Value::Int entries the iter consumer would immediately drop.
        // `[x * x for x in range(10000)]` is the canonical case.
        if let Value::Range { start, stop, step } = iterable {
            let pos = step > 0;
            let mut i = start;
            loop {
                let in_range = (pos && i < stop) || (step < 0 && i > stop);
                if !in_range {
                    break;
                }
                set_comprehension_target(state, &generator.target, &Value::Int(i)).await?;

                let mut include = true;
                for if_clause in &generator.ifs {
                    let cond = eval_expr(state, if_clause, tools).await?;
                    let cond = resolve_proxy(&cond).await?;
                    if !cond.is_truthy() {
                        include = false;
                        break;
                    }
                }
                if include {
                    eval_list_generators(ListGenContext {
                        state,
                        generators,
                        index: index + 1,
                        elt_expr,
                        results,
                        tools,
                    })
                    .await?;
                }

                let Some(next) = i.checked_add(step) else { break };
                i = next;
            }
            return Ok(());
        }

        let items = crate::eval::op::iter(state, &iterable, tools).await?;

        for item in items {
            // Set the target variable
            set_comprehension_target(state, &generator.target, &item).await?;

            // Check if-filters
            let mut include = true;
            for if_clause in &generator.ifs {
                let cond = eval_expr(state, if_clause, tools).await?;
                let cond = resolve_proxy(&cond).await?;
                if !cond.is_truthy() {
                    include = false;
                    break;
                }
            }

            if include {
                eval_list_generators(ListGenContext {
                    state,
                    generators,
                    index: index + 1,
                    elt_expr,
                    results,
                    tools,
                })
                .await?;
            }
        }

        Ok(())
    })
}

/// Per-call context for [`eval_dict_generators`].
///
/// `key_expr` and `value_expr` are both `&Expr`; without bundling, a
/// silent transposition would flip every comprehension's key/value
/// pair. The struct makes the role of each `Expr` named at every
/// recursive call site.
struct DictGenContext<'a> {
    state: &'a mut InterpreterState,
    generators: &'a [ast::Comprehension],
    index: usize,
    key_expr: &'a Expr,
    value_expr: &'a Expr,
    result_map: &'a mut indexmap::IndexMap<ValueKey, Value>,
    tools: &'a Tools,
}

/// Recursively evaluate generators for dict comprehensions.
fn eval_dict_generators<'a>(
    ctx: DictGenContext<'a>,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<(), EvalError>> + Send + 'a>> {
    Box::pin(async move {
        let DictGenContext { state, generators, index, key_expr, value_expr, result_map, tools } =
            ctx;

        if index >= generators.len() {
            let key = eval_expr(state, key_expr, tools).await?;
            let value = eval_expr(state, value_expr, tools).await?;
            let key = value_to_key(&key)?;
            result_map.insert(key, value);
            return Ok(());
        }

        let generator = &generators[index];
        let iterable = eval_expr(state, &generator.iter, tools).await?;
        let iterable = resolve_proxy(&iterable).await?;

        // Range fast path: same rationale as the list-comp variant above.
        if let Value::Range { start, stop, step } = iterable {
            let pos = step > 0;
            let mut i = start;
            loop {
                let in_range = (pos && i < stop) || (step < 0 && i > stop);
                if !in_range {
                    break;
                }
                set_comprehension_target(state, &generator.target, &Value::Int(i)).await?;

                let mut include = true;
                for if_clause in &generator.ifs {
                    let cond = eval_expr(state, if_clause, tools).await?;
                    let cond = resolve_proxy(&cond).await?;
                    if !cond.is_truthy() {
                        include = false;
                        break;
                    }
                }
                if include {
                    eval_dict_generators(DictGenContext {
                        state,
                        generators,
                        index: index + 1,
                        key_expr,
                        value_expr,
                        result_map,
                        tools,
                    })
                    .await?;
                }

                let Some(next) = i.checked_add(step) else { break };
                i = next;
            }
            return Ok(());
        }

        let items = crate::eval::op::iter(state, &iterable, tools).await?;

        for item in items {
            set_comprehension_target(state, &generator.target, &item).await?;

            let mut include = true;
            for if_clause in &generator.ifs {
                let cond = eval_expr(state, if_clause, tools).await?;
                let cond = resolve_proxy(&cond).await?;
                if !cond.is_truthy() {
                    include = false;
                    break;
                }
            }

            if include {
                eval_dict_generators(DictGenContext {
                    state,
                    generators,
                    index: index + 1,
                    key_expr,
                    value_expr,
                    result_map,
                    tools,
                })
                .await?;
            }
        }

        Ok(())
    })
}

/// Set a comprehension target variable (handles simple names and tuple unpacking).
fn set_comprehension_target<'a>(
    state: &'a mut InterpreterState,
    target: &'a Expr,
    value: &'a Value,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<(), EvalError>> + Send + 'a>> {
    Box::pin(async move {
        match target {
            Expr::Name(name_node) => {
                // Comprehension targets are comp-scoped (Python 3
                // semantics) and overwritten every iteration. The full
                // `set_variable` path runs memory accounting (estimate
                // size of old + new value) and a nonlocal-cell
                // write-through check that's irrelevant for comp
                // targets — both are pure overhead per element. Direct
                // map insert is correct: the result-accumulator's
                // memory IS tracked (the .push into results), only the
                // per-element target's churn is skipped.
                state.variables.insert(name_node.id.as_str().to_string(), value.clone());
                Ok(())
            }
            Expr::Tuple(tuple_node) => {
                let items: Vec<Value> = match value {
                    Value::List(items) => items.lock().clone(),
                    Value::Tuple(items) => items.clone(),
                    Value::String(s) => {
                        s.chars().map(|c| Value::String(c.to_string().into())).collect()
                    }
                    _ => {
                        return Err(InterpreterError::TypeError(
                            "cannot unpack non-iterable value in comprehension".into(),
                        )
                        .into());
                    }
                };

                if tuple_node.elts.len() != items.len() {
                    return Err(InterpreterError::Runtime(
                        "cannot unpack tuple of wrong size in comprehension".into(),
                    )
                    .into());
                }

                for (elem, val) in tuple_node.elts.iter().zip(items.iter()) {
                    set_comprehension_target(state, elem, val).await?;
                }
                Ok(())
            }
            _ => Err(InterpreterError::Runtime(format!(
                "unsupported comprehension target: {:?}",
                std::mem::discriminant(target)
            ))
            .into()),
        }
    })
}