|
13 | 13 | import os
|
14 | 14 | import sys
|
15 | 15 | import sysconfig
|
| 16 | +import tempfile |
16 | 17 | import threading
|
17 | 18 | import unittest
|
18 | 19 | import warnings
|
19 | 20 | from test import support
|
20 | 21 | from test.support import _4G, bigmemtest
|
21 | 22 | from test.support.import_helper import import_fresh_module
|
22 |
| -from test.support import os_helper |
23 | 23 | from test.support import requires_resource
|
24 | 24 | from test.support import threading_helper
|
25 | 25 | from http.client import HTTPException
|
@@ -414,21 +414,18 @@ def check_file_digest(self, name, data, hexdigest):
|
414 | 414 | digests = [name]
|
415 | 415 | digests.extend(self.constructors_to_test[name])
|
416 | 416 |
|
417 |
| - with open(os_helper.TESTFN, "wb") as f: |
| 417 | + with tempfile.TemporaryFile() as f: |
418 | 418 | f.write(data)
|
419 | 419 |
|
420 |
| - try: |
421 | 420 | for digest in digests:
|
422 | 421 | buf = io.BytesIO(data)
|
423 | 422 | buf.seek(0)
|
424 | 423 | self.assertEqual(
|
425 | 424 | hashlib.file_digest(buf, digest).hexdigest(), hexdigest
|
426 | 425 | )
|
427 |
| - with open(os_helper.TESTFN, "rb") as f: |
428 |
| - digestobj = hashlib.file_digest(f, digest) |
| 426 | + f.seek(0) |
| 427 | + digestobj = hashlib.file_digest(f, digest) |
429 | 428 | self.assertEqual(digestobj.hexdigest(), hexdigest)
|
430 |
| - finally: |
431 |
| - os.unlink(os_helper.TESTFN) |
432 | 429 |
|
433 | 430 | def check_no_unicode(self, algorithm_name):
|
434 | 431 | # Unicode objects are not allowed as input.
|
@@ -1172,29 +1169,29 @@ def test_normalized_name(self):
|
1172 | 1169 | def test_file_digest(self):
|
1173 | 1170 | data = b'a' * 65536
|
1174 | 1171 | d1 = hashlib.sha256()
|
1175 |
| - self.addCleanup(os.unlink, os_helper.TESTFN) |
1176 |
| - with open(os_helper.TESTFN, "wb") as f: |
| 1172 | + with tempfile.NamedTemporaryFile(delete_on_close=False) as fp: |
1177 | 1173 | for _ in range(10):
|
1178 | 1174 | d1.update(data)
|
1179 |
| - f.write(data) |
| 1175 | + fp.write(data) |
| 1176 | + fp.close() |
1180 | 1177 |
|
1181 |
| - with open(os_helper.TESTFN, "rb") as f: |
1182 |
| - d2 = hashlib.file_digest(f, hashlib.sha256) |
| 1178 | + with open(fp.name, "rb") as f: |
| 1179 | + d2 = hashlib.file_digest(f, hashlib.sha256) |
1183 | 1180 |
|
1184 |
| - self.assertEqual(d1.hexdigest(), d2.hexdigest()) |
1185 |
| - self.assertEqual(d1.name, d2.name) |
1186 |
| - self.assertIs(type(d1), type(d2)) |
| 1181 | + self.assertEqual(d1.hexdigest(), d2.hexdigest()) |
| 1182 | + self.assertEqual(d1.name, d2.name) |
| 1183 | + self.assertIs(type(d1), type(d2)) |
1187 | 1184 |
|
1188 |
| - with self.assertRaises(ValueError): |
1189 |
| - hashlib.file_digest(None, "sha256") |
| 1185 | + with self.assertRaises(ValueError): |
| 1186 | + with open(fp.name, "r") as f: |
| 1187 | + hashlib.file_digest(f, "sha256") |
1190 | 1188 |
|
1191 |
| - with self.assertRaises(ValueError): |
1192 |
| - with open(os_helper.TESTFN, "r") as f: |
1193 |
| - hashlib.file_digest(f, "sha256") |
| 1189 | + with self.assertRaises(ValueError): |
| 1190 | + with open(fp.name, "wb") as f: |
| 1191 | + hashlib.file_digest(f, "sha256") |
1194 | 1192 |
|
1195 | 1193 | with self.assertRaises(ValueError):
|
1196 |
| - with open(os_helper.TESTFN, "wb") as f: |
1197 |
| - hashlib.file_digest(f, "sha256") |
| 1194 | + hashlib.file_digest(None, "sha256") |
1198 | 1195 |
|
1199 | 1196 |
|
1200 | 1197 | if __name__ == "__main__":
|
|
0 commit comments