1313import os
1414import sys
1515import sysconfig
16+ import tempfile
1617import threading
1718import unittest
1819import warnings
1920from test import support
2021from test .support import _4G , bigmemtest
2122from test .support .import_helper import import_fresh_module
22- from test .support import os_helper
2323from test .support import requires_resource
2424from test .support import threading_helper
2525from http .client import HTTPException
@@ -414,21 +414,18 @@ def check_file_digest(self, name, data, hexdigest):
414414 digests = [name ]
415415 digests .extend (self .constructors_to_test [name ])
416416
417- with open ( os_helper . TESTFN , "wb" ) as f :
417+ with tempfile . TemporaryFile ( ) as f :
418418 f .write (data )
419419
420- try :
421420 for digest in digests :
422421 buf = io .BytesIO (data )
423422 buf .seek (0 )
424423 self .assertEqual (
425424 hashlib .file_digest (buf , digest ).hexdigest (), hexdigest
426425 )
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 )
429428 self .assertEqual (digestobj .hexdigest (), hexdigest )
430- finally :
431- os .unlink (os_helper .TESTFN )
432429
433430 def check_no_unicode (self , algorithm_name ):
434431 # Unicode objects are not allowed as input.
@@ -1172,14 +1169,22 @@ def test_normalized_name(self):
11721169 def test_file_digest (self ):
11731170 data = b'a' * 65536
11741171 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 :
11771173 for _ in range (10 ):
11781174 d1 .update (data )
1179- f .write (data )
1175+ fp .write (data )
1176+ fp .close ()
1177+
1178+ with open (fp .name , "rb" ) as f :
1179+ d2 = hashlib .file_digest (f , hashlib .sha256 )
1180+
1181+ with self .assertRaises (ValueError ):
1182+ with open (fp .name , "r" ) as f :
1183+ hashlib .file_digest (f , "sha256" )
11801184
1181- with open (os_helper .TESTFN , "rb" ) as f :
1182- d2 = hashlib .file_digest (f , hashlib .sha256 )
1185+ with self .assertRaises (ValueError ):
1186+ with open (fp .name , "wb" ) as f :
1187+ hashlib .file_digest (f , "sha256" )
11831188
11841189 self .assertEqual (d1 .hexdigest (), d2 .hexdigest ())
11851190 self .assertEqual (d1 .name , d2 .name )
@@ -1188,14 +1193,6 @@ def test_file_digest(self):
11881193 with self .assertRaises (ValueError ):
11891194 hashlib .file_digest (None , "sha256" )
11901195
1191- with self .assertRaises (ValueError ):
1192- with open (os_helper .TESTFN , "r" ) as f :
1193- hashlib .file_digest (f , "sha256" )
1194-
1195- with self .assertRaises (ValueError ):
1196- with open (os_helper .TESTFN , "wb" ) as f :
1197- hashlib .file_digest (f , "sha256" )
1198-
11991196
12001197if __name__ == "__main__" :
12011198 unittest .main ()
0 commit comments