Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Lib/test/test_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1171,7 +1171,6 @@ def four_freevars():
three_freevars.__globals__,
closure=my_closure)

@unittest.expectedFailure # TODO: RUSTPYTHON; SyntaxError: invalid decimal literal
def test_exec_filter_syntax_warnings_by_module(self):
filename = support.findfile('test_import/data/syntax_warnings.py')
with open(filename, 'rb') as f:
Expand Down
14 changes: 12 additions & 2 deletions crates/vm/src/stdlib/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -682,8 +682,18 @@ mod builtins {
if let Some(code) = crate::frame::current_code() {
opts.future_features = code.flags & bytecode::CodeFlags::FUTURE_MASK;
}
vm.compile_with_opts(source, mode, "<string>", opts)
.map_err(|err| err.into_pyexception(vm, Some(source)))?
let module = scope
.globals
.inner_getitem_opt(identifier!(vm, __name__), vm)?;
vm.compile_with_opts_and_module_object(
source,
mode,
"<string>",
opts,
module.as_deref(),
&[],
)
.map_err(|err| err.into_pyexception(vm, Some(source)))?
}
#[cfg(not(feature = "rustpython-compiler"))]
Either::B(_) => return Err(vm.new_type_error(CODEGEN_NOT_SUPPORTED)),
Expand Down
66 changes: 52 additions & 14 deletions crates/vm/src/vm/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use core::fmt;

use crate::{
AsObject, Py, PyObjectRef, PyRef, PyResult, VirtualMachine,
AsObject, Py, PyObject, PyObjectRef, PyRef, PyResult, VirtualMachine,
builtins::{PyBaseExceptionRef, PyCode, PyStr},
compiler::{self, CompileError, CompileOpts},
vm::compile_mode::{CompileStart, CompilerFlags, compile_future_features_from_flags},
Expand Down Expand Up @@ -465,7 +465,7 @@ impl VirtualMachine {
source,
escape_warnings::InvalidEscape::Char { ch, offset },
filename,
module,
module.map(AsObject::as_object),
self,
)
.map_err(VmCompileError::Warning)
Expand Down Expand Up @@ -618,6 +618,27 @@ impl VirtualMachine {
opts: CompileOpts,
module: Option<&Py<PyStr>>,
emitted_escapes: &[usize],
) -> Result<PyRef<PyCode>, VmCompileError> {
self.compile_with_opts_and_module_object(
source,
mode,
source_path,
opts,
module.map(AsObject::as_object),
emitted_escapes,
)
}

// eval()/exec() forward globals["__name__"] unchanged. In particular,
// an absent name and an explicit None have different warning semantics.
pub(crate) fn compile_with_opts_and_module_object(
&self,
source: &str,
mode: compiler::Mode,
source_path: impl Into<String>,
opts: CompileOpts,
module: Option<&PyObject>,
emitted_escapes: &[usize],
) -> Result<PyRef<PyCode>, VmCompileError> {
let source_path = source_path.into();
#[cfg(not(feature = "parser"))]
Expand All @@ -629,7 +650,7 @@ impl VirtualMachine {
self.emit_string_escape_warnings(source, &source_path, module, emitted_escapes)
.map_err(VmCompileError::Warning)?;
}
self.compile_after_parser_warnings(source, mode, source_path, opts, module)
self.compile_after_parser_warnings_object(source, mode, source_path, opts, module)
}

pub(super) fn compile_after_parser_warnings(
Expand All @@ -639,6 +660,23 @@ impl VirtualMachine {
source_path: impl Into<String>,
opts: CompileOpts,
module: Option<&Py<PyStr>>,
) -> Result<PyRef<PyCode>, VmCompileError> {
self.compile_after_parser_warnings_object(
source,
mode,
source_path,
opts,
module.map(AsObject::as_object),
)
}

fn compile_after_parser_warnings_object(
&self,
source: &str,
mode: compiler::Mode,
source_path: impl Into<String>,
opts: CompileOpts,
module: Option<&PyObject>,
) -> Result<PyRef<PyCode>, VmCompileError> {
let source_path = source_path.into();
#[cfg(not(feature = "parser"))]
Expand Down Expand Up @@ -946,7 +984,7 @@ mod escape_warnings {
source: &str,
escape: InvalidEscape,
filename: &str,
module: Option<&Py<PyStr>>,
module: Option<&PyObject>,
vm: &VirtualMachine,
) -> Result<(), CompileWarningError> {
let (lineno, column) = line_offset_at(source, escape.offset());
Expand All @@ -958,7 +996,7 @@ mod escape_warnings {
vm.ctx.new_str(warning).into(),
fname,
lineno,
module.map(|module| module.to_owned().into()),
module.map(ToOwned::to_owned),
vm.ctx.none(),
None,
None,
Expand All @@ -979,7 +1017,7 @@ mod escape_warnings {
fn warn_syntax_at_offset(
source: &str,
filename: &str,
module: Option<&Py<PyStr>>,
module: Option<&PyObject>,
offset: usize,
message: String,
vm: &VirtualMachine,
Expand All @@ -992,7 +1030,7 @@ mod escape_warnings {
message.into(),
fname,
lineno,
module.map(|module| module.to_owned().into()),
module.map(ToOwned::to_owned),
vm.ctx.none(),
None,
None,
Expand All @@ -1003,7 +1041,7 @@ mod escape_warnings {

pub(super) fn warn_syntax_at_location(
filename: &str,
module: Option<&Py<PyStr>>,
module: Option<&PyObject>,
location: compiler::core::SourceLocation,
message: String,
vm: &VirtualMachine,
Expand All @@ -1015,7 +1053,7 @@ mod escape_warnings {
message.into(),
fname,
location.line.get(),
module.map(|module| module.to_owned().into()),
module.map(ToOwned::to_owned),
vm.ctx.none(),
None,
None,
Expand Down Expand Up @@ -1797,7 +1835,7 @@ mod escape_warnings {
&self,
source: &str,
filename: &str,
module: Option<&Py<PyStr>>,
module: Option<&PyObject>,
) -> Result<(), CompileWarningError> {
let mut warnings = Vec::new();
collect_numeric_literal_warnings(source, &mut warnings);
Expand All @@ -1817,7 +1855,7 @@ mod escape_warnings {
&self,
source: &str,
filename: &str,
module: Option<&Py<PyStr>>,
module: Option<&PyObject>,
emitted_escapes: &[usize],
) -> Result<(), CompileWarningError> {
let source_file = compiler::core::SourceFileBuilder::new(filename, source).finish();
Expand Down Expand Up @@ -1880,7 +1918,7 @@ mod escape_warnings {
self.emit_parser_warning_batch(
source,
filename,
module,
module.map(AsObject::as_object),
warnings,
WarningEmission::Ast {
tokenizer_recovery: recovery
Expand All @@ -1907,7 +1945,7 @@ mod escape_warnings {
self.emit_parser_warning_batch(
source,
filename,
module,
module.map(AsObject::as_object),
warnings,
WarningEmission::Interactive(emitted),
)
Expand All @@ -1918,7 +1956,7 @@ mod escape_warnings {
&self,
source: &str,
filename: &str,
module: Option<&Py<PyStr>>,
module: Option<&PyObject>,
warnings: Vec<ParserWarning>,
emission: WarningEmission<'_>,
) -> Result<Vec<usize>, CompileWarningError> {
Expand Down
Loading