Issue #18076: Introduce imoportlib.util.decode_source().

The helper function makes it easier to implement
imoprtlib.abc.InspectLoader.get_source() by making that function
require just the raw bytes for source code and handling all other
details.
This commit is contained in:
Brett Cannon 2013-06-16 18:37:53 -04:00
parent f4375ef4d4
commit f24fecd4ac
6 changed files with 3629 additions and 3572 deletions

View file

@ -9,6 +9,27 @@ import unittest
import warnings
class DecodeSourceBytesTests(unittest.TestCase):
source = "string ='ü'"
def test_ut8_default(self):
source_bytes = self.source.encode('utf-8')
self.assertEqual(util.decode_source(source_bytes), self.source)
def test_specified_encoding(self):
source = '# coding=latin-1\n' + self.source
source_bytes = source.encode('latin-1')
assert source_bytes != source.encode('utf-8')
self.assertEqual(util.decode_source(source_bytes), source)
def test_universal_newlines(self):
source = '\r\n'.join([self.source, self.source])
source_bytes = source.encode('utf-8')
self.assertEqual(util.decode_source(source_bytes),
'\n'.join([self.source, self.source]))
class ModuleToLoadTests(unittest.TestCase):
module_name = 'ModuleManagerTest_module'