Visitar URL original
Fix SyntaxError, UnicodeDecodeError by youknowone · Pull Request #5807 · RustPython/RustPython · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Fix SyntaxError
  • Loading branch information
youknowone committed Jun 20, 2025
commit 76a4e71d8103a7bf8d06b6e1d6861007b1b4f364
6 changes: 0 additions & 6 deletions Lib/test/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,8 +425,6 @@ def test_windows_message(self):
with self.assertRaisesRegex(OSError, 'Windows Error 0x%x' % code):
ctypes.pythonapi.PyErr_SetFromWindowsErr(code)

# TODO: RUSTPYTHON
@unittest.expectedFailure
def testAttributes(self):
# test that exception attributes are happy

Expand Down Expand Up @@ -2567,8 +2565,6 @@ def test_non_utf8(self):
finally:
unlink(TESTFN)

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_attributes_new_constructor(self):
args = ("bad.py", 1, 2, "abcdefg", 1, 100)
the_exception = SyntaxError("bad bad", args)
Expand All @@ -2581,8 +2577,6 @@ def test_attributes_new_constructor(self):
self.assertEqual(error, the_exception.text)
self.assertEqual("bad bad", the_exception.msg)

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_attributes_old_constructor(self):
args = ("bad.py", 1, 2, "abcdefg")
the_exception = SyntaxError("bad bad", args)
Expand Down
6 changes: 0 additions & 6 deletions Lib/test/test_traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,6 @@ def test_caret(self):
self.assertEqual(err[1].find("("), err[2].find("^")) # in the right place
self.assertEqual(err[2].count("^"), 1)

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_nocaret(self):
exc = SyntaxError("error", ("x.py", 23, None, "bad syntax"))
err = traceback.format_exception_only(SyntaxError, exc)
Expand Down Expand Up @@ -748,8 +746,6 @@ def outer_raise():
self.assertIn('inner_raise() # Marker', blocks[2])
self.check_zero_div(blocks[2])

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_syntax_error_offset_at_eol(self):
# See #10186.
def e():
Expand Down Expand Up @@ -808,8 +804,6 @@ def __str__(self):
exp = f'<unknown>.{X.__qualname__}: I am X\n'
self.assertEqual(exp, err)

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_syntax_error_various_offsets(self):
for offset in range(-5, 10):
for add in [0, 2]:
Expand Down
38 changes: 38 additions & 0 deletions vm/src/exceptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1602,6 +1602,44 @@ pub(super) mod types {

#[pyexception]
impl PySyntaxError {
#[pyslot]
#[pymethod(name = "__init__")]
fn slot_init(zelf: PyObjectRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult<()> {
let len = args.args.len();
let new_args = args;

zelf.set_attr("print_file_and_line", vm.ctx.none(), vm)?;

if len == 2 {
if let Ok(location_tuple) = new_args.args[1]
.clone()
.downcast::<crate::builtins::PyTuple>()
{
#[allow(clippy::len_zero)]
if location_tuple.len() >= 1 {
zelf.set_attr("filename", location_tuple.fast_getitem(0).clone(), vm)?;
}
if location_tuple.len() >= 2 {
zelf.set_attr("lineno", location_tuple.fast_getitem(1).clone(), vm)?;
}
if location_tuple.len() >= 3 {
zelf.set_attr("offset", location_tuple.fast_getitem(2).clone(), vm)?;
}
if location_tuple.len() >= 4 {
zelf.set_attr("text", location_tuple.fast_getitem(3).clone(), vm)?;
}
if location_tuple.len() >= 5 {
zelf.set_attr("end_lineno", location_tuple.fast_getitem(4).clone(), vm)?;
}
if location_tuple.len() >= 6 {
zelf.set_attr("end_offset", location_tuple.fast_getitem(5).clone(), vm)?;
}
}
}

PyBaseException::slot_init(zelf, new_args, vm)
}

#[pymethod(magic)]
fn str(exc: PyBaseExceptionRef, vm: &VirtualMachine) -> PyStrRef {
fn basename(filename: &str) -> &str {
Expand Down