|
564 | 564 |
|
565 | 565 | from test import support |
566 | 566 | from tokenize import (tokenize, _tokenize, untokenize, NUMBER, NAME, OP, |
567 | | - STRING, ENDMARKER, tok_name, detect_encoding) |
| 567 | + STRING, ENDMARKER, tok_name, detect_encoding, |
| 568 | + open as tokenize_open) |
568 | 569 | from io import BytesIO |
569 | 570 | from unittest import TestCase |
570 | 571 | import os, sys, glob |
@@ -857,6 +858,26 @@ def test_short_files(self): |
857 | 858 | readline = self.get_readline((b'# coding: bad\n',)) |
858 | 859 | self.assertRaises(SyntaxError, detect_encoding, readline) |
859 | 860 |
|
| 861 | + def test_open(self): |
| 862 | + filename = support.TESTFN + '.py' |
| 863 | + self.addCleanup(support.unlink, filename) |
| 864 | + |
| 865 | + # test coding cookie |
| 866 | + for encoding in ('iso-8859-15', 'utf-8'): |
| 867 | + with open(filename, 'w', encoding=encoding) as fp: |
| 868 | + print("# coding: %s" % encoding, file=fp) |
| 869 | + print("print('euro:\u20ac')", file=fp) |
| 870 | + with tokenize_open(filename) as fp: |
| 871 | + assert fp.encoding == encoding |
| 872 | + assert fp.mode == 'r' |
| 873 | + |
| 874 | + # test BOM (no coding cookie) |
| 875 | + with open(filename, 'w', encoding='utf-8-sig') as fp: |
| 876 | + print("print('euro:\u20ac')", file=fp) |
| 877 | + with tokenize_open(filename) as fp: |
| 878 | + assert fp.encoding == 'utf-8-sig' |
| 879 | + assert fp.mode == 'r' |
| 880 | + |
860 | 881 | class TestTokenize(TestCase): |
861 | 882 |
|
862 | 883 | def test_tokenize(self): |
|
0 commit comments