Skip to content

Refactor Mapping Protocol and Item Protocol #3462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Dec 3, 2021
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
2 changes: 1 addition & 1 deletion benches/microbenchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use criterion::{
};
use rustpython_compiler::Mode;
use rustpython_vm::{
common::ascii, InitParameter, Interpreter, ItemProtocol, PyObjectWrap, PyResult, PySettings,
common::ascii, InitParameter, Interpreter, PyObjectWrap, PyResult, PySettings,
};
use std::path::{Path, PathBuf};
use std::{ffi, fs, io};
Expand Down
2 changes: 0 additions & 2 deletions examples/mini_repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
use rustpython_vm as vm;
// these are needed for special memory shenanigans to let us share a variable with Python and Rust
use std::sync::atomic::{AtomicBool, Ordering};
// this needs to be in scope in order to insert things into scope.globals
use vm::ItemProtocol;

// This has to be a macro because it uses the py_compile macro,
// which compiles python source to optimized bytecode at compile time, so that
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ extern crate log;
use clap::{App, AppSettings, Arg, ArgMatches};
use rustpython_vm::{
builtins::PyDictRef, builtins::PyInt, compile, match_class, scope::Scope, stdlib::sys,
InitParameter, Interpreter, ItemProtocol, PyObjectRef, PyResult, PySettings, TryFromObject,
TypeProtocol, VirtualMachine,
InitParameter, Interpreter, PyObjectRef, PyResult, PySettings, TryFromObject, TypeProtocol,
VirtualMachine,
};

