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
4 changes: 0 additions & 4 deletions Lib/test/test_genericalias.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,6 @@ class BaseTest(unittest.TestCase):
if ctypes is not None:
generic_types.extend((ctypes.Array, ctypes.LibraryLoader))

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_subscriptable(self):
for t in self.generic_types:
if t is None:
Expand Down Expand Up @@ -314,8 +312,6 @@ def test_dir(self):
for generic_alias_property in ("__origin__", "__args__", "__parameters__"):
self.assertIn(generic_alias_property, dir_of_gen_alias)

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_weakref(self):
for t in self.generic_types:
if t is None:
Expand Down
13 changes: 11 additions & 2 deletions stdlib/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ mod array {
AsObject, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
atomic_func,
builtins::{
PositionIterInternal, PyByteArray, PyBytes, PyBytesRef, PyDictRef, PyFloat, PyInt,
PyList, PyListRef, PyStr, PyStrRef, PyTupleRef, PyTypeRef,
PositionIterInternal, PyByteArray, PyBytes, PyBytesRef, PyDictRef, PyFloat,
PyGenericAlias, PyInt, PyList, PyListRef, PyStr, PyStrRef, PyTupleRef, PyTypeRef,
},
class_or_notimplemented,
convert::{ToPyObject, ToPyResult, TryFromBorrowedObject, TryFromObject},
Expand Down Expand Up @@ -1195,6 +1195,15 @@ mod array {

false
}

#[pyclassmethod]
fn __class_getitem__(
cls: PyTypeRef,
args: PyObjectRef,
vm: &VirtualMachine,
) -> PyGenericAlias {
PyGenericAlias::from_args(cls, args, vm)
}
}

impl Comparable for PyArray {
Expand Down
21 changes: 15 additions & 6 deletions 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::{PyStrRef, PyTypeRef},
builtins::{PyGenericAlias, PyStrRef, PyTypeRef},
class::StaticType,
common::hash::PyHash,
function::{ArgCallable, FuncArgs, OptionalArg},
Expand Down Expand Up @@ -478,11 +478,11 @@ mod _contextvars {

#[pyclassmethod]
fn __class_getitem__(
_cls: PyTypeRef,
_key: PyStrRef,
_vm: &VirtualMachine,
) -> PyResult<()> {
unimplemented!("ContextVar.__class_getitem__() is currently under construction")
cls: PyTypeRef,
args: PyObjectRef,
vm: &VirtualMachine,
) -> PyGenericAlias {
PyGenericAlias::from_args(cls, args, vm)
}
}

Expand Down Expand Up @@ -571,6 +571,15 @@ mod _contextvars {
None => ContextTokenMissing::static_type().to_owned().into(),
}
}

#[pyclassmethod]
fn __class_getitem__(
cls: PyTypeRef,
args: PyObjectRef,
vm: &VirtualMachine,
) -> PyGenericAlias {
PyGenericAlias::from_args(cls, args, vm)
}
}

