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
e776e4d9
Commit
e776e4d9
authored
Mar 20, 2016
by
Serhiy Storchaka
Browse files
Options
Browse Files
Download
Plain Diff
Issues #25643, #26581: Added new tests for detecting Python source code encoding.
parents
2d264235
69d811b4
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
81 additions
and
2 deletions
+81
-2
Lib/test/test_source_encoding.py
Lib/test/test_source_encoding.py
+81
-2
No files found.
Lib/test/test_source_encoding.py
View file @
e776e4d9
# -*- coding: koi8-r -*-
# -*- coding: koi8-r -*-
import
unittest
import
unittest
from
test.support
import
TESTFN
,
unlink
,
unload
,
rmtree
from
test.support
import
TESTFN
,
unlink
,
unload
,
rmtree
,
script_helper
,
captured_stdout
import
importlib
import
importlib
import
os
import
os
import
sys
import
sys
import
subprocess
import
subprocess
import
tempfile
class
SourceEncodingTest
(
unittest
.
TestCase
):
class
Misc
SourceEncodingTest
(
unittest
.
TestCase
):
def
test_pep263
(
self
):
def
test_pep263
(
self
):
self
.
assertEqual
(
self
.
assertEqual
(
...
@@ -142,5 +143,83 @@ class SourceEncodingTest(unittest.TestCase):
...
@@ -142,5 +143,83 @@ class SourceEncodingTest(unittest.TestCase):
msg
=
c
.
exception
.
args
[
0
])
msg
=
c
.
exception
.
args
[
0
])
class
AbstractSourceEncodingTest
:
def
test_default_coding
(
self
):
src
=
(
b'print(ascii("
\
xc3
\
xa4
"))
\
n
'
)
self
.
check_script_output
(
src
,
br"'\xe4'"
)
def
test_first_coding_line
(
self
):
src
=
(
b'#coding:iso8859-15
\
n
'
b'print(ascii("
\
xc3
\
xa4
"))
\
n
'
)
self
.
check_script_output
(
src
,
br"'\xc3\u20ac'"
)
def
test_second_coding_line
(
self
):
src
=
(
b'#
\
n
'
b'#coding:iso8859-15
\
n
'
b'print(ascii("
\
xc3
\
xa4
"))
\
n
'
)
self
.
check_script_output
(
src
,
br"'\xc3\u20ac'"
)
def
test_third_coding_line
(
self
):
# Only first two lines are tested for a magic comment.
src
=
(
b'#
\
n
'
b'#
\
n
'
b'#coding:iso8859-15
\
n
'
b'print(ascii("
\
xc3
\
xa4
"))
\
n
'
)
self
.
check_script_output
(
src
,
br"'\xe4'"
)
def
test_double_coding_line
(
self
):
# If the first line matches the second line is ignored.
src
=
(
b'#coding:iso8859-15
\
n
'
b'#coding:latin1
\
n
'
b'print(ascii("
\
xc3
\
xa4
"))
\
n
'
)
self
.
check_script_output
(
src
,
br"'\xc3\u20ac'"
)
def
test_double_coding_same_line
(
self
):
src
=
(
b'#coding:iso8859-15 coding:latin1
\
n
'
b'print(ascii("
\
xc3
\
xa4
"))
\
n
'
)
self
.
check_script_output
(
src
,
br"'\xc3\xa4'"
)
def
test_first_non_utf8_coding_line
(
self
):
src
=
(
b'#coding:iso-8859-15
\
xa4
\
n
'
b'print(ascii("
\
xc3
\
xa4
"))
\
n
'
)
self
.
check_script_output
(
src
,
br"'\xc3\u20ac'"
)
def
test_second_non_utf8_coding_line
(
self
):
src
=
(
b'
\
n
'
b'#coding:iso-8859-15
\
xa4
\
n
'
b'print(ascii("
\
xc3
\
xa4
"))
\
n
'
)
self
.
check_script_output
(
src
,
br"'\xc3\u20ac'"
)
def
test_utf8_bom
(
self
):
src
=
(
b'
\
xef
\
xbb
\
xbf
print(ascii("
\
xc3
\
xa4
"))
\
n
'
)
self
.
check_script_output
(
src
,
br"'\xe4'"
)
def
test_utf8_bom_and_utf8_coding_line
(
self
):
src
=
(
b'
\
xef
\
xbb
\
xbf
#coding:utf-8
\
n
'
b'print(ascii("
\
xc3
\
xa4
"))
\
n
'
)
self
.
check_script_output
(
src
,
br"'\xe4'"
)
class
BytesSourceEncodingTest
(
AbstractSourceEncodingTest
,
unittest
.
TestCase
):
def
check_script_output
(
self
,
src
,
expected
):
with
captured_stdout
()
as
stdout
:
exec
(
src
)
out
=
stdout
.
getvalue
().
encode
(
'latin1'
)
self
.
assertEqual
(
out
.
rstrip
(),
expected
)
class
FileSourceEncodingTest
(
AbstractSourceEncodingTest
,
unittest
.
TestCase
):
def
check_script_output
(
self
,
src
,
expected
):
with
tempfile
.
TemporaryDirectory
()
as
tmpd
:
fn
=
os
.
path
.
join
(
tmpd
,
'test.py'
)
with
open
(
fn
,
'wb'
)
as
fp
:
fp
.
write
(
src
)
res
=
script_helper
.
assert_python_ok
(
fn
)
self
.
assertEqual
(
res
.
out
.
rstrip
(),
expected
)
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
unittest
.
main
()
unittest
.
main
()
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