use std::env;
Expand Down Expand Up @@ -645,7 +645,7 @@ fn run_module(vm: &VirtualMachine, module: &str) -> PyResult<()> {
fn get_importer(path: &str, vm: &VirtualMachine) -> PyResult<Option<PyObjectRef>> {
let path_importer_cache = vm.sys_module.clone().get_attr("path_importer_cache", vm)?;
let path_importer_cache = PyDictRef::try_from_object(vm, path_importer_cache)?;
if let Some(importer) = path_importer_cache.get_item_option(path, vm)? {
if let Some(importer) = path_importer_cache.get_item_opt(path, vm)? {
return Ok(Some(importer));
}
let path = vm.ctx.new_str(path);
Expand Down
5 changes: 1 addition & 4 deletions src/shell/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,7 @@ impl<'vm> ShellHelper<'vm> {
// last: the last word, could be empty if it ends with a dot
// parents: the words before the dot

let mut current = self
.globals
.get_item_option(first.as_str(), self.vm)
.ok()??;
let mut current = self.globals.get_item_opt(first.as_str(), self.vm).ok()??;

for attr in parents {
current = current.clone().get_attr(attr.as_str(), self.vm).ok()?;
Expand Down
45 changes: 18 additions & 27 deletions stdlib/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1239,35 +1239,26 @@ mod array {
},
};

impl PyArray {
const MAPPING_METHODS: PyMappingMethods = PyMappingMethods {
length: Some(|mapping, _vm| Ok(Self::mapping_downcast(mapping).len())),
subscript: Some(|mapping, needle, vm| {
Self::mapping_downcast(mapping).getitem(needle.to_owned(), vm)
}),
ass_subscript: Some(|mapping, needle, value, vm| {
let zelf = Self::mapping_downcast(mapping);
if let Some(value) = value {
Self::setitem(zelf.to_owned(), needle.to_owned(), value, vm)
} else {
Self::delitem(zelf.to_owned(), needle.to_owned(), vm)
}
}),
};
}

impl AsMapping for PyArray {
fn as_mapping(_zelf: &PyObjectView<Self>, _vm: &VirtualMachine) -> PyMappingMethods {
PyMappingMethods {
length: Some(Self::length),
subscript: Some(Self::subscript),
ass_subscript: Some(Self::ass_subscript),
}
}

fn length(zelf: PyObjectRef, vm: &VirtualMachine) -> PyResult<usize> {
Self::downcast_ref(&zelf, vm).map(|zelf| Ok(zelf.len()))?
}

fn subscript(zelf: PyObjectRef, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult {
Self::downcast_ref(&zelf, vm).map(|zelf| zelf.getitem(needle, vm))?
}

fn ass_subscript(
zelf: PyObjectRef,
needle: PyObjectRef,
value: Option<PyObjectRef>,
vm: &VirtualMachine,
) -> PyResult<()> {
match value {
Some(value) => {
Self::downcast(zelf, vm).map(|zelf| Self::setitem(zelf, needle, value, vm))?
}
None => Self::downcast(zelf, vm).map(|zelf| Self::delitem(zelf, needle, vm))?,
}
Self::MAPPING_METHODS
}
}

Expand Down
3 changes: 1 addition & 2 deletions stdlib/src/bisect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ pub(crate) use _bisect::make_module;
#[pymodule]
mod _bisect {
use crate::vm::{
function::OptionalArg, types::PyComparisonOp::Lt, ItemProtocol, PyObjectRef, PyResult,
VirtualMachine,
function::OptionalArg, types::PyComparisonOp::Lt, PyObjectRef, PyResult, VirtualMachine,
};

#[derive(FromArgs)]
Expand Down
2 changes: 1 addition & 1 deletion stdlib/src/dis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod decl {
use crate::vm::{
builtins::{PyCode, PyDictRef, PyStrRef},
bytecode::CodeFlags,
compile, ItemProtocol, PyObjectRef, PyRef, PyResult, TryFromObject, VirtualMachine,
compile, PyObjectRef, PyRef, PyResult, TryFromObject, VirtualMachine,
};

#[pyfunction]
Expand Down
3 changes: 1 addition & 2 deletions stdlib/src/pyexpat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ mod _pyexpat {
builtins::{PyStr, PyStrRef, PyTypeRef},
function::ArgBytesLike,
function::{IntoFuncArgs, OptionalArg},
ItemProtocol, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject,
VirtualMachine,
PyContext, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, VirtualMachine,
};
use rustpython_common::lock::PyRwLock;
use std::io::Cursor;
Expand Down
2 changes: 1 addition & 1 deletion stdlib/src/scproxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod _scproxy {
use crate::vm::{
builtins::{PyDictRef, PyStr},
function::IntoPyObject,
ItemProtocol, PyResult, VirtualMachine,
PyResult, VirtualMachine,
};
use system_configuration::core_foundation::{
array::CFArray,
Expand Down
2 changes: 1 addition & 1 deletion stdlib/src/ssl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ mod _ssl {
stdlib::os::PyPathLike,
types::Constructor,
utils::{Either, ToCString},
ItemProtocol, PyObjectRef, PyObjectWeak, PyRef, PyResult, PyValue, VirtualMachine,
PyObjectRef, PyObjectWeak, PyRef, PyResult, PyValue, VirtualMachine,
},
};
use crossbeam_utils::atomic::AtomicCell;
Expand Down
48 changes: 18 additions & 30 deletions vm/src/builtins/bytearray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,23 @@ impl PyByteArray {
}
}

impl PyByteArray {
const MAPPING_METHODS: PyMappingMethods = PyMappingMethods {
length: Some(|mapping, _vm| Ok(Self::mapping_downcast(mapping).len())),
subscript: Some(|mapping, needle, vm| {
Self::mapping_downcast(mapping).getitem(needle.to_owned(), vm)
}),
ass_subscript: Some(|mapping, needle, value, vm| {
let zelf = Self::mapping_downcast(mapping);
if let Some(value) = value {
Self::setitem(zelf.to_owned(), needle.to_owned(), value, vm)
} else {
zelf.delitem(needle.to_owned(), vm)
}
}),
};
}

impl Comparable for PyByteArray {
fn cmp(
zelf: &crate::PyObjectView<Self>,
Expand Down Expand Up @@ -739,36 +756,7 @@ impl<'a> BufferResizeGuard<'a> for PyByteArray {

impl AsMapping for PyByteArray {
fn as_mapping(_zelf: &crate::PyObjectView<Self>, _vm: &VirtualMachine) -> PyMappingMethods {
PyMappingMethods {
length: Some(Self::length),
subscript: Some(Self::subscript),
ass_subscript: Some(Self::ass_subscript),
}
}

#[inline]
fn length(zelf: PyObjectRef, vm: &VirtualMachine) -> PyResult<usize> {
Self::downcast_ref(&zelf, vm).map(|zelf| Ok(zelf.len()))?
}

#[inline]
fn subscript(zelf: PyObjectRef, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult {
Self::downcast_ref(&zelf, vm).map(|zelf| zelf.getitem(needle, vm))?
}

#[inline]
fn ass_subscript(
zelf: PyObjectRef,
needle: PyObjectRef,
value: Option<PyObjectRef>,
vm: &VirtualMachine,
) -> PyResult<()> {
match value {
Some(value) => {
Self::downcast(zelf, vm).map(|zelf| Self::setitem(zelf, needle, value, vm))
}
None => Self::downcast_ref(&zelf, vm).map(|zelf| zelf.delitem(needle, vm)),
}?
Self::MAPPING_METHODS
}
}

Expand Down
36 changes: 11 additions & 25 deletions vm/src/builtins/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,16 @@ impl PyBytes {
}
}

impl PyBytes {
const MAPPING_METHODS: PyMappingMethods = PyMappingMethods {
length: Some(|mapping, _vm| Ok(Self::mapping_downcast(mapping).len())),
subscript: Some(|mapping, needle, vm| {
Self::mapping_downcast(mapping).getitem(needle.to_owned(), vm)
}),
ass_subscript: None,
};
}

static BUFFER_METHODS: BufferMethods = BufferMethods {
obj_bytes: |buffer| buffer.obj_as::<PyBytes>().as_bytes().into(),
obj_bytes_mut: |_| panic!(),
Expand All @@ -543,31 +553,7 @@ impl AsBuffer for PyBytes {

impl AsMapping for PyBytes {
fn as_mapping(_zelf: &PyObjectView<Self>, _vm: &VirtualMachine) -> PyMappingMethods {
PyMappingMethods {
length: Some(Self::length),
subscript: Some(Self::subscript),
ass_subscript: None,
}
}

#[inline]
fn length(zelf: PyObjectRef, vm: &VirtualMachine) -> PyResult<usize> {
Self::downcast_ref(&zelf, vm).map(|zelf| Ok(zelf.len()))?
}

#[inline]
fn subscript(zelf: PyObjectRef, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult {
Self::downcast_ref(&zelf, vm).map(|zelf| zelf.getitem(needle, vm))?
}

#[cold]
fn ass_subscript(
zelf: PyObjectRef,
_needle: PyObjectRef,
_value: Option<PyObjectRef>,
_vm: &VirtualMachine,
) -> PyResult<()> {
unreachable!("ass_subscript not implemented for {}", zelf.class())
Self::MAPPING_METHODS
}
}

Expand Down
Loading