]> granicus.if.org Git - python/commitdiff
Issue #22182: Use e.args to unpack exceptions correctly in distutils.file_util.move_file.
authorBerker Peksag <berker.peksag@gmail.com>
Fri, 29 Aug 2014 04:07:35 +0000 (07:07 +0300)
committerBerker Peksag <berker.peksag@gmail.com>
Fri, 29 Aug 2014 04:07:35 +0000 (07:07 +0300)
Patch by Claudiu Popa.

Lib/distutils/file_util.py
Lib/distutils/tests/test_file_util.py
Misc/NEWS

index f6ed290f137db1d87d32734c403cf18ee6132931..7b14efbc076248020bf5c2aa5f6099d0753eae30 100644 (file)
@@ -194,7 +194,7 @@ def move_file (src, dst,
     try:
         os.rename(src, dst)
     except OSError as e:
-        (num, msg) = e
+        (num, msg) = e.args
         if num == errno.EXDEV:
             copy_it = True
         else:
@@ -206,7 +206,7 @@ def move_file (src, dst,
         try:
             os.unlink(src)
         except OSError as e:
-            (num, msg) = e
+            (num, msg) = e.args
             try:
                 os.unlink(dst)
             except OSError:
index 3c3e3dcb3bff85310df31254274abae77c1b8181..270f81ebb6cc9f04ab06683b23649bec767de79b 100644 (file)
@@ -2,10 +2,13 @@
 import unittest
 import os
 import shutil
+import errno
+from unittest.mock import patch
 
 from distutils.file_util import move_file
 from distutils import log
 from distutils.tests import support
+from distutils.errors import DistutilsFileError
 from test.support import run_unittest
 
 class FileUtilTestCase(support.TempdirManager, unittest.TestCase):
@@ -58,6 +61,23 @@ class FileUtilTestCase(support.TempdirManager, unittest.TestCase):
         wanted = ['moving %s -> %s' % (self.source, self.target_dir)]
         self.assertEqual(self._logs, wanted)
 
+    @patch('os.rename', side_effect=OSError('wrong', 1))
+    def test_move_file_exception_unpacking_rename(self, _):
+        # see issue 22182
+        with self.assertRaises(DistutilsFileError):
+            with open(self.source, 'w') as fobj:
+                fobj.write('spam eggs')
+            move_file(self.source, self.target, verbose=0)
+
+    @patch('os.rename', side_effect=OSError(errno.EXDEV, 'wrong'))
+    @patch('os.unlink', side_effect=OSError('wrong', 1))
+    def test_move_file_exception_unpacking_unlink(self, rename, unlink):
+        # see issue 22182
+        with self.assertRaises(DistutilsFileError):
+            with open(self.source, 'w') as fobj:
+                fobj.write('spam eggs')
+            move_file(self.source, self.target, verbose=0)
+
 
 def test_suite():
     return unittest.makeSuite(FileUtilTestCase)
index bc9086123fdd6226752b93085bcbf25be743ad94..854fc737ea1f1c6bea9e9e85c1d42a019973cf81 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -27,6 +27,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #22182: Use e.args to unpack exceptions correctly in
+  distutils.file_util.move_file. Patch by Claudiu Popa.
+
 - The webbrowser module now uses subprocess's start_new_session=True rather
   than a potentially risky preexec_fn=os.setsid call.