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
10 changes: 5 additions & 5 deletions crates/stdlib/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ mod array {
atomic_func,
builtins::{
PositionIterInternal, PyByteArray, PyBytes, PyBytesRef, PyDictRef, PyFloat,
PyGenericAlias, PyInt, PyList, PyListRef, PyStr, PyStrRef, PyTupleRef, PyTypeRef,
PyGenericAlias, PyInt, PyList, PyListRef, PyStr, PyStrRef, PyTupleRef, PyType,
PyTypeRef,
},
class_or_notimplemented,
convert::{ToPyObject, ToPyResult, TryFromBorrowedObject, TryFromObject},
Expand Down Expand Up @@ -651,10 +652,10 @@ mod array {
type Args = (ArrayNewArgs, KwArgs);

fn py_new(
cls: PyTypeRef,
cls: &Py<PyType>,
(ArrayNewArgs { spec, init }, kwargs): Self::Args,
vm: &VirtualMachine,
) -> PyResult {
) -> PyResult<Self> {
let spec = spec.as_str().chars().exactly_one().map_err(|_| {
vm.new_type_error("array() argument 1 must be a unicode character, not str")
})?;
Expand Down Expand Up @@ -701,8 +702,7 @@ mod array {
}
}

let zelf = Self::from(array).into_ref_with_type(vm, cls)?;
Ok(zelf.into())
Ok(Self::from(array))
}
}

