Skip to content
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
34 changes: 22 additions & 12 deletions crates/stdlib/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mod _json {
PyTuple, PyType,
},
convert::ToPyResult,
function::{IntoFuncArgs, OptionalArg},
function::{ArgIntoBool, IntoFuncArgs},
protocol::PyIterReturn,
types::{Callable, Constructor, PyComparisonOp},
};
Expand Down Expand Up @@ -719,13 +719,13 @@ mod _json {
}

#[pyfunction]
fn encode_basestring(object: PyStrRef) -> Wtf8Buf {
json::encode_string(object.as_wtf8(), false)
fn encode_basestring(pystr: PyStrRef) -> Wtf8Buf {
json::encode_string(pystr.as_wtf8(), false)
}

#[pyfunction]
fn encode_basestring_ascii(object: PyStrRef) -> Wtf8Buf {
json::encode_string(object.as_wtf8(), true)
fn encode_basestring_ascii(pystr: PyStrRef) -> Wtf8Buf {
json::encode_string(pystr.as_wtf8(), true)
}

/// Which Rust-native string escaper (if any) the `encoder` callable is
Expand Down Expand Up @@ -1388,13 +1388,23 @@ mod _json {
}
}

#[pyfunction]
fn scanstring(
s: PyStrRef,
#[derive(FromArgs)]
struct ScanStringArgs {
#[pyarg(positional)]
pystr: PyStrRef,
#[pyarg(positional)]
end: isize,
strict: OptionalArg<bool>,
vm: &VirtualMachine,
) -> PyResult<(Wtf8Buf, usize)> {
#[pyarg(positional, default = ArgIntoBool::TRUE)]
strict: ArgIntoBool,
}

#[pyfunction]
fn scanstring(args: ScanStringArgs, vm: &VirtualMachine) -> PyResult<(Wtf8Buf, usize)> {
let ScanStringArgs {
pystr: s,
end,
strict,
} = args;
flame_guard!("_json::scanstring");
let wtf8 = s.as_wtf8();

Expand All @@ -1413,7 +1423,7 @@ mod _json {
};

let (result, end_char_idx, _bytes_consumed) =
json::scan_string(&wtf8[byte_idx..], end, strict.unwrap_or(true))
json::scan_string(&wtf8[byte_idx..], end, strict.into_bool())
.map_err(|e| py_decode_error(e, s, vm))?;

Ok((result, end_char_idx))
Expand Down
4 changes: 2 additions & 2 deletions crates/vm/src/stdlib/_ast/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,8 @@ pub(crate) mod _ast {
}

#[pyfunction]
fn _is_abstract(cls: PyObjectRef, vm: &VirtualMachine) -> PyResult<bool> {
is_abstract(&cls, vm)
fn _is_abstract(object: PyObjectRef, vm: &VirtualMachine) -> PyResult<bool> {
is_abstract(&object, vm)
}

fn is_abstract(cls: &PyObject, vm: &VirtualMachine) -> PyResult<bool> {
Expand Down
20 changes: 11 additions & 9 deletions crates/vm/src/stdlib/_lsprof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ mod _lsprof {
fn _pystart_callback(
zelf: &Py<Self>,
code: PyObjectRef,
_offset: PyObjectRef,
_instruction_offset: PyObjectRef,
vm: &VirtualMachine,
) {
zelf.with_operation(vm, || {
Expand All @@ -691,7 +691,7 @@ mod _lsprof {
fn _pythrow_callback(
zelf: &Py<Self>,
code: PyObjectRef,
_offset: PyObjectRef,
_instruction_offset: PyObjectRef,
_exception: PyObjectRef,
vm: &VirtualMachine,
) {
Expand All @@ -704,7 +704,7 @@ mod _lsprof {
fn _pyreturn_callback(
zelf: &Py<Self>,
code: PyObjectRef,
_offset: PyObjectRef,
_instruction_offset: PyObjectRef,
_retval: PyObjectRef,
vm: &VirtualMachine,
) {
Expand All @@ -717,17 +717,18 @@ mod _lsprof {
fn _ccall_callback(
zelf: &Py<Self>,
_code: PyObjectRef,
_offset: PyObjectRef,
_instruction_offset: PyObjectRef,
callable: PyObjectRef,
arg: PyObjectRef,
self_arg: PyObjectRef,
vm: &VirtualMachine,
) {
zelf.with_operation(vm, || {
let (builtins, missing) = {
let state = zelf.state.lock();
(state.builtins, state.missing.clone())
};
if builtins && let Some(call) = native_call(&callable, &arg, missing.as_ref()) {
if builtins && let Some(call) = native_call(&callable, &self_arg, missing.as_ref())
{
if zelf.state.lock().in_timer {
return;
}
Expand All @@ -748,17 +749,18 @@ mod _lsprof {
fn _creturn_callback(
zelf: &Py<Self>,
_code: PyObjectRef,
_offset: PyObjectRef,
_instruction_offset: PyObjectRef,
callable: PyObjectRef,
arg: PyObjectRef,
self_arg: PyObjectRef,
vm: &VirtualMachine,
) {
zelf.with_operation(vm, || {
let (builtins, missing) = {
let state = zelf.state.lock();
(state.builtins, state.missing.clone())
};
if builtins && let Some(call) = native_call(&callable, &arg, missing.as_ref()) {
if builtins && let Some(call) = native_call(&callable, &self_arg, missing.as_ref())
{
zelf.leave(call.key(), vm);
}
})
Expand Down
10 changes: 7 additions & 3 deletions crates/vm/src/stdlib/_thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub(crate) mod _thread {
frame::FrameObjectRef,
function::{
ArgCallable, ArgIndex, ArgumentError, DefaultRepr, FromArgs, FuncArgs, KwArgs,
NameExcInfo, OptionalArg, Param, ParamKind, PosArgs, PySetterValue, TimeoutSeconds,
OptionalArg, Param, ParamKind, PySetterValue, TimeoutSeconds,
},
object::{Traverse, TraverseFn},
types::{Constructor, GetAttr, PyStructSequence, Representable, SetAttr},
Expand Down Expand Up @@ -249,7 +249,9 @@ pub(crate) mod _thread {
#[pymethod]
fn __exit__(
&self,
_args: PosArgs<crate::PyObjectRef, NameExcInfo>,
_exc_type: crate::PyObjectRef,
_exc_value: crate::PyObjectRef,
_exc_tb: crate::PyObjectRef,
vm: &VirtualMachine,
) -> PyResult<()> {
self.release(vm)
Expand Down Expand Up @@ -419,7 +421,9 @@ pub(crate) mod _thread {
#[pymethod]
fn __exit__(
&self,
_args: PosArgs<crate::PyObjectRef, NameExcInfo>,
_exc_type: crate::PyObjectRef,
_exc_value: crate::PyObjectRef,
_exc_tb: crate::PyObjectRef,
vm: &VirtualMachine,
) -> PyResult<()> {
self.release(vm)
Expand Down
24 changes: 17 additions & 7 deletions crates/vm/src/stdlib/_warnings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,26 @@ mod _warnings {
struct WarnArgs {
#[pyarg(any)]
message: PyObjectRef,
#[pyarg(any, optional)]
category: OptionalArg<PyObjectRef>,
#[pyarg(any, default = None)]
category: Option<PyObjectRef>,
#[pyarg(any, default = 1)]
stacklevel: isize,
#[pyarg(any, optional)]
source: OptionalArg<PyObjectRef>,
#[pyarg(named, optional)]
#[pyarg(any, default = None)]
source: Option<PyObjectRef>,
#[pyarg(named, optional, py_default = "()")]
skip_file_prefixes: OptionalArg<PyTupleRef>,
}

#[cfg(test)]
#[test]
fn warn_clinic_default_metadata() {
use crate::function::{DefaultRepr, FromArgs};
let params = WarnArgs::PARAMS.expect("known warning parameters");
assert!(matches!(params[1].default, Some(DefaultRepr::None)));
assert!(matches!(params[3].default, Some(DefaultRepr::None)));
assert!(matches!(params[4].default, Some(DefaultRepr::Raw("()"))));
}

/// Validate and resolve the category argument, matching get_category() in C.
fn get_category(
message: &PyObject,
Expand Down Expand Up @@ -129,7 +139,7 @@ mod _warnings {
fn warn(args: WarnArgs, vm: &VirtualMachine) -> PyResult<()> {
let level = args.stacklevel;

let category = get_category(&args.message, args.category.into_option(), vm)?;
let category = get_category(&args.message, args.category, vm)?;

// Validate skip_file_prefixes: each element must be a str
let skip_prefixes = args.skip_file_prefixes.into_option();
Expand All @@ -145,7 +155,7 @@ mod _warnings {
args.message,
category,
level,
args.source.into_option(),
args.source,
skip_prefixes.as_deref(),
vm,
)
Expand Down
12 changes: 11 additions & 1 deletion crates/vm/src/stdlib/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1445,7 +1445,7 @@ mod builtins {
struct LazyImportArgs {
#[pyarg(any)]
name: PyObjectRef,
#[pyarg(any, optional)]
#[pyarg(any, optional, py_default = "None")]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if default is required, isn't it betterto model it as Option and default = None?

globals: OptionalArg<PyObjectRef>,
#[pyarg(any, optional)]
locals: Option<PyObjectRef>,
Expand All @@ -1455,6 +1455,16 @@ mod builtins {
level: i32,
}

#[cfg(test)]
#[test]
fn lazy_import_clinic_default_metadata() {
use crate::function::{DefaultRepr, FromArgs};
let params = LazyImportArgs::PARAMS.expect("known lazy-import parameters");
assert_eq!(params[1].name, "globals");
assert!(matches!(params[1].default, Some(DefaultRepr::Raw("None"))));
assert!(matches!(params[3].default, Some(DefaultRepr::Raw("()"))));
}

#[pyfunction]
fn __lazy_import__(args: LazyImportArgs, vm: &VirtualMachine) -> PyResult {
let globals = args
Expand Down
21 changes: 19 additions & 2 deletions crates/vm/src/stdlib/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1037,8 +1037,15 @@ pub mod sys {
}
}

#[derive(FromArgs)]
struct SetLazyImportsArgs {
#[pyarg(any)]
mode: PyObjectRef,
}

#[pyfunction]
fn set_lazy_imports(mode: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
fn set_lazy_imports(args: SetLazyImportsArgs, vm: &VirtualMachine) -> PyResult<()> {
let mode = args.mode;
let mode = mode
.downcast_ref::<PyStr>()
.ok_or_else(|| vm.new_type_error("mode must be a string: 'normal' or 'all'"))?;
Expand All @@ -1062,8 +1069,18 @@ pub mod sys {
.unwrap_or_else(|| vm.ctx.none())
}

#[derive(FromArgs)]
struct SetLazyImportsFilterArgs {
#[pyarg(any)]
filter: PyObjectRef,
}

#[pyfunction]
fn set_lazy_imports_filter(filter: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
fn set_lazy_imports_filter(
args: SetLazyImportsFilterArgs,
vm: &VirtualMachine,
) -> PyResult<()> {
let filter = args.filter;
let filter = if vm.is_none(&filter) {
None
} else {
Expand Down
Loading