mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-29 11:14:38 +08:00
Fix unnessessary borrow
This commit is contained in:
1 parent
0e360f5428
commit
ff4efd0f60
40 files changed
+132
-132
No files matched your search
@@ -37,10 +37,10 @@ impl<T: ?Sized> Deref for BorrowedValue<'_, T> {
|
||||
fn deref(&self) -> &T {
|
||||
match self {
|
||||
Self::Ref(r) => r,
|
||||
Self::MuLock(m) => &m,
|
||||
Self::MappedMuLock(m) => &m,
|
||||
Self::ReadLock(r) => &r,
|
||||
Self::MappedReadLock(m) => &m,
|
||||
Self::MuLock(m) => m,
|
||||
Self::MappedMuLock(m) => m,
|
||||
Self::ReadLock(r) => r,
|
||||
Self::MappedReadLock(m) => m,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -76,10 +76,10 @@ impl<T: ?Sized> Deref for BorrowedValueMut<'_, T> {
|
||||
fn deref(&self) -> &T {
|
||||
match self {
|
||||
Self::RefMut(r) => r,
|
||||
Self::MuLock(m) => &m,
|
||||
Self::MappedMuLock(m) => &m,
|
||||
Self::WriteLock(w) => &w,
|
||||
Self::MappedWriteLock(w) => &w,
|
||||
Self::MuLock(m) => m,
|
||||
Self::MappedMuLock(m) => m,
|
||||
Self::WriteLock(w) => w,
|
||||
Self::MappedWriteLock(w) => w,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -348,7 +348,7 @@ impl Compiler {
|
||||
self.emit(Instruction::PrintExpr);
|
||||
}
|
||||
} else {
|
||||
self.compile_statement(&statement)?;
|
||||
self.compile_statement(statement)?;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -470,7 +470,7 @@ impl Compiler {
|
||||
// we do this here because `from __future__` still executes that `from` statement at runtime,
|
||||
// we still need to compile the ImportFrom down below
|
||||
ImportFrom { module, names, .. } if module.as_deref() == Some("__future__") => {
|
||||
self.compile_future_features(&names)?
|
||||
self.compile_future_features(names)?
|
||||
}
|
||||
// if we find any other statement, stop accepting future statements
|
||||
_ => self.done_with_future_stmts = true,
|
||||
@@ -1068,7 +1068,7 @@ impl Compiler {
|
||||
self.emit_constant(ConstantData::Str {
|
||||
value: self.mangle(&arg.node.arg).into_owned(),
|
||||
});
|
||||
self.compile_expression(&annotation)?;
|
||||
self.compile_expression(annotation)?;
|
||||
num_annotations += 1;
|
||||
}
|
||||
}
|
||||
@@ -1159,7 +1159,7 @@ impl Compiler {
|
||||
orelse,
|
||||
finalbody,
|
||||
..
|
||||
} => self.find_ann(&body) || self.find_ann(orelse) || self.find_ann(finalbody),
|
||||
} => self.find_ann(body) || self.find_ann(orelse) || self.find_ann(finalbody),
|
||||
_ => false,
|
||||
};
|
||||
if res {
|
||||
|
||||
@@ -617,7 +617,7 @@ impl SymbolTableBuilder {
|
||||
|
||||
fn scan_parameter_annotation(&mut self, parameter: &ast::Arg) -> SymbolTableResult {
|
||||
if let Some(annotation) = ¶meter.node.annotation {
|
||||
self.scan_expression(&annotation, ExpressionContext::Load)?;
|
||||
self.scan_expression(annotation, ExpressionContext::Load)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -1075,7 +1075,7 @@ impl SymbolTableBuilder {
|
||||
// Evaluate eventual default parameters:
|
||||
self.scan_expressions(&args.defaults, ExpressionContext::Load)?;
|
||||
for expression in args.kw_defaults.iter().flatten() {
|
||||
self.scan_expression(&expression, ExpressionContext::Load)?;
|
||||
self.scan_expression(expression, ExpressionContext::Load)?;
|
||||
}
|
||||
|
||||
// Annotations are scanned in outer scope:
|
||||
|
||||
@@ -81,7 +81,7 @@ impl ArgAttribute {
|
||||
}
|
||||
match arg {
|
||||
NestedMeta::Meta(Meta::Path(path)) => {
|
||||
if path_eq(&path, "default") || path_eq(&path, "optional") {
|
||||
if path_eq(path, "default") || path_eq(path, "optional") {
|
||||
if self.default.is_none() {
|
||||
self.default = Some(None);
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ fn extract_items_into_context<'a, Item>(
|
||||
{
|
||||
for item in items {
|
||||
let r = item.try_split_attr_mut(|attrs, item| {
|
||||
let (pyitems, cfgs) = attrs_to_content_items(&attrs, new_impl_item::<Item>)?;
|
||||
let (pyitems, cfgs) = attrs_to_content_items(attrs, new_impl_item::<Item>)?;
|
||||
for pyitem in pyitems.iter().rev() {
|
||||
let r = pyitem.gen_impl_item(ImplItemArgs::<Item> {
|
||||
item,
|
||||
@@ -227,7 +227,7 @@ pub(crate) fn impl_pyclass(
|
||||
let class_name = class_meta.class_name()?;
|
||||
let module_name = class_meta.module()?;
|
||||
let base = class_meta.base()?;
|
||||
let class_def = generate_class_def(&ident, &class_name, module_name.as_deref(), base, &attrs)?;
|
||||
let class_def = generate_class_def(ident, &class_name, module_name.as_deref(), base, attrs)?;
|
||||
|
||||
let ret = quote! {
|
||||
#item
|
||||
@@ -424,7 +424,7 @@ where
|
||||
};
|
||||
let (py_name, tokens) = if args.item.is_function_or_method() || args.item.is_const() {
|
||||
let ident = args.item.get_ident().unwrap();
|
||||
let py_name = get_py_name(&attr, &ident)?;
|
||||
let py_name = get_py_name(&attr, ident)?;
|
||||
|
||||
let value = if args.item.is_const() {
|
||||
// TODO: ctx.new_value
|
||||
|
||||
@@ -39,7 +39,7 @@ pub fn impl_pymodule(
|
||||
// collect to context
|
||||
for item in items.iter_mut() {
|
||||
let r = item.try_split_attr_mut(|attrs, item| {
|
||||
let (pyitems, cfgs) = attrs_to_module_items(&attrs, new_module_item)?;
|
||||
let (pyitems, cfgs) = attrs_to_module_items(attrs, new_module_item)?;
|
||||
for pyitem in pyitems.iter().rev() {
|
||||
let r = pyitem.gen_module_item(ModuleItemArgs {
|
||||
item,
|
||||
@@ -283,7 +283,7 @@ impl ModuleItem for FunctionItem {
|
||||
|
||||
impl ModuleItem for ClassItem {
|
||||
fn gen_module_item(&self, args: ModuleItemArgs<'_>) -> Result<()> {
|
||||
let (ident, _) = pyclass_ident_and_attrs(&args.item)?;
|
||||
let (ident, _) = pyclass_ident_and_attrs(args.item)?;
|
||||
let (module_name, class_name) = {
|
||||
let class_attr = &mut args.attrs[self.inner.index];
|
||||
if self.pyattrs.is_empty() {
|
||||
@@ -350,7 +350,7 @@ impl ModuleItem for AttributeItem {
|
||||
let (py_name, tokens) = match args.item {
|
||||
Item::Fn(syn::ItemFn { sig, .. }) => {
|
||||
let ident = &sig.ident;
|
||||
let py_name = get_py_name(&attr, &ident)?;
|
||||
let py_name = get_py_name(&attr, ident)?;
|
||||
(
|
||||
py_name.clone(),
|
||||
quote! {
|
||||
@@ -359,7 +359,7 @@ impl ModuleItem for AttributeItem {
|
||||
)
|
||||
}
|
||||
Item::Const(syn::ItemConst { ident, .. }) => {
|
||||
let py_name = get_py_name(&attr, &ident)?;
|
||||
let py_name = get_py_name(&attr, ident)?;
|
||||
(
|
||||
py_name.clone(),
|
||||
quote! {
|
||||
|
||||
@@ -138,7 +138,7 @@ impl<'a, 'b> FunctionCompiler<'a, 'b> {
|
||||
continue;
|
||||
}
|
||||
|
||||
self.add_instruction(&instruction, &bytecode.constants)?;
|
||||
self.add_instruction(instruction, &bytecode.constants)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
+6
-6
@@ -142,7 +142,7 @@ where
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
print_exception(&vm, err);
|
||||
print_exception(vm, err);
|
||||
1
|
||||
}
|
||||
};
|
||||
@@ -535,9 +535,9 @@ fn run_rustpython(vm: &VirtualMachine, matches: &ArgMatches) -> PyResult<()> {
|
||||
|
||||
// Figure out if a -c option was given:
|
||||
if let Some(command) = matches.value_of("c") {
|
||||
run_command(&vm, scope, command.to_owned())?;
|
||||
run_command(vm, scope, command.to_owned())?;
|
||||
} else if let Some(module) = matches.value_of("m") {
|
||||
run_module(&vm, module)?;
|
||||
run_module(vm, module)?;
|
||||
} else if matches.is_present("install_pip") {
|
||||
let get_getpip = rustpython_vm::py_compile!(
|
||||
source = r#"\
|
||||
@@ -555,16 +555,16 @@ __import__("io").TextIOWrapper(
|
||||
eprintln!("running get-pip.py...");
|
||||
_run_string(vm, scope, getpip_code.as_str(), "get-pip.py".to_owned())?;
|
||||
} else if let Some(filename) = matches.value_of("script") {
|
||||
run_script(&vm, scope.clone(), filename)?;
|
||||
run_script(vm, scope.clone(), filename)?;
|
||||
if matches.is_present("inspect") {
|
||||
shell::run_shell(&vm, scope)?;
|
||||
shell::run_shell(vm, scope)?;
|
||||
}
|
||||
} else {
|
||||
println!(
|
||||
"Welcome to the magnificent Rust Python {} interpreter \u{1f631} \u{1f596}",
|
||||
crate_version!()
|
||||
);
|
||||
shell::run_shell(&vm, scope)?;
|
||||
shell::run_shell(vm, scope)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
+6
-6
@@ -247,7 +247,7 @@ pub trait AnyStr<'s>: 's {
|
||||
{
|
||||
if range.is_normal() {
|
||||
let start = range.start;
|
||||
let index = find(self.get_chars(range), &needle)?;
|
||||
let index = find(self.get_chars(range), needle)?;
|
||||
Some(start + index)
|
||||
} else {
|
||||
None
|
||||
@@ -260,7 +260,7 @@ pub trait AnyStr<'s>: 's {
|
||||
F: Fn(&Self, &Self) -> usize,
|
||||
{
|
||||
if range.is_normal() {
|
||||
count(self.get_chars(range), &needle)
|
||||
count(self.get_chars(range), needle)
|
||||
} else {
|
||||
0
|
||||
}
|
||||
@@ -336,10 +336,10 @@ pub trait AnyStr<'s>: 's {
|
||||
FC: Fn(&Self, &Self) -> bool,
|
||||
{
|
||||
//if self.py_starts_with(prefix) {
|
||||
if is_prefix(&self, prefix) {
|
||||
if is_prefix(self, prefix) {
|
||||
self.get_bytes(prefix_len..self.bytes_len())
|
||||
} else {
|
||||
&self
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
@@ -347,10 +347,10 @@ pub trait AnyStr<'s>: 's {
|
||||
where
|
||||
FC: Fn(&Self, &Self) -> bool,
|
||||
{
|
||||
if is_suffix(&self, suffix) {
|
||||
if is_suffix(self, suffix) {
|
||||
self.get_bytes(0..self.bytes_len() - suffix_len)
|
||||
} else {
|
||||
&self
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -350,7 +350,7 @@ impl Comparable for PyComplex {
|
||||
let result = if let Some(other) = other.payload_if_subclass::<PyComplex>(vm) {
|
||||
zelf.value == other.value
|
||||
} else {
|
||||
match float::to_op_float(&other, vm) {
|
||||
match float::to_op_float(other, vm) {
|
||||
Ok(Some(other)) => zelf.value == other.into(),
|
||||
Err(_) => false,
|
||||
Ok(None) => return Ok(PyComparisonValue::NotImplemented),
|
||||
|
||||
@@ -52,7 +52,7 @@ impl PyIter for PyFilter {
|
||||
} else {
|
||||
// the predicate itself can raise StopIteration which does stop the filter
|
||||
// iteration
|
||||
vm.invoke(&predicate, vec![next_obj.clone()])?
|
||||
vm.invoke(predicate, vec![next_obj.clone()])?
|
||||
};
|
||||
if pybool::boolval(vm, predicate_value)? {
|
||||
return Ok(next_obj);
|
||||
|
||||
@@ -177,7 +177,7 @@ fn inner_floordiv(int1: &BigInt, int2: &BigInt, vm: &VirtualMachine) -> PyResult
|
||||
if int2.is_zero() {
|
||||
Err(vm.new_zero_division_error("integer division by zero".to_owned()))
|
||||
} else {
|
||||
Ok(vm.ctx.new_int(int1.div_floor(&int2)))
|
||||
Ok(vm.ctx.new_int(int1.div_floor(int2)))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -364,12 +364,12 @@ impl PyInt {
|
||||
|
||||
#[pymethod(name = "__floordiv__")]
|
||||
fn floordiv(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
self.general_op(other, |a, b| inner_floordiv(a, b, &vm), vm)
|
||||
self.general_op(other, |a, b| inner_floordiv(a, b, vm), vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__rfloordiv__")]
|
||||
fn rfloordiv(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
self.general_op(other, |a, b| inner_floordiv(b, a, &vm), vm)
|
||||
self.general_op(other, |a, b| inner_floordiv(b, a, vm), vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__lshift__")]
|
||||
@@ -906,7 +906,7 @@ fn bytes_to_int(lit: &[u8], mut base: u32) -> Option<BigInt> {
|
||||
Some(if lit.is_empty() {
|
||||
BigInt::zero()
|
||||
} else {
|
||||
let uint = BigUint::parse_bytes(&lit, base)?;
|
||||
let uint = BigUint::parse_bytes(lit, base)?;
|
||||
BigInt::from_biguint(sign.unwrap_or(Sign::Plus), uint)
|
||||
})
|
||||
}
|
||||
@@ -950,7 +950,7 @@ pub(crate) fn try_int(obj: &PyObjectRef, vm: &VirtualMachine) -> PyResult<BigInt
|
||||
if let Some(s) = obj.downcast_ref::<PyStr>() {
|
||||
return try_convert(obj, s.as_str().as_bytes(), vm);
|
||||
}
|
||||
if let Ok(r) = try_bytes_like(vm, &obj, |x| try_convert(obj, x, vm)) {
|
||||
if let Ok(r) = try_bytes_like(vm, obj, |x| try_convert(obj, x, vm)) {
|
||||
return r;
|
||||
}
|
||||
// strict `int` check
|
||||
|
||||
@@ -240,7 +240,7 @@ impl PyList {
|
||||
|
||||
#[pymethod(name = "__rmul__")]
|
||||
fn rmul(&self, counter: isize, vm: &VirtualMachine) -> PyObjectRef {
|
||||
self.mul(counter, &vm)
|
||||
self.mul(counter, vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__imul__")]
|
||||
|
||||
@@ -174,7 +174,7 @@ mod decl {
|
||||
.parse::<compile::Mode>()
|
||||
.map_err(|err| vm.new_value_error(err.to_string()))?;
|
||||
|
||||
vm.compile(&source, mode, args.filename.as_str().to_owned())
|
||||
vm.compile(source, mode, args.filename.as_str().to_owned())
|
||||
.map(|o| o.into_object())
|
||||
.map_err(|err| vm.new_syntax_error(&err))
|
||||
}
|
||||
@@ -182,7 +182,7 @@ mod decl {
|
||||
let mode = mode_str
|
||||
.parse::<parser::Mode>()
|
||||
.map_err(|err| vm.new_value_error(err.to_string()))?;
|
||||
ast::parse(&vm, &source, mode)
|
||||
ast::parse(vm, source, mode)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -123,5 +123,5 @@ impl SlotGetattro for PyModule {
|
||||
}
|
||||
|
||||
pub(crate) fn init(context: &PyContext) {
|
||||
PyModule::extend_class(&context, &context.types.module_type);
|
||||
PyModule::extend_class(context, &context.types.module_type);
|
||||
}
|
||||
@@ -81,7 +81,7 @@ impl SlotDescriptor for PyProperty {
|
||||
if vm.is_none(&obj) {
|
||||
Ok(zelf.into_object())
|
||||
} else if let Some(getter) = zelf.getter.read().as_ref() {
|
||||
vm.invoke(&getter, (obj,))
|
||||
vm.invoke(getter, (obj,))
|
||||
} else {
|
||||
Err(vm.new_attribute_error("unreadable attribute".to_string()))
|
||||
}
|
||||
@@ -121,14 +121,14 @@ impl PyProperty {
|
||||
let zelf = PyRef::<Self>::try_from_object(vm, zelf)?;
|
||||
match value {
|
||||
Some(value) => {
|
||||
if let Some(ref setter) = zelf.setter.read().as_ref() {
|
||||
if let Some(setter) = zelf.setter.read().as_ref() {
|
||||
vm.invoke(setter, vec![obj, value]).map(drop)
|
||||
} else {
|
||||
Err(vm.new_attribute_error("can't set attribute".to_owned()))
|
||||
}
|
||||
}
|
||||
None => {
|
||||
if let Some(ref deleter) = zelf.deleter.read().as_ref() {
|
||||
if let Some(deleter) = zelf.deleter.read().as_ref() {
|
||||
vm.invoke(deleter, (obj,)).map(drop)
|
||||
} else {
|
||||
Err(vm.new_attribute_error("can't delete attribute".to_owned()))
|
||||
|
||||
@@ -167,7 +167,7 @@ pub(crate) fn get_value(obj: &PyObjectRef) -> bool {
|
||||
}
|
||||
|
||||
fn get_py_int(obj: &PyObjectRef) -> &PyInt {
|
||||
&obj.payload::<PyInt>().unwrap()
|
||||
obj.payload::<PyInt>().unwrap()
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Copy, Clone, PartialEq)]
|
||||
|
||||
@@ -94,7 +94,7 @@ impl PyType {
|
||||
if let Some(r) = f(self) {
|
||||
Some(r)
|
||||
} else {
|
||||
self.mro.iter().find_map(|cls| f(&cls))
|
||||
self.mro.iter().find_map(|cls| f(cls))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -579,7 +579,7 @@ impl Callable for PyType {
|
||||
vm_trace!("type_call: {:?}", zelf);
|
||||
let obj = call_tp_new(zelf.clone(), zelf.clone(), args.clone(), vm)?;
|
||||
|
||||
if (zelf.is(&vm.ctx.types.type_type) && args.kwargs.is_empty()) || !obj.isinstance(&zelf) {
|
||||
if (zelf.is(&vm.ctx.types.type_type) && args.kwargs.is_empty()) || !obj.isinstance(zelf) {
|
||||
return Ok(obj);
|
||||
}
|
||||
|
||||
|
||||
@@ -37,7 +37,7 @@ impl PySlice {
|
||||
|
||||
fn start_ref<'a>(&'a self, vm: &'a VirtualMachine) -> &'a PyObjectRef {
|
||||
match &self.start {
|
||||
Some(v) => &v,
|
||||
Some(v) => v,
|
||||
None => vm.ctx.none.as_object(),
|
||||
}
|
||||
}
|
||||
@@ -54,7 +54,7 @@ impl PySlice {
|
||||
|
||||
fn step_ref<'a>(&'a self, vm: &'a VirtualMachine) -> &'a PyObjectRef {
|
||||
match &self.step {
|
||||
Some(v) => &v,
|
||||
Some(v) => v,
|
||||
None => vm.ctx.none.as_object(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,5 +67,5 @@ impl SlotSetattro for PyWeakProxy {
|
||||
}
|
||||
|
||||
pub fn init(context: &PyContext) {
|
||||
PyWeakProxy::extend_class(&context, &context.types.weakproxy_type);
|
||||
PyWeakProxy::extend_class(context, &context.types.weakproxy_type);
|
||||
}
|
||||
@@ -594,7 +594,7 @@ impl PyBytesInner {
|
||||
};
|
||||
|
||||
for i in self.elements.iter() {
|
||||
if !delete.contains(&i) {
|
||||
if !delete.contains(i) {
|
||||
res.push(table[*i as usize]);
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -50,7 +50,7 @@ impl PyRwBytesLike {
|
||||
|
||||
impl PyBytesLike {
|
||||
pub fn new(vm: &VirtualMachine, obj: &PyObjectRef) -> PyResult<Self> {
|
||||
let buffer = try_buffer_from_object(vm, &obj)?;
|
||||
let buffer = try_buffer_from_object(vm, obj)?;
|
||||
if buffer.get_options().contiguous {
|
||||
Ok(Self(buffer))
|
||||
} else {
|
||||
@@ -98,7 +98,7 @@ pub fn try_rw_bytes_like<R>(
|
||||
|
||||
impl PyRwBytesLike {
|
||||
pub fn new(vm: &VirtualMachine, obj: &PyObjectRef) -> PyResult<Self> {
|
||||
let buffer = try_buffer_from_object(vm, &obj)?;
|
||||
let buffer = try_buffer_from_object(vm, obj)?;
|
||||
let options = buffer.get_options();
|
||||
if !options.contiguous {
|
||||
Err(vm.new_type_error("non-contiguous buffer is not a bytes-like object".to_owned()))
|
||||
|
||||
+4
-4
@@ -563,7 +563,7 @@ fn try_update_quantity_from_tuple<'a, I: Iterator<Item = &'a PyObjectRef>>(
|
||||
if !width_obj.isinstance(&vm.ctx.types.int_type) {
|
||||
Err(vm.new_type_error("* wants int".to_owned()))
|
||||
} else {
|
||||
let i = int::get_value(&width_obj);
|
||||
let i = int::get_value(width_obj);
|
||||
let i = int::try_to_primitive::<isize>(i, vm)? as usize;
|
||||
*q = Some(CFormatQuantity::Amount(i));
|
||||
Ok(())
|
||||
@@ -818,7 +818,7 @@ impl CFormatString {
|
||||
{
|
||||
for (_, part) in &self.parts {
|
||||
match part {
|
||||
CFormatPart::Literal(literal) => result.push_str(&literal),
|
||||
CFormatPart::Literal(literal) => result.push_str(literal),
|
||||
CFormatPart::Spec(_) => unreachable!(),
|
||||
}
|
||||
}
|
||||
@@ -835,7 +835,7 @@ impl CFormatString {
|
||||
return if is_mapping {
|
||||
for (_, part) in &self.parts {
|
||||
match part {
|
||||
CFormatPart::Literal(literal) => result.push_str(&literal),
|
||||
CFormatPart::Literal(literal) => result.push_str(literal),
|
||||
CFormatPart::Spec(spec) => {
|
||||
let value = match &spec.mapping_key {
|
||||
Some(key) => values_obj.get_item(key, vm)?,
|
||||
@@ -862,7 +862,7 @@ impl CFormatString {
|
||||
|
||||
for (_, part) in &mut self.parts {
|
||||
match part {
|
||||
CFormatPart::Literal(literal) => result.push_str(&literal),
|
||||
CFormatPart::Literal(literal) => result.push_str(literal),
|
||||
CFormatPart::Spec(spec) => {
|
||||
try_update_quantity_from_tuple(vm, &mut value_iter, &mut spec.min_field_width)?;
|
||||
try_update_quantity_from_tuple(vm, &mut value_iter, &mut spec.precision)?;
|
||||
|
||||
@@ -824,7 +824,7 @@ impl serde::Serialize for SerializeException<'_> {
|
||||
|
||||
let rendered = {
|
||||
let mut rendered = String::new();
|
||||
write_exception(&mut rendered, self.vm, &self.exc).map_err(S::Error::custom)?;
|
||||
write_exception(&mut rendered, self.vm, self.exc).map_err(S::Error::custom)?;
|
||||
rendered
|
||||
};
|
||||
struc.serialize_field("rendered", &rendered)?;
|
||||
|
||||
+4
-4
@@ -135,7 +135,7 @@ fn parse_align(text: &str) -> (Option<FormatAlign>, &str) {
|
||||
let mut chars = text.chars();
|
||||
let maybe_align = chars.next().and_then(FormatAlign::from_char);
|
||||
if maybe_align.is_some() {
|
||||
(maybe_align, &chars.as_str())
|
||||
(maybe_align, chars.as_str())
|
||||
} else {
|
||||
(None, text)
|
||||
}
|
||||
@@ -202,7 +202,7 @@ fn parse_precision(text: &str) -> Result<(Option<usize>, &str), &'static str> {
|
||||
let mut chars = text.chars();
|
||||
Ok(match chars.next() {
|
||||
Some('.') => {
|
||||
let (size, remaining) = parse_number(&chars.as_str())?;
|
||||
let (size, remaining) = parse_number(chars.as_str())?;
|
||||
if size.is_some() {
|
||||
(size, remaining)
|
||||
} else {
|
||||
@@ -823,7 +823,7 @@ impl FormatString {
|
||||
}
|
||||
|
||||
let nested_format =
|
||||
FormatString::from_str(&format_spec).map_err(|e| e.into_pyexception(vm))?;
|
||||
FormatString::from_str(format_spec).map_err(|e| e.into_pyexception(vm))?;
|
||||
let format_spec = nested_format.format_internal(vm, field_func)?;
|
||||
|
||||
pystr = call_object_format(vm, argument, *preconversion_spec, &format_spec)?;
|
||||
@@ -879,7 +879,7 @@ impl FormatString {
|
||||
FieldType::Auto | FieldType::Index(_) => {
|
||||
Err(vm.new_value_error("Format string contains positional fields".to_owned()))
|
||||
}
|
||||
FieldType::Keyword(keyword) => dict.get_item(keyword, &vm),
|
||||
FieldType::Keyword(keyword) => dict.get_item(keyword, vm),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -218,7 +218,7 @@ impl FrameRef {
|
||||
globals: &self.globals,
|
||||
builtins: &self.builtins,
|
||||
lasti: &self.lasti,
|
||||
object: &self,
|
||||
object: self,
|
||||
state: &mut state,
|
||||
};
|
||||
f(exec)
|
||||
|
||||
+2
-2
@@ -90,10 +90,10 @@ impl<'s> serde::Serialize for PyObjectSerializer<'s> {
|
||||
let pairs: Vec<_> = dict.into_iter().collect();
|
||||
let mut map = serializer.serialize_map(Some(pairs.len()))?;
|
||||
for (key, e) in pairs.iter() {
|
||||
map.serialize_entry(&self.clone_with_object(key), &self.clone_with_object(&e))?;
|
||||
map.serialize_entry(&self.clone_with_object(key), &self.clone_with_object(e))?;
|
||||
}
|
||||
map.end()
|
||||
} else if self.vm.is_none(&self.pyobject) {
|
||||
} else if self.vm.is_none(self.pyobject) {
|
||||
serializer.serialize_none()
|
||||
} else {
|
||||
Err(serde::ser::Error::custom(format!(
|
||||
|
||||
+1
-1
@@ -1034,7 +1034,7 @@ pub trait PyClassImpl: PyClassDef {
|
||||
);
|
||||
}
|
||||
Self::impl_extend_class(ctx, class);
|
||||
ctx.add_slot_wrappers(&class);
|
||||
ctx.add_slot_wrappers(class);
|
||||
if let Some(doc) = Self::DOC {
|
||||
class.set_str_attr("__doc__", ctx.new_str(doc));
|
||||
}
|
||||
|
||||
@@ -554,7 +554,7 @@ pub(crate) fn init_type_hierarchy() -> (PyTypeRef, PyTypeRef) {
|
||||
object_type
|
||||
.subclasses
|
||||
.write()
|
||||
.push(PyWeak::downgrade(&type_type.as_object()));
|
||||
.push(PyWeak::downgrade(type_type.as_object()));
|
||||
|
||||
(type_type, object_type)
|
||||
}
|
||||
|
||||
@@ -760,7 +760,7 @@ impl PyArray {
|
||||
|
||||
#[pymethod(name = "__rmul__")]
|
||||
fn rmul(&self, counter: isize, vm: &VirtualMachine) -> PyRef<Self> {
|
||||
self.mul(counter, &vm)
|
||||
self.mul(counter, vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__imul__")]
|
||||
@@ -818,7 +818,7 @@ impl Comparable for PyArray {
|
||||
let other = class_or_notimplemented!(Self, other);
|
||||
|
||||
if let PyComparisonValue::Implemented(x) =
|
||||
op.eq_only(|| Ok(zelf.array_eq(&other, vm)?.into()))?
|
||||
op.eq_only(|| Ok(zelf.array_eq(other, vm)?.into()))?
|
||||
{
|
||||
return Ok(x.into());
|
||||
}
|
||||
|
||||
@@ -107,7 +107,7 @@ mod decl {
|
||||
let crc = value.unwrap_or(0);
|
||||
|
||||
let mut digest = crc32::Digest::new_with_initial(crc32::IEEE, crc);
|
||||
data.with_ref(|bytes| digest.write(&bytes));
|
||||
data.with_ref(|bytes| digest.write(bytes));
|
||||
|
||||
Ok(vm.ctx.new_int(digest.sum32()))
|
||||
}
|
||||
|
||||
+2
-2
@@ -516,7 +516,7 @@ mod _io {
|
||||
}
|
||||
#[pyslot]
|
||||
fn tp_iternext(instance: &PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
let line = vm.call_method(&instance, "readline", ())?;
|
||||
let line = vm.call_method(instance, "readline", ())?;
|
||||
if !pybool::boolval(vm, line.clone())? {
|
||||
Err(vm.new_stop_iteration())
|
||||
} else {
|
||||
@@ -1357,7 +1357,7 @@ mod _io {
|
||||
};
|
||||
match name {
|
||||
Some(name) => {
|
||||
if let Some(_guard) = ReprGuard::enter(vm, &obj) {
|
||||
if let Some(_guard) = ReprGuard::enter(vm, obj) {
|
||||
vm.to_repr(&name).map(Some)
|
||||
} else {
|
||||
Err(vm.new_runtime_error(format!(
|
||||
|
||||
@@ -640,7 +640,7 @@ mod decl {
|
||||
let old_key = {
|
||||
let mut state = zelf.groupby.state.lock();
|
||||
|
||||
if !state.is_current(&zelf) {
|
||||
if !state.is_current(zelf) {
|
||||
return Err(vm.new_stop_iteration());
|
||||
}
|
||||
|
||||
@@ -735,14 +735,14 @@ mod decl {
|
||||
) = args.bind(vm)?;
|
||||
|
||||
let step = if !vm.is_none(&step) {
|
||||
pyobject_to_opt_usize(step, "Step", &vm)?
|
||||
pyobject_to_opt_usize(step, "Step", vm)?
|
||||
} else {
|
||||
1usize
|
||||
};
|
||||
(iter, start, stop, step)
|
||||
};
|
||||
let start = if !vm.is_none(&start) {
|
||||
pyobject_to_opt_usize(start, "Start", &vm)?
|
||||
pyobject_to_opt_usize(start, "Start", vm)?
|
||||
} else {
|
||||
0usize
|
||||
};
|
||||
@@ -752,7 +752,7 @@ mod decl {
|
||||
};
|
||||
|
||||
let stop = if !vm.is_none(&stop) {
|
||||
Some(pyobject_to_opt_usize(stop, "Stop", &vm)?)
|
||||
Some(pyobject_to_opt_usize(stop, "Stop", vm)?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
@@ -311,7 +311,7 @@ fn math_trunc(value: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
fn math_ceil(value: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
let result_or_err = try_magic_method("__ceil__", vm, &value);
|
||||
if result_or_err.is_err() {
|
||||
if let Ok(Some(v)) = float::try_float_opt(&value, &vm) {
|
||||
if let Ok(Some(v)) = float::try_float_opt(&value, vm) {
|
||||
let v = float::try_bigint(v.ceil(), vm)?;
|
||||
return Ok(vm.ctx.new_int(v));
|
||||
}
|
||||
@@ -328,7 +328,7 @@ fn math_ceil(value: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
fn math_floor(value: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
let result_or_err = try_magic_method("__floor__", vm, &value);
|
||||
if result_or_err.is_err() {
|
||||
if let Ok(Some(v)) = float::try_float_opt(&value, &vm) {
|
||||
if let Ok(Some(v)) = float::try_float_opt(&value, vm) {
|
||||
let v = float::try_bigint(v.floor(), vm)?;
|
||||
return Ok(vm.ctx.new_int(v));
|
||||
}
|
||||
@@ -379,7 +379,7 @@ where
|
||||
|
||||
let mut res = argvec[0].as_bigint().clone();
|
||||
for num in argvec[1..].iter() {
|
||||
res = op(&res, &num)
|
||||
res = op(&res, num)
|
||||
}
|
||||
res
|
||||
}
|
||||
|
||||
+3
-3
@@ -1978,7 +1978,7 @@ impl<'a> SupportFunc {
|
||||
pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
|
||||
let module = platform::make_module(vm);
|
||||
|
||||
_os::extend_module(&vm, &module);
|
||||
_os::extend_module(vm, &module);
|
||||
|
||||
let support_funcs = _os::support_funcs();
|
||||
let supports_fd = PySet::default().into_ref(vm);
|
||||
@@ -2510,8 +2510,8 @@ mod posix {
|
||||
.into_iter()
|
||||
.map(|(k, v)| -> PyResult<_> {
|
||||
let (key, value) = (
|
||||
PyPathLike::try_from_object(&vm, k)?.into_bytes(),
|
||||
PyPathLike::try_from_object(&vm, v)?.into_bytes(),
|
||||
PyPathLike::try_from_object(vm, k)?.into_bytes(),
|
||||
PyPathLike::try_from_object(vm, v)?.into_bytes(),
|
||||
);
|
||||
|
||||
if memchr::memchr(b'=', &key).is_some() {
|
||||
|
||||
@@ -218,7 +218,7 @@ pub(crate) mod _struct {
|
||||
) -> PyResult<FormatSpec> {
|
||||
let decoded_fmt = match fmt {
|
||||
Either::A(string) => string.as_str(),
|
||||
Either::B(bytes) if bytes.is_ascii() => std::str::from_utf8(&bytes).unwrap(),
|
||||
Either::B(bytes) if bytes.is_ascii() => std::str::from_utf8(bytes).unwrap(),
|
||||
_ => {
|
||||
return Err(vm.new_unicode_decode_error(
|
||||
"Struct format must be a ascii string".to_owned(),
|
||||
|
||||
@@ -248,7 +248,7 @@ fn _thread_start_new_thread(
|
||||
)
|
||||
.map(|handle| {
|
||||
vm.state.thread_count.fetch_add(1);
|
||||
thread_to_id(&handle.thread())
|
||||
thread_to_id(handle.thread())
|
||||
})
|
||||
.map_err(|err| err.into_pyexception(vm))
|
||||
}
|
||||
|
||||
@@ -133,13 +133,13 @@ fn time_asctime(t: OptionalArg<PyStructTime>, vm: &VirtualMachine) -> PyResult {
|
||||
OptionalArg::Present(t) => t.to_date_time(vm)?,
|
||||
OptionalArg::Missing => default,
|
||||
};
|
||||
let formatted_time = instant.format(&CFMT).to_string();
|
||||
let formatted_time = instant.format(CFMT).to_string();
|
||||
Ok(vm.ctx.new_str(formatted_time))
|
||||
}
|
||||
|
||||
fn time_ctime(secs: OptionalArg<Either<f64, i64>>, vm: &VirtualMachine) -> PyResult<String> {
|
||||
let instant = optional_or_localtime(secs, vm)?;
|
||||
Ok(instant.format(&CFMT).to_string())
|
||||
Ok(instant.format(CFMT).to_string())
|
||||
}
|
||||
|
||||
fn time_strftime(format: PyStrRef, t: OptionalArg<PyStructTime>, vm: &VirtualMachine) -> PyResult {
|
||||
|
||||
+40
-40
@@ -202,46 +202,46 @@ impl TypeZoo {
|
||||
|
||||
/// Fill attributes of builtin types.
|
||||
pub(crate) fn extend(context: &PyContext) {
|
||||
pytype::init(&context);
|
||||
object::init(&context);
|
||||
list::init(&context);
|
||||
set::init(&context);
|
||||
tuple::init(&context);
|
||||
dict::init(&context);
|
||||
builtinfunc::init(&context);
|
||||
function::init(&context);
|
||||
staticmethod::init(&context);
|
||||
classmethod::init(&context);
|
||||
generator::init(&context);
|
||||
coroutine::init(&context);
|
||||
asyncgenerator::init(&context);
|
||||
int::init(&context);
|
||||
float::init(&context);
|
||||
complex::init(&context);
|
||||
bytes::init(&context);
|
||||
bytearray::init(&context);
|
||||
property::init(&context);
|
||||
getset::init(&context);
|
||||
memory::init(&context);
|
||||
pystr::init(&context);
|
||||
range::init(&context);
|
||||
slice::init(&context);
|
||||
pysuper::init(&context);
|
||||
iter::init(&context);
|
||||
enumerate::init(&context);
|
||||
filter::init(&context);
|
||||
map::init(&context);
|
||||
zip::init(&context);
|
||||
pybool::init(&context);
|
||||
code::init(&context);
|
||||
frame::init(&context);
|
||||
weakref::init(&context);
|
||||
weakproxy::init(&context);
|
||||
singletons::init(&context);
|
||||
module::init(&context);
|
||||
namespace::init(&context);
|
||||
mappingproxy::init(&context);
|
||||
traceback::init(&context);
|
||||
pytype::init(context);
|
||||
object::init(context);
|
||||
list::init(context);
|
||||
set::init(context);
|
||||
tuple::init(context);
|
||||
dict::init(context);
|
||||
builtinfunc::init(context);
|
||||
function::init(context);
|
||||
staticmethod::init(context);
|
||||
classmethod::init(context);
|
||||
generator::init(context);
|
||||
coroutine::init(context);
|
||||
asyncgenerator::init(context);
|
||||
int::init(context);
|
||||
float::init(context);
|
||||
complex::init(context);
|
||||
bytes::init(context);
|
||||
bytearray::init(context);
|
||||
property::init(context);
|
||||
getset::init(context);
|
||||
memory::init(context);
|
||||
pystr::init(context);
|
||||
range::init(context);
|
||||
slice::init(context);
|
||||
pysuper::init(context);
|
||||
iter::init(context);
|
||||
enumerate::init(context);
|
||||
filter::init(context);
|
||||
map::init(context);
|
||||
zip::init(context);
|
||||
pybool::init(context);
|
||||
code::init(context);
|
||||
frame::init(context);
|
||||
weakref::init(context);
|
||||
weakproxy::init(context);
|
||||
singletons::init(context);
|
||||
module::init(context);
|
||||
namespace::init(context);
|
||||
mappingproxy::init(context);
|
||||
traceback::init(context);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -1708,7 +1708,7 @@ impl VirtualMachine {
|
||||
.class()
|
||||
.mro_find_map(|cls| cls.slots.hash.load())
|
||||
.unwrap(); // hash always exist
|
||||
hash(&obj, self)
|
||||
hash(obj, self)
|
||||
}
|
||||
|
||||
pub fn obj_len_opt(&self, obj: &PyObjectRef) -> Option<PyResult<usize>> {
|
||||
|
||||
Reference in new issue
Block a user