1616import sys
1717import sysconfig
1818import tempfile
19+ import textwrap
1920import threading
2021import unittest
21- import subprocess
22- import textwrap
2322from test import support
2423from test .support import _4G , bigmemtest
2524from test .support import hashlib_helper
26- from test .support . import_helper import import_fresh_module
25+ from test .support import import_helper
2726from test .support import requires_resource
2827from test .support import threading_helper
28+ from test .support .script_helper import assert_python_ok
2929from http .client import HTTPException
3030
3131
3939 builtin_hashes = set (map (str .strip , builtin_hash_names ))
4040
4141# Public 'hashlib' module with OpenSSL backend for PBKDF2.
42- openssl_hashlib = import_fresh_module ('hashlib' , fresh = ['_hashlib' ])
42+ openssl_hashlib = import_helper . import_fresh_module ('hashlib' , fresh = ['_hashlib' ])
4343
4444try :
4545 import _hashlib
@@ -68,6 +68,7 @@ def get_fips_mode():
6868
6969requires_sha3 = unittest .skipUnless (_sha3 , 'requires _sha3' )
7070
71+ _testcapi = import_helper .import_module ('_testcapi' )
7172
7273def hexstr (s ):
7374 assert isinstance (s , bytes ), repr (s )
@@ -1204,23 +1205,13 @@ def test_readonly_types(self):
12041205 hash_type .value = False
12051206
12061207 @unittest .skipUnless (HASH is not None , 'need _hashlib' )
1208+ @unittest .skipUnless (hasattr (_testcapi , 'set_nomemory' ),
1209+ 'need _testcapi.set_nomemory()' )
12071210 def test_hashlib_init_memory_error_no_df (self ):
12081211 """gh-145301 regression test."""
1209-
1210- try :
1211- import _testcapi
1212- if not hasattr (_testcapi , 'set_nomemory' ):
1213- self .skipTest ('requires _testcapi.set_nomemory' )
1214- except ImportError :
1215- self .skipTest ('requires _testcapi' )
1216-
12171212 code = textwrap .dedent ("""
12181213 import sys
12191214 import _testcapi
1220-
1221- if '_hashlib' in sys.modules:
1222- del sys.modules['_hashlib']
1223-
12241215 _testcapi.set_nomemory(40, 41)
12251216 try:
12261217 import _hashlib
@@ -1229,16 +1220,7 @@ def test_hashlib_init_memory_error_no_df(self):
12291220 finally:
12301221 _testcapi.remove_mem_hooks()
12311222 """ )
1232-
1233- rc = subprocess .call (
1234- [sys .executable , '-c' , code ],
1235- stdout = subprocess .DEVNULL ,
1236- stderr = subprocess .DEVNULL ,
1237- )
1238- # rc < 0 means crash (signal on Unix), which indicates double-free
1239- # rc >= 0 means normal exit (even with MemoryError), which is expected
1240- self .assertGreaterEqual (rc , 0 ,
1241- "Process crashed - Loss double-free in _hashlib" )
1223+ assert_python_ok ('-c' , code )
12421224
12431225
12441226class KDFTests (unittest .TestCase ):
0 commit comments