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
9c62772d
Commit
9c62772d
authored
Aug 27, 2007
by
Guido van Rossum
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Changes in anticipation of stricter str vs. bytes enforcement.
parent
35d94280
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
57 additions
and
50 deletions
+57
-50
Lib/test/test_bytes.py
Lib/test/test_bytes.py
+43
-39
Lib/test/test_ucn.py
Lib/test/test_ucn.py
+1
-1
Lib/test/test_unicode.py
Lib/test/test_unicode.py
+3
-3
Lib/test/test_unicode_file.py
Lib/test/test_unicode_file.py
+1
-1
Lib/test/test_unicodedata.py
Lib/test/test_unicodedata.py
+1
-1
Lib/test/test_univnewlines.py
Lib/test/test_univnewlines.py
+4
-1
Lib/test/test_zipfile.py
Lib/test/test_zipfile.py
+4
-4
No files found.
Lib/test/test_bytes.py
View file @
9c62772d
...
@@ -75,8 +75,8 @@ class BytesTest(unittest.TestCase):
...
@@ -75,8 +75,8 @@ class BytesTest(unittest.TestCase):
self
.
assertEqual
(
repr
(
bytes
([
0
])),
"b'
\
\
x00'"
)
self
.
assertEqual
(
repr
(
bytes
([
0
])),
"b'
\
\
x00'"
)
self
.
assertEqual
(
repr
(
bytes
([
0
,
1
,
254
,
255
])),
self
.
assertEqual
(
repr
(
bytes
([
0
,
1
,
254
,
255
])),
"b'
\
\
x00
\
\
x01
\
\
xfe
\
\
xff'"
)
"b'
\
\
x00
\
\
x01
\
\
xfe
\
\
xff'"
)
self
.
assertEqual
(
repr
(
b
ytes
(
'abc'
)
),
"b'abc'"
)
self
.
assertEqual
(
repr
(
b
"abc"
),
"b'abc'"
)
self
.
assertEqual
(
repr
(
b
ytes
(
"'"
)
),
"b'
\
\
''"
)
self
.
assertEqual
(
repr
(
b
"'"
),
"b'
\
\
''"
)
def
test_compare
(
self
):
def
test_compare
(
self
):
b1
=
bytes
([
1
,
2
,
3
])
b1
=
bytes
([
1
,
2
,
3
])
...
@@ -329,7 +329,7 @@ class BytesTest(unittest.TestCase):
...
@@ -329,7 +329,7 @@ class BytesTest(unittest.TestCase):
self.assertEqual(b, bytes(sample.encode(enc)))
self.assertEqual(b, bytes(sample.encode(enc)))
self.assertRaises(UnicodeEncodeError, bytes, sample, "latin1")
self.assertRaises(UnicodeEncodeError, bytes, sample, "latin1")
b = bytes(sample, "latin1", "ignore")
b = bytes(sample, "latin1", "ignore")
self.assertEqual(b, bytes(sample[:-4]))
self.assertEqual(b, bytes(sample[:-4]
, "utf-8"
))
def test_decode(self):
def test_decode(self):
sample = "Hello world
\
n
\
u1234
\
u5678
\
u9abc
\
de
f
0
\
de
f
0"
sample = "Hello world
\
n
\
u1234
\
u5678
\
u9abc
\
de
f
0
\
de
f
0"
...
@@ -349,7 +349,7 @@ class BytesTest(unittest.TestCase):
...
@@ -349,7 +349,7 @@ class BytesTest(unittest.TestCase):
def test_to_str(self):
def test_to_str(self):
sample = "Hello world
\
n
\
x80
\
x81
\
xfe
\
xff
"
sample = "Hello world
\
n
\
x80
\
x81
\
xfe
\
xff
"
b = bytes(sample)
b = bytes(sample
, "utf-8"
)
self.assertEqual(str(b), sample)
self.assertEqual(str(b), sample)
def test_from_int(self):
def test_from_int(self):
...
@@ -361,17 +361,17 @@ class BytesTest(unittest.TestCase):
...
@@ -361,17 +361,17 @@ class BytesTest(unittest.TestCase):
self.assertEqual(b, bytes([0]*10000))
self.assertEqual(b, bytes([0]*10000))
def test_concat(self):
def test_concat(self):
b1 = b
ytes("abc")
b1 = b
"abc"
b2 = b
ytes("def")
b2 = b
"def"
self.assertEqual(b1 + b2, b
ytes("abcdef")
)
self.assertEqual(b1 + b2, b
"abcdef"
)
self.assertEqual(b1 + str8("def"), b
ytes("abcdef")
)
self.assertEqual(b1 + str8("def"), b
"abcdef"
)
self.assertEqual(str8("def") + b1, b
ytes("defabc")
)
self.assertEqual(str8("def") + b1, b
"defabc"
)
self.assertRaises(TypeError, lambda: b1 + "def")
self.assertRaises(TypeError, lambda: b1 + "def")
self.assertRaises(TypeError, lambda: "abc" + b2)
self.assertRaises(TypeError, lambda: "abc" + b2)
def test_repeat(self):
def test_repeat(self):
b = b
ytes("abc")
b = b
"abc"
self.assertEqual(b * 3, b
ytes("abcabcabc")
)
self.assertEqual(b * 3, b
"abcabcabc"
)
self.assertEqual(b * 0, bytes())
self.assertEqual(b * 0, bytes())
self.assertEqual(b * -1, bytes())
self.assertEqual(b * -1, bytes())
self.assertRaises(TypeError, lambda: b * 3.14)
self.assertRaises(TypeError, lambda: b * 3.14)
...
@@ -379,13 +379,13 @@ class BytesTest(unittest.TestCase):
...
@@ -379,13 +379,13 @@ class BytesTest(unittest.TestCase):
self.assertRaises(MemoryError, lambda: b * sys.maxint)
self.assertRaises(MemoryError, lambda: b * sys.maxint)
def test_repeat_1char(self):
def test_repeat_1char(self):
self.assertEqual(b
ytes('
x
')*100, bytes('
x
'
*100))
self.assertEqual(b
'
x
'*100, bytes([ord('
x
')]
*100))
def test_iconcat(self):
def test_iconcat(self):
b = b
ytes("abc")
b = b
"abc"
b1 = b
b1 = b
b += b
ytes("def")
b += b
"def"
self.assertEqual(b, b
ytes("abcdef")
)
self.assertEqual(b, b
"abcdef"
)
self.assertEqual(b, b1)
self.assertEqual(b, b1)
self.failUnless(b is b1)
self.failUnless(b is b1)
b += str8("xyz")
b += str8("xyz")
...
@@ -398,23 +398,23 @@ class BytesTest(unittest.TestCase):
...
@@ -398,23 +398,23 @@ class BytesTest(unittest.TestCase):
self.fail("bytes += unicode didn'
t
raise
TypeError
")
self.fail("bytes += unicode didn'
t
raise
TypeError
")
def test_irepeat(self):
def test_irepeat(self):
b = b
ytes("
abc
")
b = b
"
abc
"
b1 = b
b1 = b
b *= 3
b *= 3
self.assertEqual(b, b
ytes("
abcabcabc
")
)
self.assertEqual(b, b
"
abcabcabc
"
)
self.assertEqual(b, b1)
self.assertEqual(b, b1)
self.failUnless(b is b1)
self.failUnless(b is b1)
def test_irepeat_1char(self):
def test_irepeat_1char(self):
b = b
ytes("
x
")
b = b
"
x
"
b1 = b
b1 = b
b *= 100
b *= 100
self.assertEqual(b, bytes(
"
x
"
*100))
self.assertEqual(b, bytes(
[ord("
x
")]
*100))
self.assertEqual(b, b1)
self.assertEqual(b, b1)
self.failUnless(b is b1)
self.failUnless(b is b1)
def test_contains(self):
def test_contains(self):
b = b
ytes("
abc
")
b = b
"
abc
"
self.failUnless(ord('a') in b)
self.failUnless(ord('a') in b)
self.failUnless(int(ord('a')) in b)
self.failUnless(int(ord('a')) in b)
self.failIf(200 in b)
self.failIf(200 in b)
...
@@ -424,17 +424,17 @@ class BytesTest(unittest.TestCase):
...
@@ -424,17 +424,17 @@ class BytesTest(unittest.TestCase):
self.assertRaises(TypeError, lambda: None in b)
self.assertRaises(TypeError, lambda: None in b)
self.assertRaises(TypeError, lambda: float(ord('a')) in b)
self.assertRaises(TypeError, lambda: float(ord('a')) in b)
self.assertRaises(TypeError, lambda: "
a
" in b)
self.assertRaises(TypeError, lambda: "
a
" in b)
self.failUnless(b
ytes("")
in b)
self.failUnless(b
""
in b)
self.failUnless(b
ytes("
a
")
in b)
self.failUnless(b
"
a
"
in b)
self.failUnless(b
ytes("b")
in b)
self.failUnless(b
"b"
in b)
self.failUnless(b
ytes("
c
")
in b)
self.failUnless(b
"
c
"
in b)
self.failUnless(b
ytes("
ab
")
in b)
self.failUnless(b
"
ab
"
in b)
self.failUnless(b
ytes("
bc
")
in b)
self.failUnless(b
"
bc
"
in b)
self.failUnless(b
ytes("
abc
")
in b)
self.failUnless(b
"
abc
"
in b)
self.failIf(b
ytes("
ac
")
in b)
self.failIf(b
"
ac
"
in b)
self.failIf(b
ytes("
d
")
in b)
self.failIf(b
"
d
"
in b)
self.failIf(b
ytes("
dab
")
in b)
self.failIf(b
"
dab
"
in b)
self.failIf(b
ytes("
abd
")
in b)
self.failIf(b
"
abd
"
in b)
def test_alloc(self):
def test_alloc(self):
b = bytes()
b = bytes()
...
@@ -442,7 +442,7 @@ class BytesTest(unittest.TestCase):
...
@@ -442,7 +442,7 @@ class BytesTest(unittest.TestCase):
self.assert_(alloc >= 0)
self.assert_(alloc >= 0)
seq = [alloc]
seq = [alloc]
for i in range(100):
for i in range(100):
b += b
ytes("
x
")
b += b
"
x
"
alloc = b.__alloc__()
alloc = b.__alloc__()
self.assert_(alloc >= len(b))
self.assert_(alloc >= len(b))
if alloc not in seq:
if alloc not in seq:
...
@@ -467,11 +467,10 @@ class BytesTest(unittest.TestCase):
...
@@ -467,11 +467,10 @@ class BytesTest(unittest.TestCase):
def test_join(self):
def test_join(self):
self.assertEqual(b"".join([]), bytes())
self.assertEqual(b"".join([]), bytes())
self.assertEqual(b"".join([bytes()]), bytes())
self.assertEqual(b"".join([bytes()]), bytes())
for part in [("
abc
",), ("
a
", "
bc
"), ("
ab
", "
c
"), ("
a
", "b", "
c
")]:
for lst in [[b"
abc
"], [b"
a
", b"
bc
"], [b"
ab
", b"
c
"], [b"
a
", b"b", b"
c
"]]:
lst = list(map(bytes, part))
self.assertEqual(b"".join(lst), b"
abc
")
self.assertEqual(b"".join(lst), bytes("
abc
"))
self.assertEqual(b"".join(tuple(lst)), b"
abc
")
self.assertEqual(b"".join(tuple(lst)), bytes("
abc
"))
self.assertEqual(b"".join(iter(lst)), b"
abc
")
self.assertEqual(b"".join(iter(lst)), bytes("
abc
"))
self.assertEqual(b"
.
".join([b"
ab
", b"
cd
"]), b"
ab
.
cd
")
self.assertEqual(b"
.
".join([b"
ab
", b"
cd
"]), b"
ab
.
cd
")
# XXX more...
# XXX more...
...
@@ -691,8 +690,13 @@ class BytesTest(unittest.TestCase):
...
@@ -691,8 +690,13 @@ class BytesTest(unittest.TestCase):
class BytesAsStringTest(test.string_tests.BaseTest):
class BytesAsStringTest(test.string_tests.BaseTest):
type2test = bytes
type2test = bytes
def fixtype(self, obj):
if isinstance(obj, str):
return obj.encode("
utf
-
8
")
return super().fixtype(obj)
def checkequal(self, result, object, methodname, *args):
def checkequal(self, result, object, methodname, *args):
object = bytes(object)
object = bytes(object
, "
utf
-
8
"
)
realresult = getattr(bytes, methodname)(object, *args)
realresult = getattr(bytes, methodname)(object, *args)
self.assertEqual(
self.assertEqual(
self.fixtype(result),
self.fixtype(result),
...
@@ -700,7 +704,7 @@ class BytesAsStringTest(test.string_tests.BaseTest):
...
@@ -700,7 +704,7 @@ class BytesAsStringTest(test.string_tests.BaseTest):
)
)
def checkraises(self, exc, object, methodname, *args):
def checkraises(self, exc, object, methodname, *args):
object = bytes(object)
object = bytes(object
, "
utf
-
8
"
)
self.assertRaises(
self.assertRaises(
exc,
exc,
getattr(bytes, methodname),
getattr(bytes, methodname),
...
...
Lib/test/test_ucn.py
View file @
9c62772d
...
@@ -124,7 +124,7 @@ class UnicodeNamesTest(unittest.TestCase):
...
@@ -124,7 +124,7 @@ class UnicodeNamesTest(unittest.TestCase):
# long bogus character name
# long bogus character name
self
.
assertRaises
(
self
.
assertRaises
(
UnicodeError
,
UnicodeError
,
str
,
bytes
(
"
\
\
N{%s}"
%
(
"x"
*
100000
)),
'unicode-escape'
,
'strict'
str
,
bytes
(
"
\
\
N{%s}"
%
(
"x"
*
100000
)
,
"ascii"
),
'unicode-escape'
,
'strict'
)
)
# missing closing brace
# missing closing brace
self
.
assertRaises
(
self
.
assertRaises
(
...
...
Lib/test/test_unicode.py
View file @
9c62772d
...
@@ -794,10 +794,10 @@ class UnicodeTest(
...
@@ -794,10 +794,10 @@ class UnicodeTest(
self.assertEqual(b"
\
\
N{foo}xx".decode("unicode-escape", "ignore"), "xx")
self.assertEqual(b"
\
\
N{foo}xx".decode("unicode-escape", "ignore"), "xx")
# Error handling (truncated escape sequence)
# Error handling (truncated escape sequence)
self.assertRaises(UnicodeError, "
\
\
".decode, "unicode-escape")
self.assertRaises(UnicodeError,
b
"
\
\
".decode, "unicode-escape")
self.assertRaises(TypeError, "hello".decode, "test.unicode1")
self.assertRaises(TypeError,
b
"hello".decode, "test.unicode1")
self.assertRaises(TypeError, str, "hello", "test.unicode2")
self.assertRaises(TypeError, str,
b
"hello", "test.unicode2")
self.assertRaises(TypeError, "hello".encode, "test.unicode1")
self.assertRaises(TypeError, "hello".encode, "test.unicode1")
self.assertRaises(TypeError, "hello".encode, "test.unicode2")
self.assertRaises(TypeError, "hello".encode, "test.unicode2")
# executes PyUnicode_Encode()
# executes PyUnicode_Encode()
...
...
Lib/test/test_unicode_file.py
View file @
9c62772d
...
@@ -8,7 +8,7 @@ import unittest
...
@@ -8,7 +8,7 @@ import unittest
from
test.test_support
import
run_unittest
,
TestSkipped
,
TESTFN_UNICODE
from
test.test_support
import
run_unittest
,
TestSkipped
,
TESTFN_UNICODE
from
test.test_support
import
TESTFN_ENCODING
,
TESTFN_UNICODE_UNENCODEABLE
from
test.test_support
import
TESTFN_ENCODING
,
TESTFN_UNICODE_UNENCODEABLE
try
:
try
:
TESTFN_ENCODED
=
TESTFN_UNICODE
TESTFN_ENCODED
=
TESTFN_UNICODE
.
encode
(
"utf-8"
)
# XXX is this right?
TESTFN_UNICODE
.
encode
(
TESTFN_ENCODING
)
TESTFN_UNICODE
.
encode
(
TESTFN_ENCODING
)
except
(
UnicodeError
,
TypeError
):
except
(
UnicodeError
,
TypeError
):
# Either the file system encoding is None, or the file name
# Either the file system encoding is None, or the file name
...
...
Lib/test/test_unicodedata.py
View file @
9c62772d
...
@@ -94,7 +94,7 @@ class UnicodeFunctionsTest(UnicodeDatabaseTest):
...
@@ -94,7 +94,7 @@ class UnicodeFunctionsTest(UnicodeDatabaseTest):
str
(
self
.
db
.
mirrored
(
char
)),
str
(
self
.
db
.
mirrored
(
char
)),
str
(
self
.
db
.
combining
(
char
)),
str
(
self
.
db
.
combining
(
char
)),
]
]
h
.
update
(
''
.
join
(
data
))
h
.
update
(
''
.
join
(
data
)
.
encode
(
"ascii"
)
)
result
=
h
.
hexdigest
()
result
=
h
.
hexdigest
()
self
.
assertEqual
(
result
,
self
.
expectedchecksum
)
self
.
assertEqual
(
result
,
self
.
expectedchecksum
)
...
...
Lib/test/test_univnewlines.py
View file @
9c62772d
...
@@ -36,7 +36,10 @@ class TestGenericUnivNewlines(unittest.TestCase):
...
@@ -36,7 +36,10 @@ class TestGenericUnivNewlines(unittest.TestCase):
def
setUp
(
self
):
def
setUp
(
self
):
fp
=
open
(
test_support
.
TESTFN
,
self
.
WRITEMODE
)
fp
=
open
(
test_support
.
TESTFN
,
self
.
WRITEMODE
)
fp
.
write
(
self
.
DATA
)
data
=
self
.
DATA
if
"b"
in
self
.
WRITEMODE
:
data
=
data
.
encode
(
"ascii"
)
fp
.
write
(
data
)
fp
.
close
()
fp
.
close
()
def
tearDown
(
self
):
def
tearDown
(
self
):
...
...
Lib/test/test_zipfile.py
View file @
9c62772d
...
@@ -17,7 +17,7 @@ FIXEDTEST_SIZE = 1000
...
@@ -17,7 +17,7 @@ FIXEDTEST_SIZE = 1000
class
TestsWithSourceFile
(
unittest
.
TestCase
):
class
TestsWithSourceFile
(
unittest
.
TestCase
):
def
setUp
(
self
):
def
setUp
(
self
):
self
.
line_gen
=
(
bytes
(
"Zipfile test line %d. random float: %f"
%
self
.
line_gen
=
(
bytes
(
"Zipfile test line %d. random float: %f"
%
(
i
,
random
()))
(
i
,
random
())
,
"ascii"
)
for
i
in
range
(
FIXEDTEST_SIZE
))
for
i
in
range
(
FIXEDTEST_SIZE
))
self
.
data
=
b'
\
n
'
.
join
(
self
.
line_gen
)
+
b'
\
n
'
self
.
data
=
b'
\
n
'
.
join
(
self
.
line_gen
)
+
b'
\
n
'
...
@@ -246,7 +246,7 @@ class TestsWithSourceFile(unittest.TestCase):
...
@@ -246,7 +246,7 @@ class TestsWithSourceFile(unittest.TestCase):
# Test appending to an existing file that is not a zipfile
# Test appending to an existing file that is not a zipfile
# NOTE: this test fails if len(d) < 22 because of the first
# NOTE: this test fails if len(d) < 22 because of the first
# line "fpin.seek(-22, 2)" in _EndRecData
# line "fpin.seek(-22, 2)" in _EndRecData
d
=
'I am not a ZipFile!'
*
10
d
=
b
'I am not a ZipFile!'
*
10
f
=
open
(
TESTFN2
,
'wb'
)
f
=
open
(
TESTFN2
,
'wb'
)
f
.
write
(
d
)
f
.
write
(
d
)
f
.
close
()
f
.
close
()
...
@@ -301,7 +301,7 @@ class TestZip64InSmallFiles(unittest.TestCase):
...
@@ -301,7 +301,7 @@ class TestZip64InSmallFiles(unittest.TestCase):
self
.
_limit
=
zipfile
.
ZIP64_LIMIT
self
.
_limit
=
zipfile
.
ZIP64_LIMIT
zipfile
.
ZIP64_LIMIT
=
5
zipfile
.
ZIP64_LIMIT
=
5
line_gen
=
(
bytes
(
"Test of zipfile line %d."
%
i
)
line_gen
=
(
bytes
(
"Test of zipfile line %d."
%
i
,
"ascii"
)
for
i
in
range
(
0
,
FIXEDTEST_SIZE
))
for
i
in
range
(
0
,
FIXEDTEST_SIZE
))
self
.
data
=
b'
\
n
'
.
join
(
line_gen
)
self
.
data
=
b'
\
n
'
.
join
(
line_gen
)
...
@@ -806,7 +806,7 @@ class TestsWithMultipleOpens(unittest.TestCase):
...
@@ -806,7 +806,7 @@ class TestsWithMultipleOpens(unittest.TestCase):
class
UniversalNewlineTests
(
unittest
.
TestCase
):
class
UniversalNewlineTests
(
unittest
.
TestCase
):
def
setUp
(
self
):
def
setUp
(
self
):
self
.
line_gen
=
[
bytes
(
"Test of zipfile line %d."
%
i
)
self
.
line_gen
=
[
bytes
(
"Test of zipfile line %d."
%
i
,
"ascii"
)
for
i
in
range
(
FIXEDTEST_SIZE
)]
for
i
in
range
(
FIXEDTEST_SIZE
)]
self
.
seps
=
(
'
\
r
'
,
'
\
r
\
n
'
,
'
\
n
'
)
self
.
seps
=
(
'
\
r
'
,
'
\
r
\
n
'
,
'
\
n
'
)
self
.
arcdata
,
self
.
arcfiles
=
{},
{}
self
.
arcdata
,
self
.
arcfiles
=
{},
{}
...
...
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