def test_class_constructor_only_accepts_py_modules_not_pyc(self):
# Create a module with both *.py and *.pyc.
self.py_with_pyc('foo.py')
# Create another with a *.pyc but no *.py behind it.
os.unlink(self.py_with_pyc('bar.py'))
# Now: *.py takes precedence over *.pyc ...
def get(name):
return os.path.basename(importlib.import_module(name).__file__)
self.assertTrue(get('foo'), 'foo.py')
try:
# ... and while *.pyc is importable ...
self.assertTrue(get('bar'), 'bar.pyc')
except ImportError:
try:
# (except on PyPy)
# http://doc.pypy.org/en/latest/config/objspace.lonepycfiles.html
self.assertEqual(platform.python_implementation(), 'PyPy')
except AssertionError:
# (... aaaaaand Python 3)
self.assertEqual(platform.python_version_tuple()[0], '3')
else:
# ... we refuse it.
with self.assertRaises(ValueError) as raised:
PythonPackageArchive('bar')
msg = raised.exception.args[0]
self.assertTrue(msg.startswith('Could not find a *.py source file'))
self.assertTrue(msg.endswith('bar.pyc'))
# We readily ignore a *.pyc if a *.py exists.
archive = PythonPackageArchive('foo')
archive.close()
self.assertEqual(archive.get_filenames(), ['foo.py'])
with archive.get_reader() as reader:
self.assertEqual(b'42', reader.read('foo.py'))
评论列表
文章目录