Expand Down
27 changes: 13 additions & 14 deletions crates/stdlib/src/bz2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ mod _bz2 {
DecompressArgs, DecompressError, DecompressState, DecompressStatus, Decompressor,
};
use crate::vm::{
VirtualMachine,
builtins::{PyBytesRef, PyTypeRef},
Py, VirtualMachine,
builtins::{PyBytesRef, PyType},
common::lock::PyMutex,
function::{ArgBytesLike, OptionalArg},
object::{PyPayload, PyResult},
object::PyResult,
types::Constructor,
};
use bzip2::{Decompress, Status, write::BzEncoder};
Expand Down Expand Up @@ -61,12 +61,10 @@ mod _bz2 {
impl Constructor for BZ2Decompressor {
type Args = ();

fn py_new(cls: PyTypeRef, _: Self::Args, vm: &VirtualMachine) -> PyResult {
Self {
fn py_new(_cls: &Py<PyType>, _args: Self::Args, vm: &VirtualMachine) -> PyResult<Self> {
Ok(Self {
state: PyMutex::new(DecompressState::new(Decompress::new(false), vm)),
}
.into_ref_with_type(vm, cls)
.map(Into::into)
})
}
}

Expand Down Expand Up @@ -132,8 +130,11 @@ mod _bz2 {
impl Constructor for BZ2Compressor {
type Args = (OptionalArg<i32>,);

fn py_new(cls: PyTypeRef, args: Self::Args, vm: &VirtualMachine) -> PyResult {
let (compresslevel,) = args;
fn py_new(
_cls: &Py<PyType>,
(compresslevel,): Self::Args,
vm: &VirtualMachine,
) -> PyResult<Self> {
// TODO: seriously?
// compresslevel.unwrap_or(bzip2::Compression::best().level().try_into().unwrap());
let compresslevel = compresslevel.unwrap_or(9);
Expand All @@ -144,14 +145,12 @@ mod _bz2 {
}
};

Self {
Ok(Self {
state: PyMutex::new(CompressorState {
flushed: false,
encoder: Some(BzEncoder::new(Vec::new(), level)),
}),
}
.into_ref_with_type(vm, cls)
.map(Into::into)
})
}
}

Expand Down
20 changes: 13 additions & 7 deletions crates/stdlib/src/contextvars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ thread_local! {
mod _contextvars {
use crate::vm::{
AsObject, Py, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine, atomic_func,
builtins::{PyGenericAlias, PyStrRef, PyTypeRef},
builtins::{PyGenericAlias, PyStrRef, PyType, PyTypeRef},
class::StaticType,
common::hash::PyHash,
function::{ArgCallable, FuncArgs, OptionalArg},
Expand Down Expand Up @@ -244,8 +244,8 @@ mod _contextvars {

impl Constructor for PyContext {
type Args = ();
fn py_new(_cls: PyTypeRef, _args: Self::Args, vm: &VirtualMachine) -> PyResult {
Ok(Self::empty(vm).into_pyobject(vm))
fn py_new(_cls: &Py<PyType>, _args: Self::Args, vm: &VirtualMachine) -> PyResult<Self> {
Ok(Self::empty(vm))
}
}

Expand Down Expand Up @@ -488,22 +488,28 @@ mod _contextvars {

impl Constructor for ContextVar {
type Args = ContextVarOptions;
fn py_new(_cls: PyTypeRef, args: Self::Args, vm: &VirtualMachine) -> PyResult {

fn slot_new(cls: PyTypeRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult {
let args: Self::Args = args.bind(vm)?;
let var = Self {
name: args.name.to_string(),
default: args.default.into_option(),
cached_id: 0.into(),
cached: AtomicCell::new(None),
hash: UnsafeCell::new(0),
};
let py_var = var.into_ref(&vm.ctx);
let py_var = var.into_ref_with_type(vm, cls)?;

unsafe {
// SAFETY: py_var is not exposed to python memory model yet
*py_var.hash.get() = Self::generate_hash(&py_var, vm)
};
Ok(py_var.into())
}

fn py_new(_cls: &Py<PyType>, _args: Self::Args, _vm: &VirtualMachine) -> PyResult<Self> {
unreachable!("use slot_new")
}
}

impl std::hash::Hash for ContextVar {
Expand Down Expand Up @@ -578,8 +584,8 @@ mod _contextvars {
fn slot_new(_cls: PyTypeRef, _args: FuncArgs, vm: &VirtualMachine) -> PyResult {
Err(vm.new_runtime_error("Tokens can only be created by ContextVars"))
}
fn py_new(_cls: PyTypeRef, _args: Self::Args, _vm: &VirtualMachine) -> PyResult {
unreachable!()
fn py_new(_cls: &Py<PyType>, _args: Self::Args, _vm: &VirtualMachine) -> PyResult<Self> {
unreachable!("use slot_new")
}
}

Expand Down
6 changes: 2 additions & 4 deletions crates/stdlib/src/csv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,8 @@ mod _csv {
impl Constructor for PyDialect {
type Args = PyObjectRef;

fn py_new(cls: PyTypeRef, ctx: Self::Args, vm: &VirtualMachine) -> PyResult {
Self::try_from_object(vm, ctx)?
.into_ref_with_type(vm, cls)
.map(Into::into)
fn py_new(_cls: &Py<PyType>, ctx: Self::Args, vm: &VirtualMachine) -> PyResult<Self> {
Self::try_from_object(vm, ctx)
}
}
#[pyclass(with(Constructor))]
Expand Down
10 changes: 4 additions & 6 deletions crates/stdlib/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod _json {
use super::machinery;
use crate::vm::{
AsObject, Py, PyObjectRef, PyPayload, PyResult, VirtualMachine,
builtins::{PyBaseExceptionRef, PyStrRef, PyType, PyTypeRef},
builtins::{PyBaseExceptionRef, PyStrRef, PyType},
convert::{ToPyObject, ToPyResult},
function::{IntoFuncArgs, OptionalArg},
protocol::PyIterReturn,
Expand All @@ -33,7 +33,7 @@ mod _json {
impl Constructor for JsonScanner {
type Args = PyObjectRef;

fn py_new(cls: PyTypeRef, ctx: Self::Args, vm: &VirtualMachine) -> PyResult {
fn py_new(_cls: &Py<PyType>, ctx: Self::Args, vm: &VirtualMachine) -> PyResult<Self> {
let strict = ctx.get_attr("strict", vm)?.try_to_bool(vm)?;
let object_hook = vm.option_if_none(ctx.get_attr("object_hook", vm)?);
let object_pairs_hook = vm.option_if_none(ctx.get_attr("object_pairs_hook", vm)?);
Expand All @@ -52,17 +52,15 @@ mod _json {
};
let parse_constant = ctx.get_attr("parse_constant", vm)?;

Self {
Ok(Self {
strict,
object_hook,
object_pairs_hook,
parse_float,
parse_int,
parse_constant,
ctx,
}
.into_ref_with_type(vm, cls)
.map(Into::into)
})
}
}

Expand Down
17 changes: 7 additions & 10 deletions crates/stdlib/src/lzma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ mod _lzma {
LZMA_PRESET_LEVEL_MASK as PRESET_LEVEL_MASK,
};
use rustpython_common::lock::PyMutex;
use rustpython_vm::builtins::{PyBaseExceptionRef, PyBytesRef, PyTypeRef};
use rustpython_vm::builtins::{PyBaseExceptionRef, PyBytesRef, PyType, PyTypeRef};
use rustpython_vm::convert::ToPyException;
use rustpython_vm::function::ArgBytesLike;
use rustpython_vm::types::Constructor;
use rustpython_vm::{PyObjectRef, PyPayload, PyResult, VirtualMachine};
use rustpython_vm::{Py, PyObjectRef, PyPayload, PyResult, VirtualMachine};
use std::fmt;
use xz2::stream::{Action, Check, Error, Filters, LzmaOptions, Status, Stream};

Expand Down Expand Up @@ -148,7 +148,7 @@ mod _lzma {
impl Constructor for LZMADecompressor {
type Args = LZMADecompressorConstructorArgs;

fn py_new(cls: PyTypeRef, args: Self::Args, vm: &VirtualMachine) -> PyResult {
fn py_new(_cls: &Py<PyType>, args: Self::Args, vm: &VirtualMachine) -> PyResult<Self> {
if args.format == FORMAT_RAW && args.mem_limit.is_some() {
return Err(vm.new_value_error("Cannot specify memory limit with FORMAT_RAW"));
}
Expand All @@ -161,15 +161,13 @@ mod _lzma {
// TODO: FORMAT_RAW
_ => return Err(new_lzma_error("Invalid format", vm)),
};
Self {
Ok(Self {
state: PyMutex::new(DecompressState::new(
stream_result
.map_err(|_| new_lzma_error("Failed to initialize decoder", vm))?,
vm,
)),
}
.into_ref_with_type(vm, cls)
.map(Into::into)
})
}
}

Expand Down Expand Up @@ -366,7 +364,7 @@ mod _lzma {
impl Constructor for LZMACompressor {
type Args = LZMACompressorConstructorArgs;

fn py_new(_cls: PyTypeRef, args: Self::Args, vm: &VirtualMachine) -> PyResult {
fn py_new(_cls: &Py<PyType>, args: Self::Args, vm: &VirtualMachine) -> PyResult<Self> {
let preset = args.preset.unwrap_or(PRESET_DEFAULT);
#[allow(clippy::unnecessary_cast)]
if args.format != FORMAT_XZ as i32
Expand All @@ -392,8 +390,7 @@ mod _lzma {
};
Ok(Self {
state: PyMutex::new(CompressState::new(CompressorInner::new(stream))),
}
.into_pyobject(vm))
})
}
}

Expand Down
18 changes: 7 additions & 11 deletions crates/stdlib/src/mmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mod mmap {
use crate::vm::{
AsObject, FromArgs, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult,
TryFromBorrowedObject, VirtualMachine, atomic_func,
builtins::{PyBytes, PyBytesRef, PyInt, PyIntRef, PyTypeRef},
builtins::{PyBytes, PyBytesRef, PyInt, PyIntRef, PyType, PyTypeRef},
byte::{bytes_from_object, value_from_object},
convert::ToPyException,
function::{ArgBytesLike, FuncArgs, OptionalArg},
Expand Down Expand Up @@ -388,7 +388,7 @@ mod mmap {
type Args = MmapNewArgs;

#[cfg(unix)]
fn py_new(cls: PyTypeRef, args: Self::Args, vm: &VirtualMachine) -> PyResult {
fn py_new(_cls: &Py<PyType>, args: Self::Args, vm: &VirtualMachine) -> PyResult<Self> {
use libc::{MAP_PRIVATE, MAP_SHARED, PROT_READ, PROT_WRITE};

let mut map_size = args.validate_new_args(vm)?;
Expand Down Expand Up @@ -477,7 +477,7 @@ mod mmap {
}()
.map_err(|e| e.to_pyexception(vm))?;

let m_obj = Self {
Ok(Self {
closed: AtomicCell::new(false),
mmap: PyMutex::new(Some(mmap)),
fd: AtomicCell::new(fd.map_or(-1, |fd| fd.into_raw())),
Expand All @@ -486,13 +486,11 @@ mod mmap {
pos: AtomicCell::new(0),
exports: AtomicCell::new(0),
access,
};

m_obj.into_ref_with_type(vm, cls).map(Into::into)
})
}

#[cfg(windows)]
fn py_new(cls: PyTypeRef, args: Self::Args, vm: &VirtualMachine) -> PyResult {
fn py_new(_cls: &Py<PyType>, args: Self::Args, vm: &VirtualMachine) -> PyResult<Self> {
let mut map_size = args.validate_new_args(vm)?;
let MmapNewArgs {
fileno,
Expand Down Expand Up @@ -627,7 +625,7 @@ mod mmap {
(INVALID_HANDLE_VALUE as isize, MmapObj::Write(mmap))
};

let m_obj = Self {
Ok(Self {
closed: AtomicCell::new(false),
mmap: PyMutex::new(Some(mmap)),
handle: AtomicCell::new(handle),
Expand All @@ -636,9 +634,7 @@ mod mmap {
pos: AtomicCell::new(0),
exports: AtomicCell::new(0),
access,
};

m_obj.into_ref_with_type(vm, cls).map(Into::into)
})
}
}

Expand Down
13 changes: 8 additions & 5 deletions crates/stdlib/src/overlapped.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mod _overlapped {

use crate::vm::{
Py, PyObjectRef, PyPayload, PyResult, VirtualMachine,
builtins::{PyBaseExceptionRef, PyBytesRef, PyTypeRef},
builtins::{PyBaseExceptionRef, PyBytesRef, PyType},
common::lock::PyMutex,
convert::{ToPyException, ToPyObject},
protocol::PyBuffer,
Expand Down Expand Up @@ -264,7 +264,11 @@ mod _overlapped {
impl Constructor for Overlapped {
type Args = (isize,);

fn py_new(cls: PyTypeRef, (mut event,): Self::Args, vm: &VirtualMachine) -> PyResult {
fn py_new(
_cls: &Py<PyType>,
(mut event,): Self::Args,
vm: &VirtualMachine,
) -> PyResult<Self> {
if event == INVALID_HANDLE_VALUE {
event = unsafe {
windows_sys::Win32::System::Threading::CreateEventA(
Expand All @@ -289,10 +293,9 @@ mod _overlapped {
error: 0,
data: OverlappedData::None,
};
let overlapped = Overlapped {
Ok(Overlapped {
inner: PyMutex::new(inner),
};
overlapped.into_ref_with_type(vm, cls).map(Into::into)
})
}
}

Expand Down
8 changes: 3 additions & 5 deletions crates/stdlib/src/pystruct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub(crate) mod _struct {
use crate::vm::{
AsObject, Py, PyObjectRef, PyPayload, PyResult, TryFromObject, VirtualMachine,
buffer::{FormatSpec, new_struct_error, struct_error_type},
builtins::{PyBytes, PyStr, PyStrRef, PyTupleRef, PyTypeRef},
builtins::{PyBytes, PyStr, PyStrRef, PyTupleRef, PyType, PyTypeRef},
function::{ArgBytesLike, ArgMemoryBuffer, PosArgs},
match_class,
protocol::PyIterReturn,
Expand Down Expand Up @@ -241,12 +241,10 @@ pub(crate) mod _struct {
impl Constructor for PyStruct {
type Args = IntoStructFormatBytes;

fn py_new(cls: PyTypeRef, fmt: Self::Args, vm: &VirtualMachine) -> PyResult {
fn py_new(_cls: &Py<PyType>, fmt: Self::Args, vm: &VirtualMachine) -> PyResult<Self> {
let spec = fmt.format_spec(vm)?;
let format = fmt.0;
Self { spec, format }
.into_ref_with_type(vm, cls)
.map(Into::into)
Ok(Self { spec, format })
}
}

Expand Down
Loading
Loading