diff --git a/Lib/distutils/file_util.py b/Lib/distutils/file_util.py --- a/Lib/distutils/file_util.py +++ b/Lib/distutils/file_util.py @@ -27,26 +27,26 @@ def _copy_file_contents(src, dst, buffer try: try: fsrc = open(src, 'rb') - except os.error as e: + except EnvironmentError as e: raise DistutilsFileError("could not open '%s': %s" % (src, e.strerror)) if os.path.exists(dst): try: os.unlink(dst) - except os.error as e: + except EnvironmentError as e: raise DistutilsFileError( "could not delete '%s': %s" % (dst, e.strerror)) try: fdst = open(dst, 'wb') - except os.error as e: + except EnvironmentError as e: raise DistutilsFileError( "could not create '%s': %s" % (dst, e.strerror)) while True: try: buf = fsrc.read(buffer_size) - except os.error as e: + except EnvironmentError as e: raise DistutilsFileError( "could not read from '%s': %s" % (src, e.strerror)) @@ -55,7 +55,7 @@ def _copy_file_contents(src, dst, buffer try: fdst.write(buf) - except os.error as e: + except EnvironmentError as e: raise DistutilsFileError( "could not write to '%s': %s" % (dst, e.strerror)) finally: @@ -193,7 +193,7 @@ def move_file (src, dst, copy_it = False try: os.rename(src, dst) - except os.error as e: + except EnvironmentError as e: (num, msg) = e if num == errno.EXDEV: copy_it = True @@ -205,11 +205,11 @@ def move_file (src, dst, copy_file(src, dst, verbose=verbose) try: os.unlink(src) - except os.error as e: + except EnvironmentError as e: (num, msg) = e try: os.unlink(dst) - except os.error: + except EnvironmentError: pass raise DistutilsFileError( "couldn't move '%s' to '%s' by copy/delete: " diff --git a/Lib/distutils/tests/test_install_lib.py b/Lib/distutils/tests/test_install_lib.py --- a/Lib/distutils/tests/test_install_lib.py +++ b/Lib/distutils/tests/test_install_lib.py @@ -6,9 +6,10 @@ import unittest from distutils.command.install_lib import install_lib from distutils.extension import Extension from distutils.tests import support -from distutils.errors import DistutilsOptionError +from distutils.errors import DistutilsFileError, DistutilsOptionError from test.support import run_unittest + class InstallLibTestCase(support.TempdirManager, support.LoggingSilencer, support.EnvironGuard, @@ -95,6 +96,25 @@ class InstallLibTestCase(support.Tempdir self.assertTrue('byte-compiling is disabled' in self.logs[0][1]) + def test_install_error(self): + project_dir, dist = self.create_dist(py_modules=['spam']) + os.chdir(project_dir) + self.write_file('spam.py', '# I want to be installed!') + install_dir = self.mkdtemp() + path = os.path.join(install_dir, 'spam.py') + # note: chmod on Windows only supports 0o400 and 0o600 + os.chmod(install_dir, 0o400) + + cmd = install_lib(dist) + cmd.install_dir = install_dir + cmd.ensure_finalized() + with self.assertRaises(DistutilsFileError) as cm: + cmd.run() + self.assertIn(path, str(cm.exception)) + # let's hope that this test will work on all platforms + self.assertIn('denied', str(cm.exception)) + + def test_suite(): return unittest.makeSuite(InstallLibTestCase)