Back to index

python3.2  3.2.2
Public Member Functions | Static Public Attributes
importlib.test.source.test_source_encoding.EncodingTest Class Reference
Collaboration diagram for importlib.test.source.test_source_encoding.EncodingTest:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def run_test
def create_source
def test_non_obvious_encoding
def test_default_encoding
def test_encoding_on_first_line
def test_encoding_on_second_line
def test_bom
def test_bom_and_utf_8
def test_bom_conflict

Static Public Attributes

string variable = '\u00fc'
string character = '\u00c9'
string source_line = "{0} = '{1}'\n"
string module_name = '_temp'

Detailed Description

PEP 3120 makes UTF-8 the default encoding for source code
[default encoding].

PEP 263 specifies how that can change on a per-file basis. Either the first
or second line can contain the encoding line [encoding first line]
encoding second line]. If the file has the BOM marker it is considered UTF-8
implicitly [BOM]. If any encoding is specified it must be UTF-8, else it is
an error [BOM and utf-8][BOM conflict].

Definition at line 16 of file test_source_encoding.py.


Member Function Documentation

Definition at line 42 of file test_source_encoding.py.

00042 
00043     def create_source(self, encoding):
00044         encoding_line = "# coding={0}".format(encoding)
00045         assert CODING_RE.search(encoding_line)
00046         source_lines = [encoding_line.encode('utf-8')]
00047         source_lines.append(self.source_line.encode(encoding))
00048         return b'\n'.join(source_lines)

Here is the caller graph for this function:

Definition at line 34 of file test_source_encoding.py.

00034 
00035     def run_test(self, source):
00036         with source_util.create_modules(self.module_name) as mapping:
00037             with open(mapping[self.module_name], 'wb') as file:
00038                 file.write(source)
00039             loader = _bootstrap._SourceFileLoader(self.module_name,
00040                                        mapping[self.module_name])
00041             return loader.load_module(self.module_name)

Definition at line 73 of file test_source_encoding.py.

00073 
00074     def test_bom(self):
00075         self.run_test(codecs.BOM_UTF8 + self.source_line.encode('utf-8'))

Here is the call graph for this function:

Definition at line 77 of file test_source_encoding.py.

00077 
00078     def test_bom_and_utf_8(self):
00079         source = codecs.BOM_UTF8 + self.create_source('utf-8')
00080         self.run_test(source)

Here is the call graph for this function:

Definition at line 82 of file test_source_encoding.py.

00082 
00083     def test_bom_conflict(self):
00084         source = codecs.BOM_UTF8 + self.create_source('latin-1')
00085         with self.assertRaises(SyntaxError):
00086             self.run_test(source)
00087 

Here is the call graph for this function:

Definition at line 58 of file test_source_encoding.py.

00058 
00059     def test_default_encoding(self):
00060         self.run_test(self.source_line.encode('utf-8'))

Here is the call graph for this function:

Definition at line 62 of file test_source_encoding.py.

00062 
00063     def test_encoding_on_first_line(self):
00064         encoding = 'Latin-1'
00065         source = self.create_source(encoding)
00066         self.run_test(source)

Here is the call graph for this function:

Definition at line 68 of file test_source_encoding.py.

00068 
00069     def test_encoding_on_second_line(self):
00070         source = b"#/usr/bin/python\n" + self.create_source('Latin-1')
00071         self.run_test(source)

Here is the call graph for this function:

Definition at line 49 of file test_source_encoding.py.

00049 
00050     def test_non_obvious_encoding(self):
00051         # Make sure that an encoding that has never been a standard one for
00052         # Python works.
00053         encoding_line = "# coding=koi8-r"
00054         assert CODING_RE.search(encoding_line)
00055         source = "{0}\na=42\n".format(encoding_line).encode("koi8-r")
00056         self.run_test(source)

Here is the call graph for this function:


Member Data Documentation

Definition at line 30 of file test_source_encoding.py.

Definition at line 32 of file test_source_encoding.py.

Definition at line 31 of file test_source_encoding.py.

Definition at line 29 of file test_source_encoding.py.


The documentation for this class was generated from the following file: