Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
stdlib
  • Loading branch information
ShaharNaveh committed Jul 3, 2025
commit 289256409654cc601dd39fabd358a9ae5818f891
38 changes: 19 additions & 19 deletions stdlib/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ mod array {

impl From<ArrayContentType> for PyArray {
fn from(array: ArrayContentType) -> Self {
PyArray {
Self {
array: PyRwLock::new(array),
exports: AtomicUsize::new(0),
}
Expand All @@ -659,15 +659,15 @@ mod array {
vm.new_type_error("array() argument 1 must be a unicode character, not str")
})?;

if cls.is(PyArray::class(&vm.ctx)) && !kwargs.is_empty() {
if cls.is(Self::class(&vm.ctx)) && !kwargs.is_empty() {
return Err(vm.new_type_error("array.array() takes no keyword arguments"));
}

let mut array =
ArrayContentType::from_char(spec).map_err(|err| vm.new_value_error(err))?;

if let OptionalArg::Present(init) = init {
if let Some(init) = init.payload::<PyArray>() {
if let Some(init) = init.payload::<Self>() {
match (spec, init.read().typecode()) {
(spec, ch) if spec == ch => array.frombytes(&init.get_bytes()),
(spec, 'u') => {
Expand Down Expand Up @@ -765,7 +765,7 @@ mod array {
let mut w = zelf.try_resizable(vm)?;
if zelf.is(&obj) {
w.imul(2, vm)
} else if let Some(array) = obj.payload::<PyArray>() {
} else if let Some(array) = obj.payload::<Self>() {
w.iadd(&array.read(), vm)
} else {
let iter = ArgIterable::try_from_object(vm, obj)?;
Expand Down Expand Up @@ -977,12 +977,12 @@ mod array {
}

#[pymethod]
fn __copy__(&self) -> PyArray {
fn __copy__(&self) -> Self {
self.array.read().clone().into()
}

#[pymethod]
fn __deepcopy__(&self, _memo: PyObjectRef) -> PyArray {
fn __deepcopy__(&self, _memo: PyObjectRef) -> Self {
self.__copy__()
}

Expand Down Expand Up @@ -1013,7 +1013,7 @@ mod array {
cloned = zelf.read().clone();
&cloned
} else {
match value.payload::<PyArray>() {
match value.payload::<Self>() {
Some(array) => {
guard = array.read();
&*guard
Expand Down Expand Up @@ -1059,10 +1059,10 @@ mod array {

#[pymethod]
fn __add__(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
if let Some(other) = other.payload::<PyArray>() {
if let Some(other) = other.payload::<Self>() {
self.read()
.add(&other.read(), vm)
.map(|array| PyArray::from(array).into_ref(&vm.ctx))
.map(|array| Self::from(array).into_ref(&vm.ctx))
} else {
Err(vm.new_type_error(format!(
"can only append array (not \"{}\") to array",
Expand All @@ -1079,7 +1079,7 @@ mod array {
) -> PyResult<PyRef<Self>> {
if zelf.is(&other) {
zelf.try_resizable(vm)?.imul(2, vm)?;
} else if let Some(other) = other.payload::<PyArray>() {
} else if let Some(other) = other.payload::<Self>() {
zelf.try_resizable(vm)?.iadd(&other.read(), vm)?;
} else {
return Err(vm.new_type_error(format!(
Expand Down Expand Up @@ -1454,17 +1454,17 @@ mod array {
}

impl From<MachineFormatCode> for u8 {
fn from(code: MachineFormatCode) -> u8 {
fn from(code: MachineFormatCode) -> Self {
use MachineFormatCode::*;
match code {
Int8 { signed } => signed as u8,
Int16 { signed, big_endian } => 2 + signed as u8 * 2 + big_endian as u8,
Int32 { signed, big_endian } => 6 + signed as u8 * 2 + big_endian as u8,
Int64 { signed, big_endian } => 10 + signed as u8 * 2 + big_endian as u8,
Ieee754Float { big_endian } => 14 + big_endian as u8,
Ieee754Double { big_endian } => 16 + big_endian as u8,
Utf16 { big_endian } => 18 + big_endian as u8,
Utf32 { big_endian } => 20 + big_endian as u8,
Int8 { signed } => signed as Self,
Int16 { signed, big_endian } => 2 + signed as Self * 2 + big_endian as Self,
Int32 { signed, big_endian } => 6 + signed as Self * 2 + big_endian as Self,
Int64 { signed, big_endian } => 10 + signed as Self * 2 + big_endian as Self,
Ieee754Float { big_endian } => 14 + big_endian as Self,
Ieee754Double { big_endian } => 16 + big_endian as Self,
Utf16 { big_endian } => 18 + big_endian as Self,
Utf32 { big_endian } => 20 + big_endian as Self,
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion stdlib/src/bz2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ mod _bz2 {

impl DecompressStatus for Status {
fn is_stream_end(&self) -> bool {
*self == Status::StreamEnd
*self == Self::StreamEnd
}
}

Expand Down
6 changes: 3 additions & 3 deletions stdlib/src/contextvars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ mod _contextvars {
if let Some(ctx) = ctxs.last() {
ctx.clone()
} else {
let ctx = PyContext::empty(vm);
let ctx = Self::empty(vm);
ctx.inner.idx.set(0);
ctx.inner.entered.set(true);
let ctx = ctx.into_ref(&vm.ctx);
Expand Down Expand Up @@ -253,7 +253,7 @@ mod _contextvars {
impl Constructor for PyContext {
type Args = ();
fn py_new(_cls: PyTypeRef, _args: Self::Args, vm: &VirtualMachine) -> PyResult {
Ok(PyContext::empty(vm).into_pyobject(vm))
Ok(Self::empty(vm).into_pyobject(vm))
}
}

Expand Down Expand Up @@ -499,7 +499,7 @@ mod _contextvars {
impl Constructor for ContextVar {
type Args = ContextVarOptions;
fn py_new(_cls: PyTypeRef, args: Self::Args, vm: &VirtualMachine) -> PyResult {
let var = ContextVar {
let var = Self {
name: args.name.to_string(),
default: args.default.into_option(),
cached_id: 0.into(),
Expand Down
28 changes: 14 additions & 14 deletions stdlib/src/csv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ mod _csv {
type Args = PyObjectRef;

fn py_new(cls: PyTypeRef, ctx: Self::Args, vm: &VirtualMachine) -> PyResult {
PyDialect::try_from_object(vm, ctx)?
Self::try_from_object(vm, ctx)?
.into_ref_with_type(vm, cls)
.map(Into::into)
}
Expand Down Expand Up @@ -425,10 +425,10 @@ mod _csv {
impl From<QuoteStyle> for csv_core::QuoteStyle {
fn from(val: QuoteStyle) -> Self {
match val {
QuoteStyle::Minimal => csv_core::QuoteStyle::Always,
QuoteStyle::All => csv_core::QuoteStyle::Always,
QuoteStyle::Nonnumeric => csv_core::QuoteStyle::NonNumeric,
QuoteStyle::None => csv_core::QuoteStyle::Never,
QuoteStyle::Minimal => Self::Always,
QuoteStyle::All => Self::Always,
QuoteStyle::Nonnumeric => Self::NonNumeric,
QuoteStyle::None => Self::Never,
QuoteStyle::Strings => todo!(),
QuoteStyle::Notnull => todo!(),
}
Expand All @@ -444,14 +444,14 @@ mod _csv {
}
impl TryFrom<isize> for QuoteStyle {
type Error = PyTypeError;
fn try_from(num: isize) -> Result<QuoteStyle, PyTypeError> {
fn try_from(num: isize) -> Result<Self, PyTypeError> {
match num {
0 => Ok(QuoteStyle::Minimal),
1 => Ok(QuoteStyle::All),
2 => Ok(QuoteStyle::Nonnumeric),
3 => Ok(QuoteStyle::None),
4 => Ok(QuoteStyle::Strings),
5 => Ok(QuoteStyle::Notnull),
0 => Ok(Self::Minimal),
1 => Ok(Self::All),
2 => Ok(Self::Nonnumeric),
3 => Ok(Self::None),
4 => Ok(Self::Strings),
5 => Ok(Self::Notnull),
_ => Err(PyTypeError {}),
}
}
Expand Down Expand Up @@ -488,7 +488,7 @@ mod _csv {
}
impl Default for FormatOptions {
fn default() -> Self {
FormatOptions {
Self {
dialect: DialectItem::None,
delimiter: None,
quotechar: None,
Expand Down Expand Up @@ -557,7 +557,7 @@ mod _csv {

impl FromArgs for FormatOptions {
fn from_args(vm: &VirtualMachine, args: &mut FuncArgs) -> Result<Self, ArgumentError> {
let mut res = FormatOptions::default();
let mut res = Self::default();
if let Some(dialect) = args.kwargs.swap_remove("dialect") {
res.dialect = prase_dialect_item_from_arg(vm, dialect)?;
} else if let Some(dialect) = args.args.first() {
Expand Down
26 changes: 13 additions & 13 deletions stdlib/src/hashlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ pub mod _hashlib {
#[pyclass(with(Representable))]
impl PyHasher {
fn new(name: &str, d: HashWrapper) -> Self {
PyHasher {
Self {
name: name.to_owned(),
ctx: PyRwLock::new(d),
}
Expand Down Expand Up @@ -143,7 +143,7 @@ pub mod _hashlib {

#[pymethod]
fn copy(&self) -> Self {
PyHasher::new(&self.name, self.ctx.read().clone())
Self::new(&self.name, self.ctx.read().clone())
}
}

Expand Down Expand Up @@ -173,7 +173,7 @@ pub mod _hashlib {
#[pyclass]
impl PyHasherXof {
fn new(name: &str, d: HashXofWrapper) -> Self {
PyHasherXof {
Self {
name: name.to_owned(),
ctx: PyRwLock::new(d),
}
Expand Down Expand Up @@ -216,7 +216,7 @@ pub mod _hashlib {

#[pymethod]
fn copy(&self) -> Self {
PyHasherXof::new(&self.name, self.ctx.read().clone())
Self::new(&self.name, self.ctx.read().clone())
}
}

Expand Down Expand Up @@ -367,7 +367,7 @@ pub mod _hashlib {
where
D: ThreadSafeDynDigest + BlockSizeUser + Default + 'static,
{
let mut h = HashWrapper {
let mut h = Self {
block_size: D::block_size(),
inner: Box::<D>::default(),
};
Expand Down Expand Up @@ -403,15 +403,15 @@ pub mod _hashlib {

impl HashXofWrapper {
pub fn new_shake_128(data: OptionalArg<ArgBytesLike>) -> Self {
let mut h = HashXofWrapper::Shake128(Shake128::default());
let mut h = Self::Shake128(Shake128::default());
if let OptionalArg::Present(d) = data {
d.with_ref(|bytes| h.update(bytes));
}
h
}

pub fn new_shake_256(data: OptionalArg<ArgBytesLike>) -> Self {
let mut h = HashXofWrapper::Shake256(Shake256::default());
let mut h = Self::Shake256(Shake256::default());
if let OptionalArg::Present(d) = data {
d.with_ref(|bytes| h.update(bytes));
}
Expand All @@ -420,22 +420,22 @@ pub mod _hashlib {

fn update(&mut self, data: &[u8]) {
match self {
HashXofWrapper::Shake128(h) => h.update(data),
HashXofWrapper::Shake256(h) => h.update(data),
Self::Shake128(h) => h.update(data),
Self::Shake256(h) => h.update(data),
}
}

fn block_size(&self) -> usize {
match self {
HashXofWrapper::Shake128(_) => Shake128::block_size(),
HashXofWrapper::Shake256(_) => Shake256::block_size(),
Self::Shake128(_) => Shake128::block_size(),
Self::Shake256(_) => Shake256::block_size(),
}
}

fn finalize_xof(&self, length: usize) -> Vec<u8> {
match self {
HashXofWrapper::Shake128(h) => h.clone().finalize_boxed(length).into_vec(),
HashXofWrapper::Shake256(h) => h.clone().finalize_boxed(length).into_vec(),
Self::Shake128(h) => h.clone().finalize_boxed(length).into_vec(),
Self::Shake256(h) => h.clone().finalize_boxed(length).into_vec(),
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions stdlib/src/posixsubprocess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ struct CStrPathLike {
impl TryFromObject for CStrPathLike {
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
let s = OsPath::try_from_object(vm, obj)?.into_cstring(vm)?;
Ok(CStrPathLike { s })
Ok(Self { s })
}
}
impl AsRef<CStr> for CStrPathLike {
Expand Down Expand Up @@ -176,9 +176,9 @@ enum ExecErrorContext {
impl ExecErrorContext {
fn as_msg(&self) -> &'static str {
match self {
ExecErrorContext::NoExec => "noexec",
ExecErrorContext::ChDir => "noexec:chdir",
ExecErrorContext::Exec => "",
Self::NoExec => "noexec",
Self::ChDir => "noexec:chdir",
Self::Exec => "",
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion stdlib/src/pyexpat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ mod _pyexpat {
#[pyclass]
impl PyExpatLikeXmlParser {
fn new(vm: &VirtualMachine) -> PyResult<PyExpatLikeXmlParserRef> {
Ok(PyExpatLikeXmlParser {
Ok(Self {
start_element: MutableObject::new(vm.ctx.none()),
end_element: MutableObject::new(vm.ctx.none()),
character_data: MutableObject::new(vm.ctx.none()),
Expand Down
8 changes: 4 additions & 4 deletions stdlib/src/pystruct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub(crate) mod _struct {
))),
})
.ok_or_else(|| vm.new_unicode_decode_error("Struct format must be a ascii string"))?;
Ok(IntoStructFormatBytes(fmt))
Ok(Self(fmt))
}
}

Expand Down Expand Up @@ -165,7 +165,7 @@ pub(crate) mod _struct {
vm: &VirtualMachine,
format_spec: FormatSpec,
buffer: ArgBytesLike,
) -> PyResult<UnpackIterator> {
) -> PyResult<Self> {
if format_spec.size == 0 {
Err(new_struct_error(
vm,
Expand All @@ -180,7 +180,7 @@ pub(crate) mod _struct {
),
))
} else {
Ok(UnpackIterator {
Ok(Self {
format_spec,
buffer,
offset: AtomicCell::new(0),
Expand Down Expand Up @@ -244,7 +244,7 @@ pub(crate) mod _struct {
fn py_new(cls: PyTypeRef, fmt: Self::Args, vm: &VirtualMachine) -> PyResult {
let spec = fmt.format_spec(vm)?;
let format = fmt.0;
PyStruct { spec, format }
Self { spec, format }
.into_ref_with_type(vm, cls)
.map(Into::into)
}
Expand Down
2 changes: 1 addition & 1 deletion stdlib/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ mod resource {
impl From<libc::rusage> for Rusage {
fn from(rusage: libc::rusage) -> Self {
let tv = |tv: libc::timeval| tv.tv_sec as f64 + (tv.tv_usec as f64 / 1_000_000.0);
Rusage {
Self {
ru_utime: tv(rusage.ru_utime),
ru_stime: tv(rusage.ru_utime),
ru_maxrss: rusage.ru_maxrss,
Comment thread
youknowone marked this conversation as resolved.
Expand Down
Loading