@@ -1312,7 +1312,7 @@ def do_import(self, dotted_path: Tuple[str, ...], base_path: Optional[str], alia
1312
1312
except IOError :
1313
1313
continue
1314
1314
else :
1315
- h = hashlib . md5 (text . encode ( 'utf8' )). hexdigest ( )
1315
+ h = md5_digest (text )
1316
1316
if self .used_files .get (joined_path , h ) != h :
1317
1317
raise RuntimeError ("Grammar file was changed during importing" )
1318
1318
self .used_files [joined_path ] = h
@@ -1391,7 +1391,7 @@ def verify_used_files(file_hashes):
1391
1391
if text is None : # We don't know how to load the path. ignore it.
1392
1392
continue
1393
1393
1394
- current = hashlib . md5 (text . encode ()). hexdigest ( )
1394
+ current = md5_digest (text )
1395
1395
if old != current :
1396
1396
logger .info ("File %r changed, rebuilding Parser" % path )
1397
1397
return False
@@ -1407,3 +1407,15 @@ def load_grammar(grammar, source, import_paths, global_keep_all_tokens):
1407
1407
builder = GrammarBuilder (global_keep_all_tokens , import_paths )
1408
1408
builder .load_grammar (grammar , source )
1409
1409
return builder .build (), builder .used_files
1410
+
1411
+
1412
+ def md5_digest (s : str ) -> str :
1413
+ """Get the md5 digest of a string
1414
+
1415
+ Supports the `usedforsecurity` argument for Python 3.9+ to allow running on
1416
+ a FIPS-enabled system.
1417
+ """
1418
+ if sys .version_info >= (3 , 9 ):
1419
+ return hashlib .md5 (s .encode ('utf8' ), usedforsecurity = False ).hexdigest ()
1420
+ else :
1421
+ return hashlib .md5 (s .encode ('utf8' )).hexdigest ()
0 commit comments