Skip to content

Commit 886c794

Browse files
committed
Use shared binding validation for retained code arguments
Forward the derived binding metadata and let bind_for validate once before converting. Preserve original argument owners through conversion and audit callbacks. Assisted-by: OpenAI Codex:GPT-6
1 parent b0aecbb commit 886c794

1 file changed

Lines changed: 3 additions & 51 deletions

File tree

‎crates/vm/src/builtins/code.rs‎

Lines changed: 3 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ use crate::{
1616
convert::ToPyObject,
1717
frozen,
1818
function::{
19-
ArgSpec, ArgumentError, FromArgs, FuncArgs, KwArgs, NameChanges, OptionalArg, Param,
20-
PosArgs, arity_message,
19+
ArgumentError, FromArgs, FuncArgs, KwArgs, NameChanges, OptionalArg, Param, PosArgs,
2120
},
2221
object::{Traverse, TraverseFn},
2322
types::{Comparable, Constructor, Hashable, Representable},
@@ -161,55 +160,18 @@ impl TryFromObject for CodeInt {
161160
}
162161
}
163162

164-
trait CodeArgumentSpec: FromArgs {
165-
const NAME: &'static str;
166-
const POSITIONAL_ONLY: bool;
167-
}
168-
169163
// Keep the caller's objects alive while derived conversion consumes its copy.
170164
// An early __index__ argument must survive later conversions and the audit hook.
171165
pub struct CodeArgs<T> {
172166
values: T,
173167
_owners: FuncArgs,
174168
}
175169

176-
impl<T: CodeArgumentSpec> FromArgs for CodeArgs<T> {
170+
impl<T: FromArgs> FromArgs for CodeArgs<T> {
177171
const PARAMS: Option<&'static [Param]> = T::PARAMS;
172+
const BINDING_PARAMS: Option<&'static [Param]> = T::BINDING_PARAMS;
178173

179-
// Leave outer arity checks to this preflight: the constructor rejects
180-
// keywords before arity, and both reject bad call shapes before conversion.
181174
fn from_args(vm: &VirtualMachine, args: &mut FuncArgs) -> Result<Self, ArgumentError> {
182-
let arity = T::arity();
183-
if T::POSITIONAL_ONLY {
184-
if !args.kwargs.is_empty() {
185-
return Err(vm
186-
.new_type_error(format!("{}() takes no keyword arguments", T::NAME))
187-
.into());
188-
}
189-
if !arity.contains(&args.args.len()) {
190-
return Err(vm
191-
.new_type_error(arity_message(
192-
Some(T::NAME),
193-
&arity,
194-
args.args.len() < *arity.start(),
195-
args.args.len(),
196-
))
197-
.into());
198-
}
199-
} else {
200-
let keywords: Vec<_> = T::PARAMS
201-
.unwrap_or_default()
202-
.iter()
203-
.map(|param| param.name)
204-
.collect();
205-
ArgSpec {
206-
fname: T::NAME,
207-
keywords: &keywords,
208-
required: 0,
209-
max_positional: 0,
210-
}
211-
.parse(args, vm)?;
212-
}
213175
let owners = args.clone();
214176
let values = T::from_args(vm, args)?;
215177
Ok(Self {
@@ -259,11 +221,6 @@ struct ReplaceArgs {
259221
co_exceptiontable: OptionalArg<PyBytesRef>,
260222
}
261223

262-
impl CodeArgumentSpec for ReplaceArgs {
263-
const NAME: &'static str = "replace";
264-
const POSITIONAL_ONLY: bool = false;
265-
}
266-
267224
#[derive(Clone)]
268225
#[repr(transparent)]
269226
pub struct Literal(PyObjectRef);
@@ -1228,11 +1185,6 @@ pub struct PyCodeNewArgs {
12281185
cellvars: OptionalArg<PyTupleRef>,
12291186
}
12301187

1231-
impl CodeArgumentSpec for PyCodeNewArgs {
1232-
const NAME: &'static str = "code";
1233-
const POSITIONAL_ONLY: bool = true;
1234-
}
1235-
12361188
struct CodeConstructor {
12371189
argcount: i32,
12381190
posonlyargcount: i32,

0 commit comments

Comments
 (0)