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

+75
-73
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

derive-impl/src/from_args.rs

+2-2
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

+1
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

+7-7
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

+3-3
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

+4-4
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

+4-4
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

+1-1
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

+3-3
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

+4-4
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

stdlib/src/sqlite.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -297,21 +297,21 @@ mod _sqlite {
297297
struct ConnectArgs {
298298
#[pyarg(any)]
299299
database: FsPath,
300-
#[pyarg(any, default = "5.0")]
300+
#[pyarg(any, default = 5.0)]
301301
timeout: f64,
302-
#[pyarg(any, default = "0")]
302+
#[pyarg(any, default = 0)]
303303
detect_types: c_int,
304-
#[pyarg(any, default = "Some(vm.ctx.empty_str.to_owned())")]
304+
#[pyarg(any, default = Some(vm.ctx.empty_str.to_owned()))]
305305
isolation_level: Option<PyStrRef>,
306-
#[pyarg(any, default = "true")]
306+
#[pyarg(any, default = true)]
307307
check_same_thread: bool,
308-
#[pyarg(any, default = "Connection::class(&vm.ctx).to_owned()")]
308+
#[pyarg(any, default = Connection::class(&vm.ctx).to_owned())]
309309
factory: PyTypeRef,
310310
// TODO: cache statements
311311
#[allow(dead_code)]
312-
#[pyarg(any, default = "0")]
312+
#[pyarg(any, default = 0)]
313313
cached_statements: c_int,
314-
#[pyarg(any, default = "false")]
314+
#[pyarg(any, default = false)]
315315
uri: bool,
316316
}
317317

@@ -326,13 +326,13 @@ mod _sqlite {
326326
struct BackupArgs {
327327
#[pyarg(any)]
328328
target: PyRef<Connection>,
329-
#[pyarg(named, default = "-1")]
329+
#[pyarg(named, default = -1)]
330330
pages: c_int,
331331
#[pyarg(named, optional)]
332332
progress: Option<ArgCallable>,
333333
#[pyarg(named, optional)]
334334
name: Option<PyStrRef>,
335-
#[pyarg(named, default = "0.250")]
335+
#[pyarg(named, default = 0.250)]
336336
sleep: f64,
337337
}
338338

@@ -375,7 +375,7 @@ mod _sqlite {
375375
row: i64,
376376
#[pyarg(named, default)]
377377
readonly: bool,
378-
#[pyarg(named, default = "vm.ctx.new_str(stringify!(main))")]
378+
#[pyarg(named, default = vm.ctx.new_str("main"))]
379379
name: PyStrRef,
380380
}
381381

stdlib/src/ssl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ mod _ssl {
351351
#[derive(FromArgs)]
352352
struct Txt2ObjArgs {
353353
txt: PyStrRef,
354-
#[pyarg(any, default = "false")]
354+
#[pyarg(any, default = false)]
355355
name: bool,
356356
}
357357

stdlib/src/zlib.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ mod zlib {
7474
struct PyFuncCompressArgs {
7575
#[pyarg(positional)]
7676
data: ArgBytesLike,
77-
#[pyarg(any, default = "Level::new(Z_DEFAULT_COMPRESSION)")]
77+
#[pyarg(any, default = Level::new(Z_DEFAULT_COMPRESSION))]
7878
level: Level,
79-
#[pyarg(any, default = "ArgPrimitiveIndex { value: MAX_WBITS }")]
79+
#[pyarg(any, default = ArgPrimitiveIndex { value: MAX_WBITS })]
8080
wbits: ArgPrimitiveIndex<i8>,
8181
}
8282

@@ -269,9 +269,9 @@ mod zlib {
269269
struct PyFuncDecompressArgs {
270270
#[pyarg(positional)]
271271
data: ArgBytesLike,
272-
#[pyarg(any, default = "ArgPrimitiveIndex { value: MAX_WBITS }")]
272+
#[pyarg(any, default = ArgPrimitiveIndex { value: MAX_WBITS })]
273273
wbits: ArgPrimitiveIndex<i8>,
274-
#[pyarg(any, default = "ArgPrimitiveIndex { value: DEF_BUF_SIZE }")]
274+
#[pyarg(any, default = ArgPrimitiveIndex { value: DEF_BUF_SIZE })]
275275
bufsize: ArgPrimitiveIndex<usize>,
276276
}
277277

@@ -299,7 +299,7 @@ mod zlib {
299299

300300
#[derive(FromArgs)]
301301
struct DecompressobjArgs {
302-
#[pyarg(any, default = "ArgPrimitiveIndex { value: MAX_WBITS }")]
302+
#[pyarg(any, default = ArgPrimitiveIndex { value: MAX_WBITS })]
303303
wbits: ArgPrimitiveIndex<i8>,
304304
#[pyarg(any, optional)]
305305
zdict: OptionalArg<ArgBytesLike>,
@@ -450,16 +450,16 @@ mod zlib {
450450
#[derive(FromArgs)]
451451
#[allow(dead_code)] // FIXME: use args
452452
struct CompressobjArgs {
453-
#[pyarg(any, default = "Level::new(Z_DEFAULT_COMPRESSION)")]
453+
#[pyarg(any, default = Level::new(Z_DEFAULT_COMPRESSION))]
454454
level: Level,
455455
// only DEFLATED is valid right now, it's w/e
456-
#[pyarg(any, default = "DEFLATED")]
456+
#[pyarg(any, default = DEFLATED)]
457457
method: i32,
458-
#[pyarg(any, default = "ArgPrimitiveIndex { value: MAX_WBITS }")]
458+
#[pyarg(any, default = ArgPrimitiveIndex { value: MAX_WBITS })]
459459
wbits: ArgPrimitiveIndex<i8>,
460-
#[pyarg(any, name = "memLevel", default = "DEF_MEM_LEVEL")]
460+
#[pyarg(any, name = "memLevel", default = DEF_MEM_LEVEL)]
461461
mem_level: u8,
462-
#[pyarg(any, default = "Z_DEFAULT_STRATEGY")]
462+
#[pyarg(any, default = Z_DEFAULT_STRATEGY)]
463463
strategy: i32,
464464
#[pyarg(any, optional)]
465465
zdict: Option<ArgBytesLike>,

vm/src/anystr.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ use num_traits::{cast::ToPrimitive, sign::Signed};
1010
pub struct SplitArgs<T: TryFromObject> {
1111
#[pyarg(any, default)]
1212
sep: Option<T>,
13-
#[pyarg(any, default = "-1")]
13+
#[pyarg(any, default = -1)]
1414
maxsplit: isize,
1515
}
1616

1717
#[derive(FromArgs)]
1818
pub struct SplitLinesArgs {
19-
#[pyarg(any, default = "false")]
19+
#[pyarg(any, default = false)]
2020
pub keepends: bool,
2121
}
2222

2323
#[derive(FromArgs)]
2424
pub struct ExpandTabsArgs {
25-
#[pyarg(any, default = "8")]
25+
#[pyarg(any, default = 8)]
2626
tabsize: isize,
2727
}
2828

vm/src/builtins/int.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -826,17 +826,17 @@ pub struct IntOptions {
826826
#[derive(FromArgs)]
827827
struct IntFromByteArgs {
828828
bytes: PyBytesInner,
829-
#[pyarg(any, default = "ArgByteOrder::Big")]
829+
#[pyarg(any, default = ArgByteOrder::Big)]
830830
byteorder: ArgByteOrder,
831831
#[pyarg(named, optional)]
832832
signed: OptionalArg<ArgIntoBool>,
833833
}
834834

835835
#[derive(FromArgs)]
836836
struct IntToByteArgs {
837-
#[pyarg(any, default = "1")]
837+
#[pyarg(any, default = 1)]
838838
length: usize,
839-
#[pyarg(any, default = "ArgByteOrder::Big")]
839+
#[pyarg(any, default = ArgByteOrder::Big)]
840840
byteorder: ArgByteOrder,
841841
#[pyarg(named, optional)]
842842
signed: OptionalArg<ArgIntoBool>,

vm/src/builtins/list.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ pub(crate) struct SortOptions {
9191
#[pyarg(named, default)]
9292
key: Option<PyObjectRef>,
9393
#[pytraverse(skip)]
94-
#[pyarg(named, default = "false")]
94+
#[pyarg(named, default = false)]
9595
reverse: bool,
9696
}
9797

vm/src/stdlib/builtins.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ mod builtins {
663663
sep: Option<PyStrRef>,
664664
#[pyarg(named, default)]
665665
end: Option<PyStrRef>,
666-
#[pyarg(named, default = "ArgIntoBool::FALSE")]
666+
#[pyarg(named, default = ArgIntoBool::FALSE)]
667667
flush: ArgIntoBool,
668668
#[pyarg(named, default)]
669669
file: Option<PyObjectRef>,

vm/src/stdlib/codecs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ mod _codecs {
109109
data: ArgBytesLike,
110110
#[pyarg(positional, optional)]
111111
errors: Option<PyStrRef>,
112-
#[pyarg(positional, default = "false")]
112+
#[pyarg(positional, default = false)]
113113
final_decode: bool,
114114
}
115115

0 commit comments

Comments
 (0)