Skip to content

Commit

Permalink
improve string ast
Browse files Browse the repository at this point in the history
  • Loading branch information
dvermd committed Oct 24, 2022
1 parent 247072c commit 94ee429
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 126 deletions.
6 changes: 0 additions & 6 deletions Lib/test/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,6 @@ def test_field_attr_writable(self):
x._fields = 666
self.assertEqual(x._fields, 666)

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_classattrs(self):
x = ast.Num()
self.assertEqual(x._fields, ('value', 'kind'))
Expand Down Expand Up @@ -548,8 +546,6 @@ def test_module(self):
x = ast.Module(body, [])
self.assertEqual(x.body, body)

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_nodeclasses(self):
# Zero arguments constructor explicitly allowed
x = ast.BinOp()
Expand Down Expand Up @@ -594,8 +590,6 @@ def test_nodeclasses(self):
x = ast.BinOp(1, 2, 3, foobarbaz=42)
self.assertEqual(x.foobarbaz, 42)

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_no_fields(self):
# this used to fail because Sub._fields was None
x = ast.Sub()
Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -1541,6 +1541,8 @@ def func2():
"""
self._check_error(code, "expected ':'")

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_invalid_line_continuation_error_position(self):
self._check_error(r"a = 3 \ 4",
"unexpected character after line continuation character",
Expand Down
2 changes: 0 additions & 2 deletions Lib/test/test_unicode.py
Original file line number Diff line number Diff line change
Expand Up @@ -1390,8 +1390,6 @@ def test_format_huge_item_number(self):
with self.assertRaises(ValueError):
result = format_string.format(2.34)

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_format_auto_numbering(self):
class C:
def __init__(self, x=100):
Expand Down
2 changes: 1 addition & 1 deletion compiler/ast/asdl_rs.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ def gen_classdef(self, name, fields, attrs, depth, base="AstNode"):
f"ctx.new_str(ascii!({json.dumps(f.name)})).into()" for f in fields
)
self.emit(
f"class.set_attr(identifier!(ctx, _fields), ctx.new_list(vec![{fields}]).into());",
f"class.set_attr(identifier!(ctx, _fields), ctx.new_tuple(vec![{fields}]).into());",
depth + 2,
)
attrs = ",".join(
Expand Down
2 changes: 1 addition & 1 deletion compiler/core/src/location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl Location {

pub fn reset(&mut self) {
self.row = 1;
self.column = 1;
self.column = 0;
}

pub fn go_right(&mut self) {
Expand Down
12 changes: 6 additions & 6 deletions compiler/parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ pub fn parse_program(source: &str, source_path: &str) -> Result<ast::Suite, Pars
/// assert_eq!(
/// expr,
/// ast::Expr {
/// location: ast::Location::new(1, 3),
/// end_location: Some(ast::Location::new(1, 6)),
/// location: ast::Location::new(1, 2),
/// end_location: Some(ast::Location::new(1, 5)),
/// custom: (),
/// node: ast::ExprKind::BinOp {
/// left: Box::new(ast::Expr {
/// location: ast::Location::new(1, 1),
/// end_location: Some(ast::Location::new(1, 2)),
/// location: ast::Location::new(1, 0),
/// end_location: Some(ast::Location::new(1, 1)),
/// custom: (),
/// node: ast::ExprKind::Constant {
/// value: ast::Constant::Int(1.into()),
Expand All @@ -50,8 +50,8 @@ pub fn parse_program(source: &str, source_path: &str) -> Result<ast::Suite, Pars
/// }),
/// op: ast::Operator::Add,
/// right: Box::new(ast::Expr {
/// location: ast::Location::new(1, 5),
/// end_location: Some(ast::Location::new(1, 6)),
/// location: ast::Location::new(1, 4),
/// end_location: Some(ast::Location::new(1, 5)),
/// custom: (),
/// node: ast::ExprKind::Constant {
/// value: ast::Constant::Int(2.into()),
Expand Down
9 changes: 5 additions & 4 deletions vm/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ impl FormatSpec {
}
};

self.format_sign_and_align(&magnitude_string, sign_str)
self.format_sign_and_align(&magnitude_string, sign_str, FormatAlign::Right)
}

#[inline]
Expand Down Expand Up @@ -510,12 +510,12 @@ impl FormatSpec {
},
};

self.format_sign_and_align(&magnitude_string, sign_str)
self.format_sign_and_align(&magnitude_string, sign_str, FormatAlign::Right)
}

pub(crate) fn format_string(&self, s: &str) -> Result<String, &'static str> {
match self.format_type {
Some(FormatType::String) | None => self.format_sign_and_align(s, ""),
Some(FormatType::String) | None => self.format_sign_and_align(s, "", FormatAlign::Left),
_ => Err("Unknown format code for object of type 'str'"),
}
}
Expand All @@ -524,8 +524,9 @@ impl FormatSpec {
&self,
magnitude_string: &str,
sign_str: &str,
default_align: FormatAlign,
) -> Result<String, &'static str> {
let align = self.align.unwrap_or(FormatAlign::Right);
let align = self.align.unwrap_or(default_align);

// Use the byte length as the string length since we're in ascii
let num_chars = magnitude_string.len();
Expand Down

0 comments on commit 94ee429

Please sign in to comment.