Skip to content

Commit 98a78ef

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 1cd1b5d commit 98a78ef

2 files changed

Lines changed: 70 additions & 21 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: 54 additions & 17 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>,
@@ -115,7 +116,12 @@ impl CompileWarningError {
115116
}
116117
syntax_error
117118
.as_object()
118-
.set_attr("filename", vm.ctx.new_str(self.filename), vm)
119+
.set_attr(
120+
"filename",
121+
self.original_filename
122+
.unwrap_or_else(|| vm.ctx.new_str(self.filename)),
123+
vm,
124+
)
119125
.unwrap();
120126
let text = source
121127
.and_then(|source| source.split('\n').nth(self.lineno.saturating_sub(1)))
@@ -447,6 +453,7 @@ impl VirtualMachine {
447453
self.emit_ast_parser_warnings(
448454
source,
449455
filename,
456+
None,
450457
module,
451458
start,
452459
future_features.contains(crate::bytecode::CodeFlags::FUTURE_BARRY_AS_BDFL),
@@ -465,6 +472,7 @@ impl VirtualMachine {
465472
source,
466473
escape_warnings::InvalidEscape::Char { ch, offset },
467474
filename,
475+
None,
468476
module.map(AsObject::as_object),
469477
self,
470478
)
@@ -591,6 +599,7 @@ impl VirtualMachine {
591599
source: &str,
592600
mode: compiler::Mode,
593601
filename: &str,
602+
original_filename: &Py<PyStr>,
594603
module: Option<&Py<PyStr>>,
595604
) -> Result<compiler::codegen::symboltable::SymbolTable, VmCompileError> {
596605
#[cfg(feature = "parser")]
@@ -602,11 +611,18 @@ impl VirtualMachine {
602611
};
603612
// A symbol table observes parser warnings, but does not compile
604613
// bytecode or emit code-generation and AST-preprocessing warnings.
605-
self.emit_ast_parser_warnings(source, filename, module, start, false)
606-
.map_err(VmCompileError::Warning)?;
614+
self.emit_ast_parser_warnings(
615+
source,
616+
filename,
617+
Some(original_filename),
618+
module,
619+
start,
620+
false,
621+
)
622+
.map_err(VmCompileError::Warning)?;
607623
}
608624
#[cfg(not(feature = "parser"))]
609-
let _ = module;
625+
let _ = (module, original_filename);
610626
compiler::compile_symtable(source, mode, filename).map_err(VmCompileError::Compile)
611627
}
612628

@@ -770,12 +786,14 @@ mod escape_warnings {
770786
exception: PyBaseExceptionRef,
771787
source: &str,
772788
filename: &str,
789+
original_filename: Option<&Py<PyStr>>,
773790
offset: usize,
774791
) -> CompileWarningError {
775792
let (lineno, offset) = line_offset_at(source, offset);
776793
CompileWarningError {
777794
exception,
778795
filename: filename.to_owned(),
796+
original_filename: original_filename.map(ToOwned::to_owned),
779797
lineno,
780798
offset,
781799
replacement: None,
@@ -990,13 +1008,15 @@ mod escape_warnings {
9901008
source: &str,
9911009
escape: InvalidEscape,
9921010
filename: &str,
1011+
original_filename: Option<&Py<PyStr>>,
9931012
module: Option<&PyObject>,
9941013
vm: &VirtualMachine,
9951014
) -> Result<(), CompileWarningError> {
9961015
let (lineno, column) = line_offset_at(source, escape.offset());
9971016
let warning = escape.warning_message();
9981017
let syntax_error = escape.syntax_error_message();
999-
let fname = vm.ctx.new_str(filename);
1018+
let fname =
1019+
original_filename.map_or_else(|| vm.ctx.new_str(filename), |name| name.to_owned());
10001020
warn::warn_explicit(
10011021
Some(vm.ctx.exceptions.syntax_warning.to_owned()),
10021022
vm.ctx.new_str(warning).into(),
@@ -1011,6 +1031,7 @@ mod escape_warnings {
10111031
.map_err(|exception| CompileWarningError {
10121032
exception,
10131033
filename: filename.to_owned(),
1034+
original_filename: original_filename.map(ToOwned::to_owned),
10141035
lineno,
10151036
offset: column,
10161037
replacement: Some(SyntaxErrorReplacement {
@@ -1023,13 +1044,15 @@ mod escape_warnings {
10231044
fn warn_syntax_at_offset(
10241045
source: &str,
10251046
filename: &str,
1047+
original_filename: Option<&Py<PyStr>>,
10261048
module: Option<&PyObject>,
10271049
offset: usize,
10281050
message: String,
10291051
vm: &VirtualMachine,
10301052
) -> Result<(), CompileWarningError> {
10311053
let lineno = line_number_at(source, offset);
1032-
let fname = vm.ctx.new_str(filename);
1054+
let fname =
1055+
original_filename.map_or_else(|| vm.ctx.new_str(filename), |name| name.to_owned());
10331056
let message = vm.ctx.new_str(message);
10341057
warn::warn_explicit(
10351058
Some(vm.ctx.exceptions.syntax_warning.to_owned()),
@@ -1042,7 +1065,7 @@ mod escape_warnings {
10421065
None,
10431066
vm,
10441067
)
1045-
.map_err(|err| compile_warning_error(err, source, filename, offset))
1068+
.map_err(|err| compile_warning_error(err, source, filename, original_filename, offset))
10461069
}
10471070

10481071
pub(super) fn warn_syntax_at_location(
@@ -1068,6 +1091,7 @@ mod escape_warnings {
10681091
.map_err(|exception| CompileWarningError {
10691092
exception,
10701093
filename: filename.to_owned(),
1094+
original_filename: None,
10711095
lineno: location.line.get(),
10721096
offset: location.character_offset.get(),
10731097
replacement: None,
@@ -1854,6 +1878,7 @@ mod escape_warnings {
18541878
},
18551879
Ast {
18561880
tokenizer_recovery: bool,
1881+
original_filename: Option<&'a Py<PyStr>>,
18571882
},
18581883
Interactive(&'a mut super::InteractiveWarnings),
18591884
}
@@ -1995,6 +2020,7 @@ mod escape_warnings {
19952020
&self,
19962021
source: &str,
19972022
filename: &str,
2023+
original_filename: Option<&Py<PyStr>>,
19982024
module: Option<&Py<PyStr>>,
19992025
start: CompileStart,
20002026
inherited_barry_as_flufl: bool,
@@ -2034,6 +2060,7 @@ mod escape_warnings {
20342060
start,
20352061
CompileStart::File | CompileStart::Eval | CompileStart::Single
20362062
),
2063+
original_filename,
20372064
},
20382065
)
20392066
}
@@ -2074,14 +2101,18 @@ mod escape_warnings {
20742101
single_boundary: Option<compiler::SingleModeStatementBoundary>,
20752102
emission: WarningEmission<'_>,
20762103
) -> Result<Vec<usize>, CompileWarningError> {
2077-
let (emitted_escapes, ast_tokenizer_recovery, mut interactive) = match emission {
2078-
WarningEmission::Standard {
2079-
emitted_escapes,
2080-
tokenizer_recovery,
2081-
} => (emitted_escapes, tokenizer_recovery, None),
2082-
WarningEmission::Ast { tokenizer_recovery } => (&[][..], tokenizer_recovery, None),
2083-
WarningEmission::Interactive(emitted) => (&[][..], false, Some(emitted)),
2084-
};
2104+
let (emitted_escapes, ast_tokenizer_recovery, original_filename, mut interactive) =
2105+
match emission {
2106+
WarningEmission::Standard {
2107+
emitted_escapes,
2108+
tokenizer_recovery,
2109+
} => (emitted_escapes, tokenizer_recovery, None, None),
2110+
WarningEmission::Ast {
2111+
tokenizer_recovery,
2112+
original_filename,
2113+
} => (&[][..], tokenizer_recovery, original_filename, None),
2114+
WarningEmission::Interactive(emitted) => (&[][..], false, None, Some(emitted)),
2115+
};
20852116
let mut considered_escapes = Vec::new();
20862117
let mut parser_error = None;
20872118
for warning in warnings {
@@ -2128,7 +2159,12 @@ mod escape_warnings {
21282159
considered_escapes.push(offset);
21292160
if emitted_escapes.binary_search(&offset).is_err() {
21302161
let result = warn_invalid_escape_sequence(
2131-
source, escape, filename, module, self,
2162+
source,
2163+
escape,
2164+
filename,
2165+
original_filename,
2166+
module,
2167+
self,
21322168
)
21332169
.map_err(|mut error| {
21342170
if interactive.is_some()
@@ -2163,6 +2199,7 @@ mod escape_warnings {
21632199
warn_syntax_at_offset(
21642200
source,
21652201
filename,
2202+
original_filename,
21662203
module,
21672204
last_offset,
21682205
message.clone(),

0 commit comments

Comments
 (0)