<s> from . import util as source_util <EOL> from importlib import _bootstrap <EOL> import codecs <EOL> import re <EOL> import sys <EOL> import unicodedata <EOL> import unittest <EOL> CODING_RE = re . compile ( r'<STR_LIT>' , re . ASCII ) <EOL> class EncodingTest ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> variable = '<STR_LIT>' <EOL> character = '<STR_LIT>' <EOL> source_line = "<STR_LIT>" . format ( variable , character ) <EOL> module_name = '<STR_LIT>' <EOL> def run_test ( self , source ) : <EOL> with source_util . create_modules ( self . module_name ) as mapping : <EOL> with open ( mapping [ self . module_name ] , '<STR_LIT:wb>' ) as file : <EOL> file . write ( source ) <EOL> loader = _bootstrap . SourceFileLoader ( self . module_name , <EOL> mapping [ self . module_name ] ) <EOL> return loader . load_module ( self . module_name ) <EOL> def create_source ( self , encoding ) : <EOL> encoding_line = "<STR_LIT>" . format ( encoding ) <EOL> assert CODING_RE . match ( encoding_line ) <EOL> source_lines = [ encoding_line . encode ( '<STR_LIT:utf-8>' ) ] <EOL> source_lines . append ( self . source_line . encode ( encoding ) ) <EOL> return b'<STR_LIT:\n>' . join ( source_lines ) <EOL> def test_non_obvious_encoding ( self ) : <EOL> encoding_line = "<STR_LIT>" <EOL> assert CODING_RE . match ( encoding_line ) <EOL> source = "<STR_LIT>" . format ( encoding_line ) . encode ( "<STR_LIT>" ) <EOL> self . run_test ( source ) <EOL> def test_default_encoding ( self ) : <EOL> self . run_test ( self . source_line . encode ( '<STR_LIT:utf-8>' ) ) <EOL> def test_encoding_on_first_line ( self ) : <EOL> encoding = '<STR_LIT>' <EOL> source = self . create_source ( encoding ) <EOL> self . run_test ( source ) <EOL> def test_encoding_on_second_line ( self ) : <EOL> source = b"<STR_LIT>" + self . create_source ( '<STR_LIT>' ) <EOL> self . run_test ( source ) <EOL> def test_bom ( self ) : <EOL> self . run_test ( codecs . BOM_UTF8 + self . source_line . encode ( '<STR_LIT:utf-8>' ) ) <EOL> def test_bom_and_utf_8 ( self ) : <EOL> source = codecs . BOM_UTF8 + self . create_source ( '<STR_LIT:utf-8>' ) <EOL> self . run_test ( source ) <EOL> def test_bom_conflict ( self ) : <EOL> source = codecs . BOM_UTF8 + self . create_source ( '<STR_LIT>' ) <EOL> with self . assertRaises ( SyntaxError ) : <EOL> self . run_test ( source ) <EOL> class LineEndingTest ( unittest . TestCase ) : <EOL> r"""<STR_LIT>""" <EOL> def run_test ( self , line_ending ) : <EOL> module_name = '<STR_LIT>' <EOL> source_lines = [ b"<STR_LIT>" , b"<STR_LIT>" , b'<STR_LIT>' ] <EOL> source = line_ending . join ( source_lines ) <EOL> with source_util . create_modules ( module_name ) as mapping : <EOL> with open ( mapping [ module_name ] , '<STR_LIT:wb>' ) as file : <EOL> file . write ( source ) <EOL> loader = _bootstrap . SourceFileLoader ( module_name , <EOL> mapping [ module_name ] ) <EOL> return loader . load_module ( module_name ) <EOL> def test_cr ( self ) : <EOL> self . run_test ( b'<STR_LIT:\r>' ) <EOL> def test_crlf ( self ) : <EOL> self . run_test ( b'<STR_LIT:\r\n>' ) <EOL> def test_lf ( self ) : <EOL> self . run_test ( b'<STR_LIT:\n>' ) <EOL> def test_main ( ) : <EOL> from test . support import run_unittest <EOL> run_unittest ( EncodingTest , LineEndingTest ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> test_main ( ) </s>
