8000 Merge remote-tracking branch 'upstream/main' into cleanup-itertools · RustPython/RustPython@27c64de · GitHub
[go: up one dir, main page]

Skip to content

Commit 27c64de

Browse files
committed
Merge remote-tracking branch 'upstream/main' into cleanup-itertools
2 parents 1a8a1ab + 43be4e4 commit 27c64de

File tree

17 files changed

+469
-245
lines changed

17 files changed

+469
-245
lines changed

derive-impl/src/pyclass.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ impl std::fmt::Display for AttrName {
4242

4343
impl FromStr for AttrName {
4444
type Err = String;
45+
4546
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
4647
Ok(match s {
4748
"pymethod" => Self::Method,
@@ -1216,6 +1217,7 @@ impl ItemMeta for MethodItemMeta {
12161217
fn from_inner(inner: ItemMetaInner) -> Self {
12171218
Self(inner)
12181219
}
1220+
12191221
fn inner(&self) -> &ItemMetaInner {
12201222
&self.0
12211223
}
@@ -1225,6 +1227,7 @@ impl MethodItemMeta {
12251227
fn raw(&self) -> Result<bool> {
12261228
self.inner()._bool("raw")
12271229
}
1230+
12281231
fn method_name(&self) -> Result<String> {
12291232
let inner = self.inner();
12301233
let name = inner._optional_str("name")?;
@@ -1244,6 +1247,7 @@ impl ItemMeta for GetSetItemMeta {
12441247
fn from_inner(inner: ItemMetaInner) -> Self {
12451248
Self(inner)
12461249
}
1250+
12471251
fn inner(&self) -> &ItemMetaInner {
12481252
&self.0
12491253
}
@@ -1274,8 +1278,8 @@ impl GetSetItemMeta {
12741278
if name.is_empty() {
12751279
Err(err_span!(
12761280
inner.meta_ident,
1277-
"A #[{}({typ})] fn with a {prefix}* name must \
1278-
have something after \"{prefix}\"",
1281+
r#"A #[{}({typ})] fn with a {prefix}* name must \
1282+
have something after "{prefix}""#,
12791283
inner.meta_name(),
12801284
typ = item_typ,
12811285
prefix = prefix
@@ -1286,8 +1290,8 @@ impl GetSetItemMeta {
12861290
} else {
12871291
Err(err_span!(
12881292
inner.meta_ident,
1289-
"A #[{}(setter)] fn must either have a `name` \
1290-
parameter or a fn name along the lines of \"set_*\"",
1293+
r#"A #[{}(setter)] fn must either have a `name` \
1294+
parameter or a fn name along the lines of "set_*""#,
12911295
inner.meta_name()
12921296
))
12931297
}
@@ -1337,6 +1341,7 @@ impl ItemMeta for SlotItemMeta {
13371341
fn from_inner(inner: ItemMetaInner) -> Self {
13381342
Self(inner)
13391343
}
1344+
13401345
fn inner(&self) -> &ItemMetaInner {
13411346
&self.0
13421347
}
@@ -1389,6 +1394,7 @@ impl ItemMeta for MemberItemMeta {
13891394
fn from_inner(inner: ItemMetaInner) -> Self {
13901395
Self(inner)
13911396
}
1397+
13921398
fn inner(&self) -> &ItemMetaInner {
13931399
&self.0
13941400
}
@@ -1403,8 +1409,8 @@ impl MemberItemMeta {
14031409
if name.is_empty() {
14041410
Err(err_span!(
14051411
inner.meta_ident,
1406-
"A #[{}({typ})] fn with a {prefix}* name must \
1407-
have something after \"{prefix}\"",
1412+
r#"A #[{}({typ})] fn with a {prefix}* name must \
1413+
have something after "{prefix}""#,
14081414
inner.meta_name(),
14091415
typ = item_typ,
14101416
prefix = prefix
@@ -1415,8 +1421,8 @@ impl MemberItemMeta {
14151421
} else {
14161422
Err(err_span!(
14171423
inner.meta_ident,
1418-
"A #[{}(setter)] fn must either have a `name` \
1419-
parameter or a fn name along the lines of \"set_*\"",
1424+
r#"A #[{}(setter)] fn must either have a `name` \
1425+
parameter or a fn name along the lines of "set_*""#,
14201426
inner.meta_name()
14211427
))
14221428
}

vm/src/readline.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ mod basic_readline {
2929

3030
impl<H: Helper> Readline<H> {
3131
pub fn new(helper: H) -> Self {
32-
Readline { helper }
32+
Self { helper }
3333
}
3434

3535
pub fn load_history(&mut self, _path: &Path) -> OtherResult<()> {
@@ -86,7 +86,7 @@ mod rustyline_readline {
8686
)
8787
.expect("failed to initialize line editor");
8888
repl.set_helper(Some(helper));
89-
Readline { repl }
89+
Self { repl }
9090
}
9191

9292
pub fn load_history(&mut self, path: &Path) -> OtherResult<()> {
@@ -136,7 +136,7 @@ pub struct Readline<H: Helper>(readline_inner::Readline<H>);
136136

137137
impl<H: Helper> Readline<H> {
138138
pub fn new(helper: H) -> Self {
139-
Readline(readline_inner::Readline::new(helper))
139+
Self(readline_inner::Readline::new(helper))
140140
}
141141

142142
pub fn load_history(&mut self, path: &Path) -> OtherResult<()> {

vm/src/stdlib/ast.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ mod type_parameters;
5555

5656
fn get_node_field(vm: &VirtualMachine, obj: &PyObject, field: &'static str, typ: &str) -> PyResult {
5757
vm.get_attribute_opt(obj.to_owned(), field)?
58-
.ok_or_else(|| vm.new_type_error(format!("required field \"{field}\" missing from {typ}")))
58+
.ok_or_else(|| vm.new_type_error(format!(r#"required field "{field}" missing from {typ}"#)))
5959
}
6060

6161
fn get_node_field_opt(
@@ -76,7 +76,7 @@ fn get_int_field(
7676
) -> PyResult<PyRefExact<PyInt>> {
7777
get_node_field(vm, obj, field, typ)?
7878
.downcast_exact(vm)
79-
.map_err(|_| vm.new_type_error(format!("field \"{field}\" must have integer type")))
79+
.map_err(|_| vm.new_type_error(format!(r#"field "{field}" must have integer type"#)))
8080
}
8181

8282
struct PySourceRange {
@@ -107,7 +107,7 @@ impl Row {
107107
self.0.get()
108108
}
109109

110-
fn get_one_indexed(self) -> OneIndexed {
110+
const fn get_one_indexed(self) -> OneIndexed {
111111
self.0
112112
}
113113
}

vm/src/stdlib/ast/constant.rs

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,48 +21,49 @@ impl Constant {
2121
}
2222
}
2323

24-
pub(super) fn new_int(value: ruff::Int, range: TextRange) -> Self {
24+
pub(super) const fn new_int(value: ruff::Int, range: TextRange) -> Self {
2525
Self {
2626
range,
2727
value: ConstantLiteral::Int(value),
2828
}
2929
}
3030

31-
pub(super) fn new_float(value: f64, range: TextRange) -> Self {
31+
pub(super) const fn new_float(value: f64, range: TextRange) -> Self {
3232
Self {
3333
range,
3434
value: ConstantLiteral::Float(value),
3535
}
3636
}
37-
pub(super) fn new_complex(real: f64, imag: f64, range: TextRange) -> Self {
37+
38+
pub(super) const fn new_complex(real: f64, imag: f64, range: TextRange) -> Self {
3839
Self {
3940
range,
4041
value: ConstantLiteral::Complex { real, imag },
4142
}
4243
}
4344

44-
pub(super) fn new_bytes(value: Box<[u8]>, range: TextRange) -> Self {
45+
pub(super) const fn new_bytes(value: Box<[u8]>, range: TextRange) -> Self {
4546
Self {
4647
range,
4748
value: ConstantLiteral::Bytes(value),
4849
}
4950
}
5051

51-
pub(super) fn new_bool(value: bool, range: TextRange) -> Self {
52+
pub(super) const fn new_bool(value: bool, range: TextRange) -> Self {
5253
Self {
5354
range,
5455
value: ConstantLiteral::Bool(value),
5556
}
5657
}
5758

58-
pub(super) fn new_none(range: TextRange) -> Self {
59+
pub(super) const fn new_none(range: TextRange) -> Self {
5960
Self {
6061
range,
6162
value: ConstantLiteral::None,
6263
}
6364
}
6465

65-
pub(super) fn new_ellipsis(range: TextRange) -> Self {
66+
pub(super) const fn new_ellipsis(range: TextRange) -> Self {
6667
Self {
6768
range,
6869
value: ConstantLiteral::Ellipsis,
@@ -137,30 +138,30 @@ impl Node for Constant {
137138
impl Node for ConstantLiteral {
138139
fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef {
139140
match self {
140-
ConstantLiteral::None => vm.ctx.none(),
141-
ConstantLiteral::Bool(value) => vm.ctx.new_bool(value).to_pyobject(vm),
142-
ConstantLiteral::Str { value, .. } => vm.ctx.new_str(value).to_pyobject(vm),
143-
ConstantLiteral::Bytes(value) => vm.ctx.new_bytes(value.into()).to_pyobject(vm),
144-
ConstantLiteral::Int(value) => value.ast_to_object(vm, source_code),
145-
ConstantLiteral::Tuple(value) => {
141+
Self::None => vm.ctx.none(),
142+
Self::Bool(value) => vm.ctx.new_bool(value).to_pyobject(vm),
143+
Self::Str { value, .. } => vm.ctx.new_str(value).to_pyobject(vm),
144+
Self::Bytes(value) => vm.ctx.new_bytes(value.into()).to_pyobject(vm),
145+
Self::Int(value) => value.ast_to_object(vm, source_code),
146+
Self::Tuple(value) => {
146147
let value = value
147148
.into_iter()
148149
.map(|c| c.ast_to_object(vm, source_code))
149150
.collect();
150151
vm.ctx.new_tuple(value).to_pyobject(vm)
151152
}
152-
ConstantLiteral::FrozenSet(value) => PyFrozenSet::from_iter(
153+
Self::FrozenSet(value) => PyFrozenSet::from_iter(
153154
vm,
154155
value.into_iter().map(|c| c.ast_to_object(vm, source_code)),
155156
)
156157
.unwrap()
157158
.into_pyobject(vm),
158-
ConstantLiteral::Float(value) => vm.ctx.new_float(value).into_pyobject(vm),
159-
ConstantLiteral::Complex { real, imag } => vm
159+
Self::Float(value) => vm.ctx.new_float(value).into_pyobject(vm),
160+
Self::Complex { real, imag } => vm
160161
.ctx
161162
.new_complex(num_complex::Complex::new(real, imag))
162163
.into_pyobject(vm),
163-
ConstantLiteral::Ellipsis => vm.ctx.ellipsis(),
164+
Self::Ellipsis => vm.ctx.ellipsis(),
164165
}
165166
}
166167

@@ -171,24 +172,24 @@ impl Node for ConstantLiteral {
171172
) -> PyResult<Self> {
172173
let cls = value_object.class();
173174
let value = if cls.is(vm.ctx.types.none_type) {
174-
ConstantLiteral::None
175+
Self::None
175176
} else if cls.is(vm.ctx.types.bool_type) {
176-
ConstantLiteral::Bool(if value_object.is(&vm.ctx.true_value) {
177+
Self::Bool(if value_object.is(&vm.ctx.true_value) {
177178
true
178179
} else if value_object.is(&vm.ctx.false_value) {
179180
false
180181
} else {
181182
value_object.try_to_value(vm)?
182183
})
183184
} else if cls.is(vm.ctx.types.str_type) {
184-
ConstantLiteral::Str {
185+
Self::Str {
185186
value: value_object.try_to_value::<String>(vm)?.into(),
186187
prefix: StringLiteralPrefix::Empty,
187188
}
188189
} else if cls.is(vm.ctx.types.bytes_type) {
189-
ConstantLiteral::Bytes(value_object.try_to_value::<Vec<u8>>(vm)?.into())
190+
Self::Bytes(value_object.try_to_value::<Vec<u8>>(vm)?.into())
190191
} else if cls.is(vm.ctx.types.int_type) {
191-
ConstantLiteral::Int(Node::ast_from_object(vm, source_code, value_object)?)
192+
Self::Int(Node::ast_from_object(vm, source_code, value_object)?)
192193
} else if cls.is(vm.ctx.types.tuple_type) {
193194
let tuple = value_object.downcast::<PyTuple>().map_err(|obj| {
194195
vm.new_type_error(format!(
@@ -202,18 +203,18 @@ impl Node for ConstantLiteral {
202203
.cloned()
203204
.map(|object| Node::ast_from_object(vm, source_code, object))
204205
.collect::<PyResult<_>>()?;
205-
ConstantLiteral::Tuple(tuple)
206+
Self::Tuple(tuple)
206207
} else if cls.is(vm.ctx.types.frozenset_type) {
207208
let set = value_object.downcast::<PyFrozenSet>().unwrap();
208209
let elements = set
209210
.elements()
210211
.into_iter()
211212
.map(|object| Node::ast_from_object(vm, source_code, object))
212213
.collect::<PyResult<_>>()?;
213-
ConstantLiteral::FrozenSet(elements)
214+
Self::FrozenSet(elements)
214215
} else if cls.is(vm.ctx.types.float_type) {
215216
let float = value_object.try_into_value(vm)?;
216-
ConstantLiteral::Float(float)
217+
Self::Float(float)
217218
} else if cls.is(vm.ctx.types.complex_type) {
218219
let complex = value_object.try_complex(vm)?;
219220
let complex = match complex {
@@ -226,12 +227,12 @@ impl Node for ConstantLiteral {
226227
}
227228
Some((value, _was_coerced)) => value,
228229
};
229-
ConstantLiteral::Complex {
230+
Self::Complex {
230231
real: complex.re,
231232
imag: complex.im,
232233
}
233234
} else if cls.is(vm.ctx.types.ellipsis_type) {
234-
ConstantLiteral::Ellipsis
235+
Self::Ellipsis
235236
} else {
236237
return Err(vm.new_type_error(format!(
237238
"invalid type in Constant: {}",

vm/src/stdlib/ast/exception.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,13 @@ impl Node for ruff::ExceptHandlerExceptHandler {
5252
node_add_location(&dict, _range, _vm, source_code);
5353
node.into()
5454
}
55+
5556
fn ast_from_object(
5657
_vm: &VirtualMachine,
5758
source_code: &SourceCodeOwned,
5859
_object: PyObjectRef,
5960
) -> PyResult<Self> {
60-
Ok(ruff::ExceptHandlerExceptHandler {
61+
Ok(Self {
6162
type_: get_node_field_opt(_vm, &_object, "type")?
6263
.map(|obj| Node::ast_from_object(_vm, source_code, obj))
6364
.transpose()?,

0 commit comments

Comments
 (0)
0