8000 Make FromArgs default field take an expression, not a string literal by coolreader18 · Pull Request #5642 · RustPython/RustPython · GitHub
[go: up one dir, main page]

Skip to content

Make FromArgs default field take an expression, not a string literal #5642

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions derive-impl/src/from_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ impl ArgAttribute {
return Err(meta.error("Default already set"));
}
let val = meta.value()?;
let val = val.parse::<syn::LitStr>()?;
self.default = Some(Some(val.parse()?))
} else if meta.path.is_ident("default") || meta.path.is_ident("optional") {
if self.default.is_none() {
Expand Down Expand Up @@ -138,9 +137,10 @@ fn generate_field((i, field): (usize, &Field)) -> Result<TokenStream> {
.map(|x| ::rustpython_vm::convert::TryFromObject::try_from_object(vm, x)).transpose()?
};
let ending = if let Some(default) = attr.default {
let ty = &field.ty;
let default = default.unwrap_or_else(|| parse_quote!(::std::default::Default::default()));
quote! {
.map(::rustpython_vm::function::FromArgOptional::from_inner)
.map(<#ty as ::rustpython_vm::function::FromArgOptional>::from_inner)
.unwrap_or_else(|| #default)
}
} else {
Expand Down
1 change: 1 addition & 0 deletions derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ proc-macro = true
[dependencies]
rustpython-compiler = { workspace = true }
rustpython-derive-impl = { workspace = true }
proc-macro2 = { workspace = true }
syn = { workspace = true }

[lints]
Expand Down
14 changes: 7 additions & 7 deletions stdlib/src/binascii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,15 @@ mod decl {

#[derive(FromArgs)]
struct NewlineArg {
#[pyarg(named, default = "true")]
#[pyarg(named, default = true)]
newline: bool,
}

#[derive(FromArgs)]
struct A2bBase64Args {
#[pyarg(any)]
s: ArgAsciiBuffer,
#[pyarg(named, default = "false")]
#[pyarg(named, default = false)]
strict_mode: bool,
}

Expand Down Expand Up @@ -298,7 +298,7 @@ mod decl {
struct A2bQpArgs {
#[pyarg(any)]
data: ArgAsciiBuffer,
#[pyarg(named, default = "false")]
#[pyarg(named, default = false)]
header: bool,
}
#[pyfunction]
Expand Down Expand Up @@ -366,11 +366,11 @@ mod decl {
struct B2aQpArgs {
#[pyarg(any)]
data: ArgAsciiBuffer,
#[pyarg(named, default = "false")]
#[pyarg(named, default = false)]
quotetabs: bool,
#[pyarg(named, default = "true")]
#[pyarg(named, default = true)]
istext: bool,
#[pyarg(named, default = "false")]
#[pyarg(named, default = false)]
header: bool,
}

Expand Down Expand Up @@ -689,7 +689,7 @@ mod decl {

#[derive(FromArgs)]
struct BacktickArg {
#[pyarg(named, default = "false")]
#[pyarg(named, default = false)]
backtick: bool,
}

Expand Down
6 changes: 3 additions & 3 deletions stdlib/src/faulthandler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ mod decl {
struct EnableArgs {
#[pyarg(any, default)]
file: Option<i64>,
#[pyarg(any, default = "true")]
#[pyarg(any, default = true)]
all_threads: bool,
}

Expand All @@ -50,9 +50,9 @@ mod decl {
signum: i64,
#[pyarg(any, default)]
file: Option<i64>,
#[pyarg(any, default = "true")]
#[pyarg(any, default = true)]
all_threads: bool,
#[pyarg(any, default = "false")]
#[pyarg(any, default = false)]
chain: bool,
}

Expand Down
8 changes: 4 additions & 4 deletions stdlib/src/hashlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub mod _hashlib {
name: PyStrRef,
#[pyarg(any, optional)]
data: OptionalArg<ArgBytesLike>,
#[pyarg(named, default = "true")]
#[pyarg(named, default = true)]
usedforsecurity: bool,
}

Expand All @@ -37,7 +37,7 @@ pub mod _hashlib {
pub struct BlakeHashArgs {
#[pyarg(positional, optional)]
pub data: OptionalArg<ArgBytesLike>,
#[pyarg(named, default = "true")]
#[pyarg(named, default = true)]
usedforsecurity: bool,
}

Expand All @@ -55,7 +55,7 @@ pub mod _hashlib {
pub struct HashArgs {
#[pyarg(any, optional)]
pub string: OptionalArg<ArgBytesLike>,
#[pyarg(named, default = "true")]
#[pyarg(named, default = true)]
usedforsecurity: bool,
}

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

Expand Down
8 changes: 4 additions & 4 deletions stdlib/src/mmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,13 @@ mod mmap {
fileno: RawFd,
#[pyarg(any)]
length: isize,
#[pyarg(any, default = "MAP_SHARED")]
#[pyarg(any, default = MAP_SHARED)]
flags: libc::c_int,
#[pyarg(any, default = "PROT_WRITE|PROT_READ")]
#[pyarg(any, default = PROT_WRITE|PROT_READ)]
prot: libc::c_int,
#[pyarg(any, default = "AccessMode::Default")]
#[pyarg(any, default = AccessMode::Default)]
access: AccessMode,
#[pyarg(any, default = "0")]
#[pyarg(any, default = 0)]
offset: libc::off_t,
}

Expand Down
2 changes: 1 addition & 1 deletion stdlib/src/pystruct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ pub(crate) mod _struct {
#[derive(FromArgs)]
struct UpdateFromArgs {
buffer: ArgBytesLike,
#[pyarg(any, default = "0")]
#[pyarg(any, default = 0)]
offset: isize,
}

Expand Down
6 changes: 3 additions & 3 deletions stdlib/src/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,9 +529,9 @@ mod decl {

#[derive(FromArgs)]
pub struct EpollNewArgs {
#[pyarg(any, default = "-1")]
#[pyarg(any, default = -1)]
sizehint: i32,
#[pyarg(any, default = "0")]
#[pyarg(any, default = 0)]
flags: i32,
}

Expand All @@ -555,7 +555,7 @@ mod decl {
struct EpollPollArgs {
#[pyarg(any, default)]
timeout: poll::TimeoutArg<false>,
#[pyarg(any, default = "-1")]
#[pyarg(any, default = -1)]
maxevents: i32,
}

Expand Down
8 changes: 4 additions & 4 deletions stdlib/src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1908,13 +1908,13 @@ mod _socket {
#[pyarg(positional)]
port: Option<Either<PyStrRef, i32>>,

#[pyarg(positional, default = "c::AF_UNSPEC")]
#[pyarg(positional, default = c::AF_UNSPEC)]
family: i32,
#[pyarg(positional, default = "0")]
#[pyarg(positional, default = 0)]
ty: i32,
#[pyarg(positional, default = "0")]
#[pyarg(positional, default = 0)]
proto: i32,
#[pyarg(positional, default = "0")]
#[pyarg(positional, default = 0)]
flags: i32,
}

Expand Down
20 changes: 10 additions & 10 deletions stdlib/src/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,21 +297,21 @@ mod _sqlite {
struct ConnectArgs {
#[pyarg(any)]
database: FsPath,
#[pyarg(any, default = "5.0")]
#[pyarg(any, default = 5.0)]
timeout: f64,
#[pyarg(any, default = "0")]
#[pyarg(any, default = 0)]
detect_types: c_int,
#[pyarg(any, default = "Some(vm.ctx.empty_str.to_owned())")]
#[pyarg(any, default = Some(vm.ctx.empty_str.to_owned()))]
isolation_level: Option<PyStrRef>,
#[pyarg(any, default = "true")]
#[pyarg(any, default = true)]
check_same_thread: bool,
#[pyarg(any, default = "Connection::class(&vm.ctx).to_owned()")]
#[pyarg(any, default = Connection::class(&vm.ctx).to_owned())]
factory: PyTypeRef,
// TODO: cache statements
#[allow(dead_code)]
#[pyarg(any, default = "0")]
#[pyarg(any, default = 0)]
cached_statements: c_int,
#[pyarg(any, default = "false")]
#[pyarg(any, default = false)]
uri: bool,
}

Expand All @@ -326,13 +326,13 @@ mod _sqlite {
struct BackupArgs {
#[pyarg(any)]
target: PyRef<Connection>,
#[pyarg(named, default = "-1")]
#[pyarg(named, default = -1)]
pages: c_int,
#[pyarg(named, optional)]
progress: Option<ArgCallable>,
#[pyarg(named, optional)]
name: Option<PyStrRef>,
#[pyarg(named, default = "0.250")]
#[pyarg(named, default = 0.250)]
sleep: f64,
}

Expand Down Expand Up @@ -375,7 +375,7 @@ mod _sqlite {
row: i64,
#[pyarg(named, default)]
readonly: bool,
#[pyarg(named, default = "vm.ctx.new_str(stringify!(main))")]
#[pyarg(named, default = vm.ctx.new_str("main"))]
name: PyStrRef,
}

Expand Down
2 changes: 1 addition & 1 deletion stdlib/src/ssl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ mod _ssl {
#[derive(FromArgs)]
struct Txt2ObjArgs {
txt: PyStrRef,
#[pyarg(any, default = "false")]
#[pyarg(any, default = false)]
name: bool,
}

Expand Down
20 changes: 10 additions & 10 deletions stdlib/src/zlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ mod zlib {
struct PyFuncCompressArgs {
#[pyarg(positional)]
data: ArgBytesLike,
#[pyarg(any, default = "Level::new(Z_DEFAULT_COMPRESSION)")]
#[pyarg(any, default = Level::new(Z_DEFAULT_COMPRESSION))]
level: Level,
#[pyarg(any, default = "ArgPrimitiveIndex { value: MAX_WBITS }")]
#[pyarg(any, default = ArgPrimitiveIndex { value: MAX_WBITS })]
wbits: ArgPrimitiveIndex<i8>,
}

Expand Down Expand Up @@ -269,9 +269,9 @@ mod zlib {
struct PyFuncDecompressArgs {
#[pyarg(positional)]
data: ArgBytesLike,
#[pyarg(any, default = "ArgPrimitiveIndex { value: MAX_WBITS }")]
#[pyarg(any, default = ArgPrimitiveIndex { value: MAX_WBITS })]
wbits: ArgPrimitiveIndex<i8>,
#[pyarg(any, default = "ArgPrimitiveIndex { value: DEF_BUF_SIZE }")]
#[pyarg(any, default = ArgPrimitiveIndex { value: DEF_BUF_SIZE })]
bufsize: ArgPrimitiveIndex<usize>,
}

Expand Down Expand Up @@ -299,7 +299,7 @@ mod zlib {

#[derive(FromArgs)]
struct DecompressobjArgs {
#[pyarg(any, default = "ArgPrimitiveIndex { value: MAX_WBITS }")]
#[pyarg(any, default = ArgPrimitiveIndex { value: MAX_WBITS })]
wbits: ArgPrimitiveIndex<i8>,
#[pyarg(any, optional)]
zdict: OptionalArg<ArgBytesLike>,
Expand Down Expand Up @@ -450,16 +450,16 @@ mod zlib {
#[derive(FromArgs)]
#[allow(dead_code)] // FIXME: use args
struct CompressobjArgs {
#[pyarg(any, default = "Level::new(Z_DEFAULT_COMPRESSION)")]
#[pyarg(any, default = Level::new(Z_DEFAULT_COMPRESSION))]
level: Level,
// only DEFLATED is valid right now, it's w/e
#[pyarg(any, default = "DEFLATED")]
#[pyarg(any, default = DEFLATED)]
method: i32,
#[pyarg(any, default = "ArgPrimitiveIndex { value: MAX_WBITS }")]
#[pyarg(any, default = ArgPrimitiveIndex { value: MAX_WBITS })]
wbits: ArgPrimitiveIndex<i8>,
#[pyarg(any, name = "memLevel", default = "DEF_MEM_LEVEL")]
#[pyarg(any, name = "memLevel", default = DEF_MEM_LEVEL)]
mem_level: u8,
#[pyarg(any, default = "Z_DEFAULT_STRATEGY")]
#[pyarg(any, default = Z_DEFAULT_STRATEGY)]
strategy: i32,
#[pyarg(any, optional)]
zdict: Option<ArgBytesLike>,
Expand Down
6 changes: 3 additions & 3 deletions vm/src/anystr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ use num_traits::{cast::ToPrimitive, sign::Signed};
pub struct SplitArgs<T: TryFromObject> {
#[pyarg(any, default)]
sep: Option<T>,
#[pyarg(any, default = "-1")]
#[pyarg(any, default = -1)]
maxsplit: isize,
}

#[derive(FromArgs)]
pub struct SplitLinesArgs {
#[pyarg(any, default = "false")]
#[pyarg(any, default = false)]
pub keepends: bool,
}

#[derive(FromArgs)]
pub struct ExpandTabsArgs {
#[pyarg(any, default = "8")]
#[pyarg(any, default = 8)]
tabsize: isize,
}

Expand Down
6 changes: 3 additions & 3 deletions vm/src/builtins/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -826,17 +826,17 @@ pub struct IntOptions {
#[derive(FromArgs)]
struct IntFromByteArgs {
bytes: PyBytesInner,
#[pyarg(any, default = "ArgByteOrder::Big")]
#[pyarg(any, default = ArgByteOrder::Big)]
byteorder: ArgByteOrder,
#[pyarg(named, optional)]
signed: OptionalArg<ArgIntoBool>,
}

#[derive(FromArgs)]
struct IntToByteArgs {
#[pyarg(any, default = "1")]
#[pyarg(any, default = 1)]
length: usize,
#[pyarg(any, default = "ArgByteOrder::Big")]
#[pyarg(any, default = ArgByteOrder::Big)]
byteorder: ArgByteOrder,
#[pyarg(named, optional)]
signed: OptionalArg<ArgIntoBool>,
Expand Down
2 changes: 1 addition & 1 deletion vm/src/builtins/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub(crate) struct SortOptions {
#[pyarg(named, default)]
key: Option<PyObjectRef>,
#[pytraverse(skip)]
#[pyarg(named, default = "false")]
#[pyarg(named, default = false)]
reverse: bool,
}

Expand Down
2 changes: 1 addition & 1 deletion vm/src/stdlib/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ mod builtins {
sep: Option<PyStrRef>,
#[pyarg(named, default)]
end: Option<PyStrRef>,
#[pyarg(named, default = "ArgIntoBool::FALSE")]
#[pyarg(named, default = ArgIntoBool::FALSE)]
flush: ArgIntoBool,
#[pyarg(named, default)]
file: Option<PyObjectRef>,
Expand Down
2 changes: 1 addition & 1 deletion vm/src/stdlib/codecs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ mod _codecs {
data: ArgBytesLike,
#[pyarg(positional, optional)]
errors: Option<PyStrRef>,
#[pyarg(positional, default = "false")]
#[pyarg(positional, default = false)]
final_decode: bool,
}

Expand Down
Loading
0