Author: [email protected]
Branch: py3.7
Changeset: r97938:04142a3f29de
Date: 2019-11-02 13:50 +0000
http://bitbucket.org/pypy/pypy/changeset/04142a3f29de/
Log: Implementing isascii for bytes and bytesarray (see bpo-32677)
diff --git a/pypy/objspace/std/bytearrayobject.py
b/pypy/objspace/std/bytearrayobject.py
--- a/pypy/objspace/std/bytearrayobject.py
+++ b/pypy/objspace/std/bytearrayobject.py
@@ -355,6 +355,12 @@
return space.w_NotImplemented
return space.newbool(cmp > 0 or (cmp == 0 and self._len() >=
other_len))
+ def descr_isascii(self, space):
+ for i in self._data[self._offset:]:
+ if ord(i) > 127:
+ return space.w_False
+ return space.w_True
+
def descr_inplace_add(self, space, w_other):
if isinstance(w_other, W_BytearrayObject):
self._data += w_other.getdata()
@@ -815,6 +821,12 @@
and there is at least one character in B, False otherwise.
"""
+ def isascii():
+ """B.isascii() -> bool
+
+ Return true if the string is empty or all characters in the string are
ASCII, false otherwise.
+ ASCII characters have code points in the range U+0000-U+007F."""
+
def isdigit():
"""B.isdigit() -> bool
@@ -1112,6 +1124,8 @@
doc=BytearrayDocstrings.isalnum.__doc__),
isalpha = interp2app(W_BytearrayObject.descr_isalpha,
doc=BytearrayDocstrings.isalpha.__doc__),
+ isascii = interp2app(W_BytearrayObject.descr_isascii,
+ doc=BytearrayDocstrings.isascii.__doc__),
isdigit = interp2app(W_BytearrayObject.descr_isdigit,
doc=BytearrayDocstrings.isdigit.__doc__),
islower = interp2app(W_BytearrayObject.descr_islower,
diff --git a/pypy/objspace/std/bytesobject.py b/pypy/objspace/std/bytesobject.py
--- a/pypy/objspace/std/bytesobject.py
+++ b/pypy/objspace/std/bytesobject.py
@@ -73,6 +73,12 @@
def descr_hash(self, space):
"""x.__hash__() <==> hash(x)"""
+ def descr_isascii(self, space):
+ """B.isascii() -> bool
+
+ Return true if the string is empty or all characters in the string are
ASCII, false otherwise.
+ ASCII characters have code points in the range U+0000-U+007F."""
+
def descr_iter(self, space):
"""x.__iter__() <==> iter(x)"""
@@ -601,6 +607,12 @@
x -= (x == -1) # convert -1 to -2 without creating a bridge
return space.newint(x)
+ def descr_isascii(self, space):
+ for i in self._value:
+ if ord(i) > 127:
+ return space.w_False
+ return space.w_True
+
def descr_eq(self, space, w_other):
if not isinstance(w_other, W_BytesObject):
return space.w_NotImplemented
@@ -862,6 +874,7 @@
isspace = interpindirect2app(W_AbstractBytesObject.descr_isspace),
istitle = interpindirect2app(W_AbstractBytesObject.descr_istitle),
isupper = interpindirect2app(W_AbstractBytesObject.descr_isupper),
+ isascii = interpindirect2app(W_AbstractBytesObject.descr_isascii),
join = interpindirect2app(W_AbstractBytesObject.descr_join),
ljust = interpindirect2app(W_AbstractBytesObject.descr_ljust),
rjust = interpindirect2app(W_AbstractBytesObject.descr_rjust),
diff --git a/pypy/objspace/std/test/test_bytearrayobject.py
b/pypy/objspace/std/test/test_bytearrayobject.py
--- a/pypy/objspace/std/test/test_bytearrayobject.py
+++ b/pypy/objspace/std/test/test_bytearrayobject.py
@@ -605,6 +605,17 @@
def test_hex(self):
assert bytearray(b'santa claus').hex() == "73616e746120636c617573"
+ def test_isascii(self):
+ assert bytearray(b'hello world').isascii() is True
+ assert bytearray(b'\x00\x7f').isascii() is True
+ assert bytearray(b'\x80').isascii() is False
+ ba = bytearray(b"\xff\xffHello World")
+ assert ba.isascii() is False
+ del ba[0:1]
+ assert ba.isascii() is False
+ del ba[0:1]
+ assert ba.isascii() is True
+
def test_format(self):
"""
assert bytearray(b'a%db') % 2 == b'a2b'
diff --git a/pypy/objspace/std/test/test_bytesobject.py
b/pypy/objspace/std/test/test_bytesobject.py
--- a/pypy/objspace/std/test/test_bytesobject.py
+++ b/pypy/objspace/std/test/test_bytesobject.py
@@ -281,6 +281,15 @@
assert b'aaaa'.capitalize() == b'Aaaa'
assert b'AaAa'.capitalize() == b'Aaaa'
+ def test_isascii(self):
+ assert b"hello".isascii() is True
+ assert b"\x00\x7f".isascii() is True
+ assert b"\x80".isascii() is False
+ assert b"\x97".isascii() is False
+ assert b"\xff".isascii() is False
+ assert b"Hello World\x00".isascii() is True
+ assert b"Hello World\x80".isascii() is False
+
def test_rjust(self):
s = b"abc"
assert s.rjust(2) == s
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit