@@ -868,7 +868,12 @@ def test_set_alloc_tracker_maximum_amplification(self):
868
868
self .assertIsNone (p .SetAllocTrackerMaximumAmplification (10_000 ))
869
869
self .assertIsNotNone (p .Parse (payload , True ))
870
870
871
- def test_set_alloc_tracker_maximum_amplification_invalid (self ):
871
+ def test_set_alloc_tracker_maximum_amplification_infty (self ):
872
+ inf = float ('inf' ) # an 'inf' threshold is allowed
873
+ parser = expat .ParserCreate ()
874
+ self .assertIsNone (parser .SetAllocTrackerMaximumAmplification (inf ))
875
+
876
+ def test_set_alloc_tracker_maximum_amplification_fail_for_subparser (self ):
872
877
parser = expat .ParserCreate ()
873
878
f = parser .SetAllocTrackerMaximumAmplification
874
879
@@ -882,37 +887,42 @@ def test_set_alloc_tracker_maximum_amplification_invalid(self):
882
887
self .assertRaisesRegex (expat .ExpatError , msg , fsub , 1.0 )
883
888
884
889
def test_set_alloc_tracker_activation_threshold (self ):
885
- # Run the test with EXPAT_MALLOC_DEBUG=2 to find those constants.
886
- MAX_ALLOC = 17333
887
- MIN_ALLOC = 1096
888
-
890
+ # The payload is expected to have a peak allocation of
891
+ # at least 3 bytes but strictly less than 10^5 bytes.
889
892
payload = self .exponential_expansion_payload (10 , 4 )
890
893
891
894
p = expat .ParserCreate ()
892
- p .SetAllocTrackerActivationThreshold (MAX_ALLOC + 1 )
895
+ p .SetAllocTrackerActivationThreshold (pow ( 10 , 5 ) )
893
896
self .assertIsNone (p .SetAllocTrackerMaximumAmplification (1.0 ))
894
897
# Check that we never reach the activation threshold.
895
898
self .assertIsNotNone (p .Parse (payload , True ))
896
899
897
900
p = expat .ParserCreate ()
898
- p .SetAllocTrackerActivationThreshold (MIN_ALLOC - 1 )
901
+ p .SetAllocTrackerActivationThreshold (2 )
899
902
# Check that we always reach the activation threshold.
900
903
self .assertIsNone (p .SetAllocTrackerMaximumAmplification (1.0 ))
901
904
msg = r"out of memory: line \d+, column \d+"
902
905
self .assertRaisesRegex (expat .ExpatError , msg , p .Parse , payload , True )
903
906
904
- def test_set_alloc_tracker_activation_threshold_overflow (self ):
907
+ def test_set_alloc_tracker_activation_threshold_arg_invalid (self ):
908
+ parser = expat .ParserCreate ()
909
+ f = parser .SetAllocTrackerActivationThreshold
910
+ self .assertRaises (TypeError , f , 1.0 )
911
+ self .assertRaises (TypeError , f , - 1.5 )
912
+ self .assertRaises (ValueError , f , - 5 )
913
+
914
+ def test_set_alloc_tracker_activation_threshold_arg_overflow (self ):
905
915
_testcapi = import_helper .import_module ("_testcapi" )
906
916
parser = expat .ParserCreate ()
907
917
f = parser .SetAllocTrackerActivationThreshold
908
918
self .assertRaises (OverflowError , f , _testcapi .ULLONG_MAX + 1 )
909
919
910
- def test_set_alloc_tracker_activation_threshold_invalid (self ):
920
+ def test_set_alloc_tracker_activation_threshold_fail_for_subparser (self ):
911
921
parser = expat .ParserCreate ()
912
922
subparser = parser .ExternalEntityParserCreate (None )
913
- f = subparser .SetAllocTrackerActivationThreshold
923
+ fsub = subparser .SetAllocTrackerActivationThreshold
914
924
msg = "parser must be a root parser"
915
- self .assertRaisesRegex (expat .ExpatError , msg , f , 12345 )
925
+ self .assertRaisesRegex (expat .ExpatError , msg , fsub , 12345 )
916
926
917
927
918
928
if __name__ == "__main__" :
0 commit comments