Skip to content

Commit 0b35946

Browse files
coolreader18youknowone
authored andcommitted
Make FromArgs default field take an expression, not a string literal
1 parent 24d9956 commit 0b35946

25 files changed

Lines changed: 75 additions & 73 deletions

‎Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎derive-impl/src/from_args.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ impl ArgAttribute {
7676
return Err(meta.error("Default already set"));
7777
}
7878
let val = meta.value()?;
79-
let val = val.parse::<syn::LitStr>()?;
8079
self.default = Some(Some(val.parse()?))
8180
} else if meta.path.is_ident("default") || meta.path.is_ident("optional") {
8281
if self.default.is_none() {
@@ -138,9 +137,10 @@ fn generate_field((i, field): (usize, &Field)) -> Result<TokenStream> {
138137
.map(|x| ::rustpython_vm::convert::TryFromObject::try_from_object(vm, x)).transpose()?
139138
};
140139
let ending = if let Some(default) = attr.default {
140+
let ty = &field.ty;
141141
let default = default.unwrap_or_else(|| parse_quote!(::std::default::Default::default()));
142142
quote! {
143-
.map(::rustpython_vm::function::FromArgOptional::from_inner)
143+
.map(<#ty as ::rustpython_vm::function::FromArgOptional>::from_inner)
144144
.unwrap_or_else(|| #default)
145145
}
146146
} else {

‎derive/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ proc-macro = true
1414
[dependencies]
1515
rustpython-compiler = { workspace = true }
1616
rustpython-derive-impl = { workspace = true }
17+
proc-macro2 = { workspace = true }
1718
syn = { workspace = true }
1819

1920
[lints]

‎stdlib/src/binascii.rs‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,15 +143,15 @@ mod decl {
143143

144144
#[derive(FromArgs)]
145145
struct NewlineArg {
146-
#[pyarg(named, default = "true")]
146+
#[pyarg(named, default = true)]
147147
newline: bool,
148148
}
149149

150150
#[derive(FromArgs)]
151151
struct A2bBase64Args {
152152
#[pyarg(any)]
153153
s: ArgAsciiBuffer,
154-
#[pyarg(named, default = "false")]
154+
#[pyarg(named, default = false)]
155155
strict_mode: bool,
156156
}
157157

@@ -298,7 +298,7 @@ mod decl {
298298
struct A2bQpArgs {
299299
#[pyarg(any)]
300300
data: ArgAsciiBuffer,
301-
#[pyarg(named, default = "false")]
301+
#[pyarg(named, default = false)]
302302
header: bool,
303303
}
304304
#[pyfunction]
@@ -366,11 +366,11 @@ mod decl {
366366
struct B2aQpArgs {
367367
#[pyarg(any)]
368368
data: ArgAsciiBuffer,
369-
#[pyarg(named, default = "false")]
369+
#[pyarg(named, default = false)]
370370
quotetabs: bool,
371-
#[pyarg(named, default = "true")]
371+
#[pyarg(named, default = true)]
372372
istext: bool,
373-
#[pyarg(named, default = "false")]
373+
#[pyarg(named, default = false)]
374374
header: bool,
375375
}
376376

@@ -689,7 +689,7 @@ mod decl {
689689

690690
#[derive(FromArgs)]
691691
struct BacktickArg {
692-
#[pyarg(named, default = "false")]
692+
#[pyarg(named, default = false)]
693693
backtick: bool,
694694
}
695695

‎stdlib/src/faulthandler.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ mod decl {
3434
struct EnableArgs {
3535
#[pyarg(any, default)]
3636
file: Option<i64>,
37-
#[pyarg(any, default = "true")]
37+
#[pyarg(any, default = true)]
3838
all_threads: bool,
3939
}
4040

@@ -50,9 +50,9 @@ mod decl {
5050
signum: i64,
5151
#[pyarg(any, default)]
5252
file: Option<i64>,
53-
#[pyarg(any, default = "true")]
53+
#[pyarg(any, default = true)]
5454
all_threads: bool,
55-
#[pyarg(any, default = "false")]
55+
#[pyarg(any, default = false)]
5656
chain: bool,
5757
}
5858

‎stdlib/src/hashlib.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub mod _hashlib {
2828
name: PyStrRef,
2929
#[pyarg(any, optional)]
3030
data: OptionalArg<ArgBytesLike>,
31-
#[pyarg(named, default = "true")]
31+
#[pyarg(named, default = true)]
3232
usedforsecurity: bool,
3333
}
3434

@@ -37,7 +37,7 @@ pub mod _hashlib {
3737
pub struct BlakeHashArgs {
3838
#[pyarg(positional, optional)]
3939
pub data: OptionalArg<ArgBytesLike>,
40-
#[pyarg(named, default = "true")]
40+
#[pyarg(named, default = true)]
4141
usedforsecurity: bool,
4242
}
4343

@@ -55,7 +55,7 @@ pub mod _hashlib {
5555
pub struct HashArgs {
5656
#[pyarg(any, optional)]
5757
pub string: OptionalArg<ArgBytesLike>,
58-
#[pyarg(named, default = "true")]
58+
#[pyarg(named, default = true)]
5959
usedforsecurity: bool,
6060
}
6161

@@ -331,7 +331,7 @@ pub mod _hashlib {
331331
name: PyBuffer,
332332
#[pyarg(any, optional)]
333333
data: OptionalArg<ArgBytesLike>,
334-
#[pyarg(named, default = "true")]
334+
#[pyarg(named, default = true)]
335335
digestmod: bool, // TODO: RUSTPYTHON support functions & name functions
336336
}
337337

‎stdlib/src/mmap.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,13 +191,13 @@ mod mmap {
191191
fileno: RawFd,
192192
#[pyarg(any)]
193193
length: isize,
194-
#[pyarg(any, default = "MAP_SHARED")]
194+
#[pyarg(any, default = MAP_SHARED)]
195195
flags: libc::c_int,
196-
#[pyarg(any, default = "PROT_WRITE|PROT_READ")]
196+
#[pyarg(any, default = PROT_WRITE|PROT_READ)]
197197
prot: libc::c_int,
198-
#[pyarg(any, default = "AccessMode::Default")]
198+
#[pyarg(any, default = AccessMode::Default)]
199199
access: AccessMode,
200-
#[pyarg(any, default = "0")]
200+
#[pyarg(any, default = 0)]
201201
offset: libc::off_t,
202202
}
203203

‎stdlib/src/pystruct.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ pub(crate) mod _struct {
134134
#[derive(FromArgs)]
135135
struct UpdateFromArgs {
136136
buffer: ArgBytesLike,
137-
#[pyarg(any, default = "0")]
137+
#[pyarg(any, default = 0)]
138138
offset: isize,
139139
}
140140

‎stdlib/src/select.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -529,9 +529,9 @@ mod decl {
529529

530530
#[derive(FromArgs)]
531531
pub struct EpollNewArgs {
532-
#[pyarg(any, default = "-1")]
532+
#[pyarg(any, default = -1)]
533533
sizehint: i32,
534-
#[pyarg(any, default = "0")]
534+
#[pyarg(any, default = 0)]
535535
flags: i32,
536536
}
537537

@@ -555,7 +555,7 @@ mod decl {
555555
struct EpollPollArgs {
556556
#[pyarg(any, default)]
557557
timeout: poll::TimeoutArg<false>,
558-
#[pyarg(any, default = "-1")]
558+
#[pyarg(any, default = -1)]
559559
maxevents: i32,
560560
}
561561

‎stdlib/src/socket.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1908,13 +1908,13 @@ mod _socket {
19081908
#[pyarg(positional)]
19091909
port: Option<Either<PyStrRef, i32>>,
19101910

1911-
#[pyarg(positional, default = "c::AF_UNSPEC")]
1911+
#[pyarg(positional, default = c::AF_UNSPEC)]
19121912
family: i32,
1913-
#[pyarg(positional, default = "0")]
1913+
#[pyarg(positional, default = 0)]
19141914
ty: i32,
1915-
#[pyarg(positional, default = "0")]
1915+
#[pyarg(positional, default = 0)]
19161916
proto: i32,
1917-
#[pyarg(positional, default = "0")]
1917+
#[pyarg(positional, default = 0)]
19181918
flags: i32,
19191919
}
19201920

0 commit comments

Comments
 (0)