| 
									
										
										
										
											2013-11-08 14:25:37 -05:00
										 |  |  | from .. import util | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-08 14:25:37 -05:00
										 |  |  | machinery = util.import_importlib('importlib.machinery') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | import codecs | 
					
						
							| 
									
										
										
										
											2013-12-06 14:25:01 -05:00
										 |  |  | import importlib.util | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | import re | 
					
						
							| 
									
										
										
										
											2013-12-06 14:25:01 -05:00
										 |  |  | import types | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | # Because sys.path gets essentially blanked, need to have unicodedata already | 
					
						
							|  |  |  | # imported for the parser to use. | 
					
						
							|  |  |  | import unicodedata | 
					
						
							|  |  |  | import unittest | 
					
						
							| 
									
										
										
										
											2014-01-06 20:49:04 -07:00
										 |  |  | import warnings | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-20 23:36:29 +02:00
										 |  |  | CODING_RE = re.compile(r'^[ \t\f]*#.*?coding[:=][ \t]*([-\w.]+)', re.ASCII) | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-08 14:25:37 -05:00
										 |  |  | class EncodingTest: | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     """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]. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     variable = '\u00fc' | 
					
						
							|  |  |  |     character = '\u00c9' | 
					
						
							|  |  |  |     source_line = "{0} = '{1}'\n".format(variable, character) | 
					
						
							|  |  |  |     module_name = '_temp' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def run_test(self, source): | 
					
						
							| 
									
										
										
										
											2014-05-09 14:32:57 -04:00
										 |  |  |         with util.create_modules(self.module_name) as mapping: | 
					
						
							| 
									
										
										
										
											2010-04-17 00:19:56 +00:00
										 |  |  |             with open(mapping[self.module_name], 'wb') as file: | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  |                 file.write(source) | 
					
						
							| 
									
										
										
										
											2013-11-08 14:25:37 -05:00
										 |  |  |             loader = self.machinery.SourceFileLoader(self.module_name, | 
					
						
							| 
									
										
										
										
											2012-04-14 14:10:13 -04:00
										 |  |  |                                                   mapping[self.module_name]) | 
					
						
							| 
									
										
										
										
											2013-12-06 14:25:01 -05:00
										 |  |  |             return self.load(loader) | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def create_source(self, encoding): | 
					
						
							|  |  |  |         encoding_line = "# coding={0}".format(encoding) | 
					
						
							| 
									
										
										
										
											2013-09-16 23:51:56 +03:00
										 |  |  |         assert CODING_RE.match(encoding_line) | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  |         source_lines = [encoding_line.encode('utf-8')] | 
					
						
							|  |  |  |         source_lines.append(self.source_line.encode(encoding)) | 
					
						
							|  |  |  |         return b'\n'.join(source_lines) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_non_obvious_encoding(self): | 
					
						
							|  |  |  |         # Make sure that an encoding that has never been a standard one for | 
					
						
							|  |  |  |         # Python works. | 
					
						
							|  |  |  |         encoding_line = "# coding=koi8-r" | 
					
						
							| 
									
										
										
										
											2013-09-16 23:51:56 +03:00
										 |  |  |         assert CODING_RE.match(encoding_line) | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  |         source = "{0}\na=42\n".format(encoding_line).encode("koi8-r") | 
					
						
							|  |  |  |         self.run_test(source) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # [default encoding] | 
					
						
							|  |  |  |     def test_default_encoding(self): | 
					
						
							|  |  |  |         self.run_test(self.source_line.encode('utf-8')) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # [encoding first line] | 
					
						
							|  |  |  |     def test_encoding_on_first_line(self): | 
					
						
							|  |  |  |         encoding = 'Latin-1' | 
					
						
							|  |  |  |         source = self.create_source(encoding) | 
					
						
							|  |  |  |         self.run_test(source) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # [encoding second line] | 
					
						
							|  |  |  |     def test_encoding_on_second_line(self): | 
					
						
							|  |  |  |         source = b"#/usr/bin/python\n" + self.create_source('Latin-1') | 
					
						
							|  |  |  |         self.run_test(source) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # [BOM] | 
					
						
							|  |  |  |     def test_bom(self): | 
					
						
							|  |  |  |         self.run_test(codecs.BOM_UTF8 + self.source_line.encode('utf-8')) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # [BOM and utf-8] | 
					
						
							|  |  |  |     def test_bom_and_utf_8(self): | 
					
						
							|  |  |  |         source = codecs.BOM_UTF8 + self.create_source('utf-8') | 
					
						
							|  |  |  |         self.run_test(source) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # [BOM conflict] | 
					
						
							|  |  |  |     def test_bom_conflict(self): | 
					
						
							|  |  |  |         source = codecs.BOM_UTF8 + self.create_source('latin-1') | 
					
						
							| 
									
										
										
										
											2009-08-27 23:49:21 +00:00
										 |  |  |         with self.assertRaises(SyntaxError): | 
					
						
							|  |  |  |             self.run_test(source) | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-16 11:40:40 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-06 14:25:01 -05:00
										 |  |  | class EncodingTestPEP451(EncodingTest): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def load(self, loader): | 
					
						
							|  |  |  |         module = types.ModuleType(self.module_name) | 
					
						
							|  |  |  |         module.__spec__ = importlib.util.spec_from_loader(self.module_name, loader) | 
					
						
							|  |  |  |         loader.exec_module(module) | 
					
						
							|  |  |  |         return module | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-16 11:40:40 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | (Frozen_EncodingTestPEP451, | 
					
						
							|  |  |  |  Source_EncodingTestPEP451 | 
					
						
							|  |  |  |  ) = util.test_both(EncodingTestPEP451, machinery=machinery) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-06 14:25:01 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | class EncodingTestPEP302(EncodingTest): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def load(self, loader): | 
					
						
							| 
									
										
										
										
											2014-01-06 20:49:04 -07:00
										 |  |  |         with warnings.catch_warnings(): | 
					
						
							|  |  |  |             warnings.simplefilter('ignore', DeprecationWarning) | 
					
						
							|  |  |  |             return loader.load_module(self.module_name) | 
					
						
							| 
									
										
										
										
											2013-12-06 14:25:01 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-16 11:40:40 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | (Frozen_EncodingTestPEP302, | 
					
						
							|  |  |  |  Source_EncodingTestPEP302 | 
					
						
							|  |  |  |  ) = util.test_both(EncodingTestPEP302, machinery=machinery) | 
					
						
							| 
									
										
										
										
											2013-11-08 14:25:37 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-08 14:25:37 -05:00
										 |  |  | class LineEndingTest: | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     r"""Source written with the three types of line endings (\n, \r\n, \r)
 | 
					
						
							|  |  |  |     need to be readable [cr][crlf][lf]."""
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def run_test(self, line_ending): | 
					
						
							|  |  |  |         module_name = '_temp' | 
					
						
							|  |  |  |         source_lines = [b"a = 42", b"b = -13", b''] | 
					
						
							|  |  |  |         source = line_ending.join(source_lines) | 
					
						
							| 
									
										
										
										
											2014-05-09 14:32:57 -04:00
										 |  |  |         with util.create_modules(module_name) as mapping: | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  |             with open(mapping[module_name], 'wb') as file: | 
					
						
							|  |  |  |                 file.write(source) | 
					
						
							| 
									
										
										
										
											2013-11-08 14:25:37 -05:00
										 |  |  |             loader = self.machinery.SourceFileLoader(module_name, | 
					
						
							| 
									
										
										
										
											2014-01-06 20:49:04 -07:00
										 |  |  |                                                      mapping[module_name]) | 
					
						
							|  |  |  |             return self.load(loader, module_name) | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # [cr] | 
					
						
							|  |  |  |     def test_cr(self): | 
					
						
							|  |  |  |         self.run_test(b'\r') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # [crlf] | 
					
						
							|  |  |  |     def test_crlf(self): | 
					
						
							|  |  |  |         self.run_test(b'\r\n') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # [lf] | 
					
						
							|  |  |  |     def test_lf(self): | 
					
						
							|  |  |  |         self.run_test(b'\n') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-16 11:40:40 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-06 14:25:01 -05:00
										 |  |  | class LineEndingTestPEP451(LineEndingTest): | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-06 20:49:04 -07:00
										 |  |  |     def load(self, loader, module_name): | 
					
						
							|  |  |  |         module = types.ModuleType(module_name) | 
					
						
							|  |  |  |         module.__spec__ = importlib.util.spec_from_loader(module_name, loader) | 
					
						
							| 
									
										
										
										
											2013-12-06 14:25:01 -05:00
										 |  |  |         loader.exec_module(module) | 
					
						
							|  |  |  |         return module | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-16 11:40:40 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | (Frozen_LineEndingTestPEP451, | 
					
						
							|  |  |  |  Source_LineEndingTestPEP451 | 
					
						
							|  |  |  |  ) = util.test_both(LineEndingTestPEP451, machinery=machinery) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-06 14:25:01 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | class LineEndingTestPEP302(LineEndingTest): | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-06 20:49:04 -07:00
										 |  |  |     def load(self, loader, module_name): | 
					
						
							|  |  |  |         with warnings.catch_warnings(): | 
					
						
							|  |  |  |             warnings.simplefilter('ignore', DeprecationWarning) | 
					
						
							|  |  |  |             return loader.load_module(module_name) | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-16 11:40:40 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | (Frozen_LineEndingTestPEP302, | 
					
						
							|  |  |  |  Source_LineEndingTestPEP302 | 
					
						
							|  |  |  |  ) = util.test_both(LineEndingTestPEP302, machinery=machinery) | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							| 
									
										
										
										
											2013-11-08 14:25:37 -05:00
										 |  |  |     unittest.main() |