Skip to content

Commit c33bb1a

Browse files
authored
Merge pull request #4527 from mwichmann/tarfile-mod
scons-time: add filter to tarfile extract call.
2 parents e853087 + 3813ec6 commit c33bb1a

File tree

3 files changed

+32
-42
lines changed

3 files changed

+32
-42
lines changed

CHANGES.txt

+2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ RELEASE VERSION/DATE TO BE FILLED IN LATER
6161
tweaks. Update manpage and user guide for Variables usage.
6262
- Regularize internal usage of Python version strings and drop one
6363
old Python 2-only code block in a test.
64+
- scons-time tests now supply a "filter" argument to tarfile.extract
65+
to quiet a warning which was added in Python 3.13 beta 1.
6466

6567

6668
RELEASE 4.7.0 - Sun, 17 Mar 2024 17:22:20 -0700

bin/scons-time.py

+15-13
Original file line numberDiff line numberDiff line change
@@ -223,23 +223,25 @@ def draw(self):
223223

224224
def untar(fname):
225225
import tarfile
226-
tar = tarfile.open(name=fname, mode='r')
227-
for tarinfo in tar:
228-
tar.extract(tarinfo)
229-
tar.close()
226+
with tarfile.open(name=fname, mode='r') as tar:
227+
for tarinfo in tar:
228+
try:
229+
tar.extract(tarinfo, filter="tar")
230+
except TypeError:
231+
tar.extract(tarinfo)
230232

231233

232234
def unzip(fname):
233235
import zipfile
234-
zf = zipfile.ZipFile(fname, 'r')
235-
for name in zf.namelist():
236-
dir = os.path.dirname(name)
237-
try:
238-
os.makedirs(dir)
239-
except OSError:
240-
pass
241-
with open(name, 'wb') as f:
242-
f.write(zf.read(name))
236+
with zipfile.ZipFile(fname, 'r') as zf:
237+
for name in zf.namelist():
238+
dir = os.path.dirname(name)
239+
try:
240+
os.makedirs(dir)
241+
except OSError:
242+
pass
243+
with open(name, 'wb') as f:
244+
f.write(zf.read(name))
243245

244246

245247
def read_tree(dir):

testing/framework/TestSCons_time.py

+15-29
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def write_args(fp, args):
103103
Memory before building targets: 300%(index)s
104104
Memory after building targets: 400%(index)s
105105
Object counts:
106-
pre- post- pre- post-
106+
pre- post- pre- post-
107107
read read build build Class
108108
101%(index)s 102%(index)s 103%(index)s 104%(index)s Action.CommandAction
109109
201%(index)s 202%(index)s 203%(index)s 204%(index)s Action.CommandGeneratorAction
@@ -273,20 +273,16 @@ def write_sample_directory(self, archive, dir, files):
273273

274274
def write_sample_tarfile(self, archive, dir, files):
275275
import shutil
276-
try:
277-
import tarfile
278-
except ImportError:
279-
self.skip_test('no tarfile module\n', from_framework=True)
280-
else:
281-
base, suffix = self.archive_split(archive)
276+
import tarfile
277+
base, suffix = self.archive_split(archive)
282278

283-
mode = {
284-
'.tar' : 'w',
285-
'.tar.gz' : 'w:gz',
286-
'.tgz' : 'w:gz',
287-
}
279+
mode = {
280+
'.tar' : 'w',
281+
'.tar.gz' : 'w:gz',
282+
'.tgz' : 'w:gz',
283+
}
288284

289-
tar = tarfile.open(archive, mode[suffix])
285+
with tarfile.open(archive, mode[suffix]) as tar:
290286
for name, content in files:
291287
path = os.path.join(dir, name)
292288
with open(path, 'wb') as f:
@@ -298,30 +294,20 @@ def write_sample_tarfile(self, archive, dir, files):
298294
tarinfo.gname = 'fake_group'
299295
with open(path, 'rb') as f:
300296
tar.addfile(tarinfo, f)
301-
tar.close()
302-
shutil.rmtree(dir)
303-
return self.workpath(archive)
297+
shutil.rmtree(dir)
298+
return self.workpath(archive)
304299

305300
def write_sample_zipfile(self, archive, dir, files):
306301
import shutil
307-
try:
308-
import zipfile
309-
except ImportError:
310-
311-
sys.stderr.write('no zipfile module\n')
312-
self.no_result()
313-
314-
else:
315-
316-
zip = zipfile.ZipFile(archive, 'w')
302+
import zipfile
303+
with zipfile.ZipFile(archive, 'w') as zip:
317304
for name, content in files:
318305
path = os.path.join(dir, name)
319306
with open(path, 'w') as f:
320307
f.write(content)
321308
zip.write(path)
322-
zip.close()
323-
shutil.rmtree(dir)
324-
return self.workpath(archive)
309+
shutil.rmtree(dir)
310+
return self.workpath(archive)
325311

326312
sample_project_files = [
327313
('SConstruct', SConstruct),

0 commit comments

Comments
 (0)