impl Constructor for ContextToken {
Expand Down
9 changes: 7 additions & 2 deletions vm/src/builtins/bytearray.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Implementation of the python bytearray object.
use super::{
PositionIterInternal, PyBytes, PyBytesRef, PyDictRef, PyIntRef, PyStrRef, PyTuple, PyTupleRef,
PyType, PyTypeRef,
PositionIterInternal, PyBytes, PyBytesRef, PyDictRef, PyGenericAlias, PyIntRef, PyStrRef,
PyTuple, PyTupleRef, PyType, PyTypeRef,
};
use crate::{
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject,
Expand Down Expand Up @@ -571,6 +571,11 @@ impl PyByteArray {
fn reverse(&self) {
self.borrow_buf_mut().reverse();
}

#[pyclassmethod]
fn __class_getitem__(cls: PyTypeRef, args: PyObjectRef, vm: &VirtualMachine) -> PyGenericAlias {
PyGenericAlias::from_args(cls, args, vm)
}
}

#[pyclass]
Expand Down
8 changes: 7 additions & 1 deletion vm/src/builtins/bytes.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::{
PositionIterInternal, PyDictRef, PyIntRef, PyStrRef, PyTuple, PyTupleRef, PyType, PyTypeRef,
PositionIterInternal, PyDictRef, PyGenericAlias, PyIntRef, PyStrRef, PyTuple, PyTupleRef,
PyType, PyTypeRef,
};
use crate::{
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult,
Expand Down Expand Up @@ -481,6 +482,11 @@ impl PyBytes {
let param: Vec<PyObjectRef> = self.elements().map(|x| x.to_pyobject(vm)).collect();
PyTuple::new_ref(param, &vm.ctx)
}

#[pyclassmethod]
fn __class_getitem__(cls: PyTypeRef, args: PyObjectRef, vm: &VirtualMachine) -> PyGenericAlias {
PyGenericAlias::from_args(cls, args, vm)
}
}

#[pyclass]
Expand Down
7 changes: 6 additions & 1 deletion vm/src/builtins/classmethod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{PyBoundMethod, PyStr, PyType, PyTypeRef};
use super::{PyBoundMethod, PyGenericAlias, PyStr, PyType, PyTypeRef};
use crate::{
AsObject, Context, Py, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
class::PyClassImpl,
Expand Down Expand Up @@ -170,6 +170,11 @@ impl PyClassMethod {
.set_attr("__isabstractmethod__", value, vm)?;
Ok(())
}

#[pyclassmethod]
fn __class_getitem__(cls: PyTypeRef, args: PyObjectRef, vm: &VirtualMachine) -> PyGenericAlias {
PyGenericAlias::from_args(cls, args, vm)
}
}

impl Representable for PyClassMethod {
Expand Down
7 changes: 6 additions & 1 deletion vm/src/builtins/coroutine.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{PyCode, PyStrRef, PyType};
use super::{PyCode, PyGenericAlias, PyStrRef, PyType, PyTypeRef};
use crate::{
AsObject, Context, Py, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
class::PyClassImpl,
Expand Down Expand Up @@ -72,6 +72,11 @@ impl PyCoroutine {
fn cr_origin(&self, _vm: &VirtualMachine) -> Option<(PyStrRef, usize, PyStrRef)> {
None
}

#[pyclassmethod]
fn __class_getitem__(cls: PyTypeRef, args: PyObjectRef, vm: &VirtualMachine) -> PyGenericAlias {
PyGenericAlias::from_args(cls, args, vm)
}
}

#[pyclass]
Expand Down
7 changes: 6 additions & 1 deletion vm/src/builtins/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* The mythical generator.
*/

use super::{PyCode, PyStrRef, PyType};
use super::{PyCode, PyGenericAlias, PyStrRef, PyType, PyTypeRef};
use crate::{
AsObject, Context, Py, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
class::PyClassImpl,
Expand Down Expand Up @@ -67,6 +67,11 @@ impl PyGenerator {
fn gi_yieldfrom(&self, _vm: &VirtualMachine) -> Option<PyObjectRef> {
self.inner.frame().yield_from_target()
}

#[pyclassmethod]
fn __class_getitem__(cls: PyTypeRef, args: PyObjectRef, vm: &VirtualMachine) -> PyGenericAlias {
PyGenericAlias::from_args(cls, args, vm)
}
}

#[pyclass]
Expand Down
9 changes: 7 additions & 2 deletions vm/src/builtins/memory.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{
PositionIterInternal, PyBytes, PyBytesRef, PyInt, PyListRef, PySlice, PyStr, PyStrRef, PyTuple,
PyTupleRef, PyType, PyTypeRef,
PositionIterInternal, PyBytes, PyBytesRef, PyGenericAlias, PyInt, PyListRef, PySlice, PyStr,
PyStrRef, PyTuple, PyTupleRef, PyType, PyTypeRef,
};
use crate::{
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult,
Expand Down Expand Up @@ -550,6 +550,11 @@ impl Py<PyMemoryView> {
Representable
))]
impl PyMemoryView {
#[pyclassmethod]
fn __class_getitem__(cls: PyTypeRef, args: PyObjectRef, vm: &VirtualMachine) -> PyGenericAlias {
PyGenericAlias::from_args(cls, args, vm)
}

#[pymethod]
pub fn release(&self) {
if self.released.compare_exchange(false, true).is_ok() {
Expand Down
8 changes: 7 additions & 1 deletion vm/src/builtins/range.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::{
PyInt, PyIntRef, PySlice, PyTupleRef, PyType, PyTypeRef, builtins_iter, tuple::tuple_hash,
PyGenericAlias, PyInt, PyIntRef, PySlice, PyTupleRef, PyType, PyTypeRef, builtins_iter,
tuple::tuple_hash,
};
use crate::{
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject,
Expand Down Expand Up @@ -183,6 +184,11 @@ pub fn init(context: &Context) {
Representable
))]
impl PyRange {
#[pyclassmethod]
fn __class_getitem__(cls: PyTypeRef, args: PyObjectRef, vm: &VirtualMachine) -> PyGenericAlias {
PyGenericAlias::from_args(cls, args, vm)
}

fn new(cls: PyTypeRef, stop: ArgIndex, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
PyRange {
start: vm.ctx.new_pyref(0),
Expand Down
7 changes: 6 additions & 1 deletion vm/src/builtins/slice.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// sliceobject.{h,c} in CPython
// spell-checker:ignore sliceobject
use super::{PyStrRef, PyTupleRef, PyType, PyTypeRef};
use super::{PyGenericAlias, PyStrRef, PyTupleRef, PyType, PyTypeRef};
use crate::{
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
class::PyClassImpl,
Expand Down Expand Up @@ -30,6 +30,11 @@ impl PyPayload for PySlice {

#[pyclass(with(Comparable, Representable, Hashable))]
impl PySlice {
#[pyclassmethod]
fn __class_getitem__(cls: PyTypeRef, args: PyObjectRef, vm: &VirtualMachine) -> PyGenericAlias {
PyGenericAlias::from_args(cls, args, vm)
}

#[pygetset]
fn start(&self, vm: &VirtualMachine) -> PyObjectRef {
self.start.clone().to_pyobject(vm)
Expand Down
7 changes: 6 additions & 1 deletion vm/src/builtins/staticmethod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{PyStr, PyType, PyTypeRef};
use super::{PyGenericAlias, PyStr, PyType, PyTypeRef};
use crate::{
Context, Py, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
class::PyClassImpl,
Expand Down Expand Up @@ -131,6 +131,11 @@ impl PyStaticMethod {
.set_attr("__isabstractmethod__", value, vm)?;
Ok(())
}

#[pyclassmethod]
fn __class_getitem__(cls: PyTypeRef, args: PyObjectRef, vm: &VirtualMachine) -> PyGenericAlias {
PyGenericAlias::from_args(cls, args, vm)
}
}

impl Callable for PyStaticMethod {
Expand Down
11 changes: 10 additions & 1 deletion vm/src/builtins/union.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::{genericalias, type_};
use crate::{
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
atomic_func,
builtins::{PyFrozenSet, PyStr, PyTuple, PyTupleRef, PyType},
builtins::{PyFrozenSet, PyGenericAlias, PyStr, PyTuple, PyTupleRef, PyType},
class::PyClassImpl,
common::hash,
convert::{ToPyObject, ToPyResult},
Expand Down Expand Up @@ -140,6 +140,15 @@ impl PyUnion {
fn __or__(zelf: PyObjectRef, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
type_::or_(zelf, other, vm)
}

#[pyclassmethod]
fn __class_getitem__(
cls: crate::builtins::PyTypeRef,
args: PyObjectRef,
vm: &VirtualMachine,
) -> PyGenericAlias {
PyGenericAlias::from_args(cls, args, vm)
}
}

pub fn is_unionable(obj: PyObjectRef, vm: &VirtualMachine) -> bool {
Expand Down
17 changes: 15 additions & 2 deletions vm/src/exceptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1203,7 +1203,8 @@ pub(super) mod types {
use crate::{
AsObject, PyObjectRef, PyRef, PyResult, VirtualMachine,
builtins::{
PyInt, PyStrRef, PyTupleRef, PyTypeRef, traceback::PyTracebackRef, tuple::IntoPyTuple,
PyGenericAlias, PyInt, PyStrRef, PyTupleRef, PyTypeRef, traceback::PyTracebackRef,
tuple::IntoPyTuple,
},
convert::ToPyResult,
function::{ArgBytesLike, FuncArgs},
Expand Down Expand Up @@ -1234,10 +1235,22 @@ pub(super) mod types {
#[derive(Debug)]
pub struct PySystemExit {}

#[pyexception(name, base = "PyBaseException", ctx = "base_exception_group", impl)]
#[pyexception(name, base = "PyBaseException", ctx = "base_exception_group")]
#[derive(Debug)]
pub struct PyBaseExceptionGroup {}

#[pyexception]
impl PyBaseExceptionGroup {
#[pyclassmethod]
fn __class_getitem__(
cls: PyTypeRef,
args: PyObjectRef,
vm: &VirtualMachine,
) -> PyGenericAlias {
PyGenericAlias::from_args(cls, args, vm)
}
}

#[pyexception(name, base = "PyBaseExceptionGroup", ctx = "exception_group", impl)]
#[derive(Debug)]
pub struct PyExceptionGroup {}
Expand Down
11 changes: 10 additions & 1 deletion vm/src/stdlib/functools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub(crate) use _functools::make_module;
mod _functools {
use crate::{
Py, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
builtins::{PyDict, PyTuple, PyTypeRef},
builtins::{PyDict, PyGenericAlias, PyTuple, PyTypeRef},
common::lock::PyRwLock,
function::{FuncArgs, KwArgs, OptionalArg},
object::AsObject,
Expand Down Expand Up @@ -190,6 +190,15 @@ mod _functools {

Ok(())
}

#[pyclassmethod]
fn __class_getitem__(
cls: PyTypeRef,
args: PyObjectRef,
vm: &VirtualMachine,
) -> PyGenericAlias {
PyGenericAlias::from_args(cls, args, vm)
}
}

impl Constructor for PyPartial {
Expand Down
Loading