8000 Fix bytes.isspace by hbina · Pull Request #5655 · RustPython/RustPython · GitHub
[go: up one dir, main page]

Skip to content

Fix bytes.isspace #5655

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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Next Next commit
Fix implementation of bytes.isspace to match the implementation in CP…
…ython.
  • Loading branch information
hbina committed Apr 5, 2025
commit 027f1152bfc8eb97d233d900fb80dbf7595204b7
23 changes: 0 additions & 23 deletions Lib/test/test_bigmem.py
Original file line number Diff line number Diff line change
Expand Up @@ -786,17 +786,6 @@ def test_title(self, size):
def test_swapcase(self, size):
self._test_swapcase(size)

# TODO: RUSTPYTHON
@unittest.expectedFailure
@bigmemtest(size=_2G, memuse=2)
def test_isspace(self, size):
super().test_isspace(size)

# TODO: RUSTPYTHON
@unittest.expectedFailure
@bigmemtest(size=_2G, memuse=2)
def test_istitle(self, size):
super().test_istitle(size)

class BytearrayTest(unittest.TestCase, BaseStrTest):

Expand All @@ -823,18 +812,6 @@ def test_swapcase(self, size):
test_hash = None
test_split_large = None

# TODO: RUSTPYTHON
@unittest.expectedFailure
@bigmemtest(size=_2G, memuse=2)
def test_isspace(self, size):
super().test_isspace(size)

# TODO: RUSTPYTHON
@unittest.expectedFailure
@bigmemtest(size=_2G, memuse=2)
def test_istitle(self, size):
super().test_istitle(size)

class TupleTest(unittest.TestCase):

# Tuples have a small, fixed-sized head and an array of pointers to
Expand Down
5 changes: 4 additions & 1 deletion extra_tests/snippets/builtin_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,4 +668,7 @@ def __new__(cls, value):
b = B1.fromhex('a0a1a2')
assert b.foo == 'bar'

skip_if_unsupported(3,11,test__bytes__)
skip_if_unsupported(3,11,test__bytes__)

assert " \f\n\r\t\v".encode("utf-8").isspace()
assert " \f\n\r\t\v".encode("latin-1").isspace()
9 changes: 8 additions & 1 deletion vm/src/bytes_inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,11 +364,18 @@ impl PyBytesInner {
}

pub fn isspace(&self) -> bool {
// What CPython considers whitespace is a bit different from what Rust.
// In particular, Rust does not consider vertical tabulation (\x0B) to be a whitespace.
// See https://docs.python.org/3/library/stdtypes.html#bytearray.isspace
// See https://doc.rust-lang.org/std/primitive.char.html#method.is_ascii_whitespace
// Note that str.isspace uses a different definition too.
// See https://docs.python.org/3/library/stdtypes.html#str.isspace
!self.elements.is_empty()
&& self
.elements
.iter()
.all(|x| char::from(*x).is_ascii_whitespace())
.map(|c| char::from(*c))
.all(|c| c.is_ascii_whitespace() || c == '\x0b')
}

pub fn istitle(&self) -> bool {
Expand Down
0