Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbb5d72c

Browse files
authored
Merge pull requestpython#34 from nanjekyejoannah/warn_string_cmp_with_recent_assumptions
Warn for string cmp with new string warning assumptions
2 parents35e650e +29ae1a9 commitbb5d72c

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

‎Lib/test/test_StringIO.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,23 @@ def test_unicode(self):
166166
self.assertEqual(s,unicode('abcuvwxyz!'))
167167
self.assertEqual(type(s),types.UnicodeType)
168168

169+
deftest_py3k_string_cmp(self):
170+
"abc"==123
171+
"abc"==object()
172+
u"abc"==123
173+
u"abc"==object()
174+
withtest_support.check_py3k_warnings(("comparing unicode and byte strings has different semantics in 3.x: convert the first string to bytes.",
175+
DeprecationWarning)):
176+
u"test str"=="test unicode"
177+
deprecations= []
178+
ifsys.py3kwarning:
179+
deprecations+= [
180+
("comparing unicode and byte strings has different semantics in 3.x: convert the second string to byte.",DeprecationWarning),
181+
("comparing unicode and byte strings has different semantics in 3.x: convert the first string to bytes.",DeprecationWarning)
182+
]
183+
withtest_support.check_py3k_warnings(*deprecations):
184+
"test str"==u"test unicode"
185+
169186
classTestcStringIO(TestGenericStringIO):
170187
MODULE=cStringIO
171188

‎Objects/stringobject.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,6 +1209,9 @@ string_richcompare(PyStringObject *a, PyStringObject *b, int op)
12091209
PyObject*result;
12101210

12111211
/* Make sure both arguments are strings. */
1212+
if ((PyString_Check(a)&&PyUnicode_Check(b))&&PyErr_WarnPy3k("comparing unicode and byte strings has different semantics in 3.x: convert the second string to byte.",1)<0) {
1213+
gotoout;
1214+
}
12121215
if (!(PyString_Check(a)&&PyString_Check(b))) {
12131216
result=Py_NotImplemented;
12141217
gotoout;

‎Objects/unicodeobject.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6269,6 +6269,10 @@ PyObject *PyUnicode_RichCompare(PyObject *left,
62696269
{
62706270
intresult;
62716271

6272+
if (PyString_Check(right)&&PyErr_WarnPy3k("comparing unicode and byte strings has different semantics in 3.x: convert the first string to bytes.",1)<0){
6273+
gotoonError;
6274+
}
6275+
62726276
result=PyUnicode_Compare(left,right);
62736277
if (result==-1&&PyErr_Occurred())
62746278
gotoonError;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp