mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-02 19:39:49 +09:00
Remove type clone functions from vm.ctx
which misleads to clone types when we don't need.
This commit is contained in:
@@ -248,7 +248,7 @@ pub(crate) fn impl_pystruct_sequence(
|
||||
ctx: &::rustpython_vm::pyobject::PyContext,
|
||||
) -> ::rustpython_vm::obj::objtype::PyClassRef {
|
||||
use crate::pyobject::PyClassDef;
|
||||
Self::make_class_with_base(ctx, Self::NAME, &ctx.tuple_type())
|
||||
Self::make_class_with_base(ctx, Self::NAME, &ctx.types.tuple_type)
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -472,7 +472,7 @@ impl ToTokens for GetSetNursery {
|
||||
#name,
|
||||
::rustpython_vm::pyobject::PyObject::new(
|
||||
::rustpython_vm::obj::objgetset::PyGetSet::#constructor(#name.into(), &Self::#getter #setter),
|
||||
ctx.getset_type(), None)
|
||||
ctx.types.getset_type.clone(), None)
|
||||
);
|
||||
}
|
||||
}));
|
||||
|
||||
@@ -41,7 +41,7 @@ macro_rules! add_python_function {
|
||||
// inserts the first function found in the module into the provided scope.
|
||||
$scope.globals.set_item(
|
||||
def.obj_name.as_str(),
|
||||
$vm.context().new_pyfunction(
|
||||
$vm.ctx.new_pyfunction(
|
||||
vm::obj::objcode::PyCode::new(*def.clone()).into_ref(&$vm),
|
||||
$scope.clone(),
|
||||
None,
|
||||
@@ -66,9 +66,7 @@ fn main() -> vm::pyobject::PyResult<()> {
|
||||
let scope: vm::scope::Scope = vm.new_scope_with_builtins();
|
||||
|
||||
// typing `quit()` is too long, let's make `on(False)` work instead.
|
||||
scope
|
||||
.globals
|
||||
.set_item("on", vm.context().new_function(on), &vm)?;
|
||||
scope.globals.set_item("on", vm.ctx.new_function(on), &vm)?;
|
||||
|
||||
// let's include a fibonacci function, but let's be lazy and write it in Python
|
||||
add_python_function!(
|
||||
|
||||
@@ -784,7 +784,7 @@ mod decl {
|
||||
let mut metaclass = if let Some(metaclass) = kwargs.pop_kwarg("metaclass") {
|
||||
PyClassRef::try_from_object(vm, metaclass)?
|
||||
} else {
|
||||
vm.get_type()
|
||||
vm.ctx.types.type_type.clone()
|
||||
};
|
||||
|
||||
for base in bases.clone() {
|
||||
@@ -865,31 +865,31 @@ pub fn make_module(vm: &VirtualMachine, module: PyObjectRef) {
|
||||
//set __name__ fixes: https://github.com/RustPython/RustPython/issues/146
|
||||
"__name__" => ctx.new_str(String::from("__main__")),
|
||||
|
||||
"bool" => ctx.bool_type(),
|
||||
"bytearray" => ctx.bytearray_type(),
|
||||
"bytes" => ctx.bytes_type(),
|
||||
"classmethod" => ctx.classmethod_type(),
|
||||
"complex" => ctx.complex_type(),
|
||||
"dict" => ctx.dict_type(),
|
||||
"enumerate" => ctx.enumerate_type(),
|
||||
"float" => ctx.float_type(),
|
||||
"frozenset" => ctx.frozenset_type(),
|
||||
"filter" => ctx.filter_type(),
|
||||
"int" => ctx.int_type(),
|
||||
"list" => ctx.list_type(),
|
||||
"map" => ctx.map_type(),
|
||||
"memoryview" => ctx.memoryview_type(),
|
||||
"object" => ctx.object(),
|
||||
"property" => ctx.property_type(),
|
||||
"range" => ctx.range_type(),
|
||||
"set" => ctx.set_type(),
|
||||
"slice" => ctx.slice_type(),
|
||||
"staticmethod" => ctx.staticmethod_type(),
|
||||
"str" => ctx.str_type(),
|
||||
"super" => ctx.super_type(),
|
||||
"tuple" => ctx.tuple_type(),
|
||||
"type" => ctx.type_type(),
|
||||
"zip" => ctx.zip_type(),
|
||||
"bool" => ctx.types.bool_type.clone(),
|
||||
"bytearray" => ctx.types.bytearray_type.clone(),
|
||||
"bytes" => ctx.types.bytes_type.clone(),
|
||||
"classmethod" => ctx.types.classmethod_type.clone(),
|
||||
"complex" => ctx.types.complex_type.clone(),
|
||||
"dict" => ctx.types.dict_type.clone(),
|
||||
"enumerate" => ctx.types.enumerate_type.clone(),
|
||||
"float" => ctx.types.float_type.clone(),
|
||||
"frozenset" => ctx.types.frozenset_type.clone(),
|
||||
"filter" => ctx.types.filter_type.clone(),
|
||||
"int" => ctx.types.int_type.clone(),
|
||||
"list" => ctx.types.list_type.clone(),
|
||||
"map" => ctx.types.map_type.clone(),
|
||||
"memoryview" => ctx.types.memoryview_type.clone(),
|
||||
"object" => ctx.types.object_type.clone(),
|
||||
"property" => ctx.types.property_type.clone(),
|
||||
"range" => ctx.types.range_type.clone(),
|
||||
"set" => ctx.types.set_type.clone(),
|
||||
"slice" => ctx.types.slice_type.clone(),
|
||||
"staticmethod" => ctx.types.staticmethod_type.clone(),
|
||||
"str" => ctx.types.str_type.clone(),
|
||||
"super" => ctx.types.super_type.clone(),
|
||||
"tuple" => ctx.types.tuple_type.clone(),
|
||||
"type" => ctx.types.type_type.clone(),
|
||||
"zip" => ctx.types.zip_type.clone(),
|
||||
|
||||
// Constants
|
||||
"NotImplemented" => ctx.not_implemented(),
|
||||
|
||||
@@ -100,7 +100,7 @@ pub struct Frame {
|
||||
|
||||
impl PyValue for Frame {
|
||||
fn class(vm: &VirtualMachine) -> PyClassRef {
|
||||
vm.ctx.frame_type()
|
||||
vm.ctx.types.frame_type.clone()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ pub struct PyBuiltinFunction {
|
||||
|
||||
impl PyValue for PyBuiltinFunction {
|
||||
fn class(vm: &VirtualMachine) -> PyClassRef {
|
||||
vm.ctx.builtin_function_or_method_type()
|
||||
vm.ctx.types.builtin_function_or_method_type.clone()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,7 +77,7 @@ pub struct PyBuiltinMethod {
|
||||
|
||||
impl PyValue for PyBuiltinMethod {
|
||||
fn class(vm: &VirtualMachine) -> PyClassRef {
|
||||
vm.ctx.method_descriptor_type()
|
||||
vm.ctx.types.method_descriptor_type.clone()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -77,7 +77,7 @@ impl From<Vec<u8>> for PyByteArray {
|
||||
|
||||
impl PyValue for PyByteArray {
|
||||
fn class(vm: &VirtualMachine) -> PyClassRef {
|
||||
vm.ctx.bytearray_type()
|
||||
vm.ctx.types.bytearray_type.clone()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -615,7 +615,7 @@ pub struct PyByteArrayIterator {
|
||||
|
||||
impl PyValue for PyByteArrayIterator {
|
||||
fn class(vm: &VirtualMachine) -> PyClassRef {
|
||||
vm.ctx.bytearray_iterator_type()
|
||||
vm.ctx.types.bytearray_iterator_type.clone()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -72,7 +72,7 @@ impl Deref for PyBytes {
|
||||
|
||||
impl PyValue for PyBytes {
|
||||
fn class(vm: &VirtualMachine) -> PyClassRef {
|
||||
vm.ctx.bytes_type()
|
||||
vm.ctx.types.bytes_type.clone()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -497,7 +497,7 @@ pub struct PyBytesIterator {
|
||||
|
||||
impl PyValue for PyBytesIterator {
|
||||
fn class(vm: &VirtualMachine) -> PyClassRef {
|
||||
vm.ctx.bytes_iterator_type()
|
||||
vm.ctx.types.bytes_iterator_type.clone()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -41,7 +41,7 @@ impl From<PyObjectRef> for PyClassMethod {
|
||||
|
||||
impl PyValue for PyClassMethod {
|
||||
fn class(vm: &VirtualMachine) -> PyClassRef {
|
||||
vm.ctx.classmethod_type()
|
||||
vm.ctx.types.classmethod_type.clone()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ impl fmt::Debug for PyCode {
|
||||
|
||||
impl PyValue for PyCode {
|
||||
fn class(vm: &VirtualMachine) -> PyClassRef {
|
||||
vm.ctx.code_type()
|
||||
vm.ctx.types.code_type.clone()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ type PyComplexRef = PyRef<PyComplex>;
|
||||
|
||||
impl PyValue for PyComplex {
|
||||
fn class(vm: &VirtualMachine) -> PyClassRef {
|
||||
vm.ctx.complex_type()
|
||||
vm.ctx.types.complex_type.clone()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -34,7 +34,7 @@ impl fmt::Debug for PyDict {
|
||||
|
||||
impl PyValue for PyDict {
|
||||
fn class(vm: &VirtualMachine) -> PyClassRef {
|
||||
vm.ctx.dict_type()
|
||||
vm.ctx.types.dict_type.clone()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -448,7 +448,7 @@ impl PyDictRef {
|
||||
// and prevent the creation of the KeyError exception.
|
||||
// Also note, that we prevent the creation of a full PyString object
|
||||
// if we lookup local names (which happens all of the time).
|
||||
if self.lease_class().is(&vm.ctx.dict_type()) {
|
||||
if self.lease_class().is(&vm.ctx.types.dict_type) {
|
||||
// We can take the short path here!
|
||||
match self.inner_getitem_option(key, vm) {
|
||||
Err(exc) => {
|
||||
@@ -486,7 +486,7 @@ where
|
||||
}
|
||||
|
||||
fn set_item(&self, key: T, value: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
if self.lease_class().is(&vm.ctx.dict_type()) {
|
||||
if self.lease_class().is(&vm.ctx.types.dict_type) {
|
||||
self.inner_setitem_fast(key, value, vm)
|
||||
.map(|_| vm.ctx.none())
|
||||
} else {
|
||||
@@ -496,7 +496,7 @@ where
|
||||
}
|
||||
|
||||
fn del_item(&self, key: T, vm: &VirtualMachine) -> PyResult {
|
||||
if self.lease_class().is(&vm.ctx.dict_type()) {
|
||||
if self.lease_class().is(&vm.ctx.types.dict_type) {
|
||||
self.entries.delete(vm, key).map(|_| vm.ctx.none())
|
||||
} else {
|
||||
// Fall back to slow path if we are in a dict subclass:
|
||||
|
||||
@@ -3,7 +3,7 @@ use crate::pyobject::{PyClassImpl, PyContext, PyResult, PyValue};
|
||||
use crate::vm::VirtualMachine;
|
||||
|
||||
pub(crate) fn init(context: &PyContext) {
|
||||
PyEllipsis::extend_class(context, &context.ellipsis_type());
|
||||
PyEllipsis::extend_class(context, &context.types.ellipsis_type);
|
||||
}
|
||||
|
||||
#[pyclass(module = false, name = "EllipsisType")]
|
||||
@@ -12,7 +12,7 @@ pub struct PyEllipsis;
|
||||
|
||||
impl PyValue for PyEllipsis {
|
||||
fn class(vm: &VirtualMachine) -> PyClassRef {
|
||||
vm.ctx.ellipsis_type()
|
||||
vm.ctx.types.ellipsis_type.clone()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ impl PyValue for PyEllipsis {
|
||||
impl PyEllipsis {
|
||||
#[pyslot]
|
||||
fn tp_new(cls: PyClassRef, vm: &VirtualMachine) -> PyResult {
|
||||
if issubclass(&cls, &vm.ctx.ellipsis_type()) {
|
||||
if issubclass(&cls, &vm.ctx.types.ellipsis_type) {
|
||||
Ok(vm.ctx.ellipsis())
|
||||
} else {
|
||||
Err(vm.new_type_error(format!(
|
||||
|
||||
@@ -21,7 +21,7 @@ type PyEnumerateRef = PyRef<PyEnumerate>;
|
||||
|
||||
impl PyValue for PyEnumerate {
|
||||
fn class(vm: &VirtualMachine) -> PyClassRef {
|
||||
vm.ctx.enumerate_type()
|
||||
vm.ctx.types.enumerate_type.clone()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ pub struct PyFilter {
|
||||
|
||||
impl PyValue for PyFilter {
|
||||
fn class(vm: &VirtualMachine) -> PyClassRef {
|
||||
vm.ctx.filter_type()
|
||||
vm.ctx.types.filter_type.clone()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ impl PyFloat {
|
||||
|
||||
impl PyValue for PyFloat {
|
||||
fn class(vm: &VirtualMachine) -> PyClassRef {
|
||||
vm.ctx.float_type()
|
||||
vm.ctx.types.float_type.clone()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -254,7 +254,7 @@ impl PyFunction {
|
||||
|
||||
impl PyValue for PyFunction {
|
||||
fn class(vm: &VirtualMachine) -> PyClassRef {
|
||||
vm.ctx.function_type()
|
||||
vm.ctx.types.function_type.clone()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -344,7 +344,7 @@ impl PyBoundMethod {
|
||||
|
||||
impl PyValue for PyBoundMethod {
|
||||
fn class(vm: &VirtualMachine) -> PyClassRef {
|
||||
vm.ctx.bound_method_type()
|
||||
vm.ctx.types.bound_method_type.clone()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ pub struct PyGenerator {
|
||||
|
||||
impl PyValue for PyGenerator {
|
||||
fn class(vm: &VirtualMachine) -> PyClassRef {
|
||||
vm.ctx.generator_type()
|
||||
vm.ctx.types.generator_type.clone()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -174,7 +174,7 @@ impl std::fmt::Debug for PyGetSet {
|
||||
|
||||
impl PyValue for PyGetSet {
|
||||
fn class(vm: &VirtualMachine) -> PyClassRef {
|
||||
vm.ctx.getset_type()
|
||||
vm.ctx.types.getset_type.clone()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -73,7 +73,7 @@ where
|
||||
|
||||
impl PyValue for PyInt {
|
||||
fn class(vm: &VirtualMachine) -> PyClassRef {
|
||||
vm.ctx.int_type()
|
||||
vm.ctx.types.int_type.clone()
|
||||
}
|
||||
|
||||
fn into_simple_object(self, vm: &VirtualMachine) -> PyObjectRef {
|
||||
@@ -239,7 +239,7 @@ impl PyInt {
|
||||
where
|
||||
T: Into<BigInt> + ToPrimitive,
|
||||
{
|
||||
if cls.is(&vm.ctx.int_type()) {
|
||||
if cls.is(&vm.ctx.types.int_type) {
|
||||
Ok(vm.ctx.new_int(value).downcast().unwrap())
|
||||
} else {
|
||||
PyInt::from(value).into_ref_with_type(vm, cls)
|
||||
|
||||
@@ -159,7 +159,7 @@ pub struct PySequenceIterator {
|
||||
|
||||
impl PyValue for PySequenceIterator {
|
||||
fn class(vm: &VirtualMachine) -> PyClassRef {
|
||||
vm.ctx.iter_type()
|
||||
vm.ctx.types.iter_type.clone()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -56,7 +56,7 @@ impl FromIterator<PyObjectRef> for PyList {
|
||||
|
||||
impl PyValue for PyList {
|
||||
fn class(vm: &VirtualMachine) -> PyClassRef {
|
||||
vm.ctx.list_type()
|
||||
vm.ctx.types.list_type.clone()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -834,7 +834,7 @@ pub struct PyListIterator {
|
||||
|
||||
impl PyValue for PyListIterator {
|
||||
fn class(vm: &VirtualMachine) -> PyClassRef {
|
||||
vm.ctx.list_iterator_type()
|
||||
vm.ctx.types.list_iterator_type.clone()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -873,7 +873,7 @@ pub struct PyListReverseIterator {
|
||||
|
||||
impl PyValue for PyListReverseIterator {
|
||||
fn class(vm: &VirtualMachine) -> PyClassRef {
|
||||
vm.ctx.list_reverseiterator_type()
|
||||
vm.ctx.types.list_reverseiterator_type.clone()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ type PyMapRef = PyRef<PyMap>;
|
||||
|
||||
impl PyValue for PyMap {
|
||||
fn class(vm: &VirtualMachine) -> PyClassRef {
|
||||
vm.ctx.map_type()
|
||||
vm.ctx.types.map_type.clone()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -71,7 +71,7 @@ impl PyMemoryView {
|
||||
|
||||
impl PyValue for PyMemoryView {
|
||||
fn class(vm: &VirtualMachine) -> PyClassRef {
|
||||
vm.ctx.memoryview_type()
|
||||
vm.ctx.types.memoryview_type.clone()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ pub type PyModuleRef = PyRef<PyModule>;
|
||||
|
||||
impl PyValue for PyModule {
|
||||
fn class(vm: &VirtualMachine) -> PyClassRef {
|
||||
vm.ctx.module_type()
|
||||
vm.ctx.types.module_type.clone()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ pub struct PyNamespace;
|
||||
|
||||
impl PyValue for PyNamespace {
|
||||
fn class(vm: &VirtualMachine) -> PyClassRef {
|
||||
vm.ctx.namespace_type()
|
||||
vm.ctx.types.namespace_type.clone()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ pub struct PyBaseObject;
|
||||
|
||||
impl PyValue for PyBaseObject {
|
||||
fn class(vm: &VirtualMachine) -> PyClassRef {
|
||||
vm.ctx.object()
|
||||
vm.ctx.types.object_type.clone()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ impl PyBaseObject {
|
||||
fn tp_new(vm: &VirtualMachine, mut args: PyFuncArgs) -> PyResult {
|
||||
// more or less __new__ operator
|
||||
let cls = PyClassRef::try_from_object(vm, args.shift())?;
|
||||
let dict = if cls.is(&vm.ctx.object()) {
|
||||
let dict = if cls.is(&vm.ctx.types.object_type) {
|
||||
None
|
||||
} else {
|
||||
Some(vm.ctx.new_dict())
|
||||
|
||||
@@ -54,7 +54,7 @@ pub struct PyProperty {
|
||||
|
||||
impl PyValue for PyProperty {
|
||||
fn class(vm: &VirtualMachine) -> PyClassRef {
|
||||
vm.ctx.property_type()
|
||||
vm.ctx.types.property_type.clone()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -35,7 +35,7 @@ pub struct PyRange {
|
||||
|
||||
impl PyValue for PyRange {
|
||||
fn class(vm: &VirtualMachine) -> PyClassRef {
|
||||
vm.ctx.range_type()
|
||||
vm.ctx.types.range_type.clone()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -309,7 +309,7 @@ impl PyRange {
|
||||
.map(|x| x.as_object().clone())
|
||||
.collect();
|
||||
let range_paramters_tuple = PyTuple::from(range_paramters);
|
||||
(vm.ctx.range_type(), range_paramters_tuple)
|
||||
(vm.ctx.types.range_type.clone(), range_paramters_tuple)
|
||||
}
|
||||
|
||||
#[pymethod(name = "index")]
|
||||
@@ -409,7 +409,7 @@ pub struct PyRangeIterator {
|
||||
|
||||
impl PyValue for PyRangeIterator {
|
||||
fn class(vm: &VirtualMachine) -> PyClassRef {
|
||||
vm.ctx.range_iterator_type()
|
||||
vm.ctx.types.range_iterator_type.clone()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -55,13 +55,13 @@ impl fmt::Debug for PyFrozenSet {
|
||||
|
||||
impl PyValue for PySet {
|
||||
fn class(vm: &VirtualMachine) -> PyClassRef {
|
||||
vm.ctx.set_type()
|
||||
vm.ctx.types.set_type.clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl PyValue for PyFrozenSet {
|
||||
fn class(vm: &VirtualMachine) -> PyClassRef {
|
||||
vm.ctx.frozenset_type()
|
||||
vm.ctx.types.frozenset_type.clone()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -877,7 +877,7 @@ impl PySetIterator {
|
||||
|
||||
impl PyValue for PySetIterator {
|
||||
fn class(vm: &VirtualMachine) -> PyClassRef {
|
||||
vm.ctx.set_iterator_type()
|
||||
vm.ctx.types.set_iterator_type.clone()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ pub struct PySlice {
|
||||
|
||||
impl PyValue for PySlice {
|
||||
fn class(vm: &VirtualMachine) -> PyClassRef {
|
||||
vm.ctx.slice_type()
|
||||
vm.ctx.types.slice_type.clone()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ pub type PyStaticMethodRef = PyRef<PyStaticMethod>;
|
||||
|
||||
impl PyValue for PyStaticMethod {
|
||||
fn class(vm: &VirtualMachine) -> PyClassRef {
|
||||
vm.ctx.staticmethod_type()
|
||||
vm.ctx.types.staticmethod_type.clone()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -111,7 +111,7 @@ pub struct PyStringIterator {
|
||||
|
||||
impl PyValue for PyStringIterator {
|
||||
fn class(vm: &VirtualMachine) -> PyClassRef {
|
||||
vm.ctx.str_iterator_type()
|
||||
vm.ctx.types.str_iterator_type.clone()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -144,7 +144,7 @@ pub struct PyStringReverseIterator {
|
||||
|
||||
impl PyValue for PyStringReverseIterator {
|
||||
fn class(vm: &VirtualMachine) -> PyClassRef {
|
||||
vm.ctx.str_reverseiterator_type()
|
||||
vm.ctx.types.str_reverseiterator_type.clone()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -997,7 +997,7 @@ impl PyString {
|
||||
none_str: OptionalArg<PyStringRef>,
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult {
|
||||
let new_dict = vm.context().new_dict();
|
||||
let new_dict = vm.ctx.new_dict();
|
||||
if let OptionalArg::Present(to_str) = to_str {
|
||||
match dict_or_str.downcast::<PyString>() {
|
||||
Ok(from_str) => {
|
||||
@@ -1109,7 +1109,7 @@ pub(crate) fn encode_string(
|
||||
|
||||
impl PyValue for PyString {
|
||||
fn class(vm: &VirtualMachine) -> PyClassRef {
|
||||
vm.ctx.str_type()
|
||||
vm.ctx.types.str_type.clone()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1304,7 +1304,7 @@ mod tests {
|
||||
fn str_maketrans_and_translate() {
|
||||
let vm: VirtualMachine = Default::default();
|
||||
|
||||
let table = vm.context().new_dict();
|
||||
let table = vm.ctx.new_dict();
|
||||
table.set_item("a", vm.ctx.new_str("🎅"), &vm).unwrap();
|
||||
table.set_item("b", vm.get_none(), &vm).unwrap();
|
||||
table.set_item("c", vm.ctx.new_str("xda"), &vm).unwrap();
|
||||
|
||||
@@ -30,7 +30,7 @@ pub struct PySuper {
|
||||
|
||||
impl PyValue for PySuper {
|
||||
fn class(vm: &VirtualMachine) -> PyClassRef {
|
||||
vm.ctx.super_type()
|
||||
vm.ctx.types.super_type.clone()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ pub type PyTracebackRef = PyRef<PyTraceback>;
|
||||
|
||||
impl PyValue for PyTraceback {
|
||||
fn class(vm: &VirtualMachine) -> PyClassRef {
|
||||
vm.ctx.traceback_type()
|
||||
vm.ctx.types.traceback_type.clone()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -46,7 +46,7 @@ impl<'a> BorrowValue<'a> for PyTuple {
|
||||
|
||||
impl PyValue for PyTuple {
|
||||
fn class(vm: &VirtualMachine) -> PyClassRef {
|
||||
vm.ctx.tuple_type()
|
||||
vm.ctx.types.tuple_type.clone()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -264,7 +264,7 @@ pub struct PyTupleIterator {
|
||||
|
||||
impl PyValue for PyTupleIterator {
|
||||
fn class(vm: &VirtualMachine) -> PyClassRef {
|
||||
vm.ctx.tuple_iterator_type()
|
||||
vm.ctx.types.tuple_iterator_type.clone()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -52,7 +52,7 @@ pub type PyClassRef = PyRef<PyClass>;
|
||||
|
||||
impl PyValue for PyClass {
|
||||
fn class(vm: &VirtualMachine) -> PyClassRef {
|
||||
vm.ctx.type_type()
|
||||
vm.ctx.types.type_type.clone()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -306,7 +306,7 @@ impl PyClassRef {
|
||||
|
||||
let bases: Vec<PyClassRef> = bases.iter(vm)?.collect::<Result<Vec<_>, _>>()?;
|
||||
let (metatype, base, bases) = if bases.is_empty() {
|
||||
let base = vm.ctx.object();
|
||||
let base = vm.ctx.types.object_type.clone();
|
||||
(metatype, base.clone(), vec![base])
|
||||
} else {
|
||||
// TODO
|
||||
@@ -340,13 +340,13 @@ impl PyClassRef {
|
||||
|
||||
let mut attributes = dict.to_attributes();
|
||||
if let Some(f) = attributes.get_mut("__new__") {
|
||||
if f.class().is(&vm.ctx.function_type()) {
|
||||
if f.class().is(&vm.ctx.types.function_type) {
|
||||
*f = PyStaticMethod::from(f.clone()).into_ref(vm).into_object();
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(f) = attributes.get_mut("__init_subclass__") {
|
||||
if f.class().is(&vm.ctx.function_type()) {
|
||||
if f.class().is(&vm.ctx.types.function_type) {
|
||||
*f = PyClassMethod::from(f.clone()).into_ref(vm).into_object();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ pub struct PyWeakProxy {
|
||||
|
||||
impl PyValue for PyWeakProxy {
|
||||
fn class(vm: &VirtualMachine) -> PyClassRef {
|
||||
vm.ctx.weakproxy_type()
|
||||
vm.ctx.types.weakproxy_type.clone()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ impl PyWeak {
|
||||
|
||||
impl PyValue for PyWeak {
|
||||
fn class(vm: &VirtualMachine) -> PyClassRef {
|
||||
vm.ctx.weakref_type()
|
||||
vm.ctx.types.weakref_type.clone()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ pub struct PyZip {
|
||||
|
||||
impl PyValue for PyZip {
|
||||
fn class(vm: &VirtualMachine) -> PyClassRef {
|
||||
vm.ctx.zip_type()
|
||||
vm.ctx.types.zip_type.clone()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -173,194 +173,6 @@ impl PyContext {
|
||||
context
|
||||
}
|
||||
|
||||
pub fn bytearray_type(&self) -> PyClassRef {
|
||||
self.types.bytearray_type.clone()
|
||||
}
|
||||
|
||||
pub fn bytearray_iterator_type(&self) -> PyClassRef {
|
||||
self.types.bytearray_iterator_type.clone()
|
||||
}
|
||||
|
||||
pub fn bytes_type(&self) -> PyClassRef {
|
||||
self.types.bytes_type.clone()
|
||||
}
|
||||
|
||||
pub fn bytes_iterator_type(&self) -> PyClassRef {
|
||||
self.types.bytes_iterator_type.clone()
|
||||
}
|
||||
|
||||
pub fn code_type(&self) -> PyClassRef {
|
||||
self.types.code_type.clone()
|
||||
}
|
||||
|
||||
pub fn complex_type(&self) -> PyClassRef {
|
||||
self.types.complex_type.clone()
|
||||
}
|
||||
|
||||
pub fn dict_type(&self) -> PyClassRef {
|
||||
self.types.dict_type.clone()
|
||||
}
|
||||
|
||||
pub fn float_type(&self) -> PyClassRef {
|
||||
self.types.float_type.clone()
|
||||
}
|
||||
|
||||
pub fn frame_type(&self) -> PyClassRef {
|
||||
self.types.frame_type.clone()
|
||||
}
|
||||
|
||||
pub fn int_type(&self) -> PyClassRef {
|
||||
self.types.int_type.clone()
|
||||
}
|
||||
|
||||
pub fn list_type(&self) -> PyClassRef {
|
||||
self.types.list_type.clone()
|
||||
}
|
||||
|
||||
pub fn list_iterator_type(&self) -> PyClassRef {
|
||||
self.types.list_iterator_type.clone()
|
||||
}
|
||||
|
||||
pub fn list_reverseiterator_type(&self) -> PyClassRef {
|
||||
self.types.list_reverseiterator_type.clone()
|
||||
}
|
||||
|
||||
pub fn str_iterator_type(&self) -> PyClassRef {
|
||||
self.types.str_iterator_type.clone()
|
||||
}
|
||||
|
||||
pub fn str_reverseiterator_type(&self) -> PyClassRef {
|
||||
self.types.str_reverseiterator_type.clone()
|
||||
}
|
||||
|
||||
pub fn module_type(&self) -> PyClassRef {
|
||||
self.types.module_type.clone()
|
||||
}
|
||||
|
||||
pub fn namespace_type(&self) -> PyClassRef {
|
||||
self.types.namespace_type.clone()
|
||||
}
|
||||
|
||||
pub fn set_type(&self) -> PyClassRef {
|
||||
self.types.set_type.clone()
|
||||
}
|
||||
|
||||
pub fn set_iterator_type(&self) -> PyClassRef {
|
||||
self.types.set_iterator_type.clone()
|
||||
}
|
||||
|
||||
pub fn range_type(&self) -> PyClassRef {
|
||||
self.types.range_type.clone()
|
||||
}
|
||||
|
||||
pub fn range_iterator_type(&self) -> PyClassRef {
|
||||
self.types.range_iterator_type.clone()
|
||||
}
|
||||
|
||||
pub fn slice_type(&self) -> PyClassRef {
|
||||
self.types.slice_type.clone()
|
||||
}
|
||||
|
||||
pub fn frozenset_type(&self) -> PyClassRef {
|
||||
self.types.frozenset_type.clone()
|
||||
}
|
||||
|
||||
pub fn bool_type(&self) -> PyClassRef {
|
||||
self.types.bool_type.clone()
|
||||
}
|
||||
|
||||
pub fn memoryview_type(&self) -> PyClassRef {
|
||||
self.types.memoryview_type.clone()
|
||||
}
|
||||
|
||||
pub fn tuple_type(&self) -> PyClassRef {
|
||||
self.types.tuple_type.clone()
|
||||
}
|
||||
|
||||
pub fn tuple_iterator_type(&self) -> PyClassRef {
|
||||
self.types.tuple_iterator_type.clone()
|
||||
}
|
||||
|
||||
pub fn iter_type(&self) -> PyClassRef {
|
||||
self.types.iter_type.clone()
|
||||
}
|
||||
|
||||
pub fn enumerate_type(&self) -> PyClassRef {
|
||||
self.types.enumerate_type.clone()
|
||||
}
|
||||
|
||||
pub fn filter_type(&self) -> PyClassRef {
|
||||
self.types.filter_type.clone()
|
||||
}
|
||||
|
||||
pub fn map_type(&self) -> PyClassRef {
|
||||
self.types.map_type.clone()
|
||||
}
|
||||
|
||||
pub fn zip_type(&self) -> PyClassRef {
|
||||
self.types.zip_type.clone()
|
||||
}
|
||||
|
||||
pub fn str_type(&self) -> PyClassRef {
|
||||
self.types.str_type.clone()
|
||||
}
|
||||
|
||||
pub fn super_type(&self) -> PyClassRef {
|
||||
self.types.super_type.clone()
|
||||
}
|
||||
|
||||
pub fn function_type(&self) -> PyClassRef {
|
||||
self.types.function_type.clone()
|
||||
}
|
||||
|
||||
pub fn builtin_function_or_method_type(&self) -> PyClassRef {
|
||||
self.types.builtin_function_or_method_type.clone()
|
||||
}
|
||||
|
||||
pub fn method_descriptor_type(&self) -> PyClassRef {
|
||||
self.types.method_descriptor_type.clone()
|
||||
}
|
||||
|
||||
pub fn property_type(&self) -> PyClassRef {
|
||||
self.types.property_type.clone()
|
||||
}
|
||||
|
||||
pub fn getset_type(&self) -> PyClassRef {
|
||||
self.types.getset_type.clone()
|
||||
}
|
||||
|
||||
pub fn classmethod_type(&self) -> PyClassRef {
|
||||
self.types.classmethod_type.clone()
|
||||
}
|
||||
|
||||
pub fn staticmethod_type(&self) -> PyClassRef {
|
||||
self.types.staticmethod_type.clone()
|
||||
}
|
||||
|
||||
pub fn generator_type(&self) -> PyClassRef {
|
||||
self.types.generator_type.clone()
|
||||
}
|
||||
|
||||
pub fn bound_method_type(&self) -> PyClassRef {
|
||||
self.types.bound_method_type.clone()
|
||||
}
|
||||
|
||||
pub fn weakref_type(&self) -> PyClassRef {
|
||||
self.types.weakref_type.clone()
|
||||
}
|
||||
|
||||
pub fn weakproxy_type(&self) -> PyClassRef {
|
||||
self.types.weakproxy_type.clone()
|
||||
}
|
||||
|
||||
pub fn traceback_type(&self) -> PyClassRef {
|
||||
self.types.traceback_type.clone()
|
||||
}
|
||||
|
||||
pub fn type_type(&self) -> PyClassRef {
|
||||
self.types.type_type.clone()
|
||||
}
|
||||
|
||||
pub fn none(&self) -> PyObjectRef {
|
||||
self.none.clone().into_object()
|
||||
}
|
||||
@@ -369,18 +181,10 @@ impl PyContext {
|
||||
self.ellipsis.clone().into_object()
|
||||
}
|
||||
|
||||
pub fn ellipsis_type(&self) -> PyClassRef {
|
||||
self.types.ellipsis_type.clone()
|
||||
}
|
||||
|
||||
pub fn not_implemented(&self) -> PyObjectRef {
|
||||
self.not_implemented.clone().into_object()
|
||||
}
|
||||
|
||||
pub fn object(&self) -> PyClassRef {
|
||||
self.types.object_type.clone()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn new_int<T: Into<BigInt> + ToPrimitive>(&self, i: T) -> PyObjectRef {
|
||||
if let Some(i) = i.to_i32() {
|
||||
@@ -389,7 +193,7 @@ impl PyContext {
|
||||
return self.int_cache_pool[inner_idx].clone();
|
||||
}
|
||||
}
|
||||
PyObject::new(PyInt::from(i), self.int_type(), None)
|
||||
PyObject::new(PyInt::from(i), self.types.int_type.clone(), None)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
@@ -400,32 +204,40 @@ impl PyContext {
|
||||
return self.int_cache_pool[inner_idx].clone();
|
||||
}
|
||||
}
|
||||
PyObject::new(PyInt::from(i.clone()), self.int_type(), None)
|
||||
PyObject::new(PyInt::from(i.clone()), self.types.int_type.clone(), None)
|
||||
}
|
||||
|
||||
pub fn new_float(&self, value: f64) -> PyObjectRef {
|
||||
PyObject::new(PyFloat::from(value), self.float_type(), None)
|
||||
PyObject::new(PyFloat::from(value), self.types.float_type.clone(), None)
|
||||
}
|
||||
|
||||
pub fn new_complex(&self, value: Complex64) -> PyObjectRef {
|
||||
PyObject::new(PyComplex::from(value), self.complex_type(), None)
|
||||
PyObject::new(
|
||||
PyComplex::from(value),
|
||||
self.types.complex_type.clone(),
|
||||
None,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn new_str<S>(&self, s: S) -> PyObjectRef
|
||||
where
|
||||
objstr::PyString: std::convert::From<S>,
|
||||
{
|
||||
PyObject::new(objstr::PyString::from(s), self.str_type(), None)
|
||||
PyObject::new(objstr::PyString::from(s), self.types.str_type.clone(), None)
|
||||
}
|
||||
|
||||
pub fn new_bytes(&self, data: Vec<u8>) -> PyObjectRef {
|
||||
PyObject::new(objbytes::PyBytes::from(data), self.bytes_type(), None)
|
||||
PyObject::new(
|
||||
objbytes::PyBytes::from(data),
|
||||
self.types.bytes_type.clone(),
|
||||
None,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn new_bytearray(&self, data: Vec<u8>) -> PyObjectRef {
|
||||
PyObject::new(
|
||||
objbytearray::PyByteArray::from(data),
|
||||
self.bytearray_type(),
|
||||
self.types.bytearray_type.clone(),
|
||||
None,
|
||||
)
|
||||
}
|
||||
@@ -444,32 +256,36 @@ impl PyContext {
|
||||
if elements.is_empty() {
|
||||
self.empty_tuple.clone().into_object()
|
||||
} else {
|
||||
PyObject::new(PyTuple::from(elements), self.tuple_type(), None)
|
||||
PyObject::new(PyTuple::from(elements), self.types.tuple_type.clone(), None)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_list(&self, elements: Vec<PyObjectRef>) -> PyObjectRef {
|
||||
PyObject::new(PyList::from(elements), self.list_type(), None)
|
||||
PyObject::new(PyList::from(elements), self.types.list_type.clone(), None)
|
||||
}
|
||||
|
||||
pub fn new_set(&self) -> PyObjectRef {
|
||||
// Initialized empty, as calling __hash__ is required for adding each object to the set
|
||||
// which requires a VM context - this is done in the objset code itself.
|
||||
PyObject::new(PySet::default(), self.set_type(), None)
|
||||
PyObject::new(PySet::default(), self.types.set_type.clone(), None)
|
||||
}
|
||||
|
||||
pub fn new_dict(&self) -> PyDictRef {
|
||||
PyObject::new(PyDict::default(), self.dict_type(), None)
|
||||
PyObject::new(PyDict::default(), self.types.dict_type.clone(), None)
|
||||
.downcast()
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
pub fn new_class(&self, name: &str, base: &PyClassRef, flags: PyTpFlags) -> PyClassRef {
|
||||
create_type_with_flags(name, &self.type_type(), base, flags)
|
||||
create_type_with_flags(name, &self.types.type_type, base, flags)
|
||||
}
|
||||
|
||||
pub fn new_namespace(&self) -> PyObjectRef {
|
||||
PyObject::new(PyNamespace, self.namespace_type(), Some(self.new_dict()))
|
||||
PyObject::new(
|
||||
PyNamespace,
|
||||
self.types.namespace_type.clone(),
|
||||
Some(self.new_dict()),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn new_function<F, T, R, VM>(&self, f: F) -> PyObjectRef
|
||||
@@ -478,7 +294,7 @@ impl PyContext {
|
||||
{
|
||||
PyObject::new(
|
||||
PyBuiltinFunction::from(f.into_func()),
|
||||
self.builtin_function_or_method_type(),
|
||||
self.types.builtin_function_or_method_type.clone(),
|
||||
None,
|
||||
)
|
||||
}
|
||||
@@ -487,10 +303,11 @@ impl PyContext {
|
||||
where
|
||||
F: IntoPyNativeFunc<T, R, VM>,
|
||||
{
|
||||
let stringref = |s| PyRef::new_ref(objstr::PyString::from(s), self.str_type(), None);
|
||||
let stringref =
|
||||
|s| PyRef::new_ref(objstr::PyString::from(s), self.types.str_type.clone(), None);
|
||||
PyObject::new(
|
||||
PyBuiltinFunction::new_with_name(f.into_func(), stringref(module), stringref(name)),
|
||||
self.builtin_function_or_method_type(),
|
||||
self.types.builtin_function_or_method_type.clone(),
|
||||
None,
|
||||
)
|
||||
}
|
||||
@@ -501,7 +318,7 @@ impl PyContext {
|
||||
{
|
||||
PyObject::new(
|
||||
PyBuiltinMethod::from(f.into_func()),
|
||||
self.method_descriptor_type(),
|
||||
self.types.method_descriptor_type.clone(),
|
||||
None,
|
||||
)
|
||||
}
|
||||
@@ -512,7 +329,7 @@ impl PyContext {
|
||||
{
|
||||
PyObject::new(
|
||||
PyClassMethod::from(self.new_method(f)),
|
||||
self.classmethod_type(),
|
||||
self.types.classmethod_type.clone(),
|
||||
None,
|
||||
)
|
||||
}
|
||||
@@ -522,7 +339,7 @@ impl PyContext {
|
||||
{
|
||||
PyObject::new(
|
||||
PyStaticMethod::from(self.new_method(f)),
|
||||
self.staticmethod_type(),
|
||||
self.types.staticmethod_type.clone(),
|
||||
None,
|
||||
)
|
||||
}
|
||||
@@ -531,7 +348,11 @@ impl PyContext {
|
||||
where
|
||||
F: IntoPyGetterFunc<T>,
|
||||
{
|
||||
PyObject::new(PyGetSet::with_get(name.into(), f), self.getset_type(), None)
|
||||
PyObject::new(
|
||||
PyGetSet::with_get(name.into(), f),
|
||||
self.types.getset_type.clone(),
|
||||
None,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn new_getset<G, S, T, U>(&self, name: impl Into<String>, g: G, s: S) -> PyObjectRef
|
||||
@@ -541,15 +362,19 @@ impl PyContext {
|
||||
{
|
||||
PyObject::new(
|
||||
PyGetSet::with_get_set(name.into(), g, s),
|
||||
self.getset_type(),
|
||||
self.types.getset_type.clone(),
|
||||
None,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn new_code_object(&self, code: bytecode::CodeObject) -> PyCodeRef {
|
||||
PyObject::new(objcode::PyCode::new(code), self.code_type(), None)
|
||||
.downcast()
|
||||
.unwrap()
|
||||
PyObject::new(
|
||||
objcode::PyCode::new(code),
|
||||
self.types.code_type.clone(),
|
||||
None,
|
||||
)
|
||||
.downcast()
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
pub fn new_pyfunction(
|
||||
@@ -561,7 +386,7 @@ impl PyContext {
|
||||
) -> PyObjectRef {
|
||||
PyObject::new(
|
||||
PyFunction::new(code_obj, scope, defaults, kw_only_defaults),
|
||||
self.function_type(),
|
||||
self.types.function_type.clone(),
|
||||
Some(self.new_dict()),
|
||||
)
|
||||
}
|
||||
@@ -569,7 +394,7 @@ impl PyContext {
|
||||
pub fn new_bound_method(&self, function: PyObjectRef, object: PyObjectRef) -> PyObjectRef {
|
||||
PyObject::new(
|
||||
PyBoundMethod::new(object, function),
|
||||
self.bound_method_type(),
|
||||
self.types.bound_method_type.clone(),
|
||||
None,
|
||||
)
|
||||
}
|
||||
@@ -1481,7 +1306,7 @@ pub trait PyClassImpl: PyClassDef {
|
||||
}
|
||||
|
||||
fn make_class(ctx: &PyContext) -> PyClassRef {
|
||||
Self::make_class_with_base(ctx, Self::NAME, &ctx.object())
|
||||
Self::make_class_with_base(ctx, Self::NAME, &ctx.types.object_type)
|
||||
}
|
||||
|
||||
fn make_class_with_base(ctx: &PyContext, name: &str, base: &PyClassRef) -> PyClassRef {
|
||||
@@ -1587,14 +1412,3 @@ pub fn hash_iter_unordered<'a, I: IntoIterator<Item = &'a PyObjectRef>>(
|
||||
) -> PyResult<rustpython_common::hash::PyHash> {
|
||||
rustpython_common::hash::hash_iter_unordered(iter, |obj| vm._hash(obj))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_type_type() {
|
||||
// TODO: Write this test
|
||||
PyContext::new();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -644,7 +644,7 @@ pub(crate) fn parse(vm: &VirtualMachine, source: &str, mode: Mode) -> PyResult {
|
||||
pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
|
||||
let ctx = &vm.ctx;
|
||||
|
||||
let ast_base = py_class!(ctx, "AST", &ctx.object(), PyTpFlags::HAS_DICT, {});
|
||||
let ast_base = py_class!(ctx, "AST", &ctx.types.object_type, PyTpFlags::HAS_DICT, {});
|
||||
py_module!(vm, MODULE_NAME, {
|
||||
// TODO: There's got to be a better way!
|
||||
"alias" => py_class!(ctx, "alias", &ast_base, {}),
|
||||
|
||||
@@ -1268,7 +1268,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
|
||||
let ctx = &vm.ctx;
|
||||
|
||||
// IOBase the abstract base class of the IO Module
|
||||
let io_base = py_class!(ctx, "_IOBase", &ctx.object(), {
|
||||
let io_base = py_class!(ctx, "_IOBase", &ctx.types.object_type, {
|
||||
"__enter__" => ctx.new_method(io_base_cm_enter),
|
||||
"__exit__" => ctx.new_method(io_base_cm_exit),
|
||||
"seekable" => ctx.new_method(io_base_seekable),
|
||||
|
||||
@@ -897,7 +897,7 @@ mod _struct {
|
||||
let fmt_str = match fmt {
|
||||
Either::A(s) => s,
|
||||
Either::B(b) => PyString::from(std::str::from_utf8(b.borrow_value()).unwrap())
|
||||
.into_ref_with_type(vm, vm.ctx.str_type())?,
|
||||
.into_ref_with_type(vm, vm.ctx.types.str_type.clone())?,
|
||||
};
|
||||
PyStruct { spec, fmt_str }.into_ref_with_type(vm, cls)
|
||||
}
|
||||
|
||||
@@ -26,13 +26,13 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
|
||||
let ctx = &vm.ctx;
|
||||
|
||||
py_module!(vm, "_weakref", {
|
||||
"ref" => ctx.weakref_type(),
|
||||
"proxy" => ctx.weakproxy_type(),
|
||||
"ref" => ctx.types.weakref_type.clone(),
|
||||
"proxy" => ctx.types.weakproxy_type.clone(),
|
||||
"getweakrefcount" => ctx.new_function(weakref_getweakrefcount),
|
||||
"getweakrefs" => ctx.new_function(weakref_getweakrefs),
|
||||
"ReferenceType" => ctx.weakref_type(),
|
||||
"ProxyType" => ctx.weakproxy_type(),
|
||||
"CallableProxyType" => ctx.weakproxy_type(),
|
||||
"ReferenceType" => ctx.types.weakref_type.clone(),
|
||||
"ProxyType" => ctx.types.weakproxy_type.clone(),
|
||||
"CallableProxyType" => ctx.types.weakproxy_type.clone(),
|
||||
"_remove_dead_weakref" => ctx.new_function(weakref_remove_dead_weakref),
|
||||
})
|
||||
}
|
||||
|
||||
16
vm/src/vm.rs
16
vm/src/vm.rs
@@ -617,22 +617,10 @@ impl VirtualMachine {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_type(&self) -> PyClassRef {
|
||||
self.ctx.type_type()
|
||||
}
|
||||
|
||||
pub fn get_object(&self) -> PyClassRef {
|
||||
self.ctx.object()
|
||||
}
|
||||
|
||||
pub fn get_locals(&self) -> PyDictRef {
|
||||
self.current_scope().get_locals()
|
||||
}
|
||||
|
||||
pub fn context(&self) -> &PyContext {
|
||||
&self.ctx
|
||||
}
|
||||
|
||||
// Container of the virtual machine state:
|
||||
pub fn to_str(&self, obj: &PyObjectRef) -> PyResult<PyStringRef> {
|
||||
if obj.lease_class().is(&self.ctx.types.str_type) {
|
||||
@@ -885,7 +873,7 @@ impl VirtualMachine {
|
||||
pub fn extract_elements<T: TryFromObject>(&self, value: &PyObjectRef) -> PyResult<Vec<T>> {
|
||||
// Extract elements from item, if possible:
|
||||
let cls = value.class();
|
||||
if cls.is(&self.ctx.tuple_type()) {
|
||||
if cls.is(&self.ctx.types.tuple_type) {
|
||||
value
|
||||
.payload::<PyTuple>()
|
||||
.unwrap()
|
||||
@@ -893,7 +881,7 @@ impl VirtualMachine {
|
||||
.iter()
|
||||
.map(|obj| T::try_from_object(self, obj.clone()))
|
||||
.collect()
|
||||
} else if cls.is(&self.ctx.list_type()) {
|
||||
} else if cls.is(&self.ctx.types.list_type) {
|
||||
value
|
||||
.payload::<PyList>()
|
||||
.unwrap()
|
||||
|
||||
@@ -255,7 +255,7 @@ fn new_js_error(vm: &VirtualMachine, err: JsValue) -> PyBaseExceptionRef {
|
||||
pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
|
||||
let ctx = &vm.ctx;
|
||||
py_module!(vm, "_js", {
|
||||
"JsError" => create_type("JsError", &ctx.type_type(), &ctx.exceptions.exception_type),
|
||||
"JsError" => create_type("JsError", &ctx.types.type_type, &ctx.exceptions.exception_type),
|
||||
"JsValue" => PyJsValue::make_class(ctx),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ pub fn make_stdout_object(
|
||||
let flush_method = ctx.new_method(|_self: PyObjectRef| {});
|
||||
// there's not really any point to storing this class so that there's a consistent type object,
|
||||
// we just want a half-decent repr() output
|
||||
let cls = py_class!(ctx, "JSStdout", &vm.ctx.object(), {
|
||||
let cls = py_class!(ctx, "JSStdout", &vm.ctx.types.object_type, {
|
||||
"write" => write_method,
|
||||
"flush" => flush_method,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user