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 ... get = lambda name: os.path.basename(imp.find_module(name)[1]) self.assertTrue(get('foo'), 'foo.py') try: # ... and while *.pyc is importable ... self.assertTrue(get('bar'), 'bar.pyc') except ImportError: # (except on PyPy) # http://doc.pypy.org/en/latest/config/objspace.lonepycfiles.html self.assertEqual(platform.python_implementation(), 'PyPy') else: # ... we refuse it. with self.assertRaises(ValueError) as raised: PythonPackageArchive('bar') msg = raised.exception.args[0] self.assertTrue(msg.startswith('We need a *.py source file instead')) 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('42', reader.read('foo.py'))
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, and while *.pyc is # importable, we refuse it. get = lambda name: os.path.basename(imp.find_module(name)[1]) self.assertTrue(get('foo'), 'foo.py') self.assertTrue(get('bar'), 'bar.pyc') with self.assertRaises(ValueError) as raised: PythonPackageArchive('bar') msg = raised.exception.args[0] self.assertTrue(msg.startswith('We need a *.py source file instead')) 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('42', reader.read('foo.py'))
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(modules=["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(modules=["foo"]) archive.close() self.assertEqual(archive.get_filenames(), ["foo.py"]) with archive.get_reader() as reader: self.assertEqual(b"42", reader.read("foo.py"))
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"))