diff options
Diffstat (limited to 'Lib/importlib')
-rw-r--r-- | Lib/importlib/_bootstrap.py | 4 | ||||
-rw-r--r-- | Lib/importlib/test/source/test_abc_loader.py | 61 |
2 files changed, 38 insertions, 27 deletions
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py index 2fac499..04d9bff 100644 --- a/Lib/importlib/_bootstrap.py +++ b/Lib/importlib/_bootstrap.py @@ -386,8 +386,8 @@ class SourceLoader(_LoaderBasics): except IOError: raise ImportError("source not available through get_data()") encoding = tokenize.detect_encoding(_io.BytesIO(source_bytes).readline) - # XXX Universal newlines? - return source_bytes.decode(encoding[0]) + newline_decoder = _io.IncrementalNewlineDecoder(None, True) + return newline_decoder.decode(source_bytes.decode(encoding[0])) def get_code(self, fullname): """Concrete implementation of InspectLoader.get_code. diff --git a/Lib/importlib/test/source/test_abc_loader.py b/Lib/importlib/test/source/test_abc_loader.py index ce0021f..3245907 100644 --- a/Lib/importlib/test/source/test_abc_loader.py +++ b/Lib/importlib/test/source/test_abc_loader.py @@ -7,6 +7,7 @@ from . import util as source_util import imp import inspect +import io import marshal import os import sys @@ -327,30 +328,6 @@ class PyLoaderInterfaceTests(unittest.TestCase): mock.get_filename(name) -class PyLoaderGetSourceTests(unittest.TestCase): - - """Tests for importlib.abc.PyLoader.get_source().""" - - def test_default_encoding(self): - # Should have no problems with UTF-8 text. - name = 'mod' - mock = PyLoaderMock({name: os.path.join('path', 'to', 'mod')}) - source = 'x = "ü"' - mock.source = source.encode('utf-8') - returned_source = mock.get_source(name) - self.assertEqual(returned_source, source) - - def test_decoded_source(self): - # Decoding should work. - name = 'mod' - mock = PyLoaderMock({name: os.path.join('path', 'to', 'mod')}) - source = "# coding: Latin-1\nx='ü'" - assert source.encode('latin-1') != source.encode('utf-8') - mock.source = source.encode('latin-1') - returned_source = mock.get_source(name) - self.assertEqual(returned_source, source) - - class PyPycLoaderTests(PyLoaderTests): """Tests for importlib.abc.PyPycLoader.""" @@ -752,6 +729,39 @@ class SourceLoaderBytecodeTests(SourceLoaderTestHarness): code_object = self.loader.get_code(self.name) self.verify_code(code_object) + +class SourceLoaderGetSourceTests(unittest.TestCase): + + """Tests for importlib.abc.SourceLoader.get_source().""" + + def test_default_encoding(self): + # Should have no problems with UTF-8 text. + name = 'mod' + mock = SourceOnlyLoaderMock('mod.file') + source = 'x = "ü"' + mock.source = source.encode('utf-8') + returned_source = mock.get_source(name) + self.assertEqual(returned_source, source) + + def test_decoded_source(self): + # Decoding should work. + name = 'mod' + mock = SourceOnlyLoaderMock("mod.file") + source = "# coding: Latin-1\nx='ü'" + assert source.encode('latin-1') != source.encode('utf-8') + mock.source = source.encode('latin-1') + returned_source = mock.get_source(name) + self.assertEqual(returned_source, source) + + def test_universal_newlines(self): + # PEP 302 says universal newlines should be used. + name = 'mod' + mock = SourceOnlyLoaderMock('mod.file') + source = "x = 42\r\ny = -13\r\n" + mock.source = source.encode('utf-8') + expect = io.IncrementalNewlineDecoder(None, True).decode(source) + self.assertEqual(mock.get_source(name), expect) + class AbstractMethodImplTests(unittest.TestCase): """Test the concrete abstractmethod implementations.""" @@ -852,12 +862,13 @@ class AbstractMethodImplTests(unittest.TestCase): def test_main(): from test.support import run_unittest run_unittest(PyLoaderTests, PyLoaderCompatTests, - PyLoaderInterfaceTests, PyLoaderGetSourceTests, + PyLoaderInterfaceTests, PyPycLoaderTests, PyPycLoaderInterfaceTests, SkipWritingBytecodeTests, RegeneratedBytecodeTests, BadBytecodeFailureTests, MissingPathsTests, SourceOnlyLoaderTests, SourceLoaderBytecodeTests, + SourceLoaderGetSourceTests, AbstractMethodImplTests) |