Skip to content

Commit fb83fb5

Browse files
committed
test better numeric values
1 parent 3e45613 commit fb83fb5

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

Lib/test/test_pyexpat.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,12 @@ def test_set_alloc_tracker_maximum_amplification(self):
868868
self.assertIsNone(p.SetAllocTrackerMaximumAmplification(10_000))
869869
self.assertIsNotNone(p.Parse(payload, True))
870870

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):
872877
parser = expat.ParserCreate()
873878
f = parser.SetAllocTrackerMaximumAmplification
874879

@@ -882,37 +887,42 @@ def test_set_alloc_tracker_maximum_amplification_invalid(self):
882887
self.assertRaisesRegex(expat.ExpatError, msg, fsub, 1.0)
883888

884889
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.
889892
payload = self.exponential_expansion_payload(10, 4)
890893

891894
p = expat.ParserCreate()
892-
p.SetAllocTrackerActivationThreshold(MAX_ALLOC + 1)
895+
p.SetAllocTrackerActivationThreshold(pow(10, 5))
893896
self.assertIsNone(p.SetAllocTrackerMaximumAmplification(1.0))
894897
# Check that we never reach the activation threshold.
895898
self.assertIsNotNone(p.Parse(payload, True))
896899

897900
p = expat.ParserCreate()
898-
p.SetAllocTrackerActivationThreshold(MIN_ALLOC - 1)
901+
p.SetAllocTrackerActivationThreshold(2)
899902
# Check that we always reach the activation threshold.
900903
self.assertIsNone(p.SetAllocTrackerMaximumAmplification(1.0))
901904
msg = r"out of memory: line \d+, column \d+"
902905
self.assertRaisesRegex(expat.ExpatError, msg, p.Parse, payload, True)
903906

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):
905915
_testcapi = import_helper.import_module("_testcapi")
906916
parser = expat.ParserCreate()
907917
f = parser.SetAllocTrackerActivationThreshold
908918
self.assertRaises(OverflowError, f, _testcapi.ULLONG_MAX + 1)
909919

910-
def test_set_alloc_tracker_activation_threshold_invalid(self):
920+
def test_set_alloc_tracker_activation_threshold_fail_for_subparser(self):
911921
parser = expat.ParserCreate()
912922
subparser = parser.ExternalEntityParserCreate(None)
913-
f = subparser.SetAllocTrackerActivationThreshold
923+
fsub = subparser.SetAllocTrackerActivationThreshold
914924
msg = "parser must be a root parser"
915-
self.assertRaisesRegex(expat.ExpatError, msg, f, 12345)
925+
self.assertRaisesRegex(expat.ExpatError, msg, fsub, 12345)
916926

917927

918928
if __name__ == "__main__":

0 commit comments

Comments
 (0)