Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
7fcd26a3
Commit
7fcd26a3
authored
Aug 29, 2007
by
Jeremy Hylton
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make it an error to compare a bytes object and a Unicode object.
parent
7d0af98a
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
13 additions
and
7 deletions
+13
-7
Lib/test/test_bytes.py
Lib/test/test_bytes.py
+5
-5
Objects/bytesobject.c
Objects/bytesobject.c
+8
-2
No files found.
Lib/test/test_bytes.py
View file @
7fcd26a3
...
...
@@ -130,12 +130,12 @@ class BytesTest(unittest.TestCase):
self
.
assertEqual
(
str8
(
"abc"
)
<
b"ab"
,
False
)
self
.
assertEqual
(
str8
(
"abc"
)
<=
b"ab"
,
False
)
# Bytes
should never compare equal
to Unicode!
# Bytes
can't be compared
to Unicode!
# Test this for all expected byte orders and Unicode character sizes
self
.
assert
Equal
(
b"
\
0
a
\
0
b
\
0
c"
==
"abc"
,
False
)
self
.
assert
Equal
(
b"
\
0
\
0
\
0
a
\
0
\
0
\
0
b
\
0
\
0
\
0
c"
==
"abc"
,
False
)
self
.
assert
Equal
(
b"a
\
0
b
\
0
c
\
0
"
==
"abc"
,
False
)
self
.
assert
Equal
(
b"a
\
0
\
0
\
0
b
\
0
\
0
\
0
c
\
0
\
0
\
0
"
==
"abc"
,
False
)
self
.
assert
Raises
(
TypeError
,
lambda
:
b"
\
0
a
\
0
b
\
0
c"
==
"abc"
)
self
.
assert
Raises
(
TypeError
,
lambda
:
b"
\
0
\
0
\
0
a
\
0
\
0
\
0
b
\
0
\
0
\
0
c"
==
"abc"
)
self
.
assert
Raises
(
TypeError
,
lambda
:
b"a
\
0
b
\
0
c
\
0
"
==
"abc"
)
self
.
assert
Raises
(
TypeError
,
lambda
:
b"a
\
0
\
0
\
0
b
\
0
\
0
\
0
c
\
0
\
0
\
0
"
==
"abc"
)
def
test_nohash
(
self
):
self
.
assertRaises
(
TypeError
,
hash
,
bytes
())
...
...
Objects/bytesobject.c
View file @
7fcd26a3
...
...
@@ -959,8 +959,14 @@ bytes_richcompare(PyObject *self, PyObject *other, int op)
Py_ssize_t
minsize
;
int
cmp
;
/* Bytes can be compared to anything that supports the (binary) buffer
API. Except Unicode. */
/* Bytes can be compared to anything that supports the (binary)
buffer API. Except that a comparison with Unicode is always an
error, even if the comparison is for equality. */
if
(
PyObject_IsInstance
(
self
,
(
PyObject
*
)
&
PyUnicode_Type
)
||
PyObject_IsInstance
(
other
,
(
PyObject
*
)
&
PyUnicode_Type
))
{
PyErr_SetString
(
PyExc_TypeError
,
"can't compare bytes and str"
);
return
NULL
;
}
self_size
=
_getbuffer
(
self
,
&
self_bytes
);
if
(
self_size
<
0
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment