Esempio n. 1
0
    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'))
Esempio n. 2
0
    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'))
Esempio n. 3
0
    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'))
Esempio n. 4
0
    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"))
Esempio n. 5
0
    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"))
Esempio n. 6
0
 def test_reverts_to_py_if_available(self):
     archive = PythonPackageArchive()
     py = self.py_with_pyc('foo.py')
     archive.add_py_file(py+'c')
     archive.close()
     self.assertEqual(archive.get_filenames(), ['foo.py'])
Esempio n. 7
0
 def test_can_add_py_file(self):
     archive = PythonPackageArchive()
     archive.add_py_file(self.py_with_pyc('foo.py'))
     archive.close()
     self.assertEqual(archive.get_filenames(), ['foo.py'])
Esempio n. 8
0
 def test_reverts_to_py_if_available(self):
     archive = PythonPackageArchive()
     py = self.py_with_pyc('foo.py')
     archive.add_py_file(py+'c')
     archive.close()
     self.assertEqual(archive.get_filenames(), ['foo.py'])
Esempio n. 9
0
 def test_can_add_py_file(self):
     archive = PythonPackageArchive()
     archive.add_py_file(self.py_with_pyc('foo.py'))
     archive.close()
     self.assertEqual(archive.get_filenames(), ['foo.py'])