bpo-33687: Fix call to os.chmod() in uu.decode() (GH-7282) · python/cpython@17f05bb (original) (raw)

3 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -6,6 +6,8 @@
6 6 import unittest
7 7 from test import support
8 8
9 +import os
10 +import stat
9 11 import sys
10 12 import uu
11 13 import io
@@ -218,6 +220,23 @@ def test_decodetwice(self):
218 220 with open(self.tmpin, 'rb') as f:
219 221 self.assertRaises(uu.Error, uu.decode, f)
220 222
223 +def test_decode_mode(self):
224 +# Verify that decode() will set the given mode for the out_file
225 +expected_mode = 0o444
226 +with open(self.tmpin, 'wb') as f:
227 +f.write(encodedtextwrapped(expected_mode, self.tmpout))
228 +
229 +# make file writable again, so it can be removed (Windows only)
230 +self.addCleanup(os.chmod, self.tmpout, expected_mode | stat.S_IWRITE)
231 +
232 +with open(self.tmpin, 'rb') as f:
233 +uu.decode(f)
234 +
235 +self.assertEqual(
236 +stat.S_IMODE(os.stat(self.tmpout).st_mode),
237 +expected_mode
238 + )
239 +
221 240
222 241 if __name__=="__main__":
223 242 unittest.main()
Original file line number Diff line number Diff line change
@@ -133,10 +133,7 @@ def decode(in_file, out_file=None, mode=None, quiet=False):
133 133 out_file = sys.stdout.buffer
134 134 elif isinstance(out_file, str):
135 135 fp = open(out_file, 'wb')
136 -try:
137 -os.path.chmod(out_file, mode)
138 -except AttributeError:
139 -pass
136 +os.chmod(out_file, mode)
140 137 out_file = fp
141 138 opened_files.append(out_file)
142 139 #
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1 +Fix the call to ``os.chmod()`` for ``uu.decode()`` if a mode is given or
2 +decoded. Patch by Timo Furrer.