Skip to content

Commit 26005cf

Browse files
committed
Preserve symtable warning exceptions and original filenames
Keep warning-hook exceptions untouched instead of rewriting SyntaxError subclasses. Thread the original Python filename through parser warning emission and fresh warning-as-error SyntaxError construction, preserving fsdecoded bytes paths and lone surrogates. Assisted-by: OpenAI Codex:model-version-unavailable
1 parent a788de2 commit 26005cf

2 files changed

Lines changed: 101 additions & 30 deletions

File tree

‎crates/vm/src/stdlib/_symtable.rs‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ mod _symtable {
88
compiler,
99
function::{ArgStrOrBytesLike, FsPath},
1010
types::Representable,
11+
vm::VmCompileError,
1112
};
1213
use alloc::fmt;
1314
use rustpython_codegen::symboltable::{CompilerScope, SymbolFlags, SymbolScope, SymbolTable};
@@ -153,10 +154,21 @@ mod _symtable {
153154
})
154155
.transpose()?;
155156
let symtable = vm
156-
.compile_symtable_with_module(&source, mode, &filename, module.as_deref())
157-
.map_err(|err| {
158-
let err = err.into_pyexception(vm, Some(&source));
159-
set_syntax_error_filename(err, &filename_obj, vm)
157+
.compile_symtable_with_module(
158+
&source,
159+
mode,
160+
&filename,
161+
&filename_obj,
162+
module.as_deref(),
163+
)
164+
.map_err(|err| match err {
165+
VmCompileError::Compile(err) => {
166+
let err = vm.new_syntax_error(&err, Some(&source));
167+
set_syntax_error_filename(err, &filename_obj, vm)
168+
}
169+
// Warning hooks may raise their own exception, including a
170+
// SyntaxError subclass. Propagate it without changing attributes.
171+
warning @ VmCompileError::Warning(_) => warning.into_pyexception(vm, Some(&source)),
160172
})?;
161173

162174
Ok(to_py_symbol_table(symtable, vm))

‎crates/vm/src/vm/compile.rs‎

Lines changed: 85 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use core::fmt;
66

77
use crate::{
88
AsObject, Py, PyObject, PyObjectRef, PyRef, PyResult, VirtualMachine,
9-
builtins::{PyBaseExceptionRef, PyCode, PyStr},
9+
builtins::{PyBaseExceptionRef, PyCode, PyStr, PyStrRef},
1010
compiler::{self, CompileError, CompileOpts},
1111
vm::compile_mode::{CompileStart, CompilerFlags, compile_future_features_from_flags},
1212
};
@@ -31,6 +31,7 @@ pub enum VmCompileError {
3131
pub struct CompileWarningError {
3232
exception: PyBaseExceptionRef,
3333
filename: String,
34+
original_filename: Option<PyStrRef>,
3435
lineno: usize,
3536
offset: usize,
3637
replacement: Option<SyntaxErrorReplacement>,
@@ -116,7 +117,12 @@ impl CompileWarningError {
116117
}
117118
syntax_error
118119
.as_object()
119-
.set_attr("filename", vm.ctx.new_str(self.filename), vm)
120+
.set_attr(
121+
"filename",
122+
self.original_filename
123+
.unwrap_or_else(|| vm.ctx.new_str(self.filename)),
124+
vm,
125+
)
120126
.unwrap();
121127
let text = self.diagnostic_text.map_or_else(
122128
|| {
@@ -453,10 +459,14 @@ impl VirtualMachine {
453459
self.emit_ast_parser_warnings(
454460
source,
455461
filename,
462+
None,
456463
module,
457464
start,
458-
future_features.contains(crate::bytecode::CodeFlags::FUTURE_BARRY_AS_BDFL),
459-
type_comments,
465+
escape_warnings::AstParserWarningOptions {
466+
inherited_barry_as_flufl: future_features
467+
.contains(crate::bytecode::CodeFlags::FUTURE_BARRY_AS_BDFL),
468+
type_comments,
469+
},
460470
)
461471
.map_err(|warning| {
462472
VmCompileError::Warning(warning).into_pyexception(self, Some(source))
@@ -472,6 +482,7 @@ impl VirtualMachine {
472482
source,
473483
escape_warnings::InvalidEscape::Char { ch, offset },
474484
filename,
485+
None,
475486
module.map(AsObject::as_object),
476487
self,
477488
)
@@ -558,10 +569,14 @@ impl VirtualMachine {
558569
self.emit_ast_parser_warnings(
559570
source,
560571
filename,
572+
None,
561573
module,
562574
start,
563-
future_features.contains(crate::bytecode::CodeFlags::FUTURE_BARRY_AS_BDFL),
564-
true,
575+
escape_warnings::AstParserWarningOptions {
576+
inherited_barry_as_flufl: future_features
577+
.contains(crate::bytecode::CodeFlags::FUTURE_BARRY_AS_BDFL),
578+
type_comments: true,
579+
},
565580
)
566581
.map_err(|warning| {
567582
VmCompileError::Warning(warning).into_pyexception(self, Some(source))
@@ -627,6 +642,7 @@ impl VirtualMachine {
627642
source: &str,
628643
mode: compiler::Mode,
629644
filename: &str,
645+
original_filename: &Py<PyStr>,
630646
module: Option<&Py<PyStr>>,
631647
) -> Result<compiler::codegen::symboltable::SymbolTable, VmCompileError> {
632648
#[cfg(feature = "parser")]
@@ -638,11 +654,18 @@ impl VirtualMachine {
638654
};
639655
// A symbol table observes parser warnings, but does not compile
640656
// bytecode or emit code-generation and AST-preprocessing warnings.
641-
self.emit_ast_parser_warnings(source, filename, module, start, false, false)
642-
.map_err(VmCompileError::Warning)?;
657+
self.emit_ast_parser_warnings(
658+
source,
659+
filename,
660+
Some(original_filename),
661+
module,
662+
start,
663+
escape_warnings::AstParserWarningOptions::default(),
664+
)
665+
.map_err(VmCompileError::Warning)?;
643666
}
644667
#[cfg(not(feature = "parser"))]
645-
let _ = module;
668+
let _ = (module, original_filename);
646669
compiler::compile_symtable(source, mode, filename).map_err(VmCompileError::Compile)
647670
}
648671

@@ -818,12 +841,14 @@ mod escape_warnings {
818841
exception: PyBaseExceptionRef,
819842
source: &str,
820843
filename: &str,
844+
original_filename: Option<&Py<PyStr>>,
821845
offset: usize,
822846
) -> CompileWarningError {
823847
let (lineno, offset) = tokenizer_line_offset_at(source, offset);
824848
CompileWarningError {
825849
exception,
826850
filename: filename.to_owned(),
851+
original_filename: original_filename.map(ToOwned::to_owned),
827852
lineno,
828853
offset,
829854
replacement: None,
@@ -1269,16 +1294,26 @@ mod escape_warnings {
12691294
source: &str,
12701295
escape: InvalidEscape,
12711296
filename: &str,
1297+
original_filename: Option<&Py<PyStr>>,
12721298
module: Option<&PyObject>,
12731299
vm: &VirtualMachine,
12741300
) -> Result<(), CompileWarningError> {
1275-
warn_invalid_escape_sequence_at(source, escape, filename, module, None, vm)
1301+
warn_invalid_escape_sequence_at(
1302+
source,
1303+
escape,
1304+
filename,
1305+
original_filename,
1306+
module,
1307+
None,
1308+
vm,
1309+
)
12761310
}
12771311

12781312
fn warn_invalid_escape_sequence_at(
12791313
source: &str,
12801314
escape: InvalidEscape,
12811315
filename: &str,
1316+
original_filename: Option<&Py<PyStr>>,
12821317
module: Option<&PyObject>,
12831318
diagnostic: Option<EscapeDiagnosticLocation>,
12841319
vm: &VirtualMachine,
@@ -1292,7 +1327,8 @@ mod escape_warnings {
12921327
.map_or(column + 2, |location| location.end_offset);
12931328
let warning = escape.warning_message();
12941329
let syntax_error = escape.syntax_error_message();
1295-
let fname = vm.ctx.new_str(filename);
1330+
let fname =
1331+
original_filename.map_or_else(|| vm.ctx.new_str(filename), |name| name.to_owned());
12961332
warn::warn_explicit(
12971333
Some(vm.ctx.exceptions.syntax_warning.to_owned()),
12981334
vm.ctx.new_str(warning).into(),
@@ -1307,6 +1343,7 @@ mod escape_warnings {
13071343
.map_err(|exception| CompileWarningError {
13081344
exception,
13091345
filename: filename.to_owned(),
1346+
original_filename: original_filename.map(ToOwned::to_owned),
13101347
lineno,
13111348
offset: column,
13121349
replacement: Some(SyntaxErrorReplacement {
@@ -1326,13 +1363,15 @@ mod escape_warnings {
13261363
fn warn_syntax_at_offset(
13271364
source: &str,
13281365
filename: &str,
1366+
original_filename: Option<&Py<PyStr>>,
13291367
module: Option<&PyObject>,
13301368
offset: usize,
13311369
message: String,
13321370
vm: &VirtualMachine,
13331371
) -> Result<(), CompileWarningError> {
13341372
let (lineno, _) = tokenizer_line_offset_at(source, offset);
1335-
let fname = vm.ctx.new_str(filename);
1373+
let fname =
1374+
original_filename.map_or_else(|| vm.ctx.new_str(filename), |name| name.to_owned());
13361375
let message = vm.ctx.new_str(message);
13371376
warn::warn_explicit(
13381377
Some(vm.ctx.exceptions.syntax_warning.to_owned()),
@@ -1345,7 +1384,7 @@ mod escape_warnings {
13451384
None,
13461385
vm,
13471386
)
1348-
.map_err(|err| compile_warning_error(err, source, filename, offset))
1387+
.map_err(|err| compile_warning_error(err, source, filename, original_filename, offset))
13491388
}
13501389

13511390
pub(super) fn warn_syntax_at_location(
@@ -1371,6 +1410,7 @@ mod escape_warnings {
13711410
.map_err(|exception| CompileWarningError {
13721411
exception,
13731412
filename: filename.to_owned(),
1413+
original_filename: None,
13741414
lineno: location.line.get(),
13751415
offset: location.character_offset.get(),
13761416
replacement: None,
@@ -2444,6 +2484,12 @@ mod escape_warnings {
24442484
}
24452485
}
24462486

2487+
#[derive(Default)]
2488+
pub(super) struct AstParserWarningOptions {
2489+
pub(super) inherited_barry_as_flufl: bool,
2490+
pub(super) type_comments: bool,
2491+
}
2492+
24472493
#[derive(Default)]
24482494
struct ParserWarningBoundaries {
24492495
single: Option<compiler::SingleModeStatementBoundary>,
@@ -2457,6 +2503,7 @@ mod escape_warnings {
24572503
},
24582504
Ast {
24592505
tokenizer_recovery: bool,
2506+
original_filename: Option<&'a Py<PyStr>>,
24602507
},
24612508
Interactive(&'a mut super::InteractiveWarnings),
24622509
}
@@ -2596,10 +2643,10 @@ mod escape_warnings {
25962643
&self,
25972644
source: &str,
25982645
filename: &str,
2646+
original_filename: Option<&Py<PyStr>>,
25992647
module: Option<&Py<PyStr>>,
26002648
start: CompileStart,
2601-
inherited_barry_as_flufl: bool,
2602-
type_comments: bool,
2649+
options: AstParserWarningOptions,
26032650
) -> Result<Vec<usize>, CompileWarningError> {
26042651
let mode = match start {
26052652
CompileStart::Single | CompileStart::File => Some(ruff_python_parser::Mode::Module),
@@ -2610,12 +2657,12 @@ mod escape_warnings {
26102657
source,
26112658
filename,
26122659
mode,
2613-
inherited_barry_as_flufl,
2660+
options.inherited_barry_as_flufl,
26142661
InterpolatedBodyDiagnostics::TokenAnchored {
26152662
implicit_final_newline: matches!(start, CompileStart::File),
26162663
},
26172664
matches!(start, CompileStart::Single),
2618-
type_comments,
2665+
options.type_comments,
26192666
);
26202667
self.emit_parser_warning_batch(
26212668
source,
@@ -2643,6 +2690,7 @@ mod escape_warnings {
26432690
start,
26442691
CompileStart::File | CompileStart::Eval | CompileStart::Single
26452692
),
2693+
original_filename,
26462694
},
26472695
)
26482696
}
@@ -2689,14 +2737,18 @@ mod escape_warnings {
26892737
single: single_boundary,
26902738
type_comment_error_offset,
26912739
} = boundaries;
2692-
let (emitted_escapes, ast_tokenizer_recovery, mut interactive) = match emission {
2693-
WarningEmission::Standard {
2694-
emitted_escapes,
2695-
tokenizer_recovery,
2696-
} => (emitted_escapes, tokenizer_recovery, None),
2697-
WarningEmission::Ast { tokenizer_recovery } => (&[][..], tokenizer_recovery, None),
2698-
WarningEmission::Interactive(emitted) => (&[][..], false, Some(emitted)),
2699-
};
2740+
let (emitted_escapes, ast_tokenizer_recovery, original_filename, mut interactive) =
2741+
match emission {
2742+
WarningEmission::Standard {
2743+
emitted_escapes,
2744+
tokenizer_recovery,
2745+
} => (emitted_escapes, tokenizer_recovery, None, None),
2746+
WarningEmission::Ast {
2747+
tokenizer_recovery,
2748+
original_filename,
2749+
} => (&[][..], tokenizer_recovery, original_filename, None),
2750+
WarningEmission::Interactive(emitted) => (&[][..], false, None, Some(emitted)),
2751+
};
27002752
let mut considered_escapes = alloc::collections::BTreeSet::new();
27012753
let mut parser_error = None;
27022754
for warning in warnings {
@@ -2762,7 +2814,13 @@ mod escape_warnings {
27622814
}
27632815
if emitted_escapes.binary_search(&offset).is_err() {
27642816
let result = warn_invalid_escape_sequence_at(
2765-
source, escape, filename, module, diagnostic, self,
2817+
source,
2818+
escape,
2819+
filename,
2820+
original_filename,
2821+
module,
2822+
diagnostic,
2823+
self,
27662824
)
27672825
.map_err(|mut error| {
27682826
if interactive.is_some()
@@ -2812,6 +2870,7 @@ mod escape_warnings {
28122870
let result = warn_syntax_at_offset(
28132871
source,
28142872
filename,
2873+
original_filename,
28152874
module,
28162875
last_offset,
28172876
message.clone(),

0 commit comments

Comments
 (0)