mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 18:59:04 +08:00
Remove useless and_then
This commit is contained in:
1 parent
ad29fbdd55
commit
12c96d06e4
12 files changed
+92
-110
No files matched your search
@@ -414,21 +414,20 @@ impl SymbolTableAnalyzer {
|
||||
st_typ: SymbolTableType,
|
||||
) -> Option<SymbolScope> {
|
||||
sub_tables.iter().find_map(|st| {
|
||||
st.symbols.get(name).and_then(|sym| {
|
||||
if sym.scope == SymbolScope::Free || sym.flags.contains(SymbolFlags::FREE_CLASS) {
|
||||
if st_typ == SymbolTableType::Class && name != "__class__" {
|
||||
None
|
||||
} else {
|
||||
Some(SymbolScope::Cell)
|
||||
}
|
||||
} else if sym.scope == SymbolScope::GlobalExplicit && self.tables.is_empty() {
|
||||
// the symbol is defined on the module level, and an inner scope declares
|
||||
// a global that points to it
|
||||
Some(SymbolScope::GlobalExplicit)
|
||||
} else {
|
||||
let sym = st.symbols.get(name)?;
|
||||
if sym.scope == SymbolScope::Free || sym.flags.contains(SymbolFlags::FREE_CLASS) {
|
||||
if st_typ == SymbolTableType::Class && name != "__class__" {
|
||||
None
|
||||
} else {
|
||||
Some(SymbolScope::Cell)
|
||||
}
|
||||
})
|
||||
} else if sym.scope == SymbolScope::GlobalExplicit && self.tables.is_empty() {
|
||||
// the symbol is defined on the module level, and an inner scope declares
|
||||
// a global that points to it
|
||||
Some(SymbolScope::GlobalExplicit)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -809,7 +809,7 @@ mod _sqlite {
|
||||
type Args = ConnectArgs;
|
||||
|
||||
fn py_new(cls: PyTypeRef, args: Self::Args, vm: &VirtualMachine) -> PyResult {
|
||||
Self::new(args, vm).and_then(|x| x.into_ref_with_type(vm, cls).map(Into::into))
|
||||
Ok(Self::new(args, vm)?.into_ref_with_type(vm, cls)?.into())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+8
-13
@@ -248,19 +248,14 @@ mod zlib {
|
||||
} = args;
|
||||
data.with_ref(|data| {
|
||||
let mut d = InitOptions::new(wbits.value, vm)?.decompress();
|
||||
|
||||
_decompress(data, &mut d, bufsize.value, None, false, vm).and_then(
|
||||
|(buf, stream_end)| {
|
||||
if stream_end {
|
||||
Ok(buf)
|
||||
} else {
|
||||
Err(new_zlib_error(
|
||||
"Error -5 while decompressing data: incomplete or truncated stream",
|
||||
vm,
|
||||
))
|
||||
}
|
||||
},
|
||||
)
|
||||
let (buf, stream_end) = _decompress(data, &mut d, bufsize.value, None, false, vm)?;
|
||||
if !stream_end {
|
||||
return Err(new_zlib_error(
|
||||
"Error -5 while decompressing data: incomplete or truncated stream",
|
||||
vm,
|
||||
));
|
||||
}
|
||||
Ok(buf)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -147,10 +147,10 @@ impl PyBuiltinFunction {
|
||||
}
|
||||
#[pygetset(magic)]
|
||||
fn text_signature(&self) -> Option<String> {
|
||||
self.value.doc.as_ref().and_then(|doc| {
|
||||
type_::get_text_signature_from_internal_doc(self.value.name.as_str(), doc.as_str())
|
||||
.map(|signature| signature.to_string())
|
||||
})
|
||||
let doc = self.value.doc.as_ref()?;
|
||||
let signature =
|
||||
type_::get_text_signature_from_internal_doc(self.value.name.as_str(), doc.as_str())?;
|
||||
Some(signature.to_owned())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -61,10 +61,11 @@ impl PyModule {
|
||||
}
|
||||
|
||||
fn name(zelf: PyRef<Self>, vm: &VirtualMachine) -> Option<PyStrRef> {
|
||||
zelf.as_object()
|
||||
let name = zelf
|
||||
.as_object()
|
||||
.generic_getattr_opt(identifier!(vm, __name__).to_owned(), None, vm)
|
||||
.unwrap_or(None)
|
||||
.and_then(|obj| obj.downcast::<PyStr>().ok())
|
||||
.unwrap_or_default()?;
|
||||
name.downcast::<PyStr>().ok()
|
||||
}
|
||||
|
||||
#[pymethod(magic)]
|
||||
@@ -145,10 +146,9 @@ impl Representable for PyModule {
|
||||
fn repr(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyStrRef> {
|
||||
let importlib = vm.import("_frozen_importlib", None, 0)?;
|
||||
let module_repr = importlib.get_attr("_module_repr", vm)?;
|
||||
module_repr.call((zelf.to_owned(),), vm).and_then(|obj| {
|
||||
obj.downcast()
|
||||
.map_err(|_| vm.new_type_error("_module_repr did not return a string".into()))
|
||||
})
|
||||
let repr = module_repr.call((zelf.to_owned(),), vm)?;
|
||||
repr.downcast()
|
||||
.map_err(|_| vm.new_type_error("_module_repr did not return a string".into()))
|
||||
}
|
||||
|
||||
#[cold]
|
||||
|
||||
+15
-21
@@ -81,30 +81,24 @@ pub fn make_frozen(vm: &VirtualMachine, name: &str) -> PyResult<PyRef<PyCode>> {
|
||||
}
|
||||
|
||||
pub fn import_frozen(vm: &VirtualMachine, module_name: &str) -> PyResult {
|
||||
make_frozen(vm, module_name).and_then(|frozen| {
|
||||
let module = import_codeobj(vm, module_name, frozen, false)?;
|
||||
// TODO: give a correct origname here
|
||||
module.set_attr("__origname__", vm.ctx.new_str(module_name.to_owned()), vm)?;
|
||||
Ok(module)
|
||||
})
|
||||
let frozen = make_frozen(vm, module_name)?;
|
||||
let module = import_codeobj(vm, module_name, frozen, false)?;
|
||||
// TODO: give a correct origname here
|
||||
module.set_attr("__origname__", vm.ctx.new_str(module_name.to_owned()), vm)?;
|
||||
Ok(module)
|
||||
}
|
||||
|
||||
pub fn import_builtin(vm: &VirtualMachine, module_name: &str) -> PyResult {
|
||||
vm.state
|
||||
.module_inits
|
||||
.get(module_name)
|
||||
.ok_or_else(|| {
|
||||
vm.new_import_error(
|
||||
format!("Cannot import builtin module {module_name}"),
|
||||
module_name,
|
||||
)
|
||||
})
|
||||
.and_then(|make_module_func| {
|
||||
let module = make_module_func(vm);
|
||||
let sys_modules = vm.sys_module.get_attr("modules", vm)?;
|
||||
sys_modules.set_item(module_name, module.clone(), vm)?;
|
||||
Ok(module)
|
||||
})
|
||||
let make_module_func = vm.state.module_inits.get(module_name).ok_or_else(|| {
|
||||
vm.new_import_error(
|
||||
format!("Cannot import builtin module {module_name}"),
|
||||
module_name,
|
||||
)
|
||||
})?;
|
||||
let module = make_module_func(vm);
|
||||
let sys_modules = vm.sys_module.get_attr("modules", vm)?;
|
||||
sys_modules.set_item(module_name, module.clone(), vm)?;
|
||||
Ok(module)
|
||||
}
|
||||
|
||||
#[cfg(feature = "rustpython-compiler")]
|
||||
|
||||
@@ -197,7 +197,7 @@ impl ToPyResult for PyIterReturn {
|
||||
|
||||
impl ToPyResult for PyResult<PyIterReturn> {
|
||||
fn to_pyresult(self, vm: &VirtualMachine) -> PyResult {
|
||||
self.and_then(|obj| obj.to_pyresult(vm))
|
||||
self?.to_pyresult(vm)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -234,11 +234,11 @@ where
|
||||
type Item = PyResult<T>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
PyIter::new(self.obj.borrow())
|
||||
let obj = PyIter::new(self.obj.borrow())
|
||||
.next(self.vm)
|
||||
.map(|iret| iret.into_result().ok())
|
||||
.transpose()
|
||||
.map(|x| x.and_then(|obj| T::try_from_object(self.vm, obj)))
|
||||
.transpose()?;
|
||||
Some(obj.and_then(|obj| T::try_from_object(self.vm, obj)))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
||||
+14
-15
@@ -461,20 +461,20 @@ impl PyObject {
|
||||
}
|
||||
|
||||
fn abstract_isinstance(&self, cls: &PyObject, vm: &VirtualMachine) -> PyResult<bool> {
|
||||
if let Ok(typ) = PyTypeRef::try_from_object(vm, cls.to_owned()) {
|
||||
let r = if let Ok(typ) = PyTypeRef::try_from_object(vm, cls.to_owned()) {
|
||||
if self.class().fast_issubclass(&typ) {
|
||||
Ok(true)
|
||||
true
|
||||
} else if let Ok(icls) = PyTypeRef::try_from_object(
|
||||
vm,
|
||||
self.to_owned().get_attr(identifier!(vm, __class__), vm)?,
|
||||
) {
|
||||
if icls.is(self.class()) {
|
||||
Ok(false)
|
||||
false
|
||||
} else {
|
||||
Ok(icls.fast_issubclass(&typ))
|
||||
icls.fast_issubclass(&typ)
|
||||
}
|
||||
} else {
|
||||
Ok(false)
|
||||
false
|
||||
}
|
||||
} else {
|
||||
self.check_cls(cls, vm, || {
|
||||
@@ -482,16 +482,15 @@ impl PyObject {
|
||||
"isinstance() arg 2 must be a type or tuple of types, not {}",
|
||||
cls.class()
|
||||
)
|
||||
})
|
||||
.and_then(|_| {
|
||||
let icls: PyObjectRef = self.to_owned().get_attr(identifier!(vm, __class__), vm)?;
|
||||
if vm.is_none(&icls) {
|
||||
Ok(false)
|
||||
} else {
|
||||
icls.abstract_issubclass(cls, vm)
|
||||
}
|
||||
})
|
||||
}
|
||||
})?;
|
||||
let icls: PyObjectRef = self.to_owned().get_attr(identifier!(vm, __class__), vm)?;
|
||||
if vm.is_none(&icls) {
|
||||
false
|
||||
} else {
|
||||
icls.abstract_issubclass(cls, vm)?
|
||||
}
|
||||
};
|
||||
Ok(r)
|
||||
}
|
||||
|
||||
/// Determines if `self` is an instance of `cls`, either directly, indirectly or virtually via
|
||||
|
||||
@@ -84,14 +84,12 @@ mod builtins {
|
||||
|
||||
#[pyfunction]
|
||||
fn chr(i: PyIntRef, vm: &VirtualMachine) -> PyResult<String> {
|
||||
match i
|
||||
let value = i
|
||||
.try_to_primitive::<isize>(vm)?
|
||||
.to_u32()
|
||||
.and_then(char::from_u32)
|
||||
{
|
||||
Some(value) => Ok(value.to_string()),
|
||||
None => Err(vm.new_value_error("chr() arg not in range(0x110000)".to_owned())),
|
||||
}
|
||||
.ok_or_else(|| vm.new_value_error("chr() arg not in range(0x110000)".to_owned()))?;
|
||||
Ok(value.to_string())
|
||||
}
|
||||
|
||||
#[derive(FromArgs)]
|
||||
|
||||
+6
-6
@@ -175,7 +175,7 @@ mod _io {
|
||||
impl OptionalSize {
|
||||
#[allow(clippy::wrong_self_convention)]
|
||||
pub fn to_usize(self) -> Option<usize> {
|
||||
self.size.and_then(|v| v.to_usize())
|
||||
self.size?.to_usize()
|
||||
}
|
||||
|
||||
pub fn try_usize(self, vm: &VirtualMachine) -> PyResult<Option<usize>> {
|
||||
@@ -2225,11 +2225,11 @@ mod _io {
|
||||
codec.get_incremental_encoder(Some(errors.clone()), vm)?;
|
||||
let encoding_name = vm.get_attribute_opt(incremental_encoder.clone(), "name")?;
|
||||
let encodefunc = encoding_name.and_then(|name| {
|
||||
name.payload::<PyStr>()
|
||||
.and_then(|name| match name.as_str() {
|
||||
"utf-8" => Some(textio_encode_utf8 as EncodeFunc),
|
||||
_ => None,
|
||||
})
|
||||
let name = name.payload::<PyStr>()?;
|
||||
match name.as_str() {
|
||||
"utf-8" => Some(textio_encode_utf8 as EncodeFunc),
|
||||
_ => None,
|
||||
}
|
||||
});
|
||||
Some((incremental_encoder, encodefunc))
|
||||
} else {
|
||||
|
||||
@@ -157,17 +157,17 @@ mod _sre {
|
||||
where
|
||||
F: FnOnce(&str) -> PyResult<R>,
|
||||
{
|
||||
string
|
||||
let string = string
|
||||
.payload::<PyStr>()
|
||||
.ok_or_else(|| vm.new_type_error("expected string".to_owned()))
|
||||
.and_then(|x| f(x.as_str()))
|
||||
.ok_or_else(|| vm.new_type_error("expected string".to_owned()))?;
|
||||
f(string.as_str())
|
||||
}
|
||||
|
||||
fn with_bytes<F, R>(string: &PyObject, vm: &VirtualMachine, f: F) -> PyResult<R>
|
||||
where
|
||||
F: FnOnce(&[u8]) -> PyResult<R>,
|
||||
{
|
||||
PyBuffer::try_from_borrowed_object(vm, string).and_then(|x| x.contiguous_or_collect(f))
|
||||
PyBuffer::try_from_borrowed_object(vm, string)?.contiguous_or_collect(f)
|
||||
}
|
||||
|
||||
#[pymethod(name = "match")]
|
||||
@@ -572,9 +572,8 @@ mod _sre {
|
||||
}
|
||||
#[pygetset]
|
||||
fn lastgroup(&self) -> Option<PyStrRef> {
|
||||
self.lastindex
|
||||
.to_usize()
|
||||
.and_then(|i| self.pattern.indexgroup.get(i).cloned().flatten())
|
||||
let i = self.lastindex.to_usize()?;
|
||||
self.pattern.indexgroup.get(i)?.clone()
|
||||
}
|
||||
#[pygetset]
|
||||
fn re(&self) -> PyRef<Pattern> {
|
||||
|
||||
+12
-14
@@ -188,13 +188,12 @@ mod winreg {
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult<(PyObjectRef, usize)> {
|
||||
let subkey = subkey.as_ref().map_or("", |s| s.as_str());
|
||||
key.with_key(|k| k.get_raw_value(subkey))
|
||||
.map_err(|e| e.to_pyexception(vm))
|
||||
.and_then(|regval| {
|
||||
#[allow(clippy::redundant_clone)]
|
||||
let ty = regval.vtype.clone() as usize;
|
||||
Ok((reg_to_py(regval, vm)?, ty))
|
||||
})
|
||||
let regval = key
|
||||
.with_key(|k| k.get_raw_value(subkey))
|
||||
.map_err(|e| e.to_pyexception(vm))?;
|
||||
#[allow(clippy::redundant_clone)]
|
||||
let ty = regval.vtype.clone() as usize;
|
||||
Ok((reg_to_py(regval, vm)?, ty))
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
@@ -214,18 +213,17 @@ mod winreg {
|
||||
index: u32,
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult<(String, PyObjectRef, usize)> {
|
||||
key.with_key(|k| k.enum_values().nth(index as usize))
|
||||
let (name, value) = key
|
||||
.with_key(|k| k.enum_values().nth(index as usize))
|
||||
.unwrap_or_else(|| {
|
||||
Err(io::Error::from_raw_os_error(
|
||||
winerror::ERROR_NO_MORE_ITEMS as i32,
|
||||
))
|
||||
})
|
||||
.map_err(|e| e.to_pyexception(vm))
|
||||
.and_then(|(name, value)| {
|
||||
#[allow(clippy::redundant_clone)]
|
||||
let ty = value.vtype.clone() as usize;
|
||||
Ok((name, reg_to_py(value, vm)?, ty))
|
||||
})
|
||||
.map_err(|e| e.to_pyexception(vm))?;
|
||||
#[allow(clippy::redundant_clone)]
|
||||
let ty = value.vtype.clone() as usize;
|
||||
Ok((name, reg_to_py(value, vm)?, ty))
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
|
||||
Reference in new issue
Block a user