Skip to content

Commit 58a393f

Browse files
Merge pull request RustPython#804 from jgirardet/refactore_bytes
refactor bytes
2 parents ef55efb + 9d25a21 commit 58a393f

File tree

5 files changed

+656
-119
lines changed

5 files changed

+656
-119
lines changed

parser/src/lexer.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -540,8 +540,12 @@ where
540540
let end_pos = self.get_pos();
541541

542542
let tok = if is_bytes {
543-
Tok::Bytes {
544-
value: string_content.as_bytes().to_vec(),
543+
if string_content.is_ascii() {
544+
Tok::Bytes {
545+
value: string_content.as_bytes().to_vec(),
546+
}
547+
} else {
548+
return Err(LexicalError::StringError);
545549
}
546550
} else {
547551
Tok::String {

tests/snippets/bytes.py

Lines changed: 129 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,129 @@
1-
assert b'foobar'.__eq__(2) == NotImplemented
2-
assert b'foobar'.__ne__(2) == NotImplemented
3-
assert b'foobar'.__gt__(2) == NotImplemented
4-
assert b'foobar'.__ge__(2) == NotImplemented
5-
assert b'foobar'.__lt__(2) == NotImplemented
6-
assert b'foobar'.__le__(2) == NotImplemented
1+
from testutils import assertRaises
2+
3+
# new
4+
assert bytes([1, 2, 3])
5+
assert bytes((1, 2, 3))
6+
assert bytes(range(4))
7+
assert bytes(3)
8+
assert b"bla"
9+
assert bytes("bla", "utf8")
10+
with assertRaises(TypeError):
11+
bytes("bla")
12+
13+
14+
a = b"abcd"
15+
b = b"ab"
16+
c = b"abcd"
17+
18+
#
19+
# repr
20+
assert repr(bytes([0, 1, 2])) == repr(b"\x00\x01\x02")
21+
assert repr(
22+
bytes([0, 1, 9, 10, 11, 13, 31, 32, 33, 89, 120, 255])
23+
== "b'\\x00\\x01\\t\\n\\x0b\\r\\x1f !Yx\\xff'"
24+
)
25+
assert repr(b"abcd") == "b'abcd'"
26+
27+
# len
28+
assert len(bytes("abcdé", "utf8")) == 6
29+
30+
# comp
31+
assert a == b"abcd"
32+
assert a > b
33+
assert a >= b
34+
assert b < a
35+
assert b <= a
36+
37+
assert b"foobar".__eq__(2) == NotImplemented
38+
assert b"foobar".__ne__(2) == NotImplemented
39+
assert b"foobar".__gt__(2) == NotImplemented
40+
assert b"foobar".__ge__(2) == NotImplemented
41+
assert b"foobar".__lt__(2) == NotImplemented
42+
assert b"foobar".__le__(2) == NotImplemented
43+
44+
# hash
45+
hash(a) == hash(b"abcd")
46+
47+
# iter
48+
[i for i in b"abcd"] == ["a", "b", "c", "d"]
49+
assert list(bytes(3)) == [0, 0, 0]
50+
51+
# add
52+
assert a + b == b"abcdab"
53+
54+
# contains
55+
assert b"ab" in b"abcd"
56+
assert b"cd" in b"abcd"
57+
assert b"abcd" in b"abcd"
58+
assert b"a" in b"abcd"
59+
assert b"d" in b"abcd"
60+
assert b"dc" not in b"abcd"
61+
assert 97 in b"abcd"
62+
assert 150 not in b"abcd"
63+
with assertRaises(ValueError):
64+
350 in b"abcd"
65+
66+
67+
# getitem
68+
d = b"abcdefghij"
69+
70+
assert d[1] == 98
71+
assert d[-1] == 106
72+
assert d[2:6] == b"cdef"
73+
assert d[-6:] == b"efghij"
74+
assert d[1:8:2] == b"bdfh"
75+
assert d[8:1:-2] == b"igec"
76+
77+
78+
# is_xx methods
79+
80+
assert bytes(b"1a23").isalnum()
81+
assert not bytes(b"1%a23").isalnum()
82+
83+
assert bytes(b"abc").isalpha()
84+
assert not bytes(b"abc1").isalpha()
85+
86+
# travis doesn't like this
87+
# assert bytes(b'xyz').isascii()
88+
# assert not bytes([128, 157, 32]).isascii()
89+
90+
assert bytes(b"1234567890").isdigit()
91+
assert not bytes(b"12ab").isdigit()
92+
93+
l = bytes(b"lower")
94+
b = bytes(b"UPPER")
95+
96+
assert l.islower()
97+
assert not l.isupper()
98+
assert b.isupper()
99+
assert not bytes(b"Super Friends").islower()
100+
101+
assert bytes(b" \n\t").isspace()
102+
assert not bytes(b"\td\n").isspace()
103+
104+
assert b.isupper()
105+
assert not b.islower()
106+
assert l.islower()
107+
assert not bytes(b"tuPpEr").isupper()
108+
109+
assert bytes(b"Is Title Case").istitle()
110+
assert not bytes(b"is Not title casE").istitle()
111+
112+
# upper lower
113+
l = bytes(b"lower")
114+
b = bytes(b"UPPER")
115+
assert l.lower().islower()
116+
assert b.upper().isupper()
117+
118+
# hex from hex
119+
assert bytes([0, 1, 9, 23, 90, 234]).hex() == "000109175aea"
120+
121+
bytes.fromhex("62 6c7a 34350a ") == b"blz45\n"
122+
try:
123+
bytes.fromhex("62 a 21")
124+
except ValueError as e:
125+
str(e) == "non-hexadecimal number found in fromhex() arg at position 4"
126+
try:
127+
bytes.fromhex("6Z2")
128+
except ValueError as e:
129+
str(e) == "non-hexadecimal number found in fromhex() arg at position 1"

vm/src/obj/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
pub mod objbool;
44
pub mod objbuiltinfunc;
55
pub mod objbytearray;
6+
pub mod objbyteinner;
67
pub mod objbytes;
78
pub mod objclassmethod;
89
pub mod objcode;

0 commit comments

Comments
 (0)