Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
General fixes
  • Loading branch information
ShaharNaveh committed Jul 2, 2025
commit 8c8ccdccf0812cf48f43c9f822f0ab9844e268cc
32 changes: 24 additions & 8 deletions vm/src/stdlib/sre.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,26 @@ mod _sre {
pub use rustpython_sre_engine::{CODESIZE, MAXGROUPS, MAXREPEAT, SRE_MAGIC as MAGIC};

#[pyfunction]
fn getcodesize() -> usize {
const fn getcodesize() -> usize {
CODESIZE
}

#[pyfunction]
fn ascii_iscased(ch: i32) -> bool {
(ch >= b'a' as i32 && ch <= b'z' as i32) || (ch >= b'A' as i32 && ch <= b'Z' as i32)
(b'a' as i32..=b'z' as i32).contains(&ch) || (b'A' as i32..=b'Z' as i32).contains(&ch)
}

#[pyfunction]
fn unicode_iscased(ch: i32) -> bool {
let ch = ch as u32;
ch != lower_unicode(ch) || ch != upper_unicode(ch)
}

#[pyfunction]
fn ascii_tolower(ch: i32) -> i32 {
lower_ascii(ch as u32) as i32
}

#[pyfunction]
fn unicode_tolower(ch: i32) -> i32 {
lower_unicode(ch as u32) as i32
Expand Down Expand Up @@ -348,6 +352,7 @@ mod _sre {
fn sub(zelf: PyRef<Pattern>, sub_args: SubArgs, vm: &VirtualMachine) -> PyResult {
Self::sub_impl(zelf, sub_args, false, vm)
}

#[pymethod]
fn subn(zelf: PyRef<Pattern>, sub_args: SubArgs, vm: &VirtualMachine) -> PyResult {
Self::sub_impl(zelf, sub_args, true, vm)
Expand Down Expand Up @@ -394,14 +399,17 @@ mod _sre {
fn flags(&self) -> u16 {
self.flags.bits()
}

#[pygetset]
fn groupindex(&self) -> PyDictRef {
self.groupindex.clone()
}

#[pygetset]
fn groups(&self) -> usize {
const fn groups(&self) -> usize {
self.groups
}

#[pygetset]
fn pattern(&self) -> PyObjectRef {
self.pattern.clone()
Expand Down Expand Up @@ -630,34 +638,40 @@ mod _sre {
}

#[pygetset]
fn pos(&self) -> usize {
const fn pos(&self) -> usize {
self.pos
}

#[pygetset]
fn endpos(&self) -> usize {
const fn endpos(&self) -> usize {
self.endpos
}

#[pygetset]
fn lastindex(&self) -> Option<isize> {
const fn lastindex(&self) -> Option<isize> {
if self.lastindex >= 0 {
Some(self.lastindex)
} else {
None
}
}

#[pygetset]
fn lastgroup(&self) -> Option<PyStrRef> {
let i = self.lastindex.to_usize()?;
self.pattern.indexgroup.get(i)?.clone()
}

#[pygetset]
fn re(&self) -> PyRef<Pattern> {
self.pattern.clone()
}

#[pygetset]
fn string(&self) -> PyObjectRef {
self.string.clone()
}

#[pygetset]
fn regs(&self, vm: &VirtualMachine) -> PyTupleRef {
PyTuple::new_ref(
Expand All @@ -670,10 +684,12 @@ mod _sre {
fn start(&self, group: OptionalArg<PyObjectRef>, vm: &VirtualMachine) -> PyResult<isize> {
self.span(group, vm).map(|x| x.0)
}

#[pymethod]
fn end(&self, group: OptionalArg<PyObjectRef>, vm: &VirtualMachine) -> PyResult<isize> {
self.span(group, vm).map(|x| x.1)
}

#[pymethod]
fn span(
&self,
Expand Down Expand Up @@ -705,7 +721,7 @@ mod _sre {
.into_iter()
.map(|x| {
self.get_index(x, vm)
.ok_or_else(|| vm.new_index_error("no such group".to_owned()))
.ok_or_else(|| vm.new_index_error("no such group"))
.map(|index| {
self.get_slice(index, str_drive, vm)
.map(|x| x.to_pyobject(vm))
Expand All @@ -730,7 +746,7 @@ mod _sre {
with_sre_str!(self.pattern, &self.string, vm, |str_drive| {
let i = self
.get_index(group, vm)
.ok_or_else(|| vm.new_index_error("no such group".to_owned()))?;
.ok_or_else(|| vm.new_index_error("no such group"))?;
Ok(self.get_slice(i, str_drive, vm))
})
}
Expand Down
24 changes: 12 additions & 12 deletions vm/src/stdlib/symtable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ mod symtable {
}

#[pymethod]
fn get_lineno(&self) -> u32 {
const fn get_lineno(&self) -> u32 {
self.symtable.line_number
}

#[pymethod]
fn is_nested(&self) -> bool {
const fn is_nested(&self) -> bool {
self.symtable.is_nested
}

Expand Down Expand Up @@ -131,7 +131,7 @@ mod symtable {
}

#[pymethod]
fn has_children(&self) -> bool {
const fn has_children(&self) -> bool {
!self.symtable.sub_tables.is_empty()
}

Expand Down Expand Up @@ -175,7 +175,7 @@ mod symtable {
}

#[pymethod]
fn is_declared_global(&self) -> bool {
const fn is_declared_global(&self) -> bool {
matches!(self.symbol.scope, SymbolScope::GlobalExplicit)
}

Expand All @@ -185,7 +185,7 @@ mod symtable {
}

#[pymethod]
fn is_imported(&self) -> bool {
const fn is_imported(&self) -> bool {
self.symbol.flags.contains(SymbolFlags::IMPORTED)
}

Expand All @@ -196,37 +196,37 @@ mod symtable {
}

#[pymethod]
fn is_nonlocal(&self) -> bool {
const fn is_nonlocal(&self) -> bool {
self.symbol.flags.contains(SymbolFlags::NONLOCAL)
}

#[pymethod]
fn is_referenced(&self) -> bool {
const fn is_referenced(&self) -> bool {
self.symbol.flags.contains(SymbolFlags::REFERENCED)
}

#[pymethod]
fn is_assigned(&self) -> bool {
const fn is_assigned(&self) -> bool {
self.symbol.flags.contains(SymbolFlags::ASSIGNED)
}

#[pymethod]
fn is_parameter(&self) -> bool {
const fn is_parameter(&self) -> bool {
self.symbol.flags.contains(SymbolFlags::PARAMETER)
}

#[pymethod]
fn is_free(&self) -> bool {
const fn is_free(&self) -> bool {
matches!(self.symbol.scope, SymbolScope::Free)
}

#[pymethod]
fn is_namespace(&self) -> bool {
const fn is_namespace(&self) -> bool {
!self.namespaces.is_empty()
}

#[pymethod]
fn is_annotated(&self) -> bool {
const fn is_annotated(&self) -> bool {
self.symbol.flags.contains(SymbolFlags::ANNOTATED)
}

Expand Down