Skip to content

Commit 38513cb

Browse files
authored
Merge pull request #3462 from qingshi163/mapping-protocol2
Refactor Mapping Protocol and Item Protocol
2 parents 40991b9 + ae53894 commit 38513cb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+756
-868
lines changed

benches/microbenchmarks.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use criterion::{
44
};
55
use rustpython_compiler::Mode;
66
use rustpython_vm::{
7-
common::ascii, InitParameter, Interpreter, ItemProtocol, PyObjectWrap, PyResult, PySettings,
7+
common::ascii, InitParameter, Interpreter, PyObjectWrap, PyResult, PySettings,
88
};
99
use std::path::{Path, PathBuf};
1010
use std::{ffi, fs, io};

examples/mini_repl.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
use rustpython_vm as vm;
77
// these are needed for special memory shenanigans to let us share a variable with Python and Rust
88
use std::sync::atomic::{AtomicBool, Ordering};
9-
// this needs to be in scope in order to insert things into scope.globals
10-
use vm::ItemProtocol;
119

1210
// This has to be a macro because it uses the py_compile macro,
1311
// which compiles python source to optimized bytecode at compile time, so that

src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ extern crate log;
4646
use clap::{App, AppSettings, Arg, ArgMatches};
4747
use rustpython_vm::{
4848
builtins::PyDictRef, builtins::PyInt, compile, match_class, scope::Scope, stdlib::sys,
49-
InitParameter, Interpreter, ItemProtocol, PyObjectRef, PyResult, PySettings, TryFromObject,
50-
TypeProtocol, VirtualMachine,
49+
InitParameter, Interpreter, PyObjectRef, PyResult, PySettings, TryFromObject, TypeProtocol,
50+
VirtualMachine,
5151
};
5252

