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

3 files changed

lines changed

Original file line numberDiff line numberDiff line change

@@ -6,6 +6,8 @@

66

import unittest

77

from test import support

88
9+

import os

10+

import stat

911

import sys

1012

import uu

1113

import io

@@ -218,6 +220,23 @@ def test_decodetwice(self):

218220

with open(self.tmpin, 'rb') as f:

219221

self.assertRaises(uu.Error, uu.decode, f)

220222
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+
221240
222241

if __name__=="__main__":

223242

unittest.main()

Original file line numberDiff line numberDiff line change

@@ -133,10 +133,7 @@ def decode(in_file, out_file=None, mode=None, quiet=False):

133133

out_file = sys.stdout.buffer

134134

elif isinstance(out_file, str):

135135

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)

140137

out_file = fp

141138

opened_files.append(out_file)

142139

#

Original file line numberDiff line numberDiff 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.