summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorGregory P. Smith <greg@krypto.org>2022-11-03 23:18:38 (GMT)
committerGitHub <noreply@github.com>2022-11-03 23:18:38 (GMT)
commit4c4b5ce2e529a1279cd287e2d2d73ffcb6cf2ead (patch)
tree738e223c212b3917afe011e2a8a21f4b78ebb503 /Lib/test
parentbee107028922adc55421611b4bf7da14b8a64010 (diff)
downloadcpython-4c4b5ce2e529a1279cd287e2d2d73ffcb6cf2ead.zip
cpython-4c4b5ce2e529a1279cd287e2d2d73ffcb6cf2ead.tar.gz
cpython-4c4b5ce2e529a1279cd287e2d2d73ffcb6cf2ead.tar.bz2
gh-90716: bugfixes and more tests for _pylong. (#99073)
* Properly decref on _pylong import error. * Improve the error message on _pylong TypeError. * Fix the assertion error in pydebug builds to be a TypeError. * Tie the return value comments together. These are minor followups to issues not caught among the reviewers on https://github.com/python/cpython/pull/96673.
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_int.py39
1 files changed, 39 insertions, 0 deletions
diff --git a/Lib/test/test_int.py b/Lib/test/test_int.py
index f484c59..334fea0 100644
--- a/Lib/test/test_int.py
+++ b/Lib/test/test_int.py
@@ -2,10 +2,16 @@ import sys
import time
import unittest
+from unittest import mock
from test import support
from test.test_grammar import (VALID_UNDERSCORE_LITERALS,
INVALID_UNDERSCORE_LITERALS)
+try:
+ import _pylong
+except ImportError:
+ _pylong = None
+
L = [
('0', 0),
('1', 1),
@@ -841,6 +847,39 @@ class PyLongModuleTests(unittest.TestCase):
with self.assertRaises(ValueError) as err:
int('_' + s)
+ @support.cpython_only # tests implementation details of CPython.
+ @unittest.skipUnless(_pylong, "_pylong module required")
+ @mock.patch.object(_pylong, "int_to_decimal_string")
+ def test_pylong_misbehavior_error_path_to_str(
+ self, mock_int_to_str):
+ with support.adjust_int_max_str_digits(20_000):
+ big_value = int('7'*19_999)
+ mock_int_to_str.return_value = None # not a str
+ with self.assertRaises(TypeError) as ctx:
+ str(big_value)
+ self.assertIn('_pylong.int_to_decimal_string did not',
+ str(ctx.exception))
+ mock_int_to_str.side_effect = RuntimeError("testABC")
+ with self.assertRaises(RuntimeError):
+ str(big_value)
+
+ @support.cpython_only # tests implementation details of CPython.
+ @unittest.skipUnless(_pylong, "_pylong module required")
+ @mock.patch.object(_pylong, "int_from_string")
+ def test_pylong_misbehavior_error_path_from_str(
+ self, mock_int_from_str):
+ big_value = '7'*19_999
+ with support.adjust_int_max_str_digits(20_000):
+ mock_int_from_str.return_value = b'not an int'
+ with self.assertRaises(TypeError) as ctx:
+ int(big_value)
+ self.assertIn('_pylong.int_from_string did not',
+ str(ctx.exception))
+
+ mock_int_from_str.side_effect = RuntimeError("test123")
+ with self.assertRaises(RuntimeError):
+ int(big_value)
+
if __name__ == "__main__":
unittest.main()