5353
use std::env;
@@ -645,7 +645,7 @@ fn run_module(vm: &VirtualMachine, module: &str) -> PyResult<()> {
645645
fn get_importer(path: &str, vm: &VirtualMachine) -> PyResult<Option<PyObjectRef>> {
646646
let path_importer_cache = vm.sys_module.clone().get_attr("path_importer_cache", vm)?;
647647
let path_importer_cache = PyDictRef::try_from_object(vm, path_importer_cache)?;
648-
if let Some(importer) = path_importer_cache.get_item_option(path, vm)? {
648+
if let Some(importer) = path_importer_cache.get_item_opt(path, vm)? {
649649
return Ok(Some(importer));
650650
}
651651
let path = vm.ctx.new_str(path);

src/shell/helper.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,7 @@ impl<'vm> ShellHelper<'vm> {
7474
// last: the last word, could be empty if it ends with a dot
7575
// parents: the words before the dot
7676

77-
let mut current = self
78-
.globals
79-
.get_item_option(first.as_str(), self.vm)
80-
.ok()??;
77+
let mut current = self.globals.get_item_opt(first.as_str(), self.vm).ok()??;
8178

8279
for attr in parents {
8380
current = current.clone().get_attr(attr.as_str(), self.vm).ok()?;

stdlib/src/array.rs

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,35 +1239,26 @@ mod array {
12391239
},
12401240
};
12411241

1242+
impl PyArray {
1243+
const MAPPING_METHODS: PyMappingMethods = PyMappingMethods {
1244+
length: Some(|mapping, _vm| Ok(Self::mapping_downcast(mapping).len())),
1245+
subscript: Some(|mapping, needle, vm| {
1246+
Self::mapping_downcast(mapping).getitem(needle.to_owned(), vm)
1247+
}),
1248+
ass_subscript: Some(|mapping, needle, value, vm| {
1249+
let zelf = Self::mapping_downcast(mapping);
1250+
if let Some(value) = value {
1251+
Self::setitem(zelf.to_owned(), needle.to_owned(), value, vm)
1252+
} else {
1253+
Self::delitem(zelf.to_owned(), needle.to_owned(), vm)
1254+
}
1255+
}),
1256+
};
1257+
}
1258+
12421259
impl AsMapping for PyArray {
12431260
fn as_mapping(_zelf: &PyObjectView<Self>, _vm: &VirtualMachine) -> PyMappingMethods {
1244-
PyMappingMethods {
1245-
length: Some(Self::length),
1246-
subscript: Some(Self::subscript),
1247-
ass_subscript: Some(Self::ass_subscript),
1248-
}
1249-
}
1250-
1251-
fn length(zelf: PyObjectRef, vm: &VirtualMachine) -> PyResult<usize> {
1252-
Self::downcast_ref(&zelf, vm).map(|zelf| Ok(zelf.len()))?
1253-
}
1254-
1255-
fn subscript(zelf: PyObjectRef, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult {
1256-
Self::downcast_ref(&zelf, vm).map(|zelf| zelf.getitem(needle, vm))?
1257-
}
1258-
1259-
fn ass_subscript(
1260-
zelf: PyObjectRef,
1261-
needle: PyObjectRef,
1262-
value: Option<PyObjectRef>,
1263-
vm: &VirtualMachine,
1264-
) -> PyResult<()> {
1265-
match value {
1266-
Some(value) => {
1267-
Self::downcast(zelf, vm).map(|zelf| Self::setitem(zelf, needle, value, vm))?
1268-
}
1269-
None => Self::downcast(zelf, vm).map(|zelf| Self::delitem(zelf, needle, vm))?,
1270-
}
1261+
Self::MAPPING_METHODS
12711262
}
12721263
}
12731264

stdlib/src/bisect.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ pub(crate) use _bisect::make_module;
33
#[pymodule]
44
mod _bisect {
55
use crate::vm::{
6-
function::OptionalArg, types::PyComparisonOp::Lt, ItemProtocol, PyObjectRef, PyResult,
7-
VirtualMachine,
6+
function::OptionalArg, types::PyComparisonOp::Lt, PyObjectRef, PyResult, VirtualMachine,
87
};
98

109
#[derive(FromArgs)]

stdlib/src/dis.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ mod decl {
55
use crate::vm::{
66
builtins::{PyCode, PyDictRef, PyStrRef},
77
bytecode::CodeFlags,
8-
compile, ItemProtocol, PyObjectRef, PyRef, PyResult, TryFromObject, VirtualMachine,
8+
compile, PyObjectRef, PyRef, PyResult, TryFromObject, VirtualMachine,
99
};
1010

1111
#[pyfunction]

stdlib/src/pyexpat.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ mod _pyexpat {
3535
builtins::{PyStr, PyStrRef, PyTypeRef},
3636
function::ArgBytesLike,
3737
function::{IntoFuncArgs, OptionalArg},
38-
ItemProtocol, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject,
39-
VirtualMachine,
38+
PyContext, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, VirtualMachine,
4039
};
4140
use rustpython_common::lock::PyRwLock;
4241
use std::io::Cursor;

stdlib/src/scproxy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ mod _scproxy {
77
use crate::vm::{
88
builtins::{PyDictRef, PyStr},
99
function::IntoPyObject,
10-
ItemProtocol, PyResult, VirtualMachine,
10+
PyResult, VirtualMachine,
1111
};
1212
use system_configuration::core_foundation::{
1313
array::CFArray,

stdlib/src/ssl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ mod _ssl {
3939
stdlib::os::PyPathLike,
4040
types::Constructor,
4141
utils::{Either, ToCString},
42-
ItemProtocol, PyObjectRef, PyObjectWeak, PyRef, PyResult, PyValue, VirtualMachine,
42+
PyObjectRef, PyObjectWeak, PyRef, PyResult, PyValue, VirtualMachine,
4343
},
4444
};
4545
use crossbeam_utils::atomic::AtomicCell;

vm/src/builtins/bytearray.rs

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,23 @@ impl PyByteArray {
677677
}
678678
}
679679

680+
impl PyByteArray {
681+
const MAPPING_METHODS: PyMappingMethods = PyMappingMethods {
682+
length: Some(|mapping, _vm| Ok(Self::mapping_downcast(mapping).len())),
683+
subscript: Some(|mapping, needle, vm| {
684+
Self::mapping_downcast(mapping).getitem(needle.to_owned(), vm)
685+
}),
686+
ass_subscript: Some(|mapping, needle, value, vm| {
687+
let zelf = Self::mapping_downcast(mapping);
688+
if let Some(value) = value {
689+
Self::setitem(zelf.to_owned(), needle.to_owned(), value, vm)
690+
} else {
691+
zelf.delitem(needle.to_owned(), vm)
692+
}
693+
}),
694+
};
695+
}
696+
680697
impl Comparable for PyByteArray {
681698
fn cmp(
682699
zelf: &crate::PyObjectView<Self>,
@@ -739,36 +756,7 @@ impl<'a> BufferResizeGuard<'a> for PyByteArray {
739756

740757
impl AsMapping for PyByteArray {
741758
fn as_mapping(_zelf: &crate::PyObjectView<Self>, _vm: &VirtualMachine) -> PyMappingMethods {
742-
PyMappingMethods {
743-
length: Some(Self::length),
744-
subscript: Some(Self::subscript),
745-
ass_subscript: Some(Self::ass_subscript),
746-
}
747-
}
748-
749-
#[inline]
750-
fn length(zelf: PyObjectRef, vm: &VirtualMachine) -> PyResult<usize> {
751-
Self::downcast_ref(&zelf, vm).map(|zelf| Ok(zelf.len()))?
752-
}
753-
754-
#[inline]
755-
fn subscript(zelf: PyObjectRef, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult {
756-
Self::downcast_ref(&zelf, vm).map(|zelf| zelf.getitem(needle, vm))?
757-
}
758-
759-
#[inline]
760-
fn ass_subscript(
761-
zelf: PyObjectRef,
762-
needle: PyObjectRef,
763-
value: Option<PyObjectRef>,
764-
vm: &VirtualMachine,
765-
) -> PyResult<()> {
766-
match value {
767-
Some(value) => {
768-
Self::downcast(zelf, vm).map(|zelf| Self::setitem(zelf, needle, value, vm))
769-
}
770-
None => Self::downcast_ref(&zelf, vm).map(|zelf| zelf.delitem(needle, vm)),
771-
}?
759+
Self::MAPPING_METHODS
772760
}
773761
}
774762

vm/src/builtins/bytes.rs

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,16 @@ impl PyBytes {
523523
}
524524
}
525525

526+
impl PyBytes {
527+
const MAPPING_METHODS: PyMappingMethods = PyMappingMethods {
528+
length: Some(|mapping, _vm| Ok(Self::mapping_downcast(mapping).len())),
529+
subscript: Some(|mapping, needle, vm| {
530+
Self::mapping_downcast(mapping).getitem(needle.to_owned(), vm)
531+
}),
532+
ass_subscript: None,
533+
};
534+
}
535+
526536
static BUFFER_METHODS: BufferMethods = BufferMethods {
527537
obj_bytes: |buffer| buffer.obj_as::<PyBytes>().as_bytes().into(),
528538
obj_bytes_mut: |_| panic!(),
@@ -543,31 +553,7 @@ impl AsBuffer for PyBytes {
543553

544554
impl AsMapping for PyBytes {
545555
fn as_mapping(_zelf: &PyObjectView<Self>, _vm: &VirtualMachine) -> PyMappingMethods {
546-
PyMappingMethods {
547-
length: Some(Self::length),
548-
subscript: Some(Self::subscript),
549-
ass_subscript: None,
550-
}
551-
}
552-
553-
#[inline]
554-
fn length(zelf: PyObjectRef, vm: &VirtualMachine) -> PyResult<usize> {
555-
Self::downcast_ref(&zelf, vm).map(|zelf| Ok(zelf.len()))?
556-
}
557-
558-
#[inline]
559-
fn subscript(zelf: PyObjectRef, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult {
560-
Self::downcast_ref(&zelf, vm).map(|zelf| zelf.getitem(needle, vm))?
561-
}
562-
563-
#[cold]
564-
fn ass_subscript(
565-
zelf: PyObjectRef,
566-
_needle: PyObjectRef,
567-
_value: Option<PyObjectRef>,
568-
_vm: &VirtualMachine,
569-
) -> PyResult<()> {
570-
unreachable!("ass_subscript not implemented for {}", zelf.class())
556+
Self::MAPPING_METHODS
571557
}
572558
}
573559

0 commit comments

Comments
 (0)