Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0dc10e0

Browse files
committed
Issue #22182: Use e.args to unpack exceptions correctly in distutils.file_util.move_file.
Patch by Claudiu Popa.
2 parents e6128a4 + 6685883 commit 0dc10e0

3 files changed

Lines changed: 25 additions & 2 deletions

File tree

Lib/distutils/file_util.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def move_file (src, dst,
194194
try:
195195
os.rename(src, dst)
196196
except OSError as e:
197-
(num, msg) = e
197+
(num, msg) = e.args
198198
if num == errno.EXDEV:
199199
copy_it = True
200200
else:
@@ -206,7 +206,7 @@ def move_file (src, dst,
206206
try:
207207
os.unlink(src)
208208
except OSError as e:
209-
(num, msg) = e
209+
(num, msg) = e.args
210210
try:
211211
os.unlink(dst)
212212
except OSError:

Lib/distutils/tests/test_file_util.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
import unittest
33
import os
44
import shutil
5+
import errno
6+
from unittest.mock import patch
57

68
from distutils.file_util import move_file
79
from distutils import log
810
from distutils.tests import support
11+
from distutils.errors import DistutilsFileError
912
from test.support import run_unittest
1013

1114
class FileUtilTestCase(support.TempdirManager, unittest.TestCase):
@@ -58,6 +61,23 @@ def test_move_file_verbosity(self):
5861
wanted = ['moving %s -> %s' % (self.source, self.target_dir)]
5962
self.assertEqual(self._logs, wanted)
6063

64+
@patch('os.rename', side_effect=OSError('wrong', 1))
65+
def test_move_file_exception_unpacking_rename(self, _):
66+
# see issue 22182
67+
with self.assertRaises(DistutilsFileError):
68+
with open(self.source, 'w') as fobj:
69+
fobj.write('spam eggs')
70+
move_file(self.source, self.target, verbose=0)
71+
72+
@patch('os.rename', side_effect=OSError(errno.EXDEV, 'wrong'))
73+
@patch('os.unlink', side_effect=OSError('wrong', 1))
74+
def test_move_file_exception_unpacking_unlink(self, rename, unlink):
75+
# see issue 22182
76+
with self.assertRaises(DistutilsFileError):
77+
with open(self.source, 'w') as fobj:
78+
fobj.write('spam eggs')
79+
move_file(self.source, self.target, verbose=0)
80+
6181

6282
def test_suite():
6383
return unittest.makeSuite(FileUtilTestCase)

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ Core and Builtins
124124
Library
125125
-------
126126

127+
- Issue #22182: Use e.args to unpack exceptions correctly in
128+
distutils.file_util.move_file. Patch by Claudiu Popa.
129+
127130
- The webbrowser module now uses subprocess's start_new_session=True rather
128131
than a potentially risky preexec_fn=os.setsid call.
129132

0 commit comments

Comments
 (0)